KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cruisecontrol > util > XPathAwareChild


1 /********************************************************************************
2  * CruiseControl, a Continuous Integration Toolkit
3  * Copyright (c) 2005, ThoughtWorks, Inc.
4  * 651 W Washington Ave. Suite 600
5  * Chicago, IL 60661 USA
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * + Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * + Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  *
20  * + Neither the name of ThoughtWorks, Inc., CruiseControl, nor the
21  * names of its contributors may be used to endorse or promote
22  * products derived from this software without specific prior
23  * written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
29  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  ********************************************************************************/

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 JavaDoc;
48 import java.io.FileInputStream JavaDoc;
49 import java.io.IOException JavaDoc;
50 import java.io.InputStream JavaDoc;
51
52 /**
53  * <p>This class represents a plugin subelement that returns either a fixed value or uses an xpath expression to
54  * retrieve a value from an XML document. Instances of this class represent an element that has either a fixed value,
55  * e.g.</p>
56  * <pre>
57  * &lt;mychild value="foo"/&gt;
58  * </pre>
59  * <p>or one that has an xpath expression that will be executed against CruiseControl's log file at execution
60  * time, e.g.</p>
61  * <pre>
62  * &lt;mychild xpathExpression="sum(cruisecontrol/testsuite/@tests)"/&gt;
63  * </pre>
64  * <p>or one that has an xpath expression that will be executed against a named xml file at execution
65  * time, e.g.</p>
66  * <pre>
67  * &lt;mychild xpathExpression="sum(cruisecontrol/testsuite/@tests)" xmlFile="/path/to/my/xml/file"/&gt;
68  * </pre>
69  * For example usage in a plugin, see
70  * {@link net.sourceforge.cruisecontrol.publishers.sfee.SfeeDocumentManagerPublisher#createDescription()}.
71  *
72  * @author <a HREF="mailto:pj@thoughtworks.com">Paul Julius</a>
73  * @author <a HREF="mailto:krs@thoughtworks.com">Kent Spillner</a>
74  * @see net.sourceforge.cruisecontrol.publishers.sfee.SfeeDocumentManagerPublisher
75  */

76 public class XPathAwareChild {
77
78     private static final Logger LOG = Logger.getLogger(XPathAwareChild.class);
79
80     private String JavaDoc value;
81     private String JavaDoc xpathExpression;
82     private InputStream JavaDoc in;
83     private String JavaDoc xmlFile;
84     private boolean wasValidated;
85
86     /**
87      * If this value is set, then it is considered to be a "fixed" value that will be returned by the
88      * <code>lookupValue</code> method. If this is set, then <code>xpathExpression</code> and <code>xmlFile</code>
89      * should NOT be set.
90      */

91     public void setValue(String JavaDoc value) {
92         markDirty();
93         this.value = value;
94     }
95
96     public void setXPathExpression(String JavaDoc xpathExpression) {
97         markDirty();
98         this.xpathExpression = xpathExpression;
99     }
100
101     /**
102      * Used for testing. CruiseControl will not be able to call this method directly, so it has not applicability to
103      * the intended use of this class. Use this method from unit tests to set an InputStream instead of an actual
104      * xmlFile.
105      */

106     public void setInputStream(InputStream JavaDoc in) {
107         markDirty();
108         this.in = in;
109     }
110
111     public void setXMLFile(String JavaDoc filename) {
112         markDirty();
113         xmlFile = filename;
114     }
115
116     public String JavaDoc getFixedValue() {
117         return value;
118     }
119
120     public String JavaDoc getXpathExpression() {
121         return xpathExpression;
122     }
123
124     /**
125      * Looks up the appropriate value based on how the class is being used. It will return either the fixed value, or
126      * execute the xpath expression against the appropriate xml file/log.
127      */

128     public String JavaDoc lookupValue(Element log) throws CruiseControlException {
129         if (!wasValidated) {
130             throw new IllegalStateException JavaDoc(
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 JavaDoc e) {
141                 throw new CruiseControlException(e);
142             }
143         }
144     }
145
146     private String JavaDoc evaluateXpath(Element log) throws IOException JavaDoc, JDOMException, CruiseControlException {
147
148         Object JavaDoc 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 JavaDoc(new File JavaDoc(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 JavaDoc result = xpath.valueOf(searchContext);
168         LOG.debug("Evaluated xpath [" + xpathExpression + "] with result [" + result + "]");
169         return result;
170     }
171
172     /**
173      * Must be called after setting all the instance values and before calling <code>lookupValue</code>.
174      *
175      * @throws CruiseControlException
176      */

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     /**
198      * Called to indicate that this classs needs to be validated, as after a change to a member variable.
199      */

200     protected void markDirty() {
201         wasValidated = false;
202     }
203
204
205 }
206
Popular Tags