You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

XmlProperty.java 28 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. package org.apache.tools.ant.taskdefs;
  19. import java.io.File;
  20. import java.io.IOException;
  21. import java.util.Hashtable;
  22. import javax.xml.parsers.DocumentBuilderFactory;
  23. import javax.xml.parsers.DocumentBuilder;
  24. import javax.xml.parsers.ParserConfigurationException;
  25. import org.apache.tools.ant.BuildException;
  26. import org.apache.tools.ant.Project;
  27. import org.apache.tools.ant.types.Path;
  28. import org.apache.tools.ant.types.Resource;
  29. import org.apache.tools.ant.types.ResourceCollection;
  30. import org.apache.tools.ant.types.XMLCatalog;
  31. import org.apache.tools.ant.types.resources.FileResource;
  32. import org.apache.tools.ant.util.FileUtils;
  33. import org.w3c.dom.Document;
  34. import org.w3c.dom.Element;
  35. import org.w3c.dom.NamedNodeMap;
  36. import org.w3c.dom.Node;
  37. import org.w3c.dom.NodeList;
  38. import org.xml.sax.SAXException;
  39. import org.xml.sax.EntityResolver;
  40. /**
  41. * Loads property values from a valid XML file, generating the
  42. * property names from the file's element and attribute names.
  43. *
  44. * <p>Example:</p>
  45. * <pre>
  46. * &lt;root-tag myattr="true"&gt;
  47. * &lt;inner-tag someattr="val"&gt;Text&lt;/inner-tag&gt;
  48. * &lt;a2&gt;&lt;a3&gt;&lt;a4&gt;false&lt;/a4&gt;&lt;/a3&gt;&lt;/a2&gt;
  49. * &lt;x&gt;x1&lt;/x&gt;
  50. * &lt;x&gt;x2&lt;/x&gt;
  51. * &lt;/root-tag&gt;
  52. *</pre>
  53. *
  54. * <p>this generates the following properties:</p>
  55. *
  56. * <pre>
  57. * root-tag(myattr)=true
  58. * root-tag.inner-tag=Text
  59. * root-tag.inner-tag(someattr)=val
  60. * root-tag.a2.a3.a4=false
  61. * root-tag.x=x1,x2
  62. * </pre>
  63. *
  64. * <p>The <i>collapseAttributes</i> property of this task can be set
  65. * to true (the default is false) which will instead result in the
  66. * following properties (note the difference in names of properties
  67. * corresponding to XML attributes):</p>
  68. *
  69. * <pre>
  70. * root-tag.myattr=true
  71. * root-tag.inner-tag=Text
  72. * root-tag.inner-tag.someattr=val
  73. * root-tag.a2.a3.a4=false
  74. * root-tag.x=x1,x2
  75. * </pre>
  76. *
  77. * <p>Optionally, to more closely mirror the abilities of the Property
  78. * task, a selected set of attributes can be treated specially. To
  79. * enable this behavior, the "semanticAttributes" property of this task
  80. * must be set to true (it defaults to false). If this attribute is
  81. * specified, the following attributes take on special meaning
  82. * (setting this to true implicitly sets collapseAttributes to true as
  83. * well):</p>
  84. *
  85. * <ul>
  86. * <li><b>value</b>: Identifies a text value for a property.</li>
  87. * <li><b>location</b>: Identifies a file location for a property.</li>
  88. * <li><b>id</b>: Sets an id for a property</li>
  89. * <li><b>refid</b>: Sets a property to the value of another property
  90. * based upon the provided id</li>
  91. * <li><b>pathid</b>: Defines a path rather than a property with
  92. * the given id.</li>
  93. * </ul>
  94. *
  95. * <p>For example, with keepRoot = false, the following properties file:</p>
  96. *
  97. * <pre>
  98. * &lt;root-tag&gt;
  99. * &lt;build&gt;
  100. * &lt;build folder="build"&gt;
  101. * &lt;classes id="build.classes" location="${build.folder}/classes"/&gt;
  102. * &lt;reference refid="build.classes"/&gt;
  103. * &lt;/build&gt;
  104. * &lt;compile&gt;
  105. * &lt;classpath pathid="compile.classpath"&gt;
  106. * &lt;pathelement location="${build.classes}"/&gt;
  107. * &lt;/classpath&gt;
  108. * &lt;/compile&gt;
  109. * &lt;run-time&gt;
  110. * &lt;jars&gt;*.jar&lt;/jars&gt;
  111. * &lt;classpath pathid="run-time.classpath"&gt;
  112. * &lt;path refid="compile.classpath"/&gt;
  113. * &lt;pathelement path="${run-time.jars}"/&gt;
  114. * &lt;/classpath&gt;
  115. * &lt;/run-time&gt;
  116. * &lt;/root-tag&gt;
  117. * </pre>
  118. *
  119. * <p>is equivalent to the following entries in a build file:</p>
  120. *
  121. * <pre>
  122. * &lt;property name="build" location="build"/&gt;
  123. * &lt;property name="build.classes" location="${build.location}/classes"/&gt;
  124. * &lt;property name="build.reference" refid="build.classes"/&gt;
  125. *
  126. * &lt;property name="run-time.jars" value="*.jar/&gt;
  127. *
  128. * &lt;classpath id="compile.classpath"&gt;
  129. * &lt;pathelement location="${build.classes}"/&gt;
  130. * &lt;/classpath&gt;
  131. *
  132. * &lt;classpath id="run-time.classpath"&gt;
  133. * &lt;path refid="compile.classpath"/&gt;
  134. * &lt;pathelement path="${run-time.jars}"/&gt;
  135. * &lt;/classpath&gt;
  136. * </pre>
  137. *
  138. * <p> This task <i>requires</i> the following attributes:</p>
  139. *
  140. * <ul>
  141. * <li><b>file</b>: The name of the file to load.</li>
  142. * </ul>
  143. *
  144. * <p>This task supports the following attributes:</p>
  145. *
  146. * <ul>
  147. * <li><b>prefix</b>: Optionally specify a prefix applied to
  148. * all properties loaded. Defaults to an empty string.</li>
  149. * <li><b>keepRoot</b>: Indicate whether the root xml element
  150. * is kept as part of property name. Defaults to true.</li>
  151. * <li><b>validate</b>: Indicate whether the xml file is validated.
  152. * Defaults to false.</li>
  153. * <li><b>collapseAttributes</b>: Indicate whether attributes are
  154. * stored in property names with parens or with period
  155. * delimiters. Defaults to false, meaning properties
  156. * are stored with parens (i.e., foo(attr)).</li>
  157. * <li><b>semanticAttributes</b>: Indicate whether attributes
  158. * named "location", "value", "refid" and "path"
  159. * are interpreted as ant properties. Defaults
  160. * to false.</li>
  161. * <li><b>rootDirectory</b>: Indicate the directory to use
  162. * as the root directory for resolving location
  163. * properties. Defaults to the directory
  164. * of the project using the task.</li>
  165. * <li><b>includeSemanticAttribute</b>: Indicate whether to include
  166. * the semantic attribute ("location" or "value") as
  167. * part of the property name. Defaults to false.</li>
  168. * </ul>
  169. *
  170. * @ant.task name="xmlproperty" category="xml"
  171. */
  172. public class XmlProperty extends org.apache.tools.ant.Task {
  173. private Resource src;
  174. private String prefix = "";
  175. private boolean keepRoot = true;
  176. private boolean validate = false;
  177. private boolean collapseAttributes = false;
  178. private boolean semanticAttributes = false;
  179. private boolean includeSemanticAttribute = false;
  180. private File rootDirectory = null;
  181. private Hashtable addedAttributes = new Hashtable();
  182. private XMLCatalog xmlCatalog = new XMLCatalog();
  183. private static final String ID = "id";
  184. private static final String REF_ID = "refid";
  185. private static final String LOCATION = "location";
  186. private static final String VALUE = "value";
  187. private static final String PATH = "path";
  188. private static final String PATHID = "pathid";
  189. private static final String[] ATTRIBUTES = new String[] {
  190. ID, REF_ID, LOCATION, VALUE, PATH, PATHID
  191. };
  192. private static final FileUtils FILE_UTILS = FileUtils.getFileUtils();
  193. /**
  194. * Constructor.
  195. */
  196. public XmlProperty() {
  197. super();
  198. }
  199. /**
  200. * Initializes the task.
  201. */
  202. public void init() {
  203. super.init();
  204. xmlCatalog.setProject(getProject());
  205. }
  206. /**
  207. * @return the xmlCatalog as the entityresolver.
  208. */
  209. protected EntityResolver getEntityResolver() {
  210. return xmlCatalog;
  211. }
  212. /**
  213. * Run the task.
  214. * @throws BuildException The exception raised during task execution.
  215. * @todo validate the source file is valid before opening, print a better error message
  216. * @todo add a verbose level log message listing the name of the file being loaded
  217. */
  218. public void execute()
  219. throws BuildException {
  220. Resource r = getResource();
  221. if (r == null) {
  222. String msg = "XmlProperty task requires a source resource";
  223. throw new BuildException(msg);
  224. }
  225. try {
  226. log("Loading " + src, Project.MSG_VERBOSE);
  227. if (r.isExists()) {
  228. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  229. factory.setValidating(validate);
  230. factory.setNamespaceAware(false);
  231. DocumentBuilder builder = factory.newDocumentBuilder();
  232. builder.setEntityResolver(getEntityResolver());
  233. Document document = null;
  234. if (src instanceof FileResource) {
  235. document = builder.parse(((FileResource) src).getFile());
  236. } else {
  237. document = builder.parse(src.getInputStream());
  238. }
  239. Element topElement = document.getDocumentElement();
  240. // Keep a hashtable of attributes added by this task.
  241. // This task is allow to override its own properties
  242. // but not other properties. So we need to keep track
  243. // of which properties we've added.
  244. addedAttributes = new Hashtable();
  245. if (keepRoot) {
  246. addNodeRecursively(topElement, prefix, null);
  247. } else {
  248. NodeList topChildren = topElement.getChildNodes();
  249. int numChildren = topChildren.getLength();
  250. for (int i = 0; i < numChildren; i++) {
  251. addNodeRecursively(topChildren.item(i), prefix, null);
  252. }
  253. }
  254. } else {
  255. log("Unable to find property resource: " + r,
  256. Project.MSG_VERBOSE);
  257. }
  258. } catch (SAXException sxe) {
  259. // Error generated during parsing
  260. Exception x = sxe;
  261. if (sxe.getException() != null) {
  262. x = sxe.getException();
  263. }
  264. throw new BuildException("Failed to load "+src,x);
  265. } catch (ParserConfigurationException pce) {
  266. // Parser with specified options can't be built
  267. throw new BuildException(pce);
  268. } catch (IOException ioe) {
  269. // I/O error
  270. throw new BuildException("Failed to load " + src,ioe);
  271. }
  272. }
  273. /** Iterate through all nodes in the tree. */
  274. private void addNodeRecursively(Node node, String prefix,
  275. Object container) {
  276. // Set the prefix for this node to include its tag name.
  277. String nodePrefix = prefix;
  278. if (node.getNodeType() != Node.TEXT_NODE) {
  279. if (prefix.trim().length() > 0) {
  280. nodePrefix += ".";
  281. }
  282. nodePrefix += node.getNodeName();
  283. }
  284. // Pass the container to the processing of this node,
  285. Object nodeObject = processNode(node, nodePrefix, container);
  286. // now, iterate through children.
  287. if (node.hasChildNodes()) {
  288. NodeList nodeChildren = node.getChildNodes();
  289. int numChildren = nodeChildren.getLength();
  290. for (int i = 0; i < numChildren; i++) {
  291. // For each child, pass the object added by
  292. // processNode to its children -- in other word, each
  293. // object can pass information along to its children.
  294. addNodeRecursively(nodeChildren.item(i), nodePrefix,
  295. nodeObject);
  296. }
  297. }
  298. }
  299. void addNodeRecursively(org.w3c.dom.Node node, String prefix) {
  300. addNodeRecursively(node, prefix, null);
  301. }
  302. /**
  303. * Process the given node, adding any required attributes from
  304. * this child node alone -- but <em>not</em> processing any
  305. * children.
  306. *
  307. * @param node the XML Node to parse
  308. * @param prefix A string to prepend to any properties that get
  309. * added by this node.
  310. * @param container Optionally, an object that a parent node
  311. * generated that this node might belong to. For example, this
  312. * node could be within a node that generated a Path.
  313. * @return the Object created by this node. Generally, this is
  314. * either a String if this node resulted in setting an attribute,
  315. * or a Path.
  316. */
  317. public Object processNode (Node node, String prefix, Object container) {
  318. // Parse the attribute(s) and text of this node, adding
  319. // properties for each.
  320. // if the "path" attribute is specified, then return the created path
  321. // which will be passed to the children of this node.
  322. Object addedPath = null;
  323. // The value of an id attribute of this node.
  324. String id = null;
  325. if (node.hasAttributes()) {
  326. NamedNodeMap nodeAttributes = node.getAttributes();
  327. // Is there an id attribute?
  328. Node idNode = nodeAttributes.getNamedItem(ID);
  329. id = (semanticAttributes && idNode != null
  330. ? idNode.getNodeValue() : null);
  331. // Now, iterate through the attributes adding them.
  332. for (int i = 0; i < nodeAttributes.getLength(); i++) {
  333. Node attributeNode = nodeAttributes.item(i);
  334. if (!semanticAttributes) {
  335. String attributeName = getAttributeName(attributeNode);
  336. String attributeValue = getAttributeValue(attributeNode);
  337. addProperty(prefix + attributeName, attributeValue, null);
  338. } else {
  339. String nodeName = attributeNode.getNodeName();
  340. String attributeValue = getAttributeValue(attributeNode);
  341. Path containingPath = (container != null
  342. && container instanceof Path ? (Path) container : null);
  343. /*
  344. * The main conditional logic -- if the attribute
  345. * is somehow "special" (i.e., it has known
  346. * semantic meaning) then deal with it
  347. * appropriately.
  348. */
  349. if (nodeName.equals(ID)) {
  350. // ID has already been found above.
  351. continue;
  352. } else if (containingPath != null
  353. && nodeName.equals(PATH)) {
  354. // A "path" attribute for a node within a Path object.
  355. containingPath.setPath(attributeValue);
  356. } else if (container instanceof Path
  357. && nodeName.equals(REF_ID)) {
  358. // A "refid" attribute for a node within a Path object.
  359. containingPath.setPath(attributeValue);
  360. } else if (container instanceof Path
  361. && nodeName.equals(LOCATION)) {
  362. // A "location" attribute for a node within a
  363. // Path object.
  364. containingPath.setLocation(resolveFile(attributeValue));
  365. } else if (nodeName.equals(PATHID)) {
  366. // A node identifying a new path
  367. if (container != null) {
  368. throw new BuildException("XmlProperty does not "
  369. + "support nested paths");
  370. }
  371. addedPath = new Path(getProject());
  372. getProject().addReference(attributeValue, addedPath);
  373. } else {
  374. // An arbitrary attribute.
  375. String attributeName = getAttributeName(attributeNode);
  376. addProperty(prefix + attributeName, attributeValue, id);
  377. }
  378. }
  379. }
  380. }
  381. String nodeText = null;
  382. boolean emptyNode = false;
  383. boolean semanticEmptyOverride = false;
  384. if (node.getNodeType() == Node.ELEMENT_NODE
  385. && semanticAttributes
  386. && node.hasAttributes()
  387. && (node.getAttributes().getNamedItem(VALUE) != null
  388. || node.getAttributes().getNamedItem(LOCATION) != null
  389. || node.getAttributes().getNamedItem(REF_ID) != null
  390. || node.getAttributes().getNamedItem(PATH) != null
  391. || node.getAttributes().getNamedItem(PATHID) != null)) {
  392. semanticEmptyOverride = true;
  393. }
  394. if (node.getNodeType() == Node.TEXT_NODE) {
  395. // For the text node, add a property.
  396. nodeText = getAttributeValue(node);
  397. } else if ((node.getNodeType() == Node.ELEMENT_NODE)
  398. && (node.getChildNodes().getLength() == 1)
  399. && (node.getFirstChild().getNodeType() == Node.CDATA_SECTION_NODE)) {
  400. nodeText = node.getFirstChild().getNodeValue();
  401. if ("".equals(nodeText) && !semanticEmptyOverride) {
  402. emptyNode = true;
  403. }
  404. } else if ((node.getNodeType() == Node.ELEMENT_NODE)
  405. && (node.getChildNodes().getLength() == 0)
  406. && !semanticEmptyOverride) {
  407. nodeText = "";
  408. emptyNode = true;
  409. } else if ((node.getNodeType() == Node.ELEMENT_NODE)
  410. && (node.getChildNodes().getLength() == 1)
  411. && (node.getFirstChild().getNodeType() == Node.TEXT_NODE)
  412. && ("".equals(node.getFirstChild().getNodeValue()))
  413. && !semanticEmptyOverride) {
  414. nodeText = "";
  415. emptyNode = true;
  416. }
  417. if (nodeText != null) {
  418. // If the containing object was a String, then use it as the ID.
  419. if (semanticAttributes && id == null
  420. && container instanceof String) {
  421. id = (String) container;
  422. }
  423. if (nodeText.trim().length() != 0 || emptyNode) {
  424. addProperty(prefix, nodeText, id);
  425. }
  426. }
  427. // Return the Path we added or the ID of this node for
  428. // children to reference if needed. Path objects are
  429. // definitely used by child path elements, and ID may be used
  430. // for a child text node.
  431. return (addedPath != null ? addedPath : id);
  432. }
  433. /**
  434. * Actually add the given property/value to the project
  435. * after writing a log message.
  436. */
  437. private void addProperty (String name, String value, String id) {
  438. String msg = name + ":" + value;
  439. if (id != null) {
  440. msg += ("(id=" + id + ")");
  441. }
  442. log(msg, Project.MSG_DEBUG);
  443. if (addedAttributes.containsKey(name)) {
  444. // If this attribute was added by this task, then
  445. // we append this value to the existing value.
  446. // We use the setProperty method which will
  447. // forcibly override the property if it already exists.
  448. // We need to put these properties into the project
  449. // when we read them, though (instead of keeping them
  450. // outside of the project and batch adding them at the end)
  451. // to allow other properties to reference them.
  452. value = (String) addedAttributes.get(name) + "," + value;
  453. getProject().setProperty(name, value);
  454. } else {
  455. getProject().setNewProperty(name, value);
  456. }
  457. addedAttributes.put(name, value);
  458. if (id != null) {
  459. getProject().addReference(id, value);
  460. }
  461. }
  462. /**
  463. * Return a reasonable attribute name for the given node.
  464. * If we are using semantic attributes or collapsing
  465. * attributes, the returned name is ".nodename".
  466. * Otherwise, we return "(nodename)". This is long-standing
  467. * (and default) &lt;xmlproperty&gt; behavior.
  468. */
  469. private String getAttributeName (Node attributeNode) {
  470. String attributeName = attributeNode.getNodeName();
  471. if (semanticAttributes) {
  472. // Never include the "refid" attribute as part of the
  473. // attribute name.
  474. if (attributeName.equals(REF_ID)) {
  475. return "";
  476. // Otherwise, return it appended unless property to hide it is set.
  477. } else if (!isSemanticAttribute(attributeName)
  478. || includeSemanticAttribute) {
  479. return "." + attributeName;
  480. } else {
  481. return "";
  482. }
  483. } else if (collapseAttributes) {
  484. return "." + attributeName;
  485. } else {
  486. return "(" + attributeName + ")";
  487. }
  488. }
  489. /**
  490. * Return whether the provided attribute name is recognized or not.
  491. */
  492. private static boolean isSemanticAttribute (String attributeName) {
  493. for (int i = 0; i < ATTRIBUTES.length; i++) {
  494. if (attributeName.equals(ATTRIBUTES[i])) {
  495. return true;
  496. }
  497. }
  498. return false;
  499. }
  500. /**
  501. * Return the value for the given attribute.
  502. * If we are not using semantic attributes, its just the
  503. * literal string value of the attribute.
  504. *
  505. * <p>If we <em>are</em> using semantic attributes, then first
  506. * dependent properties are resolved (i.e., ${foo} is resolved
  507. * based on the foo property value), and then an appropriate data
  508. * type is used. In particular, location-based properties are
  509. * resolved to absolute file names. Also for refid values, look
  510. * up the referenced object from the project.</p>
  511. */
  512. private String getAttributeValue (Node attributeNode) {
  513. String nodeValue = attributeNode.getNodeValue().trim();
  514. if (semanticAttributes) {
  515. String attributeName = attributeNode.getNodeName();
  516. nodeValue = getProject().replaceProperties(nodeValue);
  517. if (attributeName.equals(LOCATION)) {
  518. File f = resolveFile(nodeValue);
  519. return f.getPath();
  520. } else if (attributeName.equals(REF_ID)) {
  521. Object ref = getProject().getReference(nodeValue);
  522. if (ref != null) {
  523. return ref.toString();
  524. }
  525. }
  526. }
  527. return nodeValue;
  528. }
  529. /**
  530. * The XML file to parse; required.
  531. * @param src the file to parse
  532. */
  533. public void setFile(File src) {
  534. setSrcResource(new FileResource(src));
  535. }
  536. /**
  537. * The resource to pack; required.
  538. * @param src resource to expand
  539. */
  540. public void setSrcResource(Resource src) {
  541. if (src.isDirectory()) {
  542. throw new BuildException("the source can't be a directory");
  543. }
  544. if (src instanceof FileResource && !supportsNonFileResources()) {
  545. throw new BuildException("Only FileSystem resources are"
  546. + " supported.");
  547. }
  548. this.src = src;
  549. }
  550. /**
  551. * Set the source resource.
  552. * @param a the resource to pack as a single element Resource collection.
  553. */
  554. public void addConfigured(ResourceCollection a) {
  555. if (a.size() != 1) {
  556. throw new BuildException("only single argument resource collections"
  557. + " are supported as archives");
  558. }
  559. setSrcResource((Resource) a.iterator().next());
  560. }
  561. /**
  562. * the prefix to prepend to each property
  563. * @param prefix the prefix to prepend to each property
  564. */
  565. public void setPrefix(String prefix) {
  566. this.prefix = prefix.trim();
  567. }
  568. /**
  569. * flag to include the xml root tag as a
  570. * first value in the property name; optional,
  571. * default is true
  572. * @param keepRoot if true (default), include the xml root tag
  573. */
  574. public void setKeeproot(boolean keepRoot) {
  575. this.keepRoot = keepRoot;
  576. }
  577. /**
  578. * flag to validate the XML file; optional, default false
  579. * @param validate if true validate the XML file, default false
  580. */
  581. public void setValidate(boolean validate) {
  582. this.validate = validate;
  583. }
  584. /**
  585. * flag to treat attributes as nested elements;
  586. * optional, default false
  587. * @param collapseAttributes if true treat attributes as nested elements
  588. */
  589. public void setCollapseAttributes(boolean collapseAttributes) {
  590. this.collapseAttributes = collapseAttributes;
  591. }
  592. /**
  593. * Attribute to enable special handling of attributes - see ant manual.
  594. * @param semanticAttributes if true enable the special handling.
  595. */
  596. public void setSemanticAttributes(boolean semanticAttributes) {
  597. this.semanticAttributes = semanticAttributes;
  598. }
  599. /**
  600. * The directory to use for resolving file references.
  601. * Ignored if semanticAttributes is not set to true.
  602. * @param rootDirectory the directory.
  603. */
  604. public void setRootDirectory(File rootDirectory) {
  605. this.rootDirectory = rootDirectory;
  606. }
  607. /**
  608. * Include the semantic attribute name as part of the property name.
  609. * Ignored if semanticAttributes is not set to true.
  610. * @param includeSemanticAttribute if true include the sematic attribute
  611. * name.
  612. */
  613. public void setIncludeSemanticAttribute(boolean includeSemanticAttribute) {
  614. this.includeSemanticAttribute = includeSemanticAttribute;
  615. }
  616. /**
  617. * add an XMLCatalog as a nested element; optional.
  618. * @param catalog the XMLCatalog to use
  619. */
  620. public void addConfiguredXMLCatalog(XMLCatalog catalog) {
  621. xmlCatalog.addConfiguredXMLCatalog(catalog);
  622. }
  623. /* Expose members for extensibility */
  624. /**
  625. * @return the file attribute.
  626. */
  627. protected File getFile () {
  628. if (src instanceof FileResource) {
  629. return ((FileResource) src).getFile();
  630. } else {
  631. return null;
  632. }
  633. }
  634. /**
  635. * @return the resource.
  636. */
  637. protected Resource getResource() {
  638. // delegate this way around to support subclasses that
  639. // overwrite getFile
  640. File f = getFile();
  641. if (f != null) {
  642. return new FileResource(f);
  643. } else {
  644. return src;
  645. }
  646. }
  647. /**
  648. * @return the prefix attribute.
  649. */
  650. protected String getPrefix () {
  651. return this.prefix;
  652. }
  653. /**
  654. * @return the keeproot attribute.
  655. */
  656. protected boolean getKeeproot () {
  657. return this.keepRoot;
  658. }
  659. /**
  660. * @return the validate attribute.
  661. */
  662. protected boolean getValidate () {
  663. return this.validate;
  664. }
  665. /**
  666. * @return the collapse attributes attribute.
  667. */
  668. protected boolean getCollapseAttributes () {
  669. return this.collapseAttributes;
  670. }
  671. /**
  672. * @return the semantic attributes attribute.
  673. */
  674. protected boolean getSemanticAttributes () {
  675. return this.semanticAttributes;
  676. }
  677. /**
  678. * @return the root directory attribute.
  679. */
  680. protected File getRootDirectory () {
  681. return this.rootDirectory;
  682. }
  683. /**
  684. * @return the include semantic attribute.
  685. */
  686. protected boolean getIncludeSementicAttribute () {
  687. return this.includeSemanticAttribute;
  688. }
  689. /**
  690. * Let project resolve the file - or do it ourselves if
  691. * rootDirectory has been set.
  692. */
  693. private File resolveFile(String fileName) {
  694. if (rootDirectory == null) {
  695. return FILE_UTILS.resolveFile(getProject().getBaseDir(),fileName);
  696. }
  697. return FILE_UTILS.resolveFile(rootDirectory, fileName);
  698. }
  699. /**
  700. * Whether this task can deal with non-file resources.
  701. *
  702. * <p>This implementation returns true only if this task is
  703. * &lt;xmlproperty&gt;. Any subclass of this class that also wants to
  704. * support non-file resources needs to override this method. We
  705. * need to do so for backwards compatibility reasons since we
  706. * can't expect subclasses to support resources.</p>
  707. *
  708. * @since Ant 1.7
  709. */
  710. protected boolean supportsNonFileResources() {
  711. return getClass().equals(XmlProperty.class);
  712. }
  713. }