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