1 16 17 package xni; 18 19 import org.apache.xerces.parsers.XMLGrammarPreparser; 20 import org.apache.xerces.parsers.IntegratedParserConfiguration; 21 import org.apache.xerces.util.SymbolTable; 22 import org.apache.xerces.util.XMLGrammarPoolImpl; 23 import org.apache.xerces.impl.Constants; 24 25 import org.apache.xerces.xni.grammars.XMLGrammarDescription; 26 import org.apache.xerces.xni.grammars.Grammar; 27 import org.apache.xerces.xni.parser.XMLInputSource; 28 import org.apache.xerces.xni.parser.XMLParserConfiguration; 29 30 import java.util.Vector ; 31 32 47 public class XMLGrammarBuilder { 48 49 53 55 56 public static final String SYMBOL_TABLE = 57 Constants.XERCES_PROPERTY_PREFIX + Constants.SYMBOL_TABLE_PROPERTY; 58 59 60 public static final String GRAMMAR_POOL = 61 Constants.XERCES_PROPERTY_PREFIX + Constants.XMLGRAMMAR_POOL_PROPERTY; 62 63 65 66 protected static final String NAMESPACES_FEATURE_ID = "http://xml.org/sax/features/namespaces"; 67 68 69 protected static final String VALIDATION_FEATURE_ID = "http://xml.org/sax/features/validation"; 70 71 72 protected static final String SCHEMA_VALIDATION_FEATURE_ID = "http://apache.org/xml/features/validation/schema"; 73 74 75 protected static final String SCHEMA_FULL_CHECKING_FEATURE_ID = "http://apache.org/xml/features/validation/schema-full-checking"; 76 77 public static final int BIG_PRIME = 2039; 82 83 85 86 protected static final boolean DEFAULT_SCHEMA_FULL_CHECKING = false; 87 88 92 93 public static void main(String argv[]) { 94 95 if (argv.length < 2) { 97 printUsage(); 98 System.exit(1); 99 } 100 101 XMLParserConfiguration parserConfiguration = null; 102 String arg = null; 103 int i = 0; 104 105 arg = argv[i]; 106 if (arg.equals("-p")) { 107 i++; 109 String parserName = argv[i]; 110 111 try { 113 ClassLoader cl = ObjectFactory.findClassLoader(); 114 parserConfiguration = (XMLParserConfiguration)ObjectFactory.newInstance(parserName, cl, true); 115 } 116 catch (Exception e) { 117 parserConfiguration = null; 118 System.err.println("error: Unable to instantiate parser configuration ("+parserName+")"); 119 } 120 i++; 121 } 122 arg = argv[i]; 123 Vector externalDTDs = null; 125 if (arg.equals("-d")) { 126 externalDTDs= new Vector (); 127 i++; 128 while (i < argv.length && !(arg = argv[i]).startsWith("-")) { 129 externalDTDs.addElement(arg); 130 i++; 131 } 132 if (externalDTDs.size() == 0) { 134 printUsage(); 135 System.exit(1); 136 } 137 } 138 139 Vector schemas = null; 141 boolean schemaFullChecking = DEFAULT_SCHEMA_FULL_CHECKING; 142 if(i < argv.length) { 143 arg = argv[i]; 144 if (arg.equals("-f")) { 145 schemaFullChecking = true; 146 i++; 147 arg = argv[i]; 148 } else if (arg.equals("-F")) { 149 schemaFullChecking = false; 150 i++; 151 arg = argv[i]; 152 } 153 if (arg.equals("-a")) { 154 if(externalDTDs != null) { 155 printUsage(); 156 System.exit(1); 157 } 158 159 161 schemas= new Vector (); 162 i++; 163 while (i < argv.length && !(arg = argv[i]).startsWith("-")) { 164 schemas.addElement(arg); 165 i++; 166 } 167 168 if (schemas.size() == 0) { 170 printUsage(); 171 System.exit(1); 172 } 173 } 174 175 } 176 Vector ifiles = null; 178 if (i < argv.length) { 179 if (!arg.equals("-i")) { 180 printUsage(); 181 System.exit(1); 182 } 183 184 i++; 185 ifiles = new Vector (); 186 while (i < argv.length && !(arg = argv[i]).startsWith("-")) { 187 ifiles.addElement(arg); 188 i++; 189 } 190 191 if (ifiles.size() == 0 || i != argv.length) { 194 printUsage(); 195 System.exit(1); 196 } 197 } 198 199 204 SymbolTable sym = new SymbolTable(BIG_PRIME); 205 XMLGrammarPreparser preparser = new XMLGrammarPreparser(sym); 206 XMLGrammarPoolImpl grammarPool = new XMLGrammarPoolImpl(); 207 boolean isDTD = false; 208 if(externalDTDs != null) { 209 preparser.registerPreparser(XMLGrammarDescription.XML_DTD, null); 210 isDTD = true; 211 } else if(schemas != null) { 212 preparser.registerPreparser(XMLGrammarDescription.XML_SCHEMA, null); 213 isDTD = false; 214 } else { 215 System.err.println("No schema or DTD specified!"); 216 System.exit(1); 217 } 218 preparser.setProperty(GRAMMAR_POOL, grammarPool); 219 preparser.setFeature(NAMESPACES_FEATURE_ID, true); 220 preparser.setFeature(VALIDATION_FEATURE_ID, true); 221 preparser.setFeature(SCHEMA_VALIDATION_FEATURE_ID, true); 223 preparser.setFeature(SCHEMA_FULL_CHECKING_FEATURE_ID, schemaFullChecking); 224 226 try { 227 if(isDTD) { 228 for (i = 0; i < externalDTDs.size(); i++) { 229 Grammar g = preparser.preparseGrammar(XMLGrammarDescription.XML_DTD, stringToXIS((String )externalDTDs.elementAt(i))); 230 } 232 } else { for (i = 0; i < schemas.size(); i++) { 234 Grammar g = preparser.preparseGrammar(XMLGrammarDescription.XML_SCHEMA, stringToXIS((String )schemas.elementAt(i))); 235 } 237 } 238 } catch (Exception e) { 239 e.printStackTrace(); 240 System.exit(1); 241 } 242 if(parserConfiguration == null) { 245 parserConfiguration = new IntegratedParserConfiguration(sym, grammarPool); 246 } else { 247 parserConfiguration.setProperty(SYMBOL_TABLE, sym); 249 parserConfiguration.setProperty(GRAMMAR_POOL, grammarPool); 250 } 251 try{ 253 parserConfiguration.setFeature(NAMESPACES_FEATURE_ID, true); 254 parserConfiguration.setFeature(VALIDATION_FEATURE_ID, true); 255 parserConfiguration.setFeature(SCHEMA_VALIDATION_FEATURE_ID, true); 258 parserConfiguration.setFeature(SCHEMA_FULL_CHECKING_FEATURE_ID, schemaFullChecking); 259 } catch (Exception e) { 260 e.printStackTrace(); 261 System.exit(1); 262 } 263 if (ifiles != null) { 265 try { 266 for (i = 0; i < ifiles.size(); i++) { 267 parserConfiguration.parse(stringToXIS((String )ifiles.elementAt(i))); 268 } 269 } catch (Exception e) { 270 e.printStackTrace(); 271 System.exit(1); 272 } 273 } 274 275 } 277 281 282 private static void printUsage() { 283 284 System.err.println("usage: java xni.XMLGrammarBuilder [-p config_file] -d uri ... | [-f|-F] -a uri ... [-i uri ...]"); 285 System.err.println(); 286 287 System.err.println("options:"); 288 System.err.println(" -p config_file: configuration to use for instance validation"); 289 System.err.println(" -d grammars to preparse are DTD external subsets"); 290 System.err.println(" -f | -F Turn on/off Schema full checking (default "+ 291 (DEFAULT_SCHEMA_FULL_CHECKING ? "on" : "off)")); 292 System.err.println(" -a uri ... Provide a list of schema documents"); 293 System.err.println(" -i uri ... Provide a list of instance documents to validate"); 294 System.err.println(); 295 System.err.println("NOTE: both -d and -a cannot be specified!"); 296 297 } 299 private static XMLInputSource stringToXIS(String uri) { 300 return new XMLInputSource(null, uri, null); 301 } 302 } 304 | Popular Tags |