Index: jrst2/src/java/org/codelutin/jrst/AdvancedReader.java diff -u jrst2/src/java/org/codelutin/jrst/AdvancedReader.java:1.1.1.1 jrst2/src/java/org/codelutin/jrst/AdvancedReader.java:1.2 --- jrst2/src/java/org/codelutin/jrst/AdvancedReader.java:1.1.1.1 Mon Oct 30 19:34:05 2006 +++ jrst2/src/java/org/codelutin/jrst/AdvancedReader.java Fri Nov 10 14:21:11 2006 @@ -23,9 +23,9 @@ * Created: 27 oct. 06 00:24:57 * * @author poussin - * @version $Revision: 1.1.1.1 $ + * @version $Revision: 1.2 $ * - * Last update: $Date: 2006/10/30 19:34:05 $ + * Last update: $Date: 2006/11/10 14:21:11 $ * by : $Author: bpoussin $ */ @@ -276,7 +276,7 @@ * @throws IOException */ public void skipBlankLines() throws IOException { - readUntil("\\s*\\S+.*"); + readUntil("^\\s*\\S+.*"); } /** Index: jrst2/src/java/org/codelutin/jrst/JRST.java diff -u jrst2/src/java/org/codelutin/jrst/JRST.java:1.1 jrst2/src/java/org/codelutin/jrst/JRST.java:1.2 --- jrst2/src/java/org/codelutin/jrst/JRST.java:1.1 Mon Nov 6 15:08:46 2006 +++ jrst2/src/java/org/codelutin/jrst/JRST.java Fri Nov 10 14:21:11 2006 @@ -23,9 +23,9 @@ * Created: 3 nov. 06 20:56:00 * * @author poussin - * @version $Revision: 1.1 $ + * @version $Revision: 1.2 $ * - * Last update: $Date: 2006/11/06 15:08:46 $ + * Last update: $Date: 2006/11/10 14:21:11 $ * by : $Author: bpoussin $ */ @@ -42,6 +42,8 @@ import javax.xml.transform.TransformerException; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.codelutin.util.StringUtil; import org.dom4j.Document; import org.dom4j.DocumentException; @@ -57,36 +59,48 @@ /** + * FIXME: 'JRST --help' doesn't work, but 'JRST --help toto' work :( + * * @author poussin */ @CommandLineInterface(application="JRST") public class JRST { - static final private String docbook = "/xsl/dn2dbk.xsl"; - static final private String html = "/xsl/rst2xhtml.xsl"; + /** to use log facility, just put in your code: log.info(\"...\"); */ + static private Log log = LogFactory.getLog(JRST.class); - static final private String walshDir = "/docbook-xsl-nwalsh"; - - static final private String xhtml = walshDir + "/xhtml/docbook.xsl"; - static final private String javahelp = walshDir + "/javahelp/javahelp.xsl"; + // XSL Stylesheet to transforme something to other format + static final private String rst2xhtml = "/xsl/rst2xhtml.xsl"; + static final private String rst2xdoc = "/xsl/rst2xdoc.xsl"; + static final private String rst2docbook = "/xsl/dn2dbk.xsl"; + static final private String walshDir = "/docbook-xsl-nwalsh"; + static final private String docbook2xhtml = walshDir + "/xhtml/docbook.xsl"; + static final private String docbook2javahelp = walshDir + "/javahelp/javahelp.xsl"; // static final private String dbkx2html = walshDir + "/html/onechunk.xsl"; - static final private String htmlhelp = walshDir + "/htmlhelp/htmlhelp.xsl"; + static final private String docbook2htmlhelp = walshDir + "/htmlhelp/htmlhelp.xsl"; - static final private String rst2xdoc = "/xsl/rst2xdoc.xsl"; + // Out put type available + static final public String TYPE_HTML = "html"; + static final public String TYPE_XDOC = "xdoc"; + static final public String TYPE_DOCBOOK = "docbook"; + static final public String TYPE_XHTML = "xhtml"; + static final public String TYPE_JAVAHELP = "javahelp"; + static final public String TYPE_HTMLHELP = "htmlhelp"; + /** key, Out type; value: chain of XSL file to provide wanted file for output */ static private Map stylesheets = null; static { stylesheets = new HashMap(); - stylesheets.put("docbook", docbook); - stylesheets.put("html", html); - stylesheets.put("xhtml", docbook+","+xhtml); - stylesheets.put("javahelp", docbook+","+javahelp); - stylesheets.put("htmlhelp", docbook+","+htmlhelp); - stylesheets.put("xdoc", rst2xdoc); + stylesheets.put(TYPE_HTML, rst2xhtml); + stylesheets.put(TYPE_XDOC, rst2xdoc); + stylesheets.put(TYPE_DOCBOOK, rst2docbook); + stylesheets.put(TYPE_XHTML, rst2docbook+","+docbook2xhtml); + stylesheets.put(TYPE_JAVAHELP, rst2docbook+","+docbook2javahelp); + stylesheets.put(TYPE_HTMLHELP, rst2docbook+","+docbook2htmlhelp); } - static public void main(String [] args) throws ArgumentValidationException, IOException, TransformerException, DocumentException { + static public void main(String [] args) throws Exception { JRSTOption option = CliFactory.parseArguments(JRSTOption.class, args); if (option.isHelp()) { @@ -94,45 +108,65 @@ System.out.println(cli.getHelpMessage()); return; } - - // prepare the output flux - XMLWriter out = null; - if (option.isOutFile()) { - out = new XMLWriter(new FileWriter(option.getFile()), new OutputFormat(" ", true)); - } else { - out = new XMLWriter(System.out, new OutputFormat(" ", true)); - } - + // search xsl file list to apply String xslList = null; if (option.isXslFile()) { xslList = option.getXslFile(); } else { - xslList = stylesheets.get(option.getOutType()); + xslList = option.getOutType(); } - // parse rst file - URL url = option.getFile().toURL(); - Reader in = new InputStreamReader(url.openStream()); - JRSTReader jrst = new JRSTReader(); - Document doc = jrst.read(in); - - // apply xsl on rst xml document - String [] xsls = StringUtil.split(xslList, ","); - for (String xsl : xsls) { - URL stylesheet = null; - File file = new File(xsl); - if (file.exists()) { - stylesheet = file.toURL(); + generate(xslList, option.getFile(), option.getOutFile(), option.isForce()); + } + + public static void generate(String xslListOrOutType, File fileIn, File fileOut, + boolean overwrite) throws Exception { + if (fileOut != null && fileOut.exists() && overwrite == false) { + log.info("Don't generate file "+fileOut+", because already exists"); + } else { + // search xsl file list to apply + String xslList = stylesheets.get(xslListOrOutType); + if (xslListOrOutType == null) { + xslList = xslListOrOutType; + } + + // prepare the output flux + XMLWriter out = null; + if (fileOut != null) { + fileOut.getParentFile().mkdirs(); + out = new XMLWriter(new FileWriter(fileOut), new OutputFormat(" ", true)); } else { - stylesheet = JRSTReaderTest.class.getResource(xsl); + out = new XMLWriter(System.out, new OutputFormat(" ", true)); } + + // parse rst file + URL url = fileIn.toURL(); + Reader in = new InputStreamReader(url.openStream()); + JRSTReader jrst = new JRSTReader(); + Document doc = jrst.read(in); + + // apply xsl on rst xml document JRSTGenerator gen = new JRSTGenerator(); - doc = gen.transform(doc, stylesheet); + String [] xsls = StringUtil.split(xslList, ","); + for (String xsl : xsls) { + URL stylesheet = null; + File file = new File(xsl); + if (file.exists()) { + stylesheet = file.toURL(); + } else { + stylesheet = JRST.class.getResource(xsl); + } + doc = gen.transform(doc, stylesheet); + } + + // write generated document + out.write(doc); + + if (fileOut != null) { + out.close(); + } } - - // write generated document - out.write(doc); } public static interface JRSTOption { @@ -140,6 +174,9 @@ @Option(description="display this help and exit") boolean isHelp(); + @Option(description="overwrite existing out file") + boolean isForce(); + @Option(shortName="x", description = "XSL file list to apply, comma separated") public String getXslFile(); public boolean isXslFile(); Index: jrst2/src/java/org/codelutin/jrst/JRSTGenerator.java diff -u jrst2/src/java/org/codelutin/jrst/JRSTGenerator.java:1.4 jrst2/src/java/org/codelutin/jrst/JRSTGenerator.java:1.5 --- jrst2/src/java/org/codelutin/jrst/JRSTGenerator.java:1.4 Mon Nov 6 15:08:46 2006 +++ jrst2/src/java/org/codelutin/jrst/JRSTGenerator.java Fri Nov 10 14:21:11 2006 @@ -23,9 +23,9 @@ * Created: 30 oct. 06 00:14:18 * * @author poussin - * @version $Revision: 1.4 $ + * @version $Revision: 1.5 $ * - * Last update: $Date: 2006/11/06 15:08:46 $ + * Last update: $Date: 2006/11/10 14:21:11 $ * by : $Author: bpoussin $ */ @@ -40,7 +40,6 @@ import java.net.URL; import java.util.LinkedList; -import javax.xml.transform.ErrorListener; import javax.xml.transform.Source; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerException; @@ -62,7 +61,6 @@ import org.xml.sax.EntityResolver; import org.xml.sax.InputSource; import org.xml.sax.SAXException; -import org.xml.sax.ext.EntityResolver2; /** @@ -82,7 +80,7 @@ static boolean DEBUG = true; - static private Log log = LogFactory.getLog(JRSTGeneratorTest.class); + static private Log log = LogFactory.getLog(JRSTGenerator.class); protected Writer out = null; protected int sectionLevel = 0; Index: jrst2/src/java/org/codelutin/jrst/JRSTLexer.java diff -u jrst2/src/java/org/codelutin/jrst/JRSTLexer.java:1.3 jrst2/src/java/org/codelutin/jrst/JRSTLexer.java:1.4 --- jrst2/src/java/org/codelutin/jrst/JRSTLexer.java:1.3 Mon Nov 6 15:08:46 2006 +++ jrst2/src/java/org/codelutin/jrst/JRSTLexer.java Fri Nov 10 14:21:11 2006 @@ -23,9 +23,9 @@ * Created: 28 oct. 06 00:44:20 * * @author poussin - * @version $Revision: 1.3 $ + * @version $Revision: 1.4 $ * - * Last update: $Date: 2006/11/06 15:08:46 $ + * Last update: $Date: 2006/11/10 14:21:11 $ * by : $Author: bpoussin $ */ @@ -426,7 +426,7 @@ in.skipBlankLines(); String line = in.readLine(); // (?i) case inensitive on docinfo item - if (line.matches("^:((?i)"+DOCINFO_ITEM+"):.*$")) { + if (line != null && line.matches("^:((?i)"+DOCINFO_ITEM+"):.*$")) { result = DocumentHelper.createElement(DOCINFO); result.addAttribute("level", "0"); String infotype = line.substring(1, line.indexOf(":", 1)); @@ -549,7 +549,7 @@ } cell.addAttribute(CELL_INDEX_START, String.valueOf(start)); cell.addAttribute(CELL_INDEX_END, String.valueOf(end)); - cell.setText(cell.getText() + "\n" + content); + cell.setText(cell.getText() + content + "\n"); start = end + 1; // +1 to pass + or | at end of cell content = ""; } else { @@ -601,7 +601,7 @@ cell = (Element)row.node(cellNumber); } - cell.setText(cell.getText() + "\n" + content); + cell.setText(cell.getText() + content + "\n"); // on a ajouter des choses dans la cell, donc // ce n'est pas la fin cell.addAttribute(CELL_END, "false"); @@ -900,26 +900,28 @@ Element result = null; in.skipBlankLines(); String line = in.readLine(); - if (startsWithTitleChar(line)) { - String [] titles = in.readLines(2); - if (titles.length == 2 - && line.length() >= titles[0].length() - && line.length() == titles[1].length() - && line.equals(titles[1])) { - result = DocumentHelper.createElement(TITLE) - .addAttribute("type", "double") - .addAttribute("char", titles[1].substring(0, 1)) - .addText(titles[0]); - } - } else { - String title = in.readLine(); - if (title != null && - startsWithTitleChar(title) && - line.length() == title.length()) { - result = DocumentHelper.createElement(TITLE) - .addAttribute("type", "simple") - .addAttribute("char", title.substring(0, 1)) - .addText(line); + if (line != null) { + if (startsWithTitleChar(line)) { + String [] titles = in.readLines(2); + if (titles.length == 2 + && line.length() >= titles[0].length() + && line.length() == titles[1].length() + && line.equals(titles[1])) { + result = DocumentHelper.createElement(TITLE) + .addAttribute("type", "double") + .addAttribute("char", titles[1].substring(0, 1)) + .addText(titles[0]); + } + } else { + String title = in.readLine(); + if (title != null && + startsWithTitleChar(title) && + line.length() == title.length()) { + result = DocumentHelper.createElement(TITLE) + .addAttribute("type", "simple") + .addAttribute("char", title.substring(0, 1)) + .addText(line); + } } } Index: jrst2/src/java/org/codelutin/jrst/JRSTReader.java diff -u jrst2/src/java/org/codelutin/jrst/JRSTReader.java:1.3 jrst2/src/java/org/codelutin/jrst/JRSTReader.java:1.4 --- jrst2/src/java/org/codelutin/jrst/JRSTReader.java:1.3 Mon Nov 6 15:08:47 2006 +++ jrst2/src/java/org/codelutin/jrst/JRSTReader.java Fri Nov 10 14:21:11 2006 @@ -23,9 +23,9 @@ * Created: 27 oct. 06 00:15:34 * * @author poussin - * @version $Revision: 1.3 $ + * @version $Revision: 1.4 $ * - * Last update: $Date: 2006/11/06 15:08:47 $ + * Last update: $Date: 2006/11/10 14:21:11 $ * by : $Author: bpoussin $ */ @@ -464,6 +464,10 @@ */ private Element composeBody(JRSTLexer lexer, Element parent) throws DocumentException, IOException { Element item = lexer.peekTitleOrBodyElement(); + if (item == null && !lexer.eof()) { + item = lexer.peekTitleOrBodyElement(); + System.out.println(item); + } while (!lexer.eof() && itemNotEquals(TITLE, item) && isUpperLevel(item, parent)) { if (itemEquals(PARAGRAPH, item)) { lexer.remove(); @@ -639,10 +643,11 @@ entry.addAttribute("morecols", String.valueOf(morecols)); } -// JRSTReader reader = new JRSTReader(); -// Document doc = reader.read(new StringReader(text)); -// entry.appendContent(doc.getRootElement()); - entry.setText(text); + // FIXME parse entry text in table + JRSTReader reader = new JRSTReader(); + Document doc = reader.read(new StringReader(text)); + entry.appendContent(doc.getRootElement()); +// entry.setText(text); } } if ("true".equals(rows.get(r).attributeValue(JRSTLexer.ROW_END_HEADER))) {