01: /*
02: * User: mrettig
03: * Date: Jul 31, 2002
04: * Time: 3:58:14 PM
05: */
06: package net.sourceforge.jaxor.parser;
07:
08: import net.sourceforge.jaxor.util.SystemException;
09: import org.xml.sax.InputSource;
10: import org.xml.sax.SAXException;
11: import org.xml.sax.XMLReader;
12:
13: import javax.xml.parsers.ParserConfigurationException;
14: import javax.xml.parsers.SAXParserFactory;
15: import java.io.File;
16: import java.io.FileInputStream;
17: import java.io.IOException;
18: import java.io.InputStream;
19: import java.util.HashMap;
20: import java.util.Map;
21:
22: public class GeneratorConfig {
23:
24: private final static Map PROPS_BY_NAME = new HashMap();
25:
26: public static Generator getParser(File config) {
27: try {
28: GeneratorConfig parser = (GeneratorConfig) PROPS_BY_NAME
29: .get(config);
30: if (parser == null) {
31: parser = new GeneratorConfig(config);
32: PROPS_BY_NAME.put(config, parser);
33: }
34: return parser._generator;
35: } catch (Exception e) {
36: throw new SystemException(e);
37: }
38: }
39:
40: private Generator _generator;
41:
42: public GeneratorConfig() {
43: }
44:
45: public Generator getGenerator() {
46: return _generator;
47: }
48:
49: public void addGenerator(Generator generator) {
50: _generator = generator;
51: }
52:
53: private GeneratorConfig(File config) throws IOException,
54: SAXException, ParserConfigurationException {
55: InputStream stream = new FileInputStream(config);
56: parse(new InputSource(stream));
57: }
58:
59: public void parse(InputSource stream) throws SAXException,
60: ParserConfigurationException, IOException {
61: XMLReader _parser = SAXParserFactory.newInstance()
62: .newSAXParser().getXMLReader();
63: DynamicParser parser = new DynamicParser(this );
64: parser.addAlias("import", Import.class);
65: parser.addAlias("extends", Extends.class);
66: parser.addAlias("implements", Implements.class);
67: parser.addAlias("attribute", Attribute.class);
68: parser.addAlias("column", Attribute.class);
69: _parser.setContentHandler(parser);
70: _parser.parse(stream);
71: }
72: }
|