Project Report: fawkez

Packagesummary org.jcoderz.commons.taskdefs

org.jcoderz.commons.taskdefs.XpathPropertyTask

LineHitsNoteSource
1  /*
2   * $Id: ApiDocTask.java 1011 2008-06-16 17:57:36Z amandel $
3   *
4   * Copyright 2006, The jCoderZ.org Project. All rights reserved.
5   *
6   * Redistribution and use in source and binary forms, with or without
7   * modification, are permitted provided that the following conditions are
8   * met:
9   *
10   *    * Redistributions of source code must retain the above copyright
11   *      notice, this list of conditions and the following disclaimer.
12   *    * Redistributions in binary form must reproduce the above
13   *      copyright notice, this list of conditions and the following
14   *      disclaimer in the documentation and/or other materials
15   *      provided with the distribution.
16   *    * Neither the name of the jCoderZ.org Project nor the names of
17   *      its contributors may be used to endorse or promote products
18   *      derived from this software without specific prior written
19   *      permission.
20   *
21   * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND
22   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24   * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS
25   * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28   * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29   * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30   * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32   */
33  package org.jcoderz.commons.taskdefs;
34  
35  import java.io.File;
36  import java.io.FileInputStream;
37  import java.io.IOException;
38  import java.io.InputStream;
39  
40  import javax.xml.xpath.XPath;
41  import javax.xml.xpath.XPathExpressionException;
42  import javax.xml.xpath.XPathFactory;
43  
44  import org.apache.tools.ant.BuildException;
45  import org.apache.tools.ant.Task;
46  import org.jcoderz.commons.util.IoUtil;
47  import org.xml.sax.InputSource;
48  
49  /**
50   * This task allows to set a property based on an xpath expression,
51   * evaluated on a given xml file.
52   * 
53   * <p>The tasks needs 3 properties to be set:
54   * 
55   * <dl>
56   * <dt><code>name</code></dt><dd>The name of the property to be set.</dd>
57   * <dt><code>file</code></dt><dd>The xml file to be parsed.</dd>
58   * <dt><code>xpath</code></dt><dd>The xpath expression
59   *   to be evaluated on the given file.</dd>
60   * </dl></p>
61   *
62   * <p>The task can be defined using the following taskdef:
63   * <pre>
64   *     &lt;taskdef name="xpathproperty" 
65   *         classname="org.jcoderz.commons.taskdefs.XpathPropertyTask"
66   *         classpath="fawkez-all.jar"/>
67   * </pre></p>
68   * 
69   * <p>A possible use would be:
70   * <pre>
71   *     &lt;xpathproperty 
72   *        name="xpathtest" 
73   *        xpath="/project/@name" 
74   *        file="build.xml"/>
75   * </pre>
76   * Which would set the property <code>xpathtest</code> to <tt>fawkeZ</tt>
77   * is applied to the fawkez build.xml.</p>
78   * 
79   * <p>The task requires java5 to function properly.</p>
80   *
81   * @author Andreas Mandel
82   */
830 public class XpathPropertyTask
84      extends Task
85  {
86      /** Task name. */
87      public static final String NAME = "XpathProperty";
88  
89      private File mFile;
90      private String mXpath;
91      private String mName;
92      
93      /**
94       * Set the file to be parsed.
95       * The file must exist and be readable.
96       * @param xmlFile the file to be parsed.
97       */
98      public void setFile (File xmlFile)
99      {
1000         mFile = xmlFile;
1010     }
102      
103      /**
104       * Set the xpath expression to be evaluated.
105 (1)     * See <a href="http://java.sun.com/j2se/1.5.0/docs/api/javax/xml/xpath/package-summary.html">javadoc</a>
106       * or <a href="http://www.w3.org/TR/xpath">XPath Recommendation</a> for
107       * details about XPath.
108       * @param xpath the xpath expression to be evaluated.
109       */
110      public void setXpath (String xpath)
111      {
1120         mXpath = xpath;
1130     }
114  
115      /**
116       * Set the name of the property to be set.
117       * @param name the name of the property to be set.
118       */
119      public void setName (String name)
120      {
1210         mName = name;
1220     }
123      
124      /** Perform the evaluation. */
125      public void execute ()
126      {
1270         validate();
1280         final XPathFactory factory = XPathFactory.newInstance();
1290         final XPath xPath = factory.newXPath();
1300         InputStream in = null;
1310         String result = null;
132          try
133          {
1340             in = new FileInputStream(mFile);
1350             result = xPath.evaluate(
136                  mXpath, new InputSource(in));
137          }
1380         catch (IOException ex)
139          {
1400             throw new BuildException(
141                  "Coud not read '" + mFile
142                  + "'. (" + ex.getMessage() + ")", ex);
143          }
1440         catch (XPathExpressionException e)
145          {
1460             throw new BuildException(
147                  "Coud not evauate xpath expression '" + mXpath
148                  + "'. (" + e.getMessage() + ")", e);
149          }
150          finally
151          {
1520             IoUtil.close(in);
1530         }
1540         getProject().setNewProperty(mName, result);
1550     }
156      
157      /** Plain validation of the attributes set. */
158      private void validate ()
159      {
1600         if (mFile == null || !mFile.exists() || !mFile.canRead())
161          {
1620             throw new BuildException(
163                  "The file attribute must be set to an existing readable file.");
164          }
1650         if (mXpath == null)
166          {
1670             throw new BuildException(
168                  "The xpath attribute must be set.");
169          }
1700         if (mName == null)
171          {
1720             throw new BuildException(
173                  "The name attribute must be set.");
174          }
1750     }
176      
177      
178      
179  }

Findings in this File

c (1) 105 : 0 Line is longer than 80 characters.