1 package com.quantum.model.xml;
3 import java.sql.SQLException;
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;
13 import org.w3c.dom.Document;
14 import org.w3c.dom.Element;
15 import org.w3c.dom.Text;
20 public class ModelToXMLConverter {
22 private static final ModelToXMLConverter instance = new ModelToXMLConverter();
24 private ModelToXMLConverter() {
27 public static ModelToXMLConverter getInstance() {
28 return ModelToXMLConverter.instance;
31 public void createRoot(Document document) {
32 document.appendChild(document.createElement("SAVED_DATA"));
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());
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);
51 bookmarkRoot.appendChild(driverNode);
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]);
70 Element otherSchemas = (Element) bookmark.appendChild(document.createElement("Other_Schemas")); //$NON-NLS-1$
71 otherSchemas.setAttribute("schemaRule",
74 : b.useUsernameAsSchema() ? "useDefault" : "useSelected");
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$
82 Entity[] quickList = b.getQuickListEntries();
83 Element quickListEntity = document.createElement("quickList");
84 for (int j = 0, length = (quickList == null) ? 0 : quickList.length;
87 ModelToXMLConverter.getInstance().convert(quickListEntity, quickList[j]);
89 bookmark.appendChild(quickListEntity);
90 convertQueryList(b, bookmark);
94 public void convert(Element parent, Entity entity) {
95 convert(parent, entity, true);
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());
104 element.setAttribute("isSynonym", entity.isSynonym() ? "true" : "false");
107 convert(element, entity.getColumns());
108 } catch (NotConnectedException e) {
109 } catch (SQLException e) {
112 parent.appendChild(element);
115 public void convert(Element element, Column[] columns) {
116 for (int i = 0, length = (columns == null) ? 0 : columns.length;
119 convert(element, columns[i]);
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()));
134 if (column.getNumberOfFractionalDigits() > 0) {
135 element.setAttribute("numberOfFractionalDigits", String.valueOf(column.getNumberOfFractionalDigits()));
138 if (column.getSize() > 0) {
139 element.setAttribute("size", String.valueOf(column.getSize()));
142 if (column.getRemarks().length() > 0)
143 MetaDataXMLInterface.createElementText(element, "remarks", column.getRemarks());
144 parent.appendChild(element);
148 public String getEntityDOMNodeName(Entity entity) {
149 if (Entity.TABLE_TYPE.equals(entity.getType())) {
151 } else if (Entity.VIEW_TYPE.equals(entity.getType())) {
153 } else if (Entity.SEQUENCE_TYPE.equals(entity.getType())) {
160 public void convertQueryList(
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;
170 Element query = document.createElement("query");
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);
179 parent.appendChild(queryListEntity);