001: /*
002: Copyright (C) 2003 Together
003:
004: This library is free software; you can redistribute it and/or
005: modify it under the terms of the GNU Lesser General Public
006: License as published by the Free Software Foundation; either
007: version 2.1 of the License, or (at your option) any later version.
008:
009: This library is distributed in the hope that it will be useful,
010: but WITHOUT ANY WARRANTY; without even the implied warranty of
011: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: Lesser General Public License for more details.
013:
014: You should have received a copy of the GNU Lesser General Public
015: License along with this library; if not, write to the Free Software
016: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018:
019: package org.enhydra.xml;
020:
021: import java.io.IOException;
022: import java.util.ArrayList;
023: import java.util.List;
024: import java.util.Properties;
025:
026: //import org.apache.xerces.parsers.DOMParser;
027: import org.w3c.dom.DOMException;
028: import org.w3c.dom.Document;
029: import org.w3c.dom.Node;
030: import org.w3c.dom.NodeList;
031: import org.w3c.dom.Element;
032: import org.xml.sax.SAXException;
033:
034: /**
035: * @author Tweety
036: *
037: * A class representing
038: *
039: * @version 1.0
040: */
041: public class XMLConfig extends SearchElement {
042:
043: /**
044: * Constructs an empty <code>SearchElement</code>.
045: */
046: public XMLConfig() {
047: }
048:
049: /**
050: * Constructs an <code>XMLConfig</code> with the given
051: * document owner and node name.
052: *
053: * @param ownerDoc the document owner of the node, as a <code>Document</code>.
054: * @param name is the name of the node, as a <code>String</code>.
055: */
056: public XMLConfig(Document ownerDoc, String name) {
057: super (ownerDoc, name);
058: }
059:
060: /**
061: * Constructs an <code>XMLConfig</code> from a given node
062: * (creates the children subtree too), as a <code>Node</code>
063: *
064: * @param node , as a <code>Node</code>.
065: */
066: public XMLConfig(Node node) {
067: super (node);
068: }
069:
070: /**
071: * Constructs a <code>XMLConfig</code> from the given node,
072: * without creating entire children subtree.
073: *
074: * @param node , as a <code>XMLConfig</code>.
075: */
076: public XMLConfig(XMLConfig node) {
077: super ((HashMapElement) node);
078: }
079:
080: /**
081: * Creates new instance of the XMLConfig class from the given <code>Node</code>.
082: *
083: * @param node , as a <code>Node</code>.
084: *
085: * @return new instance of the XMLConfig class.
086: */
087: protected Node newElementInstance(Node node) {
088: return new XMLConfig(node);
089: }
090:
091: /**
092: * Creates new instance of <code>XMLConfig</code> from a given document
093: * as a <code>Document</code>
094: *
095: * @param document document ant type of node.
096: *
097: * @return new instance of <code>XMLConfig</code> from a given document.
098: */
099: public static XMLConfig newXMLConfigInstance(Document document) {
100: Node root = document.getDocumentElement();
101: return new XMLConfig(root);
102: }
103:
104: /**
105: * Returns <code>XMLConfig</code> as a subconfiguration with the given condition.
106: *
107: * @param name is type of node.
108: * @return node
109: */
110: public XMLConfig getSection(String name) {
111: return (XMLConfig) getFirstSubElementsByCondition(name);
112: }
113:
114: // /**
115: // * .
116: // */
117: // public void setText(String namePath, String text, boolean create) {
118: //
119: // if (!create) {
120: // setText(namePath,text);
121: // return;
122: // }
123: // NodeList nodes = this.getSubElementsByTagName(namePath);
124: // if (nodes != null && nodes.getLength() > 0)
125: // ((SearchElement) nodes.item(0)).setText(text);
126: // }
127: //
128: //
129: // /**
130: // * @return recursive funtion that fullfills the <code>list</code>
131: // * parameter with all the nodes in the given path.
132: // */
133: // private void createSubElementsByTag(String namePath) {
134: //
135: // String[] keys = namePath.split(this.TAG_SEPARATOR, 2);
136: // if (keys.length == 1) {
137: // List fList = (List) this.children.get(tagName);
138: // if (fList != null) {
139: // for (int i = 0; i < fList.size(); i++) {
140: // HashMapElement elm = (HashMapElement) fList.get(i);
141: // String val = (String) elm.getText();
142: // if (val != null)
143: // if (val.equals(tagValue))
144: // list.add(elm);
145: // }
146: // } else {
147: // Element newElement = new XMLConfig(this.ownerDocument,keys[0]);
148: // this.appendChild(newElement);
149: // }
150: // return ;
151: // }
152: // NodeList tagChildren = this.getChildrenByTagName(keys[0]);
153: // if (tagChildren != null) {
154: // for (int i = 0; i < tagChildren.getLength(); i++)
155: // ((SearchElement) tagChildren.item(i)).getSubElementsByTagText(keys[1], tagValue, list);
156: // } else {
157: // Element newElement = new XMLConfig(this.ownerDocument,keys[0]);
158: // this.appendChild(newElement);
159: // }
160: // }
161:
162: public static void main(String[] args) {
163: try {
164: System.out.println("Reading document ...");
165: Document doc = XMLDocumentFactory.parse("input.xml");
166: System.out.println("Creating node ...");
167: XMLConfig node = XMLConfig.newXMLConfigInstance(doc);
168: System.out.println("Serialize node ...");
169:
170: Properties prop = new Properties();
171: prop.put(javax.xml.transform.OutputKeys.DOCTYPE_PUBLIC,
172: "http:///enhydra.org");
173: prop.put(javax.xml.transform.OutputKeys.VERSION, "1.1");
174: XMLDocumentFactory.serialize(node, "output.xml ", prop);
175: System.out.println("Searching ...");
176:
177: for (int i = 0; i < 1000; i++) {
178: if (i % 10 == 0)
179: System.out.print("\rprogress " + i / 10 + " %");
180: XMLConfig section = node
181: .getSection("database/package/package/package/package/package/package/table/column@id=EMAILADRESSE");
182: // node.getSubElementsByAttrValue("database/package/package/package/package/package/table@dbTableName","OLSERVER");
183: }
184: System.out.println("\rprogress 100 %");
185: System.out.println();
186:
187: // NodeList nodeList = node.getSubElementsByAttrValue("database/package/package/package/package/package/table@dbTableName","OLSERVER");
188: XMLConfig section = node
189: .getSection("database/package/package/package/package/package/package/table/column@id=EMAILADRESSE");
190: section.setAttr("type@javaType", "Zoka");
191: if (section == null)
192: System.out.println("section = null");
193: else {
194: System.out.println("section = " + section);
195: }
196:
197: // NodeList nodeList = node.getSubElementsByTagText("databaseManager/database/type","Oracle");
198: // NodeList nodeList = node.getSubElementsByCondition("database/package/package/package/package/package/table@dbTableName=OLSERVER");
199: // if (nodeList == null)
200: // System.out.println("nodeList = null");
201: // else {
202: // for (int i=0; i<nodeList.getLength(); i++)
203: // System.out.println("node["+i+"] = "+nodeList.item(i));
204: // }
205:
206: } catch (Exception e) {
207: e.printStackTrace();
208: System.out.println("NOOOOOOOOOOOOO");
209: }
210: }
211: }
|