View Javadoc

1   /*
2    * joey-gen and its relative products are published under the terms
3    * of the Apache Software License.
4    * 
5    * Created on 2004/08/16 14:24:25
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          //WmTable
64          d.addProps("*/database/table", Table.class);
65  
66          //d.addProps("*/column/id-method-parameter", WmIdMethodParameter.class,
67          // "addIdMethodParameter");
68  
69          d.addProps("*/column", Column.class);
70          //d.addProps("*/column/inheritance", WmInheritance.class,
71          // "addInheritance");
72          //d.addProps("*/column/property", WmProperty.class, "addProperty");
73          //d.addProps("*/property/format", WmColumnFormat.class, "setFormat");
74          //d.addProps("*/column/format", WmColumnFormat.class, "setFormat");
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 }