01: package org.kohsuke.rngom.digested;
02:
03: import org.kohsuke.rngom.ast.builder.BuildException;
04: import org.kohsuke.rngom.ast.builder.SchemaBuilder;
05: import org.kohsuke.rngom.ast.util.CheckingSchemaBuilder;
06: import org.kohsuke.rngom.parse.Parseable;
07: import org.kohsuke.rngom.parse.compact.CompactParseable;
08: import org.kohsuke.rngom.parse.xml.SAXParseable;
09: import org.xml.sax.ErrorHandler;
10: import org.xml.sax.InputSource;
11: import org.xml.sax.SAXException;
12: import org.xml.sax.SAXParseException;
13: import org.xml.sax.helpers.DefaultHandler;
14:
15: /**
16: * @author Kohsuke Kawaguchi (kk@kohsuke.org)
17: */
18: public class Main {
19: public static void main(String[] args) throws Exception {
20: Parseable p;
21:
22: ErrorHandler eh = new DefaultHandler() {
23: public void error(SAXParseException e) throws SAXException {
24: throw e;
25: }
26: };
27:
28: // the error handler passed to Parseable will receive parsing errors.
29: if (args[0].endsWith(".rng"))
30: p = new SAXParseable(new InputSource(args[0]), eh);
31: else
32: p = new CompactParseable(new InputSource(args[0]), eh);
33:
34: // the error handler passed to CheckingSchemaBuilder will receive additional
35: // errors found during the RELAX NG restrictions check.
36: // typically you'd want to pass in the same error handler,
37: // as there's really no distinction between those two kinds of errors.
38: SchemaBuilder sb = new CheckingSchemaBuilder(
39: new DSchemaBuilderImpl(), eh);
40: try {
41: // run the parser
42: p.parse(sb);
43: } catch (BuildException e) {
44: // I found that Crimson doesn't show the proper stack trace
45: // when a RuntimeException happens inside a SchemaBuilder.
46: // the following code shows the actual exception that happened.
47: if (e.getCause() instanceof SAXException) {
48: SAXException se = (SAXException) e.getCause();
49: if (se.getException() != null)
50: se.getException().printStackTrace();
51: }
52: throw e;
53: }
54: }
55: }
|