if (argv.length < 2) {
printUsage();
System.exit(1);
}
XMLParserConfiguration parserConfiguration = null;
String arg = null;
int i = 0;
arg = argv[i];
if (arg.equals("-p")) {
// get parser name
i++;
String parserName = argv[i];
// create parser
try {
ClassLoader cl = ObjectFactory.findClassLoader();
parserConfiguration = (XMLParserConfiguration)ObjectFactory.newInstance(parserName, cl, true);
}
catch (Exception e) {
parserConfiguration = null;
System.err.println("error: Unable to instantiate parser configuration ("+parserName+")");
}
i++;
}
arg = argv[i];
// process -d
Vector externalDTDs = null;
if (arg.equals("-d")) {
externalDTDs= new Vector();
i++;
while (i < argv.length && !(arg = argv[i]).startsWith("-")) {
externalDTDs.addElement(arg);
i++;
}
// has to be at least one dTD or schema , and there has to be other parameters
if (externalDTDs.size() == 0) {
printUsage();
System.exit(1);
}
}
// process -f/F
Vector schemas = null;
boolean schemaFullChecking = DEFAULT_SCHEMA_FULL_CHECKING;
if(i < argv.length) {
arg = argv[i];
if (arg.equals("-f")) {
schemaFullChecking = true;
i++;
arg = argv[i];
} else if (arg.equals("-F")) {
schemaFullChecking = false;
i++;
arg = argv[i];
}
if (arg.equals("-a")) {
if(externalDTDs != null) {
printUsage();
System.exit(1);
}
// process -a: schema files
schemas= new Vector();
i++;
while (i < argv.length && !(arg = argv[i]).startsWith("-")) {
schemas.addElement(arg);
i++;
}
// has to be at least one dTD or schema , and there has to be other parameters
if (schemas.size() == 0) {
printUsage();
System.exit(1);
}
}
}
// process -i: instance files, if any
Vector ifiles = null;
if (i < argv.length) {
if (!arg.equals("-i")) {
printUsage();
System.exit(1);
}
i++;
ifiles = new Vector();
while (i < argv.length && !(arg = argv[i]).startsWith("-")) {
ifiles.addElement(arg);
i++;
}
// has to be at least one instance file, and there has to be no more
// parameters
if (ifiles.size() == 0 || i != argv.length) {
printUsage();
System.exit(1);
}
}
// now we have all our arguments. We only
// need to parse the DTD's/schemas, put them
// in a grammar pool, possibly instantiate an
// appropriate configuration, and we're on our way.
SymbolTable sym = new SymbolTable(BIG_PRIME);
XMLGrammarPreparser preparser = new XMLGrammarPreparser(sym);
XMLGrammarPoolImpl grammarPool = new XMLGrammarPoolImpl();
boolean isDTD = false;
if(externalDTDs != null) {
preparser.registerPreparser(XMLGrammarDescription.XML_DTD, null);
isDTD = true;
} else if(schemas != null) {
preparser.registerPreparser(XMLGrammarDescription.XML_SCHEMA, null);
isDTD = false;
} else {
System.err.println("No schema or DTD specified!");
System.exit(1);
}
preparser.setProperty(GRAMMAR_POOL, grammarPool);
preparser.setFeature(NAMESPACES_FEATURE_ID, true);
preparser.setFeature(VALIDATION_FEATURE_ID, true);
// note we can set schema features just in case...
preparser.setFeature(SCHEMA_VALIDATION_FEATURE_ID, true);
preparser.setFeature(SCHEMA_FULL_CHECKING_FEATURE_ID, schemaFullChecking);
// parse the grammar...
try {
if(isDTD) {
for (i = 0; i < externalDTDs.size(); i++) {
Grammar g = preparser.preparseGrammar(XMLGrammarDescription.XML_DTD, stringToXIS((String)externalDTDs.elementAt(i)));
// we don't really care about g; grammarPool will take care of everything.
}
} else { // must be schemas!
for (i = 0; i < schemas.size(); i++) {
Grammar g = preparser.preparseGrammar(XMLGrammarDescription.XML_SCHEMA, stringToXIS((String)schemas.elementAt(i)));
// we don't really care about g; grammarPool will take care of everything.
}
}
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
// Now we have a grammar pool and a SymbolTable; just
// build a configuration and we're on our way!
if(parserConfiguration == null) {
parserConfiguration = new IntegratedParserConfiguration(sym, grammarPool);
} else {
// set GrammarPool and SymbolTable...
parserConfiguration.setProperty(SYMBOL_TABLE, sym);
parserConfiguration.setProperty(GRAMMAR_POOL, grammarPool);
}
// now must reset features, unfortunately:
try{
parserConfiguration.setFeature(NAMESPACES_FEATURE_ID, true);
parserConfiguration.setFeature(VALIDATION_FEATURE_ID, true);
// now we can still do schema features just in case,
// so long as it's our configuraiton......
parserConfiguration.setFeature(SCHEMA_VALIDATION_FEATURE_ID, true);
parserConfiguration.setFeature(SCHEMA_FULL_CHECKING_FEATURE_ID, schemaFullChecking);
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
// then for each instance file, try to validate it
if (ifiles != null) {
try {
for (i = 0; i < ifiles.size(); i++) {
parserConfiguration.parse(stringToXIS((String)ifiles.elementAt(i)));
}
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}