1 package com.quantum.model.xml;
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.JDBCDriver;
8 import com.quantum.model.Schema;
9 import com.quantum.sql.metadata.MetaDataXMLInterface;
11 import org.w3c.dom.Document;
12 import org.w3c.dom.Element;
13 import org.w3c.dom.Text;
18 public class ModelToXMLConverter {
20 private static final ModelToXMLConverter instance = new ModelToXMLConverter();
22 private ModelToXMLConverter() {
25 public static ModelToXMLConverter getInstance() {
26 return ModelToXMLConverter.instance;
29 public void createRoot(Document document) {
30 document.appendChild(document.createElement("SAVED_DATA"));
32 public void convert(Element bookmarkRoot, JDBCDriver jdbcDriver) {
33 Document document = bookmarkRoot.getOwnerDocument();
34 Element driverNode = document.createElement("jdbcDriver");
35 driverNode.setAttribute("name", jdbcDriver.getName());
36 driverNode.setAttribute("version", jdbcDriver.getVersion());
37 driverNode.setAttribute("jarFileName", jdbcDriver.getJarFileName());
38 driverNode.setAttribute("className", jdbcDriver.getClassName());
39 bookmarkRoot.appendChild(driverNode);
42 public void convert(Element bookmarkRoot, Bookmark b) {
43 Document document = bookmarkRoot.getOwnerDocument();
44 Element bookmark = MetaDataXMLInterface.createElementText(bookmarkRoot,"bookmark", ""); //$NON-NLS-1$ //$NON-NLS-2$
45 MetaDataXMLInterface.createElementText(bookmark,"name", b.getName()); //$NON-NLS-1$
46 MetaDataXMLInterface.createElementText(bookmark,"username", b.getUsername()); //$NON-NLS-1$
47 MetaDataXMLInterface.createElementText(bookmark,"password", b.getPassword()); //$NON-NLS-1$
48 MetaDataXMLInterface.createElementText(bookmark,"prompt", b.getPromptForPassword() ? "true" : "false"); //$NON-NLS-1$
49 MetaDataXMLInterface.createElementText(bookmark,"connect", b.getConnect()); //$NON-NLS-1$
50 MetaDataXMLInterface.createElementText(bookmark,"autoCommit", b.isAutoCommit() ? "true" : "false"); //$NON-NLS-1$
51 MetaDataXMLInterface.createElementText(bookmark,"autoCommitPreference", b.getAutoCommitPreference()); //$NON-NLS-1$
52 MetaDataXMLInterface.createElementText(bookmark,"driver", b.getJDBCDriver().getClassName()); //$NON-NLS-1$
53 MetaDataXMLInterface.createElementText(bookmark,"type", b.getType()); //$NON-NLS-1$
54 MetaDataXMLInterface.createElementText(bookmark,"driverLocation", b.getJDBCDriver().getJarFileName()); //$NON-NLS-1$
55 Element otherSchemas = (Element) bookmark.appendChild(document.createElement(Messages.getString("ExportXMLAction.OtherSchemas"))); //$NON-NLS-1$
56 Schema[] schemas = b.getSchemas();
57 for (int i = 0, length = (schemas == null) ? 0 : schemas.length;
60 if (!schemas[i].isDefault()) {
61 MetaDataXMLInterface.createElementText(
62 otherSchemas,Messages.getString("ExportXMLAction.SchemaName"), schemas[i].getName()); //$NON-NLS-1$
65 Entity[] quickList = b.getQuickListEntries();
66 Element quickListEntity = document.createElement("quickList");
67 for (int j = 0, length = (quickList == null) ? 0 : quickList.length;
70 ModelToXMLConverter.getInstance().convert(quickListEntity, quickList[j]);
72 bookmark.appendChild(quickListEntity);
73 convertQueryList(b, bookmark);
77 public void convert(Element parent, Entity entity) {
78 convert(parent, entity, true);
80 public void convert(Element parent, Entity entity, boolean recurse) {
81 Document document = parent.getOwnerDocument();
82 Element element = document.createElement(getEntityDOMNodeName(entity));
83 element.setAttribute("name", entity.getName());
84 if (entity.getSchema() != null) {
85 element.setAttribute("schema", entity.getSchema());
88 convert(element, entity.getColumns());
90 parent.appendChild(element);
93 public void convert(Element element, Column[] columns) {
94 for (int i = 0, length = (columns == null) ? 0 : columns.length;
97 convert(element, columns[i]);
101 public void convert(Element parent, Column column) {
102 Document document = parent.getOwnerDocument();
103 Element element = document.createElement("column");
104 element.setAttribute("name", column.getName());
105 element.setAttribute("type", column.getTypeName());
106 element.setAttribute("primaryKey", column.isPrimaryKey() ? "true" : "false");
107 element.setAttribute("nullable", column.isNullable() ? "true" : "false");
108 if (column.isNumeric()) {
109 if (column.getSize() > 0) {
110 element.setAttribute("size", String.valueOf(column.getSize()));
112 if (column.getNumberOfFractionalDigits() > 0) {
113 element.setAttribute("numberOfFractionalDigits", String.valueOf(column.getNumberOfFractionalDigits()));
116 if (column.getSize() > 0) {
117 element.setAttribute("size", String.valueOf(column.getSize()));
120 if (column.getRemarks().length() > 0)
121 MetaDataXMLInterface.createElementText(element, "remarks", column.getRemarks());
122 parent.appendChild(element);
126 public String getEntityDOMNodeName(Entity entity) {
127 if (Entity.TABLE_TYPE.equals(entity.getType())) {
129 } else if (Entity.VIEW_TYPE.equals(entity.getType())) {
131 } else if (Entity.SEQUENCE_TYPE.equals(entity.getType())) {
138 public void convertQueryList(
142 Document document = parent.getOwnerDocument();
143 String[] queryList = bookmark.getQueries();
144 Element queryListEntity = document.createElement("queryList");
145 for (int j = 0, length = (queryList == null) ? 0 : queryList.length;
148 Element query = document.createElement("query");
150 // will have more possibilities later
151 Element queryString = document.createElement("queryString");
152 Text queryText = document.createTextNode(queryList[j]);
153 queryString.appendChild(queryText);
154 query.appendChild(queryString);
155 queryListEntity.appendChild(query);
157 parent.appendChild(queryListEntity);