1 31 32 package org.opencms.util.ant; 33 34 import java.io.File ; 35 36 import org.apache.commons.digester.Digester; 37 import org.apache.tools.ant.BuildException; 38 import org.apache.tools.ant.Task; 39 40 import org.xml.sax.ErrorHandler ; 41 import org.xml.sax.SAXParseException ; 42 43 52 public class CmsAntTaskReadXMLProperty extends Task { 53 54 55 private String m_attribute; 57 58 private String m_element; 60 61 private String m_property; 63 64 private String m_value = ","; 65 66 67 private String m_xmlFile; 69 72 public CmsAntTaskReadXMLProperty() { 73 74 super(); 75 } 76 77 87 public void execute() throws BuildException { 88 89 boolean isAttr = (m_attribute != null && m_attribute.trim().length() > 0); 90 91 Digester digester = new Digester(); 93 digester.setValidating(false); 94 digester.setEntityResolver(null); 95 digester.setRuleNamespaceURI(null); 96 digester.setErrorHandler(new ErrorHandler () { 97 98 101 public void error(SAXParseException exception) { 102 103 log(exception.getMessage(), exception.getLineNumber()); 104 } 105 106 109 public void fatalError(SAXParseException exception) { 110 111 log(exception.getMessage(), exception.getLineNumber()); 112 } 113 114 117 public void warning(SAXParseException exception) { 118 119 log(exception.getMessage(), exception.getLineNumber()); 120 } 121 122 }); 123 124 digester.push(this); 126 if (!isAttr) { 127 digester.addCallMethod(m_element, "setValue", 0); 128 } else { 129 digester.addCallMethod(m_element, "setValue", 1); 130 digester.addCallParam(m_element, 0, m_attribute); 131 } 132 try { 134 digester.parse(new File (getXmlFile())); 135 } catch (Exception e) { 136 throw new BuildException(e); 137 } 138 139 getProject().setProperty(m_property, m_value.substring(1)); 140 } 141 142 147 public String getAttribute() { 148 149 return m_attribute; 150 } 151 152 157 public String getElement() { 158 159 return m_element; 160 } 161 162 167 public String getProperty() { 168 169 return m_property; 170 } 171 172 177 public String getValue() { 178 179 return m_value; 180 } 181 182 187 public String getXmlFile() { 188 189 return m_xmlFile; 190 } 191 192 197 public void setAttribute(String attribute) { 198 199 m_attribute = attribute; 200 } 201 202 207 public void setElement(String element) { 208 209 m_element = element; 210 } 211 212 217 public void setProperty(String property) { 218 219 m_property = property; 220 } 221 222 227 public void setValue(String value) { 228 229 if (m_value.length() > 1) { 230 m_value += ","; 231 } 232 m_value += value; 233 } 234 235 240 public void setXmlFile(String xmlFile) { 241 242 m_xmlFile = xmlFile; 243 } 244 } | Popular Tags |