/** Main program. */
public static void main(String argv[]) {
// create parser and set features/properties
DOMParser parser = new DOMParser();
/***
try { parser.setFeature("http://apache.org/xml/features/dom/defer-node-expansion", false); }
catch (Exception e) { System.err.println("warning: unable to set feature."); }
/***/
try { parser.setFeature("http://apache.org/xml/features/domx/grammar-access", true); }
catch (Exception e) { System.err.println("warning: unable to set feature."); }
// create grammar writer
XGrammarWriter writer = new XGrammarWriter();
// run through command line args
if (argv.length == 0) {
printUsage();
}
else {
for (int i = 0; i < argv.length; i++) {
String arg = argv[i];
if (arg.startsWith("-")) {
if (arg.equals("-d") || arg.equals("--dtd")) {
writer.setOutputFormat(OutputFormat.DTD);
continue;
}
if (arg.equals("-x") || arg.equals("--schema")) {
writer.setOutputFormat(OutputFormat.XML_SCHEMA);
continue;
}
if (arg.equals("-v") || arg.equals("--verbose")) {
writer.setVerbose(true);
continue;
}
if (arg.equals("-q") || arg.equals("--quiet")) {
writer.setVerbose(false);
continue;
}
if (arg.equals("-h") || arg.equals("--help")) {
printUsage();
break;
}
if (arg.equals("--")) {
if (i < argv.length - 1) {
System.err.println("error: Missing argument to -- option.");
break;
}
arg = argv[++i];
// let fall through
}
else {
System.err.println("error: Unknown option ("+arg+").");
}
}
// parse file and print grammar
try {
parser.parse(arg);
Document document = parser.getDocument();
writer.printGrammar(arg, document.getDoctype());
}
catch (Exception e) {
System.err.println("error: Error parsing document ("+arg+").");
e.printStackTrace(System.err);