1
2
3
4
5
6
7 package org.asyrinx.joey.gen.model.rdb.xml;
8
9 import java.io.FileInputStream;
10 import java.io.IOException;
11 import java.io.InputStream;
12
13 import org.apache.commons.logging.LogFactory;
14 import org.asyrinx.brownie.core.xml.digester.AsyrinxDigester;
15 import org.asyrinx.joey.gen.model.EnumerationEntry;
16 import org.asyrinx.joey.gen.model.rdb.Column;
17 import org.asyrinx.joey.gen.model.rdb.Database;
18 import org.asyrinx.joey.gen.model.rdb.Databases;
19 import org.asyrinx.joey.gen.model.rdb.RdbEnumeration;
20 import org.asyrinx.joey.gen.model.rdb.ForeignKey;
21 import org.asyrinx.joey.gen.model.rdb.ForeignKeyEntry;
22 import org.asyrinx.joey.gen.model.rdb.Index;
23 import org.asyrinx.joey.gen.model.rdb.IndexEntry;
24 import org.asyrinx.joey.gen.model.rdb.Table;
25 import org.asyrinx.joey.gen.model.rdb.TablePattern;
26 import org.asyrinx.joey.gen.model.rdb.TablePatternParam;
27 import org.asyrinx.joey.gen.model.rdb.Unique;
28 import org.xml.sax.SAXException;
29
30 /***
31 * @author akima
32 */
33 public class XmlToRdbImpl implements XmlToRdb {
34
35 /***
36 *
37 */
38 public XmlToRdbImpl() {
39 super();
40 }
41
42 private boolean debug = false;
43
44 public Databases load(String filename) throws IOException, SAXException {
45 final FileInputStream inputStream = new FileInputStream(filename);
46 return load(inputStream);
47 }
48
49 public Databases load(InputStream sourceStream) throws IOException, SAXException {
50 final AsyrinxDigester d = new AsyrinxDigester();
51 d.setValidating(false);
52 if (isDebug()) {
53 d.setLogger(LogFactory.getLog(this.getClass()));
54 }
55
56 d.addBodyToProp("*/description", "description");
57
58 d.addRoot("databases", Databases.class);
59 d.addProps("databases/database", Database.class);
60 d.addSetProperties("database");
61
62 d.addProps("*/database/enum", RdbEnumeration.class);
63 d.addProps("*/database/enum/enum-entry", EnumerationEntry.class);
64
65 d.addProps("*/database/table", Table.class);
66
67 d.addProps("*/table/pattern", TablePattern.class);
68 d.addProps("*/table/pattern/param", TablePatternParam.class);
69
70 d.addProps("*/column", Column.class);
71
72 d.addProps("*/foreign-key", ForeignKey.class);
73 d.addProps("*/foreign-key/reference", ForeignKeyEntry.class);
74
75 d.addProps("*/index", Index.class);
76 d.addProps("*/index/index-column", IndexEntry.class);
77
78 d.addProps("*/unique", Unique.class);
79 d.addProps("*/unique/unique-column", IndexEntry.class);
80
81 final Object result = d.parse(sourceStream);
82
83 return (Databases) result;
84 }
85
86 /***
87 * @return Returns the debug.
88 */
89 public boolean isDebug() {
90 return debug;
91 }
92
93 /***
94 * @param debug
95 * The debug to set.
96 */
97 public void setDebug(boolean debug) {
98 this.debug = debug;
99 }
100 }