latest quantum sources 2.3.2
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / sql / metadata / MetaDataXMLInterface.java
1 /*
2  * Created on 15/04/2003
3  *
4  */
5 package com.quantum.sql.metadata;
6
7 import java.util.Vector;
8
9 import com.quantum.util.StringMatrix;
10
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;
16
17
18 /**
19  * @author panic
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.
23  */
24 public class MetaDataXMLInterface {
25
26         /**
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
31          */
32         public static void metaDataToXML(ObjectMetaData metadata, Document doc, Element root) {
33                 StringMatrix columns = metadata.getColumns();
34                 if (columns != null)
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$
45         }
46
47         /**
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
52          */
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);
66         }
67
68         /**
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
74          */
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);
82                                 if (value != null)
83                                         tableName.appendChild(doc.createTextNode(value));
84                         }
85                 }
86         }
87
88         /**
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.
94         */
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$
104                                         continue;
105                                 String value = null;
106                                 if (node != null && node.hasChildNodes()) {
107                                         Node valueNode = node.getFirstChild();
108                                         if (valueNode instanceof Text) {
109                                                 value = valueNode.getNodeValue();
110                                         }
111                                 }
112                                 if (!matrix.contains(header))
113                                         matrix.addHeader(header);
114                                 matrix.addAt(header, value, i);
115                         }
116                 }
117         }
118         /**
119          * Creates a new Element with a text value
120          * @param root
121          * @param key
122          * @param value
123          * @return
124          */
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);
129         
130                 root.appendChild(newElement);
131                 if (value != null && value.length() > 0)
132                 {
133                    Text valueText = doc.createTextNode(value);
134                    newElement.appendChild(valueText);
135                 }
136                 return newElement;
137         }
138       
139         public static String getElementText(Element root, String key){
140                 return getElementText(root, key, "");
141         }
142         /**
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
147          * @return
148          */
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);
154                 } else {
155                         NodeList children = root.getElementsByTagName(key);
156                         if (children.getLength() > 0) {
157                                 Element column = (Element) children.item(0);
158                                 value = extractText(column, defValue);
159                         }
160                 }
161                 return value;
162         }
163
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();
170                         }
171                 }
172                 return value;
173         }
174         
175         /**
176          * Gets a Vector with the String values of the keys
177          *  that are children of 'root' and that match 'key'
178          * @param root
179          * @param key
180          * @return
181          */
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, ""));
188                 }
189                 return result;
190         }
191 }