001: package org.enhydra.xml;
002:
003: import java.util.Properties;
004:
005: import org.w3c.dom.Document;
006: import org.w3c.dom.Node;
007:
008: /**
009: * @author Tweety
010: *
011: * A class representing
012: *
013: * @version 1.0
014: */
015: public class XMLConfig extends SearchElement {
016:
017: /**
018: * Constructs an empty <code>SearchElement</code>.
019: */
020: public XMLConfig() {
021: }
022:
023: /**
024: * Constructs an <code>XMLConfig</code> with the given
025: * document owner and node name.
026: *
027: * @param ownerDoc the document owner of the node, as a <code>Document</code>.
028: * @param nodeName the name of the node, as a <code>String</code>.
029: */
030: public XMLConfig(Document ownerDoc, String name) {
031: super (ownerDoc, name);
032: }
033:
034: /**
035: * Constructs an <code>XMLConfig</code> from a given node
036: * (creates the children subtree too), as a <code>Node</code>
037: *
038: * @param node, as a <code>Node</code>.
039: */
040: public XMLConfig(Node node) {
041: super (node);
042: }
043:
044: /**
045: * Constructs a <code>XMLConfig</code> from the given node,
046: * without creating entire children subtree.
047: *
048: * @param element, as a <code>XMLConfig</code>.
049: */
050: public XMLConfig(XMLConfig node) {
051: super ((HashMapElement) node);
052: }
053:
054: /**
055: * Creates new instance of the XMLConfig class from the given <code>Node</code>.
056: *
057: * @param node, as a <code>Node</code>.
058: *
059: * @return new instance of the XMLConfig class.
060: */
061: protected Node newElementInstance(Node node) {
062: return new XMLConfig(node);
063: }
064:
065: /**
066: * Creates new instance of <code>XMLConfig</code> from a given document
067: * as a <code>Document</code>
068: *
069: * @param document document ant type of node.
070: *
071: * @return new instance of <code>XMLConfig</code> from a given document.
072: */
073: public static XMLConfig newXMLConfigInstance(Document document) {
074: Node root = document.getDocumentElement();
075: return new XMLConfig(root);
076: }
077:
078: /**
079: * Returns <code>XMLConfig</code> as a subconfiguration with the given condition.
080: *
081: * @param document document ant type of node.
082: */
083: public XMLConfig getSection(String name) {
084: return (XMLConfig) getFirstSubElementsByCondition(name);
085: }
086:
087: // /**
088: // * .
089: // */
090: // public void setText(String namePath, String text, boolean create) {
091: //
092: // if (!create) {
093: // setText(namePath,text);
094: // return;
095: // }
096: // NodeList nodes = this.getSubElementsByTagName(namePath);
097: // if (nodes != null && nodes.getLength() > 0)
098: // ((SearchElement) nodes.item(0)).setText(text);
099: // }
100: //
101: //
102: // /**
103: // * @return recursive funtion that fullfills the <code>list</code>
104: // * parameter with all the nodes in the given path.
105: // */
106: // private void createSubElementsByTag(String namePath) {
107: //
108: // String[] keys = namePath.split(this.TAG_SEPARATOR, 2);
109: // if (keys.length == 1) {
110: // List fList = (List) this.children.get(tagName);
111: // if (fList != null) {
112: // for (int i = 0; i < fList.size(); i++) {
113: // HashMapElement elm = (HashMapElement) fList.get(i);
114: // String val = (String) elm.getText();
115: // if (val != null)
116: // if (val.equals(tagValue))
117: // list.add(elm);
118: // }
119: // } else {
120: // Element newElement = new XMLConfig(this.ownerDocument,keys[0]);
121: // this.appendChild(newElement);
122: // }
123: // return ;
124: // }
125: // NodeList tagChildren = this.getChildrenByTagName(keys[0]);
126: // if (tagChildren != null) {
127: // for (int i = 0; i < tagChildren.getLength(); i++)
128: // ((SearchElement) tagChildren.item(i)).getSubElementsByTagText(keys[1], tagValue, list);
129: // } else {
130: // Element newElement = new XMLConfig(this.ownerDocument,keys[0]);
131: // this.appendChild(newElement);
132: // }
133: // }
134:
135: public static void main(String[] args) {
136: try {
137: System.out.println("Reading document ...");
138: Document doc = XMLDocumentFactory.parse("input.xml");
139: System.out.println("Creating node ...");
140: XMLConfig node = XMLConfig.newXMLConfigInstance(doc);
141: System.out.println("Serialize node ...");
142:
143: Properties prop = new Properties();
144: prop.put(javax.xml.transform.OutputKeys.DOCTYPE_PUBLIC,
145: "http:///enhydra.org");
146: prop.put(javax.xml.transform.OutputKeys.VERSION, "1.1");
147: XMLDocumentFactory.serialize(node, "output.xml ", prop);
148: System.out.println("Searching ...");
149:
150: for (int i = 0; i < 1000; i++) {
151: if (i % 10 == 0)
152: System.out.print("\rprogress " + i / 10 + " %");
153: XMLConfig section = node
154: .getSection("database/package/package/package/package/package/package/table/column@id=EMAILADRESSE");
155: // node.getSubElementsByAttrValue("database/package/package/package/package/package/table@dbTableName","OLSERVER");
156: }
157: System.out.println("\rprogress 100 %");
158: System.out.println();
159:
160: // NodeList nodeList = node.getSubElementsByAttrValue("database/package/package/package/package/package/table@dbTableName","OLSERVER");
161: XMLConfig section = node
162: .getSection("database/package/package/package/package/package/package/table/column@id=EMAILADRESSE");
163: section.setAttr("type@javaType", "Zoka");
164: /* Alex
165: * if (section == null)
166: System.out.println("section = null");
167: else {*/
168: System.out.println("section = " + section);
169: // }
170:
171: // NodeList nodeList = node.getSubElementsByTagText("databaseManager/database/type","Oracle");
172: // NodeList nodeList = node.getSubElementsByCondition("database/package/package/package/package/package/table@dbTableName=OLSERVER");
173: // if (nodeList == null)
174: // System.out.println("nodeList = null");
175: // else {
176: // for (int i=0; i<nodeList.getLength(); i++)
177: // System.out.println("node["+i+"] = "+nodeList.item(i));
178: // }
179:
180: } catch (Exception e) {
181: e.printStackTrace();
182: System.out.println("NOOOOOOOOOOOOO");
183: }
184: }
185: }
|