From 057a46982292c765e664f886097fdaf33ed22a62 Mon Sep 17 00:00:00 2001 From: "David A. Madore" Date: Wed, 31 Aug 2011 22:26:55 +0200 Subject: Wrap it all in a single command handler. --- org/madore/damlengine/DamlEngine.java | 70 ++++++++++++++++++++++++++++++++--- 1 file changed, 64 insertions(+), 6 deletions(-) (limited to 'org/madore/damlengine/DamlEngine.java') diff --git a/org/madore/damlengine/DamlEngine.java b/org/madore/damlengine/DamlEngine.java index e54ee86..874a220 100644 --- a/org/madore/damlengine/DamlEngine.java +++ b/org/madore/damlengine/DamlEngine.java @@ -1,9 +1,14 @@ package org.madore.damlengine; +import java.util.regex.Pattern; +import java.util.regex.Matcher; import java.io.InputStream; import java.io.FileInputStream; +import java.io.InputStreamReader; import java.io.OutputStream; +import java.io.FileOutputStream; import java.io.OutputStreamWriter; +import java.io.BufferedReader; import javax.xml.XMLConstants; import javax.xml.namespace.NamespaceContext; import javax.xml.parsers.DocumentBuilderFactory; @@ -117,12 +122,65 @@ public final class DamlEngine { public static void main(String[] args) throws Exception { - if ( args.length == 0 ) { - System.err.println("expecting filename as argument"); - } - - for (String fname : args) { - fullProcess (new FileInputStream(fname), System.out); + BufferedReader buf = new BufferedReader(new InputStreamReader(System.in, "UTF-8")); + String line; + Matcher matcher; + + while ( ( line = buf.readLine() ) != null ) { + if ( Pattern.matches("^\\s+$", line) ) + continue; + else if ( (matcher=Pattern.compile("process\\s+(\\S+)(?:\\s+\\>\\s*(\\S+))?\\s*").matcher(line)).matches() ) { + String inf = matcher.group(1); + String outf = matcher.group(2); + InputStream in = new FileInputStream(inf); + OutputStream out = (outf != null) + ? new FileOutputStream(outf) + : System.out; + fullProcess(in, out); + } else if ( (matcher=Pattern.compile("process-weblog-month\\s+(\\d{4})\\-(\\d{2})(?:\\s+\\>\\s*(\\S+))?\\s*").matcher(line)).matches() ) { + String year = matcher.group(1); + String month = matcher.group(2); + String outf = matcher.group(3); + OutputStream out = (outf != null) + ? new FileOutputStream(outf) + : System.out; + WeblogSelect.fullProcess(new Context.WeblogMonthSelectionContext(year,month), + out); + } else if ( (matcher=Pattern.compile("process-weblog-cat\\s+([a-z0-9\\-]+)(?:\\s+\\>\\s*(\\S+))?\\s*").matcher(line)).matches() ) { + String code = matcher.group(1); + String outf = matcher.group(2); + OutputStream out = (outf != null) + ? new FileOutputStream(outf) + : System.out; + WeblogSelect.fullProcess(new Context.WeblogCategorySelectionContext(code), + out); + } else if ( (matcher=Pattern.compile("process-weblog-recent\\s+(\\d+)(?:\\s+\\>\\s*(\\S+))?\\s*").matcher(line)).matches() ) { + int count = Integer.parseInt(matcher.group(1)); + String outf = matcher.group(2); + OutputStream out = (outf != null) + ? new FileOutputStream(outf) + : System.out; + WeblogSelect.fullProcess(new Context.WeblogRecentSelectionContext(count), + out); + } else if ( (matcher=Pattern.compile("process-weblog-index(?:\\s+\\>\\s*(\\S+))?\\s*").matcher(line)).matches() ) { + String outf = matcher.group(1); + OutputStream out = (outf != null) + ? new FileOutputStream(outf) + : System.out; + WeblogIndexSelect.fullProcess(out); + } else if ( (matcher=Pattern.compile("process-weblog-rss(?:\\s+\\>\\s*(\\S+))?\\s*").matcher(line)).matches() ) { + String outf = matcher.group(1); + OutputStream out = (outf != null) + ? new FileOutputStream(outf) + : System.out; + WeblogRSS.fullProcess(out); + } else if ( (matcher=Pattern.compile("populate-weblog\\s+(\\S+)\\s*").matcher(line)).matches() ) { + String inf = matcher.group(1); + InputStream in = new FileInputStream(inf); + WeblogPopulate.populate(in); + } else { + throw new IllegalArgumentException("couldn't understand command"); + } } } -- cgit v1.2.3