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());
106 convert(element, entity.getColumns());
107 } catch (NotConnectedException e) {
108 } catch (SQLException e) {
111 parent.appendChild(element);
114 public void convert(Element element, Column[] columns) {
115 for (int i = 0, length = (columns == null) ? 0 : columns.length;
118 convert(element, columns[i]);
122 public void convert(Element parent, Column column) {
123 Document document = parent.getOwnerDocument();
124 Element element = document.createElement("column");
125 element.setAttribute("name", column.getName());
126 element.setAttribute("type", column.getTypeName());
127 element.setAttribute("primaryKey", column.isPrimaryKey() ? "true" : "false");
128 element.setAttribute("nullable", column.isNullable() ? "true" : "false");
129 if (column.isNumeric()) {
130 if (column.getSize() > 0) {
131 element.setAttribute("size", String.valueOf(column.getSize()));
133 if (column.getNumberOfFractionalDigits() > 0) {
134 element.setAttribute("numberOfFractionalDigits", String.valueOf(column.getNumberOfFractionalDigits()));
137 if (column.getSize() > 0) {
138 element.setAttribute("size", String.valueOf(column.getSize()));
141 if (column.getRemarks().length() > 0)
142 MetaDataXMLInterface.createElementText(element, "remarks", column.getRemarks());
143 parent.appendChild(element);
147 public String getEntityDOMNodeName(Entity entity) {
148 if (Entity.TABLE_TYPE.equals(entity.getType())) {
150 } else if (Entity.VIEW_TYPE.equals(entity.getType())) {
152 } else if (Entity.SEQUENCE_TYPE.equals(entity.getType())) {
159 public void convertQueryList(
163 Document document = parent.getOwnerDocument();
164 String[] queryList = bookmark.getQueries();
165 Element queryListEntity = document.createElement("queryList");
166 for (int j = 0, length = (queryList == null) ? 0 : queryList.length;
169 Element query = document.createElement("query");
171 // will have more possibilities later
172 Element queryString = document.createElement("queryString");
173 Text queryText = document.createTextNode(queryList[j]);
174 queryString.appendChild(queryText);
175 query.appendChild(queryString);
176 queryListEntity.appendChild(query);
178 parent.appendChild(queryListEntity);