KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > xml > XMLConfig


1 package org.enhydra.xml;
2
3 import java.util.Properties JavaDoc;
4
5 import org.w3c.dom.Document JavaDoc;
6 import org.w3c.dom.Node JavaDoc;
7
8 /**
9  * @author Tweety
10  *
11  * A class representing
12  *
13  * @version 1.0
14  */

15 public class XMLConfig extends SearchElement {
16
17
18     /**
19      * Constructs an empty <code>SearchElement</code>.
20      */

21     public XMLConfig() {
22     }
23
24
25     /**
26      * Constructs an <code>XMLConfig</code> with the given
27      * document owner and node name.
28      *
29      * @param ownerDoc the document owner of the node, as a <code>Document</code>.
30      * @param nodeName the name of the node, as a <code>String</code>.
31      */

32     public XMLConfig(Document JavaDoc ownerDoc, String JavaDoc name) {
33         super(ownerDoc, name);
34     }
35
36
37     /**
38      * Constructs an <code>XMLConfig</code> from a given node
39      * (creates the children subtree too), as a <code>Node</code>
40      *
41      * @param node, as a <code>Node</code>.
42      */

43     public XMLConfig(Node JavaDoc node) {
44         super(node);
45     }
46
47
48     /**
49      * Constructs a <code>XMLConfig</code> from the given node,
50      * without creating entire children subtree.
51      *
52      * @param element, as a <code>XMLConfig</code>.
53      */

54     public XMLConfig(XMLConfig node) {
55         super((HashMapElement)node);
56     }
57
58
59     /**
60      * Creates new instance of the XMLConfig class from the given <code>Node</code>.
61      *
62      * @param node, as a <code>Node</code>.
63      *
64      * @return new instance of the XMLConfig class.
65      */

66     protected Node JavaDoc newElementInstance(Node JavaDoc node) {
67         return new XMLConfig(node);
68     }
69
70
71     /**
72      * Creates new instance of <code>XMLConfig</code> from a given document
73      * as a <code>Document</code>
74      *
75      * @param document document ant type of node.
76      *
77      * @return new instance of <code>XMLConfig</code> from a given document.
78      */

79     public static XMLConfig newXMLConfigInstance(Document JavaDoc document) {
80         Node JavaDoc root = document.getDocumentElement();
81         return new XMLConfig(root);
82     }
83
84
85     /**
86      * Returns <code>XMLConfig</code> as a subconfiguration with the given condition.
87      *
88      * @param document document ant type of node.
89      */

90     public XMLConfig getSection(String JavaDoc name) {
91         return (XMLConfig)getFirstSubElementsByCondition(name);
92     }
93
94
95 // /**
96
// * .
97
// */
98
// public void setText(String namePath, String text, boolean create) {
99
//
100
// if (!create) {
101
// setText(namePath,text);
102
// return;
103
// }
104
// NodeList nodes = this.getSubElementsByTagName(namePath);
105
// if (nodes != null && nodes.getLength() > 0)
106
// ((SearchElement) nodes.item(0)).setText(text);
107
// }
108
//
109
//
110
// /**
111
// * @return recursive funtion that fullfills the <code>list</code>
112
// * parameter with all the nodes in the given path.
113
// */
114
// private void createSubElementsByTag(String namePath) {
115
//
116
// String[] keys = namePath.split(this.TAG_SEPARATOR, 2);
117
// if (keys.length == 1) {
118
// List fList = (List) this.children.get(tagName);
119
// if (fList != null) {
120
// for (int i = 0; i < fList.size(); i++) {
121
// HashMapElement elm = (HashMapElement) fList.get(i);
122
// String val = (String) elm.getText();
123
// if (val != null)
124
// if (val.equals(tagValue))
125
// list.add(elm);
126
// }
127
// } else {
128
// Element newElement = new XMLConfig(this.ownerDocument,keys[0]);
129
// this.appendChild(newElement);
130
// }
131
// return ;
132
// }
133
// NodeList tagChildren = this.getChildrenByTagName(keys[0]);
134
// if (tagChildren != null) {
135
// for (int i = 0; i < tagChildren.getLength(); i++)
136
// ((SearchElement) tagChildren.item(i)).getSubElementsByTagText(keys[1], tagValue, list);
137
// } else {
138
// Element newElement = new XMLConfig(this.ownerDocument,keys[0]);
139
// this.appendChild(newElement);
140
// }
141
// }
142

143
144
145     public static void main(String JavaDoc[] args) {
146         try {
147             System.out.println("Reading document ...");
148             Document JavaDoc doc = XMLDocumentFactory.parse("input.xml");
149             System.out.println("Creating node ...");
150             XMLConfig node = XMLConfig.newXMLConfigInstance(doc);
151             System.out.println("Serialize node ...");
152
153             Properties JavaDoc prop = new Properties JavaDoc();
154             prop.put(javax.xml.transform.OutputKeys.DOCTYPE_PUBLIC,"http:///enhydra.org");
155             prop.put(javax.xml.transform.OutputKeys.VERSION,"1.1");
156             XMLDocumentFactory.serialize(node, "output.xml ",prop);
157             System.out.println("Searching ...");
158
159             for (int i=0; i < 1000; i++) {
160                 if ( i%10 == 0)
161                     System.out.print("\rprogress "+i/10+" %");
162                 XMLConfig section = node.getSection("database/package/package/package/package/package/package/table/column@id=EMAILADRESSE");
163 // node.getSubElementsByAttrValue("database/package/package/package/package/package/table@dbTableName","OLSERVER");
164
}
165             System.out.println("\rprogress 100 %");
166             System.out.println();
167
168 // NodeList nodeList = node.getSubElementsByAttrValue("database/package/package/package/package/package/table@dbTableName","OLSERVER");
169
XMLConfig section = node.getSection("database/package/package/package/package/package/package/table/column@id=EMAILADRESSE");
170             section.setAttr("type@javaType","Zoka");
171             if (section == null)
172                 System.out.println("section = null");
173             else {
174                 System.out.println("section = "+section);
175             }
176
177
178 // NodeList nodeList = node.getSubElementsByTagText("databaseManager/database/type","Oracle");
179
// NodeList nodeList = node.getSubElementsByCondition("database/package/package/package/package/package/table@dbTableName=OLSERVER");
180
// if (nodeList == null)
181
// System.out.println("nodeList = null");
182
// else {
183
// for (int i=0; i<nodeList.getLength(); i++)
184
// System.out.println("node["+i+"] = "+nodeList.item(i));
185
// }
186

187         } catch(Exception JavaDoc e) {
188             e.printStackTrace();
189             System.out.println("NOOOOOOOOOOOOO");
190         }
191     }
192 }
193
Popular Tags