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/Context.java | 8 + org/madore/damlengine/DamlEngine.java | 70 ++++++++- org/madore/damlengine/WeblogIndexSelect.java | 6 +- org/madore/damlengine/WeblogPopulate.java | 154 +++++++++---------- org/madore/damlengine/WeblogRSS.java | 13 +- org/madore/damlengine/WeblogSelect.java | 32 ++-- org/madore/damlengine/weblog-cat-template.daml | 60 ++++++++ org/madore/damlengine/weblog-index-template.daml | 60 ++++++++ org/madore/damlengine/weblog-month-template.daml | 178 ++++++++++++++++++++++ org/madore/damlengine/weblog-recent-template.daml | 60 ++++++++ 10 files changed, 536 insertions(+), 105 deletions(-) create mode 100644 org/madore/damlengine/weblog-cat-template.daml create mode 100644 org/madore/damlengine/weblog-index-template.daml create mode 100644 org/madore/damlengine/weblog-month-template.daml create mode 100644 org/madore/damlengine/weblog-recent-template.daml diff --git a/org/madore/damlengine/Context.java b/org/madore/damlengine/Context.java index 388a076..12e2f1b 100644 --- a/org/madore/damlengine/Context.java +++ b/org/madore/damlengine/Context.java @@ -49,6 +49,14 @@ public class Context implements Cloneable { } } + public static class WeblogRecentSelectionContext + extends WeblogSelectionContext { + public int count; + public WeblogRecentSelectionContext(int count) { + this.count = count; + } + } + public WeblogSelectionContext wsc; public static class EntryContext { 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"); + } } } diff --git a/org/madore/damlengine/WeblogIndexSelect.java b/org/madore/damlengine/WeblogIndexSelect.java index c9bf617..83c5461 100644 --- a/org/madore/damlengine/WeblogIndexSelect.java +++ b/org/madore/damlengine/WeblogIndexSelect.java @@ -1,16 +1,18 @@ package org.madore.damlengine; +import java.io.OutputStream; + public final class WeblogIndexSelect { private WeblogIndexSelect() { // Forbid instantiation throw new AssertionError("WeblogIndexSelect cannot be instantiated"); } - public static void main(String[] args) + public static void fullProcess(OutputStream out) throws Exception { DamlEngine.fullProcess(DamlEngine.class.getResourceAsStream("weblog-index-template.daml"), - System.out, null); + out, null); } diff --git a/org/madore/damlengine/WeblogPopulate.java b/org/madore/damlengine/WeblogPopulate.java index 0d28d92..56e51ac 100644 --- a/org/madore/damlengine/WeblogPopulate.java +++ b/org/madore/damlengine/WeblogPopulate.java @@ -1,6 +1,7 @@ package org.madore.damlengine; import java.util.regex.Pattern; +import java.io.InputStream; import java.security.MessageDigest; import java.sql.Connection; import java.sql.PreparedStatement; @@ -25,15 +26,11 @@ public final class WeblogPopulate { return sb.toString(); } - public static void main(String[] args) + public static void populate(InputStream in) throws Exception { final DocumentBuilder db = DamlEngine.GetDocumentBuilder.db; - if ( args.length == 0 ) { - System.err.println("expecting filename as argument"); - } - final DOMImplementationLS domi = (DOMImplementationLS)(db.getDOMImplementation()); LSSerializer ser = domi.createLSSerializer(); @@ -56,83 +53,81 @@ public final class WeblogPopulate { final PreparedStatement setCatSt = conn.prepareStatement("INSERT INTO incat(id,code) VALUES (?,?)"); - for (String fname : args) { - Document doc = db.parse(fname); - XPathFactory xpf = XPathFactory.newInstance(); - XPath xp = xpf.newXPath(); - xp.setNamespaceContext(new DamlEngine.DamlNSMapping()); - XPathExpression expr = xp.compile("//d:weblog/d:entry"); - XPathExpression texpr = xp.compile("d:title"); - NodeList entries = (NodeList)(expr.evaluate(doc, XPathConstants.NODESET)); - for ( int i=0 ; i(); wsc.xmlData = new ArrayList(); while ( selRes.next() ) { @@ -36,8 +46,8 @@ public final class WeblogSelect { wsc.xmlData.add(content); } - DamlEngine.fullProcess(DamlEngine.class.getResourceAsStream("weblog-month-template.daml"), - System.out, wsc); + DamlEngine.fullProcess(DamlEngine.class.getResourceAsStream(templateResourceName), + out, wsc); } diff --git a/org/madore/damlengine/weblog-cat-template.daml b/org/madore/damlengine/weblog-cat-template.daml new file mode 100644 index 0000000..261686f --- /dev/null +++ b/org/madore/damlengine/weblog-cat-template.daml @@ -0,0 +1,60 @@ + + + + %HTMLlat1; + + %HTMLspecial; + + %HTMLsymbol; + + + david+wwwmadoreorg"> +]> + + + +David Madore's WebLog + +David Alexander Madore's WebLog / Diary + +David Alexander Madore, WebLog, diary + + + + + +

This WebLog is bilingual, some entries are in English and others +are in French. A few of them have a version in either language. +Other than that, the French entries are not translations of +the English ones or vice versa. Of course, if you understand only +English, the English entries ought to be quite understandable without +reading the French ones.

+ +

Ce WebLog est bilingue, certaines entrées sont en +anglais et d'autres sont en français. Quelques-unes ont une version +dans chaque langue. À part ça, les entrées en français ne +sont pas des traductions de celles en anglais ou vice versa. +Bien sûr, si vous ne comprenez que le français, les entrées en +français devraient être assez compréhensibles sans lire celles en +anglais.

+ +

Note that the first entry comes last! Notez que la +première entrée vient en dernier !

+ +

Index of all entries — +Index de toutes les entréesXML (RSS 1.0) — Recent +comments — Commentaires +récents

+ + + +
+ +
diff --git a/org/madore/damlengine/weblog-index-template.daml b/org/madore/damlengine/weblog-index-template.daml new file mode 100644 index 0000000..fb45477 --- /dev/null +++ b/org/madore/damlengine/weblog-index-template.daml @@ -0,0 +1,60 @@ + + + + %HTMLlat1; + + %HTMLspecial; + + %HTMLsymbol; + + + david+wwwmadoreorg"> +]> + + + +David Madore's WebLog + +David Alexander Madore's WebLog / Diary + +David Alexander Madore, WebLog, diary + + + + + +

This WebLog is bilingual, some entries are in English and others +are in French. A few of them have a version in either language. +Other than that, the French entries are not translations of +the English ones or vice versa. Of course, if you understand only +English, the English entries ought to be quite understandable without +reading the French ones.

+ +

Ce WebLog est bilingue, certaines entrées sont en +anglais et d'autres sont en français. Quelques-unes ont une version +dans chaque langue. À part ça, les entrées en français ne +sont pas des traductions de celles en anglais ou vice versa. +Bien sûr, si vous ne comprenez que le français, les entrées en +français devraient être assez compréhensibles sans lire celles en +anglais.

+ +

Note that the first entry comes last! Notez que la +première entrée vient en dernier !

+ +

Index of all entries — +Index de toutes les entréesXML (RSS 1.0) — Recent +comments — Commentaires +récents

+ + + +
+ +
diff --git a/org/madore/damlengine/weblog-month-template.daml b/org/madore/damlengine/weblog-month-template.daml new file mode 100644 index 0000000..f0cf0b3 --- /dev/null +++ b/org/madore/damlengine/weblog-month-template.daml @@ -0,0 +1,178 @@ + + + + %HTMLlat1; + + %HTMLspecial; + + %HTMLsymbol; + + + david+wwwmadoreorg"> +]> + + + +David Madore's WebLog + +David Alexander Madore's WebLog / Diary + +David Alexander Madore, WebLog, diary + + + + + +

This WebLog is bilingual, some entries are in English and others +are in French. A few of them have a version in either language. +Other than that, the French entries are not translations of +the English ones or vice versa. Of course, if you understand only +English, the English entries ought to be quite understandable without +reading the French ones.

+ +

Ce WebLog est bilingue, certaines entrées sont en +anglais et d'autres sont en français. Quelques-unes ont une version +dans chaque langue. À part ça, les entrées en français ne +sont pas des traductions de celles en anglais ou vice versa. +Bien sûr, si vous ne comprenez que le français, les entrées en +français devraient être assez compréhensibles sans lire celles en +anglais.

+ +

Note that the first entry comes last! Notez que la +première entrée vient en dernier !

+ +

Index of all entries — +Index de toutes les entréesXML (RSS 1.0) — Recent +comments — Commentaires +récents

+ + + +

Entries by month / Entrées par mois:

+ + + + + + + + + + + + + + + + + + + + + +
2011JanFebMarAprMayJunJulAugSep
2010JanFebMarAprMayJunJulAugSepOctNovDec
2009JanFebMarAprMayJunJulAugSepOctNovDec
2008JanFebMarAprMayJunJulAugSepOctNovDec
2007JanFebMarAprMayJunJulAugSepOctNovDec
2006JanFebMarAprMayJunJulAugSepOctNovDec
2005JanFebMarAprMayJunJulAugSepOctNovDec
2004JanFebMarAprMayJunJulAugSepOctNovDec
2003MayJunJulAugSepOctNovDec
+ +
+ +
diff --git a/org/madore/damlengine/weblog-recent-template.daml b/org/madore/damlengine/weblog-recent-template.daml new file mode 100644 index 0000000..261686f --- /dev/null +++ b/org/madore/damlengine/weblog-recent-template.daml @@ -0,0 +1,60 @@ + + + + %HTMLlat1; + + %HTMLspecial; + + %HTMLsymbol; + + + david+wwwmadoreorg"> +]> + + + +David Madore's WebLog + +David Alexander Madore's WebLog / Diary + +David Alexander Madore, WebLog, diary + + + + + +

This WebLog is bilingual, some entries are in English and others +are in French. A few of them have a version in either language. +Other than that, the French entries are not translations of +the English ones or vice versa. Of course, if you understand only +English, the English entries ought to be quite understandable without +reading the French ones.

+ +

Ce WebLog est bilingue, certaines entrées sont en +anglais et d'autres sont en français. Quelques-unes ont une version +dans chaque langue. À part ça, les entrées en français ne +sont pas des traductions de celles en anglais ou vice versa. +Bien sûr, si vous ne comprenez que le français, les entrées en +français devraient être assez compréhensibles sans lire celles en +anglais.

+ +

Note that the first entry comes last! Notez que la +première entrée vient en dernier !

+ +

Index of all entries — +Index de toutes les entréesXML (RSS 1.0) — Recent +comments — Commentaires +récents

+ + + +
+ +
-- cgit v1.2.3