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("jarFileName", jdbcDriver.getJarFileName());
41 driverNode.setAttribute("className", jdbcDriver.getClassName());
42 bookmarkRoot.appendChild(driverNode);
45 public void convert(Element bookmarkRoot, Bookmark b) {
46 Document document = bookmarkRoot.getOwnerDocument();
47 Element bookmark = MetaDataXMLInterface.createElementText(bookmarkRoot,"bookmark", ""); //$NON-NLS-1$ //$NON-NLS-2$
48 MetaDataXMLInterface.createElementText(bookmark,"name", b.getName()); //$NON-NLS-1$
49 MetaDataXMLInterface.createElementText(bookmark,"username", b.getUsername()); //$NON-NLS-1$
50 MetaDataXMLInterface.createElementText(bookmark,"password", b.getPassword()); //$NON-NLS-1$
51 MetaDataXMLInterface.createElementText(bookmark,"prompt", b.getPromptForPassword() ? "true" : "false"); //$NON-NLS-1$
52 MetaDataXMLInterface.createElementText(bookmark,"connect", b.getConnect()); //$NON-NLS-1$
53 MetaDataXMLInterface.createElementText(bookmark,"autoCommit", b.isAutoCommit() ? "true" : "false"); //$NON-NLS-1$
54 MetaDataXMLInterface.createElementText(bookmark,"autoCommitPreference", b.getAutoCommitPreference()); //$NON-NLS-1$
55 MetaDataXMLInterface.createElementText(bookmark,"driver", b.getJDBCDriver().getClassName()); //$NON-NLS-1$
56 MetaDataXMLInterface.createElementText(bookmark,"type", b.getJDBCDriver().getType()); //$NON-NLS-1$
57 MetaDataXMLInterface.createElementText(bookmark,"driverLocation", b.getJDBCDriver().getJarFileName()); //$NON-NLS-1$
58 Element otherSchemas = (Element) bookmark.appendChild(document.createElement("Other_Schemas")); //$NON-NLS-1$
59 otherSchemas.setAttribute("schemaRule",
62 : b.useUsernameAsSchema() ? "useDefault" : "useSelected");
64 Schema[] schemas = b.getSchemaSelections();
65 for (int i = 0, length = (schemas == null) ? 0 : schemas.length; i < length; i++) {
66 MetaDataXMLInterface.createElementText(
67 otherSchemas,"schema", schemas[i].getName()); //$NON-NLS-1$
70 Entity[] quickList = b.getQuickListEntries();
71 Element quickListEntity = document.createElement("quickList");
72 for (int j = 0, length = (quickList == null) ? 0 : quickList.length;
75 ModelToXMLConverter.getInstance().convert(quickListEntity, quickList[j]);
77 bookmark.appendChild(quickListEntity);
78 convertQueryList(b, bookmark);
82 public void convert(Element parent, Entity entity) {
83 convert(parent, entity, true);
85 public void convert(Element parent, Entity entity, boolean recurse) {
86 Document document = parent.getOwnerDocument();
87 Element element = document.createElement(getEntityDOMNodeName(entity));
88 element.setAttribute("name", entity.getName());
89 if (entity.getSchema() != null) {
90 element.setAttribute("schema", entity.getSchema());
94 convert(element, entity.getColumns());
95 } catch (NotConnectedException e) {
96 } catch (SQLException e) {
99 parent.appendChild(element);
102 public void convert(Element element, Column[] columns) {
103 for (int i = 0, length = (columns == null) ? 0 : columns.length;
106 convert(element, columns[i]);
110 public void convert(Element parent, Column column) {
111 Document document = parent.getOwnerDocument();
112 Element element = document.createElement("column");
113 element.setAttribute("name", column.getName());
114 element.setAttribute("type", column.getTypeName());
115 element.setAttribute("primaryKey", column.isPrimaryKey() ? "true" : "false");
116 element.setAttribute("nullable", column.isNullable() ? "true" : "false");
117 if (column.isNumeric()) {
118 if (column.getSize() > 0) {
119 element.setAttribute("size", String.valueOf(column.getSize()));
121 if (column.getNumberOfFractionalDigits() > 0) {
122 element.setAttribute("numberOfFractionalDigits", String.valueOf(column.getNumberOfFractionalDigits()));
125 if (column.getSize() > 0) {
126 element.setAttribute("size", String.valueOf(column.getSize()));
129 if (column.getRemarks().length() > 0)
130 MetaDataXMLInterface.createElementText(element, "remarks", column.getRemarks());
131 parent.appendChild(element);
135 public String getEntityDOMNodeName(Entity entity) {
136 if (Entity.TABLE_TYPE.equals(entity.getType())) {
138 } else if (Entity.VIEW_TYPE.equals(entity.getType())) {
140 } else if (Entity.SEQUENCE_TYPE.equals(entity.getType())) {
147 public void convertQueryList(
151 Document document = parent.getOwnerDocument();
152 String[] queryList = bookmark.getQueries();
153 Element queryListEntity = document.createElement("queryList");
154 for (int j = 0, length = (queryList == null) ? 0 : queryList.length;
157 Element query = document.createElement("query");
159 // will have more possibilities later
160 Element queryString = document.createElement("queryString");
161 Text queryText = document.createTextNode(queryList[j]);
162 queryString.appendChild(queryText);
163 query.appendChild(queryString);
164 queryListEntity.appendChild(query);
166 parent.appendChild(queryListEntity);