System.exit(1);
}
// variables
XMLDocumentParser parser = null;
XMLParserConfiguration parserConfig = null;
boolean namespaces = DEFAULT_NAMESPACES;
boolean validation = DEFAULT_VALIDATION;
boolean schemaValidation = DEFAULT_SCHEMA_VALIDATION;
boolean notifyCharRefs = DEFAULT_NOTIFY_CHAR_REFS;
// process arguments
for (int i = 0; i < argv.length; i++) {
String arg = argv[i];
if (arg.startsWith("-")) {
String option = arg.substring(1);
if (option.equals("p")) {
// get parser name
if (++i == argv.length) {
System.err.println("error: Missing argument to -p option.");
continue;
}
String parserName = argv[i];
// create parser
try {
parserConfig = (XMLParserConfiguration)Class.forName(parserName).newInstance();
parser = null;
}
catch (Exception e) {
parserConfig = null;
System.err.println("error: Unable to instantiate parser configuration ("+parserName+")");
}
continue;
}
if (option.equalsIgnoreCase("n")) {
namespaces = option.equals("n");
continue;
}
if (option.equalsIgnoreCase("v")) {
validation = option.equals("v");
continue;
}
if (option.equalsIgnoreCase("s")) {
schemaValidation = option.equals("s");
continue;
}
if (option.equalsIgnoreCase("c")) {
notifyCharRefs = option.equals("c");
continue;
}
if (option.equals("h")) {
printUsage();
continue;
}
}
// use default parser?
if (parserConfig == null) {
// create parser
try {
parserConfig = (XMLParserConfiguration)Class.forName(DEFAULT_PARSER_CONFIG).newInstance();
}
catch (Exception e) {
System.err.println("error: Unable to instantiate parser configuration ("+DEFAULT_PARSER_CONFIG+")");
continue;
}
}
// set parser features
if (parser == null) {
parser = new DocumentTracer(parserConfig);
}
try {
parserConfig.setFeature(NAMESPACES_FEATURE_ID, namespaces);
}
catch (XMLConfigurationException e) {
System.err.println("warning: Parser does not support feature ("+NAMESPACES_FEATURE_ID+")");
}
try {
parserConfig.setFeature(VALIDATION_FEATURE_ID, validation);
}
catch (XMLConfigurationException e) {
System.err.println("warning: Parser does not support feature ("+VALIDATION_FEATURE_ID+")");
}
try {
parserConfig.setFeature(SCHEMA_VALIDATION_FEATURE_ID, schemaValidation);
}
catch (XMLConfigurationException e) {
if (e.getType() == XMLConfigurationException.NOT_SUPPORTED) {
System.err.println("warning: Parser does not support feature ("+SCHEMA_VALIDATION_FEATURE_ID+")");
}
}
try {
parserConfig.setFeature(NOTIFY_CHAR_REFS_FEATURE_ID, notifyCharRefs);
}
catch (XMLConfigurationException e) {
if (e.getType() == XMLConfigurationException.NOT_RECOGNIZED) {
e.printStackTrace();
}