1 package org.enhydra.kelp.ant; 2 3 4 import javax.xml.parsers.ParserConfigurationException ; 5 import javax.xml.transform.Result ; 6 import javax.xml.transform.Source ; 7 import javax.xml.transform.Transformer ; 8 import javax.xml.transform.TransformerConfigurationException ; 9 import javax.xml.transform.TransformerException ; 10 import javax.xml.transform.TransformerFactory ; 11 import javax.xml.transform.TransformerFactoryConfigurationError ; 12 import javax.xml.transform.dom.DOMSource ; 13 import javax.xml.transform.stream.StreamResult ; 14 15 import java.io.File ; 16 import java.io.FileNotFoundException ; 17 import java.io.FileOutputStream ; 18 import java.io.IOException ; 19 import java.io.OutputStream ; 20 import java.io.PrintStream ; 21 22 import org.w3c.dom.DOMImplementation ; 23 import org.w3c.dom.Document ; 24 import org.w3c.dom.Element ; 25 import org.w3c.dom.NamedNodeMap ; 26 import org.w3c.dom.Node ; 27 import org.w3c.dom.NodeList ; 28 import org.xml.sax.SAXException ; 29 30 import org.apache.crimson.tree.TextNode; 31 32 import org.apache.xml.serialize.OutputFormat; 33 import org.apache.xml.serialize.XMLSerializer; 34 import org.apache.xerces.parsers.DOMParser; 35 import org.apache.xml.serialize.Method; 36 37 45 public class AntXMLUtil { 46 47 48 public Document document; 50 51 52 private String xmlFileName; 54 55 56 57 AntXMLUtil() throws Exception { 58 this(""); 59 } 60 61 62 AntXMLUtil(String fileName) throws Exception { 63 64 this.xmlFileName = fileName; 65 try{ 66 DOMParser parser = new DOMParser(); 67 parser.parse(xmlFileName); 68 document = parser.getDocument(); 69 70 }catch(Exception e){ 71 throw e; 72 } 73 74 } 75 76 77 78 81 public Node getProjectNode() { 82 NodeList list = document.getElementsByTagName("project"); 83 if(list.getLength() > 0) 84 return list.item(0); 85 else 86 return null; 87 } 88 89 93 public Node getProjectProperty(String propertyName) { 94 Node project = this.getProjectNode(); 95 NodeList childs = project.getChildNodes(); 96 for(int i = 0; i < childs.getLength(); i++) { 97 if(childs.item(i).getNodeName().equals("property")) { 98 Node nameAttribute = childs.item(i).getAttributes().getNamedItem("name"); 99 if(nameAttribute != null && nameAttribute.getNodeValue().equals(propertyName)) 100 return childs.item(i); 101 } 102 } 103 return null; 104 } 105 106 107 111 public Node getTargetByName(String targetName) { 112 NodeList allTargets = document.getElementsByTagName("target"); 113 for(int i = 0; i < allTargets.getLength(); i++) { 114 String nameValue = allTargets.item(i).getAttributes().getNamedItem("name").getNodeValue(); 115 116 if(nameValue.equals(targetName)) 117 return allTargets.item(i); 118 } 119 return null; 120 } 121 122 123 128 public Node getChildByTagName(Node subRoot, String tagName) { 129 NodeList childs = subRoot.getChildNodes(); 130 for(int i = 0; i < childs.getLength(); i++) { 131 if(childs.item(i).getNodeName().equals(tagName)) { 132 return childs.item(i); 133 } 134 } 135 return null; 136 } 137 138 143 public Node getAttributeByName(Node subRoot, String attributeName) { 144 return subRoot.getAttributes().getNamedItem(attributeName); 145 } 146 147 151 public Node getPatternsetById(String id) { 152 Node projectNode = this.getProjectNode(); 153 NodeList childs = projectNode.getChildNodes(); 154 for(int i = 0; i < childs.getLength(); i++) { 155 if(childs.item(i).getNodeName().equals("patternset")) { 156 if(childs.item(i).getAttributes().getNamedItem("id").getNodeValue().equals(id)) 157 return childs.item(i); 158 } 159 } 160 161 return null; 162 } 163 164 168 public Node getFiltersetById(String id) { 169 Node projectNode = this.getProjectNode(); 170 NodeList childs = projectNode.getChildNodes(); 171 for(int i = 0; i < childs.getLength(); i++) { 172 if(childs.item(i).getNodeName().equals("filterset")) { 173 if(childs.item(i).getAttributes().getNamedItem("id").getNodeValue().equalsIgnoreCase(id)) 174 return childs.item(i); 175 } 176 } 177 178 return null; 179 } 180 181 public boolean saveDocument() { 182 183 try{ 184 FileOutputStream os = new FileOutputStream (xmlFileName); 185 OutputFormat of = new OutputFormat(); of.setIndenting(true); 187 of.setIndent(4); 188 of.setMethod(Method.XML); 189 of.setPreserveSpace(true); 190 XMLSerializer out = new XMLSerializer(os,of); 191 out.serialize(document); 193 } catch (Exception e) { 194 e.printStackTrace(); 195 return false; 196 } 197 return true; 198 } 199 200 } 201 | Popular Tags |