View Javadoc
1 /* 2 Copyright (C) 2003 Together 3 4 This library is free software; you can redistribute it and/or 5 modify it under the terms of the GNU Lesser General Public 6 License as published by the Free Software Foundation; either 7 version 2.1 of the License, or (at your option) any later version. 8 9 This library is distributed in the hope that it will be useful, 10 but WITHOUT ANY WARRANTY; without even the implied warranty of 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 Lesser General Public License for more details. 13 14 You should have received a copy of the GNU Lesser General Public 15 License along with this library; if not, write to the Free Software 16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 17 */ 18 19 package org.enhydra.xml; 20 21 import java.io.IOException; 22 import java.util.ArrayList; 23 import java.util.List; 24 import java.util.Properties; 25 26 //import org.apache.xerces.parsers.DOMParser; 27 import org.w3c.dom.DOMException; 28 import org.w3c.dom.Document; 29 import org.w3c.dom.Node; 30 import org.w3c.dom.NodeList; 31 import org.w3c.dom.Element; 32 import org.xml.sax.SAXException; 33 34 /*** 35 * @author Tweety 36 * 37 * A class representing 38 * 39 * @version 1.0 40 */ 41 public class XMLConfig extends SearchElement { 42 43 44 /*** 45 * Constructs an empty <code>SearchElement</code>. 46 */ 47 public XMLConfig() { 48 } 49 50 51 /*** 52 * Constructs an <code>XMLConfig</code> with the given 53 * document owner and node name. 54 * 55 * @param ownerDoc the document owner of the node, as a <code>Document</code>. 56 * @param name is the name of the node, as a <code>String</code>. 57 */ 58 public XMLConfig(Document ownerDoc, String name) { 59 super(ownerDoc, name); 60 } 61 62 63 /*** 64 * Constructs an <code>XMLConfig</code> from a given node 65 * (creates the children subtree too), as a <code>Node</code> 66 * 67 * @param node , as a <code>Node</code>. 68 */ 69 public XMLConfig(Node node) { 70 super(node); 71 } 72 73 74 /*** 75 * Constructs a <code>XMLConfig</code> from the given node, 76 * without creating entire children subtree. 77 * 78 * @param node , as a <code>XMLConfig</code>. 79 */ 80 public XMLConfig(XMLConfig node) { 81 super((HashMapElement)node); 82 } 83 84 85 /*** 86 * Creates new instance of the XMLConfig class from the given <code>Node</code>. 87 * 88 * @param node , as a <code>Node</code>. 89 * 90 * @return new instance of the XMLConfig class. 91 */ 92 protected Node newElementInstance(Node node) { 93 return new XMLConfig(node); 94 } 95 96 97 /*** 98 * Creates new instance of <code>XMLConfig</code> from a given document 99 * as a <code>Document</code> 100 * 101 * @param document document ant type of node. 102 * 103 * @return new instance of <code>XMLConfig</code> from a given document. 104 */ 105 public static XMLConfig newXMLConfigInstance(Document document) { 106 Node root = document.getDocumentElement(); 107 return new XMLConfig(root); 108 } 109 110 111 /*** 112 * Returns <code>XMLConfig</code> as a subconfiguration with the given condition. 113 * 114 * @param name is type of node. 115 * @return node 116 */ 117 public XMLConfig getSection(String name) { 118 return (XMLConfig)getFirstSubElementsByCondition(name); 119 } 120 121 122 // /*** 123 // * . 124 // */ 125 // public void setText(String namePath, String text, boolean create) { 126 // 127 // if (!create) { 128 // setText(namePath,text); 129 // return; 130 // } 131 // NodeList nodes = this.getSubElementsByTagName(namePath); 132 // if (nodes != null && nodes.getLength() > 0) 133 // ((SearchElement) nodes.item(0)).setText(text); 134 // } 135 // 136 // 137 // /*** 138 // * @return recursive funtion that fullfills the <code>list</code> 139 // * parameter with all the nodes in the given path. 140 // */ 141 // private void createSubElementsByTag(String namePath) { 142 // 143 // String[] keys = namePath.split(this.TAG_SEPARATOR, 2); 144 // if (keys.length == 1) { 145 // List fList = (List) this.children.get(tagName); 146 // if (fList != null) { 147 // for (int i = 0; i < fList.size(); i++) { 148 // HashMapElement elm = (HashMapElement) fList.get(i); 149 // String val = (String) elm.getText(); 150 // if (val != null) 151 // if (val.equals(tagValue)) 152 // list.add(elm); 153 // } 154 // } else { 155 // Element newElement = new XMLConfig(this.ownerDocument,keys[0]); 156 // this.appendChild(newElement); 157 // } 158 // return ; 159 // } 160 // NodeList tagChildren = this.getChildrenByTagName(keys[0]); 161 // if (tagChildren != null) { 162 // for (int i = 0; i < tagChildren.getLength(); i++) 163 // ((SearchElement) tagChildren.item(i)).getSubElementsByTagText(keys[1], tagValue, list); 164 // } else { 165 // Element newElement = new XMLConfig(this.ownerDocument,keys[0]); 166 // this.appendChild(newElement); 167 // } 168 // } 169 170 171 172 public static void main(String[] args) { 173 try { 174 System.out.println("Reading document ..."); 175 Document doc = XMLDocumentFactory.parse("input.xml"); 176 System.out.println("Creating node ..."); 177 XMLConfig node = XMLConfig.newXMLConfigInstance(doc); 178 System.out.println("Serialize node ..."); 179 180 Properties prop = new Properties(); 181 prop.put(javax.xml.transform.OutputKeys.DOCTYPE_PUBLIC,"http:///enhydra.org"); 182 prop.put(javax.xml.transform.OutputKeys.VERSION,"1.1"); 183 XMLDocumentFactory.serialize(node, "output.xml ",prop); 184 System.out.println("Searching ..."); 185 186 for (int i=0; i < 1000; i++) { 187 if ( i%10 == 0) 188 System.out.print("\rprogress "+i/10+" %"); 189 XMLConfig section = node.getSection("database/package/package/package/package/package/package/table/column@id=EMAILADRESSE"); 190 // node.getSubElementsByAttrValue("database/package/package/package/package/package/table@dbTableName","OLSERVER"); 191 } 192 System.out.println("\rprogress 100 %"); 193 System.out.println(); 194 195 // NodeList nodeList = node.getSubElementsByAttrValue("database/package/package/package/package/package/table@dbTableName","OLSERVER"); 196 XMLConfig section = node.getSection("database/package/package/package/package/package/package/table/column@id=EMAILADRESSE"); 197 section.setAttr("type@javaType","Zoka"); 198 if (section == null) 199 System.out.println("section = null"); 200 else { 201 System.out.println("section = "+section); 202 } 203 204 205 // NodeList nodeList = node.getSubElementsByTagText("databaseManager/database/type","Oracle"); 206 // NodeList nodeList = node.getSubElementsByCondition("database/package/package/package/package/package/table@dbTableName=OLSERVER"); 207 // if (nodeList == null) 208 // System.out.println("nodeList = null"); 209 // else { 210 // for (int i=0; i<nodeList.getLength(); i++) 211 // System.out.println("node["+i+"] = "+nodeList.item(i)); 212 // } 213 214 } catch(Exception e) { 215 e.printStackTrace(); 216 System.out.println("NOOOOOOOOOOOOO"); 217 } 218 } 219 }

This page was automatically generated by Maven