reduce warnings due to core.runtime deprecations
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / model / xml / ModelToXMLConverter.java
1 package com.quantum.model.xml;
2
3 import com.quantum.Messages;
4 import com.quantum.model.Bookmark;
5 import com.quantum.model.Column;
6 import com.quantum.model.Entity;
7 import com.quantum.model.JDBCDriver;
8 import com.quantum.model.Schema;
9 import com.quantum.sql.metadata.MetaDataXMLInterface;
10
11 import org.w3c.dom.Document;
12 import org.w3c.dom.Element;
13 import org.w3c.dom.Text;
14
15 /**
16  * @author BC
17  */
18 public class ModelToXMLConverter {
19     
20     private static final ModelToXMLConverter instance = new ModelToXMLConverter();
21     
22     private ModelToXMLConverter() {
23     }
24     
25     public static ModelToXMLConverter getInstance() {
26         return ModelToXMLConverter.instance;
27     }
28
29     public void createRoot(Document document) {
30         document.appendChild(document.createElement("SAVED_DATA"));
31     }
32     public void convert(Element bookmarkRoot, JDBCDriver jdbcDriver) {
33         Document document = bookmarkRoot.getOwnerDocument();
34         Element driverNode = document.createElement("jdbcDriver");
35         driverNode.setAttribute("name", jdbcDriver.getName());
36         driverNode.setAttribute("version", jdbcDriver.getVersion());
37         driverNode.setAttribute("jarFileName", jdbcDriver.getJarFileName());
38         driverNode.setAttribute("className", jdbcDriver.getClassName());
39         bookmarkRoot.appendChild(driverNode);
40     }
41
42     public void convert(Element bookmarkRoot, Bookmark b) {
43         Document document = bookmarkRoot.getOwnerDocument();
44         Element bookmark = MetaDataXMLInterface.createElementText(bookmarkRoot,"bookmark", ""); //$NON-NLS-1$ //$NON-NLS-2$
45         MetaDataXMLInterface.createElementText(bookmark,"name", b.getName()); //$NON-NLS-1$
46         MetaDataXMLInterface.createElementText(bookmark,"username", b.getUsername()); //$NON-NLS-1$
47         MetaDataXMLInterface.createElementText(bookmark,"password", b.getPassword()); //$NON-NLS-1$
48         MetaDataXMLInterface.createElementText(bookmark,"prompt", b.getPromptForPassword() ? "true" : "false"); //$NON-NLS-1$
49         MetaDataXMLInterface.createElementText(bookmark,"connect", b.getConnect()); //$NON-NLS-1$
50                 MetaDataXMLInterface.createElementText(bookmark,"autoCommit", b.isAutoCommit() ? "true" : "false"); //$NON-NLS-1$
51                 MetaDataXMLInterface.createElementText(bookmark,"autoCommitPreference", b.getAutoCommitPreference()); //$NON-NLS-1$
52         MetaDataXMLInterface.createElementText(bookmark,"driver", b.getJDBCDriver().getClassName()); //$NON-NLS-1$
53         MetaDataXMLInterface.createElementText(bookmark,"type", b.getType()); //$NON-NLS-1$
54                 MetaDataXMLInterface.createElementText(bookmark,"driverLocation", b.getJDBCDriver().getJarFileName()); //$NON-NLS-1$
55         Element otherSchemas = (Element) bookmark.appendChild(document.createElement(Messages.getString("ExportXMLAction.OtherSchemas"))); //$NON-NLS-1$
56         Schema[] schemas = b.getSchemas();
57         for (int i = 0, length = (schemas == null) ? 0 : schemas.length;
58             i < length;
59             i++) {
60             if (!schemas[i].isDefault()) {
61                 MetaDataXMLInterface.createElementText(
62                     otherSchemas,Messages.getString("ExportXMLAction.SchemaName"), schemas[i].getName()); //$NON-NLS-1$
63             }
64         }
65         Entity[] quickList = b.getQuickListEntries();
66         Element quickListEntity = document.createElement("quickList");
67         for (int j = 0, length = (quickList == null) ? 0 : quickList.length;
68             j < length;
69             j++) {
70             ModelToXMLConverter.getInstance().convert(quickListEntity, quickList[j]);
71         }
72         bookmark.appendChild(quickListEntity);
73         convertQueryList(b, bookmark);
74     }
75
76     
77     public void convert(Element parent, Entity entity) {
78         convert(parent, entity, true);
79     }
80     public void convert(Element parent, Entity entity, boolean recurse) {
81         Document document = parent.getOwnerDocument();
82         Element element = document.createElement(getEntityDOMNodeName(entity));
83         element.setAttribute("name", entity.getName());
84         if (entity.getSchema() != null) {
85             element.setAttribute("schema", entity.getSchema());
86         }
87         if (recurse) {
88             convert(element, entity.getColumns());
89         }
90         parent.appendChild(element);
91     }
92
93     public void convert(Element element, Column[] columns) {
94         for (int i = 0, length = (columns == null) ? 0 : columns.length;
95             i < length;
96             i++) {
97             convert(element, columns[i]);
98         }
99     }
100
101     public void convert(Element parent, Column column) {
102         Document document = parent.getOwnerDocument();
103         Element element = document.createElement("column");
104         element.setAttribute("name", column.getName());
105         element.setAttribute("type", column.getTypeName());
106         element.setAttribute("primaryKey", column.isPrimaryKey() ? "true" : "false");
107         element.setAttribute("nullable", column.isNullable() ? "true" : "false");
108         if (column.isNumeric()) {
109             if (column.getSize() > 0) {
110                 element.setAttribute("size", String.valueOf(column.getSize()));
111             }
112             if (column.getNumberOfFractionalDigits() > 0) {
113                 element.setAttribute("numberOfFractionalDigits", String.valueOf(column.getNumberOfFractionalDigits()));
114             }
115         } else {
116             if (column.getSize() > 0) {
117                 element.setAttribute("size", String.valueOf(column.getSize()));
118             }
119         }
120                 if (column.getRemarks().length() > 0)
121                         MetaDataXMLInterface.createElementText(element, "remarks", column.getRemarks());
122         parent.appendChild(element);
123     }
124
125
126     public String getEntityDOMNodeName(Entity entity) {
127         if (Entity.TABLE_TYPE.equals(entity.getType())) {
128             return "table";
129         } else if (Entity.VIEW_TYPE.equals(entity.getType())) {
130             return "view";
131         } else if (Entity.SEQUENCE_TYPE.equals(entity.getType())) {
132             return "sequence";
133         } else {
134             return "unknown";
135         }
136     }
137
138     public void convertQueryList(
139         Bookmark bookmark,
140         Element parent) {
141             
142         Document document = parent.getOwnerDocument();
143         String[] queryList = bookmark.getQueries();
144         Element queryListEntity = document.createElement("queryList");
145         for (int j = 0, length = (queryList == null) ? 0 : queryList.length;
146             j < length;
147             j++) {
148             Element query = document.createElement("query");
149             
150             // will have more possibilities later
151             Element queryString = document.createElement("queryString");
152             Text queryText = document.createTextNode(queryList[j]);
153             queryString.appendChild(queryText);
154             query.appendChild(queryString);
155             queryListEntity.appendChild(query);
156         }
157         parent.appendChild(queryListEntity);
158     }
159     
160 }