1 16 17 package jaxp; 18 19 import java.util.Vector ; 20 21 import javax.xml.XMLConstants ; 22 import javax.xml.parsers.DocumentBuilder ; 23 import javax.xml.parsers.DocumentBuilderFactory ; 24 import javax.xml.parsers.SAXParser ; 25 import javax.xml.parsers.SAXParserFactory ; 26 import javax.xml.transform.stream.StreamSource ; 27 import javax.xml.validation.Schema ; 28 import javax.xml.validation.SchemaFactory ; 29 30 import org.xml.sax.SAXException ; 31 import org.xml.sax.SAXParseException ; 32 import org.xml.sax.helpers.DefaultHandler ; 33 34 42 public class ParserAPIUsage extends DefaultHandler { 43 44 46 47 protected static final String DEFAULT_API_TO_USE = "sax"; 48 49 50 protected static final boolean DEFAULT_XINCLUDE = false; 51 52 53 protected static final boolean DEFAULT_SECURE_PROCESSING = false; 54 55 59 60 public ParserAPIUsage() { 61 } 63 67 68 public void warning(SAXParseException ex) throws SAXException { 69 printError("Warning", ex); 70 } 72 73 public void error(SAXParseException ex) throws SAXException { 74 printError("Error", ex); 75 } 77 78 public void fatalError(SAXParseException ex) throws SAXException { 79 printError("Fatal Error", ex); 80 throw ex; 81 } 83 87 88 protected void printError(String type, SAXParseException ex) { 89 90 System.err.print("["); 91 System.err.print(type); 92 System.err.print("] "); 93 String systemId = ex.getSystemId(); 94 if (systemId != null) { 95 int index = systemId.lastIndexOf('/'); 96 if (index != -1) 97 systemId = systemId.substring(index + 1); 98 System.err.print(systemId); 99 } 100 System.err.print(':'); 101 System.err.print(ex.getLineNumber()); 102 System.err.print(':'); 103 System.err.print(ex.getColumnNumber()); 104 System.err.print(": "); 105 System.err.print(ex.getMessage()); 106 System.err.println(); 107 System.err.flush(); 108 109 } 111 public static void main(String [] argv) { 112 113 if (argv.length == 0) { 115 printUsage(); 116 System.exit(1); 117 } 118 119 ParserAPIUsage parserAPIUsage = new ParserAPIUsage(); 121 Vector schemas = null; 122 String docURI = argv[argv.length - 1]; 123 String apiToUse = DEFAULT_API_TO_USE; 124 boolean xincludeProcessing = DEFAULT_XINCLUDE; 125 boolean secureProcessing = DEFAULT_SECURE_PROCESSING; 126 127 for (int i = 0; i < argv.length - 1; ++i) { 129 String arg = argv[i]; 130 if (arg.startsWith("-")) { 131 String option = arg.substring(1); 132 if (arg.equals("-a")) { 133 if (schemas == null) { 135 schemas = new Vector (); 136 } 137 while (i + 1 < argv.length - 1 && !(arg = argv[i + 1]).startsWith("-")) { 138 schemas.add(arg); 139 ++i; 140 } 141 continue; 142 } 143 if (arg.equals("-api")) { 144 if (i + 1 < argv.length - 1 && !(arg = argv[i + 1]).startsWith("-")) { 145 if (arg.equals("sax") || arg.equals("dom")) { 146 apiToUse = arg; 147 } 148 else { 149 System.err.println("error: unknown source type ("+arg+")."); 150 } 151 } 152 continue; 153 } 154 if (option.equalsIgnoreCase("xi")) { 155 xincludeProcessing = option.equals("xi"); 156 continue; 157 } 158 if (option.equalsIgnoreCase("sp")) { 159 secureProcessing = option.equals("sp"); 160 continue; 161 } 162 if (option.equals("h")) { 163 printUsage(); 164 continue; 165 } 166 System.err.println("error: unknown option ("+option+")."); 167 continue; 168 } 169 } 170 171 try { 172 Schema schema = null; 174 if (schemas != null && schemas.size() > 0) { 175 SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); 176 factory.setErrorHandler(parserAPIUsage); 177 final int length = schemas.size(); 178 StreamSource [] sources = new StreamSource [length]; 179 for (int j = 0; j < length; ++j) { 180 sources[j] = new StreamSource ((String ) schemas.elementAt(j)); 181 } 182 schema = factory.newSchema(sources); 183 } 184 185 if ("dom".equals(apiToUse)) { 186 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 188 dbf.setNamespaceAware(true); 189 dbf.setXIncludeAware(xincludeProcessing); 190 dbf.setSchema(schema); 191 dbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, secureProcessing); 192 193 DocumentBuilder db = dbf.newDocumentBuilder(); 195 196 db.setErrorHandler(parserAPIUsage); 198 db.parse(docURI); 199 db.reset(); 200 db.setErrorHandler(parserAPIUsage); 201 db.parse(docURI); 202 } 203 else { 205 SAXParserFactory spf = SAXParserFactory.newInstance(); 207 spf.setNamespaceAware(true); 208 spf.setXIncludeAware(xincludeProcessing); 209 spf.setSchema(schema); 210 spf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, secureProcessing); 211 212 SAXParser sp = spf.newSAXParser(); 214 215 sp.parse(docURI, parserAPIUsage); 217 sp.reset(); 218 sp.parse(docURI, parserAPIUsage); 219 } 220 } 221 catch (SAXParseException e) { 222 } 224 catch (Exception e) { 225 System.err.println("error: Parse error occurred - "+e.getMessage()); 226 if (e instanceof SAXException ) { 227 Exception nested = ((SAXException )e).getException(); 228 if (nested != null) { 229 e = nested; 230 } 231 } 232 e.printStackTrace(System.err); 233 } 234 235 } 237 241 private static void printUsage() { 242 243 System.err.println("usage: java jaxp.ParserAPIUsage (options) uri"); 244 System.err.println(); 245 246 System.err.println("options:"); 247 System.err.println(" -a uri ... Provide a list of schema documents."); 248 System.err.println(" -api (sax|dom) Select API to use (sax|dom)."); 249 System.err.println(" -xi | -XI Turn on/off XInclude processing."); 250 System.err.println(" -sp | -SP Turn on/off secure processing."); 251 System.err.println(" -h This help screen."); 252 253 System.err.println(); 254 System.err.println("defaults:"); 255 System.err.println(" API to use: " + DEFAULT_API_TO_USE); 256 System.err.print(" XInclude: "); 257 System.err.println(DEFAULT_XINCLUDE ? "on" : "off"); 258 System.err.print(" Secure processing: "); 259 System.err.println(DEFAULT_SECURE_PROCESSING ? "on" : "off"); 260 261 } 263 } | Popular Tags |