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.Schema;
8 import com.quantum.sql.metadata.MetaDataXMLInterface;
10 import org.w3c.dom.Document;
11 import org.w3c.dom.Element;
12 import org.w3c.dom.Text;
17 public class ModelToXMLConverter {
19 private static final ModelToXMLConverter instance = new ModelToXMLConverter();
21 private ModelToXMLConverter() {
24 public static ModelToXMLConverter getInstance() {
25 return ModelToXMLConverter.instance;
28 public void createRoot(Document document) {
29 document.appendChild(document.createElement("SAVED_DATA"));
32 public void convert(Element bookmarkRoot, Bookmark b) {
33 Document document = bookmarkRoot.getOwnerDocument();
34 Element bookmark = MetaDataXMLInterface.createElementText(bookmarkRoot,"bookmark", ""); //$NON-NLS-1$ //$NON-NLS-2$
35 MetaDataXMLInterface.createElementText(bookmark,"name", b.getName()); //$NON-NLS-1$
36 MetaDataXMLInterface.createElementText(bookmark,"username", b.getUsername()); //$NON-NLS-1$
37 MetaDataXMLInterface.createElementText(bookmark,"password", b.getPassword()); //$NON-NLS-1$
38 MetaDataXMLInterface.createElementText(bookmark,"prompt", b.getPromptForPassword() ? "true" : "false"); //$NON-NLS-1$
39 MetaDataXMLInterface.createElementText(bookmark,"connect", b.getConnect()); //$NON-NLS-1$
40 MetaDataXMLInterface.createElementText(bookmark,"autoCommit", b.isAutoCommit() ? "true" : "false"); //$NON-NLS-1$
41 MetaDataXMLInterface.createElementText(bookmark,"autoCommitPreference", b.getAutoCommitPreference()); //$NON-NLS-1$
42 MetaDataXMLInterface.createElementText(bookmark,"driver", b.getDriver()); //$NON-NLS-1$
43 MetaDataXMLInterface.createElementText(bookmark,"type", b.getType()); //$NON-NLS-1$
44 MetaDataXMLInterface.createElementText(bookmark,"driverLocation", b.getDriverFile()); //$NON-NLS-1$
45 Element otherSchemas = (Element) bookmark.appendChild(document.createElement(Messages.getString("ExportXMLAction.OtherSchemas"))); //$NON-NLS-1$
46 Schema[] schemas = b.getSchemas();
47 for (int i = 0, length = (schemas == null) ? 0 : schemas.length;
50 if (!schemas[i].isDefault()) {
51 MetaDataXMLInterface.createElementText(
52 otherSchemas,Messages.getString("ExportXMLAction.SchemaName"), schemas[i].getName()); //$NON-NLS-1$
55 Entity[] quickList = b.getQuickListEntries();
56 Element quickListEntity = document.createElement("quickList");
57 for (int j = 0, length = (quickList == null) ? 0 : quickList.length;
60 ModelToXMLConverter.getInstance().convert(quickListEntity, quickList[j]);
62 bookmark.appendChild(quickListEntity);
63 convertQueryList(b, bookmark);
67 public void convert(Element parent, Entity entity) {
68 convert(parent, entity, true);
70 public void convert(Element parent, Entity entity, boolean recurse) {
71 Document document = parent.getOwnerDocument();
72 Element element = document.createElement(getEntityDOMNodeName(entity));
73 element.setAttribute("name", entity.getName());
74 if (entity.getSchema() != null) {
75 element.setAttribute("schema", entity.getSchema());
78 convert(element, entity.getColumns());
80 parent.appendChild(element);
83 public void convert(Element element, Column[] columns) {
84 for (int i = 0, length = (columns == null) ? 0 : columns.length;
87 convert(element, columns[i]);
91 public void convert(Element parent, Column column) {
92 Document document = parent.getOwnerDocument();
93 Element element = document.createElement("column");
94 element.setAttribute("name", column.getName());
95 element.setAttribute("type", column.getTypeName());
96 element.setAttribute("primaryKey", column.isPrimaryKey() ? "true" : "false");
97 element.setAttribute("nullable", column.isNullable() ? "true" : "false");
98 if (column.isNumeric()) {
99 if (column.getSize() > 0) {
100 element.setAttribute("size", String.valueOf(column.getSize()));
102 if (column.getNumberOfFractionalDigits() > 0) {
103 element.setAttribute("numberOfFractionalDigits", String.valueOf(column.getNumberOfFractionalDigits()));
106 if (column.getSize() > 0) {
107 element.setAttribute("size", String.valueOf(column.getSize()));
110 if (column.getRemarks().length() > 0)
111 MetaDataXMLInterface.createElementText(element, "remarks", column.getRemarks());
112 parent.appendChild(element);
116 public String getEntityDOMNodeName(Entity entity) {
117 if (Entity.TABLE_TYPE.equals(entity.getType())) {
119 } else if (Entity.VIEW_TYPE.equals(entity.getType())) {
121 } else if (Entity.SEQUENCE_TYPE.equals(entity.getType())) {
128 public void convertQueryList(
132 Document document = parent.getOwnerDocument();
133 String[] queryList = bookmark.getQueries();
134 Element queryListEntity = document.createElement("queryList");
135 for (int j = 0, length = (queryList == null) ? 0 : queryList.length;
138 Element query = document.createElement("query");
140 // will have more possibilities later
141 Element queryString = document.createElement("queryString");
142 Text queryText = document.createTextNode(queryList[j]);
143 queryString.appendChild(queryText);
144 query.appendChild(queryString);
145 queryListEntity.appendChild(query);
147 parent.appendChild(queryListEntity);