1 8 9 import java.io.InputStream ; 10 import java.io.FileInputStream ; 11 import java.io.BufferedInputStream ; 12 import java.io.OutputStream ; 13 import java.io.FileOutputStream ; 14 import java.io.Writer ; 15 import java.io.PrintWriter ; 16 import java.io.FileWriter ; 17 import java.io.BufferedWriter ; 18 import java.io.FileWriter ; 19 import java.io.IOException ; 20 21 import java.util.ArrayList ; 22 23 import org.w3c.dom.NodeList ; 24 import org.w3c.dom.Node ; 25 import org.w3c.dom.Element ; 26 import org.w3c.dom.Attr ; 27 import org.w3c.dom.Document ; 28 import org.w3c.dom.DOMException ; 29 import org.w3c.dom.NamedNodeMap ; 30 31 import javax.xml.parsers.DocumentBuilder ; 32 import javax.xml.parsers.DocumentBuilderFactory ; 33 import javax.xml.parsers.FactoryConfigurationError ; 34 import javax.xml.parsers.ParserConfigurationException ; 35 36 import org.xml.sax.SAXException ; 37 import org.xml.sax.SAXParseException ; 38 39 import org.apache.tools.ant.BuildException; 40 import org.apache.tools.ant.taskdefs.Property; 41 42 import org.apache.tools.ant.BuildException; 43 import org.apache.tools.ant.taskdefs.Property; 44 45 import org.apache.tools.ant.Task; 46 import org.apache.tools.ant.Project; 47 import org.apache.tools.ant.AntClassLoader; 48 import org.apache.tools.ant.ProjectHelper; 49 import org.apache.tools.ant.types.Path; 50 import org.apache.tools.ant.types.Reference; 51 52 67 68 public class XmlPropertyTask extends org.apache.tools.ant.Task 69 { 70 private String src; 71 private org.w3c.dom.Document document; 72 73 76 public XmlPropertyTask() 77 { 78 super(); 79 } 80 81 84 85 public void init() 86 { 87 super.init(); 88 } 89 90 94 public void execute() 95 throws org.apache.tools.ant.BuildException 96 { 97 BufferedInputStream configurationStream = null; 98 99 try 100 { 101 configurationStream = 102 new BufferedInputStream (new FileInputStream (src)); 103 104 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 105 106 factory.setValidating(false); 107 factory.setNamespaceAware(false); 108 109 DocumentBuilder builder = factory.newDocumentBuilder(); 110 document = builder.parse( configurationStream ); 111 112 addNodeRecursively(document.getDocumentElement(),"xml"); 113 114 } catch (SAXException sxe) { 115 Exception x = sxe; 117 if (sxe.getException() != null) 118 x = sxe.getException(); 119 throw new BuildException(x); 120 121 } catch (ParserConfigurationException pce) { 122 throw new BuildException(pce); 124 } catch (IOException ioe) { 125 throw new BuildException(ioe); 127 } 128 finally 129 { 130 if(configurationStream!=null){ 131 try{configurationStream.close();}catch(Exception e){}} 132 } 133 } 134 135 136 void addNodeRecursively(org.w3c.dom.Node node, String text) { 137 138 if (node.hasAttributes()) { 139 org.w3c.dom.NamedNodeMap nodeAttributes = node.getAttributes(); 140 for (int i = 0; i < nodeAttributes.getLength(); i++) { 141 Node attributeNode = nodeAttributes.item(i); 142 String attributeName = text+"."+node.getNodeName()+"."+attributeNode.getNodeName(); 143 String attributeValue = attributeNode.getNodeValue(); 144 log(attributeName+":"+attributeValue, Project.MSG_VERBOSE); 145 project.setUserProperty(attributeName,attributeValue); 146 } 147 } 148 149 if(node.getNodeType()==Node.TEXT_NODE){ 150 String nodeText = node.getNodeValue(); 151 if(nodeText.trim().length()!=0) 152 { 153 log(text+":"+nodeText, Project.MSG_VERBOSE); 154 project.setUserProperty(text,nodeText); 155 } 156 } 157 158 if (node.hasChildNodes()) { 159 text+=("."+node.getNodeName()); 160 org.w3c.dom.NodeList nodeChildren = node.getChildNodes(); 161 for (int i = 0; i < nodeChildren.getLength(); i++) { 162 addNodeRecursively(nodeChildren.item(i), text); 163 } 164 } 165 } 166 167 public void setFile(String src) 168 { 169 this.src = src; 170 } 171 } 172 173 174 227 | Popular Tags |