1 23 24 package org.enhydra.xml.devtools; 25 26 import java.io.FileOutputStream ; 27 import java.io.IOException ; 28 import java.io.OutputStream ; 29 import java.io.PrintWriter ; 30 31 import org.enhydra.apache.xerces.parsers.DOMParser; 32 import org.enhydra.xml.dom.DOMInfo; 33 import org.enhydra.xml.io.XMLEntityResolver; 34 import org.w3c.dom.Document ; 35 import org.xml.sax.ErrorHandler ; 36 import org.xml.sax.SAXException ; 37 import org.xml.sax.SAXParseException ; 38 39 49 public class XMLParse implements ErrorHandler { 50 51 private int fErrorCnt = 0; 52 53 54 private boolean fDump = false; 55 private boolean fNameSpaces = false; 56 private boolean fClassPathResolve = false; 57 private boolean fDebugFlag = false; 58 private boolean fGrammar = false; 59 private boolean fValidate = true; 60 61 64 private void printError(String msg, 65 SAXParseException ex) { 66 System.err.println(ex.getSystemId() + ":" 67 + ex.getLineNumber() + ":" 68 + ex.getColumnNumber() + ":" 69 + msg + ":" 70 + ex.toString()); 71 } 72 73 76 public void warning(SAXParseException ex) { 77 printError("Warning", ex); 78 } 79 80 83 public void error(SAXParseException ex) { 84 printError("Error", ex); 85 fErrorCnt++; 86 } 87 88 91 public void fatalError(SAXParseException ex) throws SAXException { 92 printError("Fatal error", ex); 93 throw ex; 94 } 95 96 99 class XercesDOMParser extends DOMParser { 100 103 public XercesDOMParser() throws SAXException { 104 setValidation(fValidate); 105 setNamespaces(fNameSpaces); 106 setDeferNodeExpansion(false); 107 if (fGrammar) { 108 setFeature("http://apache.org/xml/features/domx/grammar-access", 109 true); 110 } 111 } 112 } 113 114 117 private Document parse(String xmlFile) throws IOException , SAXException { 118 XercesDOMParser parser = new XercesDOMParser(); 119 parser.setErrorHandler(this); 120 if (fClassPathResolve) { 121 XMLEntityResolver resolver = new XMLEntityResolver(); 122 resolver.setDefaultResolving(); 123 parser.setEntityResolver(resolver); 124 if (fDebugFlag) { 125 resolver.setDebugWriter(new PrintWriter (System.err, true)); 126 } 127 } 128 129 parser.parse(xmlFile); 130 return parser.getDocument(); 131 } 132 133 136 private void process(String xmlFile, 137 OutputStream output) throws IOException , SAXException { 138 Document doc = parse(xmlFile); 139 if (fDump) { 140 DOMInfo.printTree(null, doc, output); 141 } 142 } 143 144 147 private void run(String [] args) throws Throwable { 148 int argIdx = 0; 149 while ((argIdx < args.length) && (args[argIdx].startsWith("-"))) { 150 if (args[argIdx].equals("-dump")) { 151 fDump = true; 152 } else if (args[argIdx].equals("-ns")) { 153 fNameSpaces = true; 154 } else if (args[argIdx].equals("-cp-resolve")) { 155 fClassPathResolve = true; 156 } else if (args[argIdx].equals("-debug")) { 157 fDebugFlag = true; 158 } else if (args[argIdx].equals("-grammar")) { 159 fGrammar = true; 160 } else if (args[argIdx].equals("-no-validation")) { 161 fValidate = false; 162 } else { 163 System.err.println("Error: invalid argument \"" + args[argIdx] 164 + "\", expected one of -dump, -ns, -cp-resolve, -debug, -grammar"); 165 System.exit(1); 166 } 167 argIdx++; 168 } 169 170 int argsLeft = args.length-argIdx; 171 if ((argsLeft < 1) || (argsLeft > 2)) { 172 System.err.println("wrong # args: xml-parse [options] xml.file [output]"); 173 System.exit(1); 174 } 175 String xmlFile = args[argIdx]; 176 String outFile = (argsLeft > 1) ? args[argIdx+1] : null; 177 178 OutputStream out; 179 if (argsLeft == 1) { 180 out = System.out; 181 } else { 182 out = new FileOutputStream (args[argIdx+1]); 183 } 184 process(xmlFile, out); 185 out.flush(); 186 } 187 188 191 public static void main(String [] args) throws Throwable { 192 try { 193 new XMLParse().run(args); 194 } catch (Throwable error) { 195 if (error instanceof ExceptionInInitializerError ) { 196 error = ((ExceptionInInitializerError )error).getException(); 197 } 198 System.err.println(error.toString()); 199 error.printStackTrace(); 200 System.exit(2); 201 } 202 203 } 204 } 205 | Popular Tags |