1 37 package net.sourceforge.cruisecontrol.util; 38 39 import net.sourceforge.cruisecontrol.CruiseControlException; 40 import org.apache.log4j.Logger; 41 import org.jdom.Document; 42 import org.jdom.Element; 43 import org.jdom.JDOMException; 44 import org.jdom.input.SAXBuilder; 45 import org.jdom.xpath.XPath; 46 47 import java.io.File ; 48 import java.io.FileInputStream ; 49 import java.io.IOException ; 50 import java.io.InputStream ; 51 52 76 public class XPathAwareChild { 77 78 private static final Logger LOG = Logger.getLogger(XPathAwareChild.class); 79 80 private String value; 81 private String xpathExpression; 82 private InputStream in; 83 private String xmlFile; 84 private boolean wasValidated; 85 86 91 public void setValue(String value) { 92 markDirty(); 93 this.value = value; 94 } 95 96 public void setXPathExpression(String xpathExpression) { 97 markDirty(); 98 this.xpathExpression = xpathExpression; 99 } 100 101 106 public void setInputStream(InputStream in) { 107 markDirty(); 108 this.in = in; 109 } 110 111 public void setXMLFile(String filename) { 112 markDirty(); 113 xmlFile = filename; 114 } 115 116 public String getFixedValue() { 117 return value; 118 } 119 120 public String getXpathExpression() { 121 return xpathExpression; 122 } 123 124 128 public String lookupValue(Element log) throws CruiseControlException { 129 if (!wasValidated) { 130 throw new IllegalStateException ( 131 "This child was not validated." 132 + " Should not be calling lookupValue() unless it has first been validated."); 133 } 134 135 if (value != null) { 136 return value; 137 } else { 138 try { 139 return evaluateXpath(log); 140 } catch (Exception e) { 141 throw new CruiseControlException(e); 142 } 143 } 144 } 145 146 private String evaluateXpath(Element log) throws IOException , JDOMException, CruiseControlException { 147 148 Object searchContext; 149 if (in == null && xmlFile == null && log == null) { 150 throw new CruiseControlException("current cruisecontrol log not set."); 151 } else if (xmlFile != null) { 152 LOG.debug("Using file specified [" + xmlFile + "] to evaluate xpath."); 153 searchContext = new SAXBuilder().build(new FileInputStream (new File (xmlFile))); 154 } else if (in != null) { 155 LOG.debug("Using the specified input stream to evaluate xpath. This should happen during testing."); 156 searchContext = new SAXBuilder().build(in); 157 } else { 158 LOG.debug("Using CruiseControl's log file to evaluate xpath."); 159 if (log.getParent() != null) { 160 searchContext = log.getParent(); 161 } else { 162 searchContext = new Document(log); 163 } 164 } 165 166 XPath xpath = XPath.newInstance(xpathExpression); 167 String result = xpath.valueOf(searchContext); 168 LOG.debug("Evaluated xpath [" + xpathExpression + "] with result [" + result + "]"); 169 return result; 170 } 171 172 177 public void validate() throws CruiseControlException { 178 if (xpathExpression == null && xmlFile != null) { 179 throw new CruiseControlException("xmlFile should only be set if xpathExpression is also set."); 180 } 181 182 if (value == null && xpathExpression == null) { 183 throw new CruiseControlException("Either value or xpathExpression must be set."); 184 } 185 if (value != null && xpathExpression != null) { 186 throw new CruiseControlException("value and xpathExpression should not both be set."); 187 } 188 189 190 markClean(); 191 } 192 193 private void markClean() { 194 wasValidated = true; 195 } 196 197 200 protected void markDirty() { 201 wasValidated = false; 202 } 203 204 205 } 206 | Popular Tags |