2 * Created on 15/04/2003
5 package com.quantum.sql.metadata;
7 import java.util.Vector;
9 import com.quantum.util.StringMatrix;
11 import org.w3c.dom.Document;
12 import org.w3c.dom.Element;
13 import org.w3c.dom.Node;
14 import org.w3c.dom.NodeList;
15 import org.w3c.dom.Text;
20 * Handles interface between an ObjectMetaData and XML storage.
21 * It can write an existing metadata object to XML and generate
22 * a new ObjectMetaData from existing XML.
24 public class MetaDataXMLInterface {
27 * Exports an ObjectMetaData to an XML document, under a root Element
28 * @param metadata The ObjectMetaData to export
29 * @param doc A valid XML document to receive the export
30 * @param root The Element under which all the XML data will be written
32 public static void metaDataToXML(ObjectMetaData metadata, Document doc, Element root) {
33 StringMatrix columns = metadata.getColumns();
35 stringMatrixToXML(columns, doc, root, "COLUMN"); //$NON-NLS-1$
36 StringMatrix primaryKeys = metadata.getPrimaryKeys();
37 if (primaryKeys != null)
38 stringMatrixToXML(primaryKeys, doc, root, "PRIMARY_KEY_ITEM"); //$NON-NLS-1$
39 StringMatrix foreignKeys = metadata.getForeignKeys();
40 if (foreignKeys != null)
41 stringMatrixToXML(foreignKeys, doc, root, "FOREIGN_KEY_ITEM"); //$NON-NLS-1$
42 StringMatrix indexInfo = metadata.getIndexInfo();
43 if (indexInfo != null)
44 stringMatrixToXML(indexInfo, doc, root, "INDEX_ITEM"); //$NON-NLS-1$
48 * Imports an XML element to an ObjectMetaData. The format has to be the same as extracted by
49 * the MetaDataToXML function.
50 * @param metadata The ObjectMetaData to fill up. Usually empty.
51 * @param root The Element having all the XML data
53 public static void xmlToMetaData(ObjectMetaData metadata, Element root) {
54 StringMatrix columns = new StringMatrix();
55 MetaDataXMLInterface.xmlToStringMatrix(columns, root, "COLUMN"); //$NON-NLS-1$
56 metadata.setColumns(columns);
57 StringMatrix primaryKeys = new StringMatrix();
58 MetaDataXMLInterface.xmlToStringMatrix(primaryKeys, root, "PRIMARY_KEY_ITEM"); //$NON-NLS-1$
59 metadata.setPrimaryKeys(primaryKeys);
60 StringMatrix foreignKeys = new StringMatrix();
61 MetaDataXMLInterface.xmlToStringMatrix(foreignKeys, root, "FOREIGN_KEY_ITEM"); //$NON-NLS-1$
62 metadata.setForeignKeys(foreignKeys);
63 StringMatrix indexInfo = new StringMatrix();
64 MetaDataXMLInterface.xmlToStringMatrix(indexInfo, root, "INDEX_ITEM"); //$NON-NLS-1$
65 metadata.setIndexInfo(indexInfo);
69 * Adds a StringMatrix to an XML document
70 * @param matrix The StringMatrix to add
71 * @param doc The XmlDocument to which it will be added
72 * @param root An element of the previous document under which the info will be added
73 * @param sub A key under which each row of the StringMatrix will be added
75 public static void stringMatrixToXML(StringMatrix matrix, Document doc, Element root, String sub) {
76 for (int i = 0; i < matrix.size(); i++) {
77 Element localRoot = (Element) root.appendChild(doc.createElement(sub));
78 for (int j = 0; j < matrix.getNumColumns(); j++) {
79 String key = matrix.getHeaderColumn(j);
80 Element tableName = (Element) localRoot.appendChild(doc.createElement(key));
81 String value = matrix.get(key, i);
83 tableName.appendChild(doc.createTextNode(value));
89 * Fills a StringMatrix with the data from XML, usually extracted with the StringMatrixToXML function
90 * @param matrix The matrix to fill up, usually empty.
91 * @param root The Element with all the data in XML DOM
92 * @param sub The String to select the Nodes which interest us. Only the selected nodes will be added
93 * to the StringMatrix.
95 private static void xmlToStringMatrix(StringMatrix matrix, Element root, String sub) {
96 NodeList columns = root.getElementsByTagName(sub);
97 for (int i = 0; i < columns.getLength(); i++) {
98 Node column = columns.item(i);
99 NodeList columnList = column.getChildNodes();
100 for (int j = 0; j < columnList.getLength(); j++) {
101 Node node = columnList.item(j);
102 String header = node.getNodeName();
103 if (header.equals("#text")) //$NON-NLS-1$
106 if (node != null && node.hasChildNodes()) {
107 Node valueNode = node.getFirstChild();
108 if (valueNode instanceof Text) {
109 value = valueNode.getNodeValue();
112 if (!matrix.contains(header))
113 matrix.addHeader(header);
114 matrix.addAt(header, value, i);
119 * Creates a new Element with a text value
125 public static Element createElementText(Element root, String key, String value){
126 // get the XmlDocument for use as a factory
127 Document doc = root.getOwnerDocument();
128 Element newElement = doc.createElement(key);
130 root.appendChild(newElement);
131 if (value != null && value.length() > 0)
133 Text valueText = doc.createTextNode(value);
134 newElement.appendChild(valueText);
139 public static String getElementText(Element root, String key){
140 return getElementText(root, key, "");
143 * gets the text value from an element or a child of it
144 * @param root element root
145 * @param key key to search
146 * @param defValue default string value if not found
149 public static String getElementText(Element root, String key, String defValue){
150 // get the XmlDocument for use as a factory
151 String value = defValue;
152 if (root.getNodeName().equals(key)){
153 value = extractText(root, defValue);
155 NodeList children = root.getElementsByTagName(key);
156 if (children.getLength() > 0) {
157 Element column = (Element) children.item(0);
158 value = extractText(column, defValue);
164 public static String extractText(Element node, String defValue){
165 String value = defValue;
166 if (node != null && node.hasChildNodes()) {
167 Node valueNode = node.getFirstChild();
168 if (valueNode instanceof Text) {
169 value = valueNode.getNodeValue();
176 * Gets a Vector with the String values of the keys
177 * that are children of 'root' and that match 'key'
182 public static Vector getVectorText(Element root, String key) {
183 Vector result = new Vector();
184 NodeList children = root.getElementsByTagName(key);
185 for (int i = 0; i < children.getLength(); i++) {
186 Element column = (Element) children.item(i);
187 result.add(extractText(column, ""));