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

This page automatically generated by Maven