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.Unique;
26 import org.xml.sax.SAXException;
27
28 /***
29 * @author akima
30 */
31 public class XmlToRdb {
32
33 /***
34 *
35 */
36 public XmlToRdb() {
37 super();
38 }
39
40 private boolean debug = false;
41
42 public Databases load(String filename) throws IOException, SAXException {
43 final FileInputStream inputStream = new FileInputStream(filename);
44 return load(inputStream);
45 }
46
47 public Databases load(InputStream sourceStream) throws IOException, SAXException {
48 final AsyrinxDigester d = new AsyrinxDigester();
49 d.setValidating(false);
50 if (isDebug()) {
51 d.setLogger(LogFactory.getLog(this.getClass()));
52 }
53
54 d.addBodyToProp("*/description", "description");
55
56 d.addRoot("databases", Databases.class);
57 d.addProps("databases/database", Database.class);
58 d.addSetProperties("database");
59
60 d.addProps("*/database/enum", RdbEnumeration.class);
61 d.addProps("*/database/enum/enum-entry", EnumerationEntry.class);
62
63
64 d.addProps("*/database/table", Table.class);
65
66
67
68
69 d.addProps("*/column", Column.class);
70
71
72
73
74
75
76 d.addProps("*/foreign-key", ForeignKey.class);
77 d.addProps("*/foreign-key/reference", ForeignKeyEntry.class);
78
79 d.addProps("*/index", Index.class);
80 d.addProps("*/index/index-column", IndexEntry.class);
81
82 d.addProps("*/unique", Unique.class);
83 d.addProps("*/unique/unique-column", IndexEntry.class);
84
85 final Object result = d.parse(sourceStream);
86 return (Databases) result;
87 }
88
89 /***
90 * @return Returns the debug.
91 */
92 public boolean isDebug() {
93 return debug;
94 }
95
96 /***
97 * @param debug
98 * The debug to set.
99 */
100 public void setDebug(boolean debug) {
101 this.debug = debug;
102 }
103 }