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