| Line | Hits | Note | Source |
|---|---|---|---|
| 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 | * <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 | * <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 | */ | ||
| 83 | 0 | 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 | { | ||
| 100 | 0 | mFile = xmlFile; | |
| 101 | 0 | } | |
| 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 | { | ||
| 112 | 0 | mXpath = xpath; | |
| 113 | 0 | } | |
| 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 | { | ||
| 121 | 0 | mName = name; | |
| 122 | 0 | } | |
| 123 | |||
| 124 | /** Perform the evaluation. */ | ||
| 125 | public void execute () | ||
| 126 | { | ||
| 127 | 0 | validate(); | |
| 128 | 0 | final XPathFactory factory = XPathFactory.newInstance(); | |
| 129 | 0 | final XPath xPath = factory.newXPath(); | |
| 130 | 0 | InputStream in = null; | |
| 131 | 0 | String result = null; | |
| 132 | try | ||
| 133 | { | ||
| 134 | 0 | in = new FileInputStream(mFile); | |
| 135 | 0 | result = xPath.evaluate( | |
| 136 | mXpath, new InputSource(in)); | ||
| 137 | } | ||
| 138 | 0 | catch (IOException ex) | |
| 139 | { | ||
| 140 | 0 | throw new BuildException( | |
| 141 | "Coud not read '" + mFile | ||
| 142 | + "'. (" + ex.getMessage() + ")", ex); | ||
| 143 | } | ||
| 144 | 0 | catch (XPathExpressionException e) | |
| 145 | { | ||
| 146 | 0 | throw new BuildException( | |
| 147 | "Coud not evauate xpath expression '" + mXpath | ||
| 148 | + "'. (" + e.getMessage() + ")", e); | ||
| 149 | } | ||
| 150 | finally | ||
| 151 | { | ||
| 152 | 0 | IoUtil.close(in); | |
| 153 | 0 | } | |
| 154 | 0 | getProject().setNewProperty(mName, result); | |
| 155 | 0 | } | |
| 156 | |||
| 157 | /** Plain validation of the attributes set. */ | ||
| 158 | private void validate () | ||
| 159 | { | ||
| 160 | 0 | if (mFile == null || !mFile.exists() || !mFile.canRead()) | |
| 161 | { | ||
| 162 | 0 | throw new BuildException( | |
| 163 | "The file attribute must be set to an existing readable file."); | ||
| 164 | } | ||
| 165 | 0 | if (mXpath == null) | |
| 166 | { | ||
| 167 | 0 | throw new BuildException( | |
| 168 | "The xpath attribute must be set."); | ||
| 169 | } | ||
| 170 | 0 | if (mName == null) | |
| 171 | { | ||
| 172 | 0 | throw new BuildException( | |
| 173 | "The name attribute must be set."); | ||
| 174 | } | ||
| 175 | 0 | } | |
| 176 | |||
| 177 | |||
| 178 | |||
| 179 | } |
|
|
(1) | 105 | : | 0 | Line is longer than 80 characters. |