Index: jrst/src/java/org/codelutin/jrst/Parser.java diff -u jrst/src/java/org/codelutin/jrst/Parser.java:1.12 jrst/src/java/org/codelutin/jrst/Parser.java:1.13 --- jrst/src/java/org/codelutin/jrst/Parser.java:1.12 Fri Apr 22 14:54:08 2005 +++ jrst/src/java/org/codelutin/jrst/Parser.java Fri Apr 14 07:05:52 2006 @@ -23,10 +23,10 @@ * * @author Benjamin Poussin * Copyright Code Lutin - * @version $Revision: 1.12 $ + * @version $Revision: 1.13 $ * - * Mise a jour: $Date: 2005/04/22 14:54:08 $ - * par : $Author: bpoussin $ + * Mise a jour: $Date: 2006/04/14 07:05:52 $ + * par : $Author: ruchaud $ */ package org.codelutin.jrst; @@ -48,10 +48,13 @@ public abstract class Parser { // Parser - // Nivos de débuggage + // Nivos de d?buggage public static final Object DEBUG_LEVEL0 = null; + public static final Object DEBUG_LEVEL1 = new Object(); + public static final Object DEBUG_LEVEL2 = new Object(); + public static final Object DEBUG_LEVEL3 = new Object(); // pour activer le mode debugage @@ -59,73 +62,77 @@ // formats de sortie static final public Object TYPE_RST = new Object(); + static final public Object TYPE_HTML = new Object(); + static final public Object TYPE_XDOC = new Object(); + static final public Object TYPE_XML = new Object(); static protected boolean overwrite = true; static public void help() { - System.out.println("--[ JRsT 0.0a - www.codelutin.com - 2003,2004 ]--"); - System.out.println(""); - System.out.println("usage : jrst [--html|--xdoc|--xml|--rst] [[--overwrite (true|false)] -o outfile] document.rst"); - System.out.println(""); - System.out.println("-h, --help this help"); - System.out.println("--html (default)"); - System.out.println("--xdoc "); - System.out.println("--xml "); - System.out.println("--rst generate with the selected format"); - System.out.println("--overwrite don't generate file allready uptodate"); - System.out.println("-o file output file (--output)"); - System.out.println("-v # verbosity : 0 <= # <= 3"); + System.out.println("--[ JRsT 0.0a - www.codelutin.com - 2003,2004 ]--"); + System.out.println(""); + System.out + .println("usage : jrst [--html|--xdoc|--xml|--rst] [[--overwrite (true|false)] -o outfile] document.rst"); + System.out.println(""); + System.out.println("-h, --help this help"); + System.out.println("--html (default)"); + System.out.println("--xdoc "); + System.out.println("--xml "); + System.out.println("--rst generate with the selected format"); + System.out + .println("--overwrite don't generate file allready uptodate"); + System.out.println("-o file output file (--output)"); + System.out.println("-v # verbosity : 0 <= # <= 3"); } - - static public void main(String [] args) throws Exception { + static public void main(String[] args) throws Exception { String fileIn = null; String fileOut = null; Object type = null; - if(args.length > 0){ - for(int i = 0; i < args.length; i ++) { + if (args.length > 0) { + for (int i = 0; i < args.length; i++) { if ("-h".equals(args[i]) || "--help".equals(args[i])) { help(); return; - }else if ( "--html".equals(args[i]) ) { + } else if ("--html".equals(args[i])) { type = TYPE_HTML; - }else if ( "--xdoc".equals(args[i]) ) { + } else if ("--xdoc".equals(args[i])) { type = TYPE_XDOC; - }else if ( "--xml".equals(args[i]) ) { + } else if ("--xml".equals(args[i])) { type = TYPE_XML; - }else if ( "--rst".equals(args[i]) ) { + } else if ("--rst".equals(args[i])) { type = TYPE_RST; - }else if ( "--overwrite".equals(args[i]) ) { + } else if ("--overwrite".equals(args[i])) { overwrite = "false".equalsIgnoreCase(args[++i]); - }else if ( "-o".equals(args[i]) ) { + } else if ("-o".equals(args[i])) { fileOut = args[++i]; - }else if ( "-v".equals(args[i]) ) { + } else if ("-v".equals(args[i])) { String value = args[++i]; - if ( "0".equals(value) ) + if ("0".equals(value)) DEBUG = DEBUG_LEVEL0; - else if ( "1".equals(value) ) + else if ("1".equals(value)) DEBUG = DEBUG_LEVEL1; - else if ( "2".equals(value) ) + else if ("2".equals(value)) DEBUG = DEBUG_LEVEL2; - else if ( "3".equals(value) ) + else if ("3".equals(value)) DEBUG = DEBUG_LEVEL3; - }else if (args[i].matches("\\-+.*")) { - System.out.println("Unknown argument : " +args[i]+ "\n"); + } else if (args[i].matches("\\-+.*")) { + System.out.println("Unknown argument : " + args[i] + "\n"); help(); return; - }else{ + } else { fileIn = args[i]; } } if (type == null) { type = TYPE_HTML; } - }else{ + } else { help(); return; } @@ -134,39 +141,52 @@ } - static public void parse(Object type, String fileIn, String fileOut) throws Exception{ - if(!overwrite && new File(fileIn).lastModified() <= new File(fileOut).lastModified()){ - if(DEBUG == DEBUG_LEVEL0){ - System.out.println("File " + fileOut +" is uptodate compared to " + fileIn); + static public void parse(Object type, String fileIn, String fileOut, + boolean overwrite) throws Exception { + Parser.overwrite = overwrite; + parse(type,fileIn,fileOut); + } + + static public void parse(Object type, String fileIn, String fileOut) + throws Exception { + if (!overwrite + && new File(fileIn).lastModified() <= new File(fileOut) + .lastModified()) { + if (DEBUG == DEBUG_LEVEL0) { + System.out.println("File " + fileOut + + " is uptodate compared to " + fileIn); } return; } Reader in = null; - try{ + try { in = new FileReader(fileIn); - }catch (IOException ioe) { - System.err.println("Error with outfile ("+fileIn+") : "+ioe.getMessage()); + } catch (IOException ioe) { + System.err.println("Error with outfile (" + fileIn + ") : " + + ioe.getMessage()); return; } Writer out = null; // fichier de sortie - if (fileOut != null){ + if (fileOut != null) { File foout = new File(fileOut); - if ( foout.getParent() != null) { + if (foout.getParent() != null) { File parent = foout.getParentFile(); - if (! parent.mkdirs()){ - System.err.println("Path "+foout.getParent()+" not created"); + if (!parent.mkdirs()) { + System.err.println("Path " + foout.getParent() + + " not created"); } } try { out = new FileWriter(fileOut); - }catch ( IOException ioe) { - System.err.println("Error with infile ("+fileOut+") : "+ioe.getMessage()); + } catch (IOException ioe) { + System.err.println("Error with infile (" + fileOut + ") : " + + ioe.getMessage()); } } else { out = new PrintWriter(System.out); @@ -174,68 +194,70 @@ parse(type, in, out); } - static public void parse(Object type, Reader in, Writer out) throws Exception{ + static public void parse(Object type, Reader in, Writer out) + throws Exception { in = new LineNumberReader(in); out = new BufferedWriter(out); - // Instanciation du Générateur + // Instanciation du G?n?rateur Generator gen = null; if (type == TYPE_HTML) { gen = new HtmlGenerator(); - }else if (type == TYPE_XDOC) { + } else if (type == TYPE_XDOC) { gen = new XdocGenerator(); - }else if (type == TYPE_XML) { + } else if (type == TYPE_XML) { gen = new XmlGenerator(); - }else if (type == TYPE_RST) { + } else if (type == TYPE_RST) { gen = new RstGenerator(); - }else{ + } else { System.err.println("unknown outfile kind -HTML by default-"); gen = new HtmlGenerator(); } - if(out != null){ + if (out != null) { gen.setOs(out); } - - // Lecture de la hiérarchie des éléments + // Lecture de la hi?rarchie des ?l?ments DocumentFactory document = new DocumentFactory(); FactoryParser fp = new FactoryParser(Resource.getURL("jrst.xml")); - document = (DocumentFactory)fp.getInstance(); + document = (DocumentFactory) fp.getInstance(); - /** PARSAGE **/ + /** PARSAGE * */ if (DEBUG != null) System.out.println("\033[00;31mParsing...\033[00m"); ParseResult result = ParseResult.IN_PROGRESS; - // on considère qu'avant le document il y a une ligne blanche - int c = (int)'\n'; - while(c != -1 && ((result = document.parse(c)) == ParseResult.IN_PROGRESS)){ + // on consid?re qu'avant le document il y a une ligne blanche + int c = (int) '\n'; + while (c != -1 + && ((result = document.parse(c)) == ParseResult.IN_PROGRESS)) { c = in.read(); } - // après le document il y a une ligne blanche + // apr?s le document il y a une ligne blanche if (c == -1) { - result = document.parse((int)'\n'); - result = document.parseEnd((int)'D'); // 'D' like DAT green - }else{ -// result = document.parseEnd((int)'D'); // 'D' like DAT green - document.getBuffer().delete(0,result.getConsumedCharCount()-50); + result = document.parse((int) '\n'); + result = document.parseEnd((int) 'D'); // 'D' like DAT green + } else { + // result = document.parseEnd((int)'D'); // 'D' like DAT green + document.getBuffer().delete(0, result.getConsumedCharCount() - 50); } - /** GENERATION **/ + /** GENERATION * */ if (DEBUG != null) System.out.println("\033[00;31mGenerating...\033[00m"); - // lancement de la génération du document de sortie + // lancement de la g?n?ration du document de sortie Element e = document.getElement(); gen.visit(e); - if (out != null) { out.close(); gen.close(); } - if(result == ParseResult.FAILED){ - System.out.println("\033[01;31m------[-- ERROR ### --]-------------------------------------\033[00m"); - System.out.println("Nombre de caractères lu :"+ result.getConsumedCharCount()); + if (result == ParseResult.FAILED) { + System.out + .println("\033[01;31m------[-- ERROR ### --]-------------------------------------\033[00m"); + System.out.println("Nombre de caract?res lu :" + + result.getConsumedCharCount()); System.out.println(result.getError()); System.out.print("buffer was : ["); System.out.print(document.getBuffer().toString());