Created
February 21, 2014 11:37
-
-
Save giuscri/9132862 to your computer and use it in GitHub Desktop.
Unix pipes in Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Questa classe non e' stata utilizzata davvero. | |
*/ | |
public abstract class AbstractFilter implements Filter { | |
/* | |
public StringBuffer process(String in) { | |
return process(new StringBuffer(in)); | |
} | |
public StringBuffer process(StringBuffer in); | |
*/ | |
} | |
//public class AllCaps extends AbstractFilter ... | |
public class AllCaps implements Filter { | |
public StringBuffer process(StringBuffer in) { | |
return new StringBuffer(in.toString().toUpperCase()); | |
} | |
public StringBuffer process(String in) { | |
return process(new StringBuffer(in)); | |
} | |
/* | |
public String toString() { | |
... | |
} | |
*/ | |
} | |
// public class DoubleChars extends AbstractFilter ... | |
public class DoubleChars implements Filter { | |
public StringBuffer process(StringBuffer in) { | |
StringBuffer out = new StringBuffer(); | |
for (int index = 0; index < in.toString().length(); index++) { | |
out.append(in.charAt(index)); | |
out.append(in.charAt(index)); | |
} | |
return out; | |
} | |
public StringBuffer process(String in) { | |
return process(new StringBuffer(in)); | |
} | |
/* | |
public String toString() { | |
... | |
} | |
*/ | |
} | |
interface Filter { | |
public StringBuffer process(StringBuffer in); | |
public StringBuffer process(String in); | |
} | |
import java.util.Scanner; | |
import java.io.*; | |
// Encapsulation might have been way better. As instance, | |
// using an Menu-object would have improved readability ... | |
public class Main { | |
public static void main(String[] args) | |
throws Exception | |
{ | |
if (args.length==0) { | |
System.out.println("Usage: Main <nomefile>"); | |
return; | |
} | |
Boolean haveToQuit = false; | |
Pipe thePipe = null; | |
String inFile = args[0]; | |
System.out.println("**stai lavorando su \""+inFile+"\" **"); | |
do { | |
printAvailableChoises(); | |
System.out.print(">>> "); | |
Scanner in = new Scanner(System.in); | |
int sceltaUtente = in.nextInt(); | |
switch (sceltaUtente) { | |
case 1: | |
thePipe = new Pipe(); | |
break; | |
case 2: | |
try { | |
printAvailableFilter(); | |
System.out.print(">>> "); | |
sceltaUtente = in.nextInt(); | |
switch (sceltaUtente) { | |
case 1: | |
thePipe.addFilter(new AllCaps()); | |
break; | |
case 2: | |
thePipe.addFilter(new DoubleChars()); | |
break; | |
case 3: | |
thePipe.addFilter(new RemoveWhitespace()); | |
break; | |
case 4: | |
in.nextLine(); | |
/* ^ Still not getting why I need it ... */ | |
System.out.println("Carattere replacee?"); | |
Character replacee = new Character((in.nextLine().toCharArray())[0]); | |
System.out.println("Carattere replacer?"); | |
Character replacer = new Character((in.nextLine().toCharArray())[0]); | |
thePipe.addFilter(new Translate(replacee,replacer)); | |
break; | |
} | |
} catch (NullPointerException e) { | |
System.out.println("Devi creare prima una pipe."); | |
Thread.sleep(1000); | |
} catch (ArrayIndexOutOfBoundsException e) { | |
System.out.println("Mancano dati per il filtro Translate ..."); | |
Thread.sleep(1000); | |
} | |
break; | |
case 3: | |
if (thePipe!=null) { | |
processAndPrintLinesFrom(thePipe,inFile); | |
} else { | |
processAndPrintLinesFrom(new Pipe(),inFile); | |
} | |
break; | |
case 4: | |
haveToQuit = true; | |
break; | |
} | |
} while (!haveToQuit); | |
} | |
public static void printAvailableChoises() { | |
System.out.println("1 - Crea pipe."); | |
System.out.println("2 - Aggiungi filtro."); | |
System.out.println("3 - Processa file e stampa."); | |
System.out.println("4 - Esci."); | |
} | |
public static void printAvailableFilter() { | |
System.out.println("1 - Aggiungi filtro AllCaps."); | |
System.out.println("2 - Aggiungi filtro DoubleChars."); | |
System.out.println("3 - Aggiungi filtro RemoveWhitespace."); | |
System.out.println("4 - Aggiungi filtro Translate."); | |
} | |
public static void processAndPrintLinesFrom(Pipe inPipe, String inFile) throws FileNotFoundException { | |
BufferedReader br = new BufferedReader(new FileReader(inFile)); | |
try { | |
StringBuilder sb = new StringBuilder(); | |
String line; | |
while ((line=br.readLine()) != null) { | |
sb.append(inPipe.flow(line)+"\n"); | |
} | |
System.out.print(sb); | |
br.close(); | |
} catch(IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
import java.util.Vector; | |
public class Pipe { | |
public Pipe() { | |
theFilterChain = new Vector<Filter>(0); | |
} | |
public StringBuffer flow(StringBuffer in) { | |
if (theFilterChain.size()!=0) | |
for (Filter tmpFilter : theFilterChain) { | |
in = tmpFilter.process(in); | |
} | |
return in; | |
} | |
public StringBuffer flow(String in) { | |
return flow(new StringBuffer(in)); | |
} | |
public void addFilter(Filter f) { | |
theFilterChain.add(f); | |
} | |
public int length() { | |
return theFilterChain.size(); | |
} | |
public String toString() { | |
return theFilterChain.toString(); | |
} | |
private Vector<Filter> theFilterChain; | |
} | |
//public class RemoveWhitespace extends AbstractFilter ... | |
public class RemoveWhitespace implements Filter { | |
public StringBuffer process(String in) { | |
return process(new StringBuffer(in)); | |
} | |
public StringBuffer process(StringBuffer in) { | |
int fromIndex = 0; | |
while (true) { | |
int firstWhitespaceOccurrance = | |
in.indexOf(" ", fromIndex); | |
if (firstWhitespaceOccurrance!=-1) { | |
in.deleteCharAt(firstWhitespaceOccurrance); | |
fromIndex = firstWhitespaceOccurrance; | |
} else { | |
break; | |
} | |
} | |
return in; | |
} | |
/* | |
public String toString() { | |
... | |
} | |
*/ | |
} | |
//public class Translate extends AbstractFilter ... | |
public class Translate implements Filter { | |
public Translate(Character replacee, Character replacer) { | |
this.replacee = replacee; | |
this.replacer = replacer; | |
} | |
public StringBuffer process(StringBuffer in) { | |
int fromIndex = 0; | |
while (true) { | |
int tmpIndex = in.toString().indexOf(replacee.charValue(),fromIndex); | |
if (tmpIndex >= 0) { | |
in.setCharAt(tmpIndex, replacer); | |
fromIndex = tmpIndex; | |
} else { | |
break; | |
} | |
} | |
return in; | |
} | |
public StringBuffer process(String in) { | |
return process(new StringBuffer(in)); | |
} | |
/* | |
public String toString() { | |
... | |
} | |
*/ | |
private Character replacee; | |
private Character replacer; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment