01: package net.sourceforge.jaxor.parser;
02:
03: import net.sourceforge.jaxor.util.SystemException;
04: import org.xml.sax.*;
05:
06: import javax.xml.parsers.SAXParser;
07: import javax.xml.parsers.SAXParserFactory;
08: import java.io.FileInputStream;
09: import java.io.FileNotFoundException;
10:
11: public class EntityParser {
12: private String _srcfile;
13: private Jaxor _result;
14: private final EntityResolver _resolver;
15:
16: public EntityParser(String srcfile, EntityResolver resolver) {
17: _srcfile = srcfile;
18: _resolver = resolver;
19: }
20:
21: public void run(boolean validating) {
22: try {
23: generate(new InputSource(new FileInputStream(_srcfile)),
24: validating);
25: } catch (FileNotFoundException e) {
26: throw new SystemException(e);
27: }
28: }
29:
30: public void generate(InputSource inputSource) {
31: generate(inputSource, true);
32: }
33:
34: public void generate(InputSource inputSource, boolean validating) {
35: try {
36: SAXParserFactory saxParserFactory = SAXParserFactory
37: .newInstance();
38: saxParserFactory.setValidating(validating);
39: SAXParser saxParser = saxParserFactory.newSAXParser();
40: XMLReader reader = saxParser.getXMLReader();
41: DynamicParser dynamic = new DynamicParser(this );
42: dynamic.addAlias("method", JMethod.class);
43: dynamic.addAlias("sqlgen", Jaxor.class);
44: dynamic.addAlias("primary-key", PrimaryKey.class);
45: dynamic.addAlias("list-ref", ListRef.class);
46: dynamic.addAlias("entity-ref", EntityRef.class);
47: dynamic.addAlias("lazy-field", Field.class);
48: dynamic.addAlias("column", Attribute.class);
49: reader.setErrorHandler(new MyErrorHandler());
50: reader.setEntityResolver(_resolver);
51: reader.setContentHandler(dynamic);
52: reader.parse(inputSource);
53: } catch (Exception e) {
54: throw new SystemException("Parse Failed", e);
55: }
56: }
57:
58: public void addJaxor(Jaxor j) {
59: _result = j;
60: }
61:
62: public Jaxor getJaxor() {
63: return _result;
64: }
65:
66: private class MyErrorHandler implements ErrorHandler {
67: public void error(SAXParseException e) throws SAXException {
68: warning(e);
69: }
70:
71: public void fatalError(SAXParseException e) throws SAXException {
72: warning(e);
73: }
74:
75: public void warning(SAXParseException e) throws SAXException {
76: throw e;
77: }
78: }
79: }
|