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.

custom-programming.html 13 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. -->
  15. <html>
  16. <head>
  17. <meta http-equiv="Content-Language" content="en-us">
  18. <link rel="stylesheet" type="text/css" href="../stylesheets/style.css">
  19. <title>Custom Components</title>
  20. </head>
  21. <body>
  22. <h2>Custom Components</h2>
  23. <h3>Overview</h3>
  24. <p>
  25. Custom components are conditions, selectors, filters and other objects
  26. that are defined outside Apache Ant core.
  27. </p>
  28. <p>
  29. In Ant 1.6 custom conditions, selectors and filters has been
  30. overhauled.
  31. </p>
  32. <p>
  33. It is now possible to define custom conditions, selectors and filters
  34. that behave like Ant Core components. This is achieved by allowing
  35. datatypes defined in build scripts to be used as custom components if
  36. the class of the datatype is compatible, or has been adapted by an
  37. adapter class.
  38. </p>
  39. <p>
  40. The old methods of defining custom components are still supported.
  41. </p>
  42. <h3>Definition and use</h3>
  43. <p>
  44. A custom component is a normal Java class that implements a particular
  45. interface or extends a particular class, or has been adapted to the
  46. interface or class.
  47. </p>
  48. <p>
  49. It is exactly like writing
  50. a <a href="../develop.html#writingowntask">custom task</a>. One
  51. defines attributes and nested elements by writing <em>setter</em>
  52. methods and <em>add</em> methods.
  53. </p>
  54. <p>
  55. After the class has been written, it is added to the ant system by
  56. using <code>&lt;typedef&gt;</code>.
  57. </p>
  58. <h3 id="customconditions">Custom Conditions</h3>
  59. <p>
  60. Custom conditions are datatypes that
  61. implement <code>org.apache.tools.ant.taskdefs.condition.Condition</code>.
  62. For example a custom condition that returns true if a string is all
  63. upper case could be written as:
  64. </p>
  65. <pre>
  66. package com.mydomain;
  67. import org.apache.tools.ant.BuildException;
  68. import org.apache.tools.ant.taskdefs.condition.Condition;
  69. public class AllUpperCaseCondition implements Condition {
  70. private String value;
  71. // The setter for the "value" attribute
  72. public void setValue(String value) {
  73. this.value = value;
  74. }
  75. // This method evaluates the condition
  76. public boolean eval() {
  77. if (value == null) {
  78. throw new BuildException("value attribute is not set");
  79. }
  80. return value.toUpperCase().equals(value);
  81. }
  82. }</pre>
  83. <p>
  84. Adding the condition to the system is achieved as follows:
  85. </p>
  86. <pre>
  87. &lt;typedef
  88. name="alluppercase"
  89. classname="com.mydomain.AllUpperCaseCondition"
  90. classpath="${mydomain.classes}"/&gt;</pre>
  91. <p>
  92. This condition can now be used wherever a Core Ant condition is used.
  93. </p>
  94. <pre>
  95. &lt;condition property="allupper"&gt;
  96. &lt;alluppercase value="THIS IS ALL UPPER CASE"/&gt;
  97. &lt;/condition&gt;</pre>
  98. <h3 id="customselectors">Custom Selectors</h3>
  99. <p>
  100. Custom selectors are datatypes that
  101. implement <code>org.apache.tools.ant.types.selectors.FileSelector</code>.
  102. </p>
  103. <p>
  104. There is only one method required, <code>public boolean
  105. isSelected(File basedir, String filename, File file)</code>. It
  106. returns true or false depending on whether the given file should be
  107. selected or not.
  108. </p>
  109. <p>
  110. An example of a custom selection that selects filenames ending
  111. in <samp>.java</samp> would be:
  112. </p>
  113. <pre>
  114. package com.mydomain;
  115. import java.io.File;
  116. import org.apache.tools.ant.types.selectors.FileSelector;
  117. public class JavaSelector implements FileSelector {
  118. public boolean isSelected(File b, String filename, File f) {
  119. return filename.toLowerCase().endsWith(".java");
  120. }
  121. }</pre>
  122. <p>
  123. Adding the selector to the system is achieved as follows:
  124. </p>
  125. <pre>
  126. &lt;typedef
  127. name="javaselector"
  128. classname="com.mydomain.JavaSelector"
  129. classpath="${mydomain.classes}"/&gt;</pre>
  130. <p>
  131. This selector can now be used wherever a Core Ant selector is used,
  132. for example:
  133. </p>
  134. <pre>
  135. &lt;copy todir="to"&gt;
  136. &lt;fileset dir="src"&gt;
  137. &lt;javaselector/&gt;
  138. &lt;/fileset&gt;
  139. &lt;/copy&gt;</pre>
  140. <p>
  141. One may
  142. use <code>org.apache.tools.ant.types.selectors.BaseSelector</code>, a
  143. convenience class that provides reasonable default behaviour. It has
  144. some predefined behaviours you can take advantage of. Any time you
  145. encounter a problem when setting attributes or adding tags, you can
  146. call <code>setError(String errmsg)</code> and the class will know that
  147. there is a problem. Then, at the top of your <code>isSelected()</code>
  148. method call <code>validate()</code> and a BuildException will be
  149. thrown with the contents of your error
  150. message. The <code>validate()</code> method also gives you a last
  151. chance to check your settings for consistency because it
  152. calls <code>verifySettings()</code>. Override this method and
  153. call <code>setError()</code> within it if you detect any problems in
  154. how your selector is set up.
  155. </p>
  156. <p>
  157. To write custom selector containers one should
  158. extend <code>org.apache.tools.ant.types.selectors.BaseSelectorContainer</code>.
  159. Implement the <code>public boolean isSelected(File baseDir, String
  160. filename, File file)</code> method to do the right thing. Chances are
  161. you'll want to iterate over the selectors under you, so
  162. use <code>selectorElements()</code> to get an iterator that will do
  163. that.
  164. </p>
  165. <p>
  166. For example to create a selector container that will select files if a
  167. certain number of contained selectors select, one could write a
  168. selector as follows:
  169. </p>
  170. <pre>
  171. public class MatchNumberSelectors extends BaseSelectorContainer {
  172. private int number = -1;
  173. public void setNumber(int number) {
  174. this.number = number;
  175. }
  176. public void verifySettings() {
  177. if (number &lt; 0) {
  178. throw new BuildException("Number attribute should be set");
  179. }
  180. }
  181. public boolean isSelected(File baseDir, String filename, File file) {
  182. validate();
  183. int numberSelected = 0;
  184. for (Enumeration e = selectorElements(); e.hasNextElement();) {
  185. FileSelector s = (FileSelector) e.nextElement();
  186. if (s.isSelected(baseDir, filename, file)) {
  187. numberSelected++;
  188. }
  189. }
  190. return numberSelected == number;
  191. }
  192. }</pre>
  193. <p>
  194. To define and use this selector one could do:
  195. </p>
  196. <pre>
  197. &lt;typedef name="numberselected"
  198. classname="com.mydomain.MatchNumberSelectors"/&gt;
  199. ...
  200. &lt;fileset dir="${src.path}"&gt;
  201. &lt;numberselected number="2"&gt;
  202. &lt;contains text="script" casesensitive="no"/&gt;
  203. &lt;size value="4" units="Ki" when="more"/&gt;
  204. &lt;javaselector/&gt;
  205. &lt;/numberselected&gt;
  206. &lt;/fileset&gt;</pre>
  207. <p>
  208. <em>The custom selector</em>
  209. </p>
  210. <p>
  211. The custom selector was the pre Ant 1.6 way of defining custom
  212. selectors. This method is still supported for backward compatibility.
  213. </p>
  214. <p>
  215. You can write your own selectors and use them within the selector
  216. containers by specifying them within the <code>&lt;custom&gt;</code>
  217. tag.
  218. </p>
  219. <p>
  220. To create a new Custom Selector, you have to create a class that
  221. implements <code>org.apache.tools.ant.types.selectors.ExtendFileSelector</code>.
  222. The easiest way to do that is through the convenience base
  223. class <code>org.apache.tools.ant.types.selectors.BaseExtendSelector</code>,
  224. which provides all of the methods for
  225. supporting <code>&lt;param&gt;</code> tags. First, override
  226. the <code>isSelected()</code> method, and optionally
  227. the <code>verifySettings()</code> method. If your custom selector
  228. requires parameters to be set, you can also override
  229. the <code>setParameters()</code> method and interpret the parameters
  230. that are passed in any way you like. Several of the core selectors
  231. demonstrate how to do that because they can also be used as custom
  232. selectors.
  233. </p>
  234. <p>
  235. Once that is written, you include it in your build file by using
  236. the <code>&lt;custom&gt;</code> tag.
  237. </p>
  238. <table class="attr">
  239. <tr>
  240. <th>Attribute</th>
  241. <th>Description</th>
  242. <th>Required</th>
  243. </tr>
  244. <tr>
  245. <td>classname</td>
  246. <td>
  247. The name of your class that
  248. implements <code>org.apache.tools.ant.types.selectors.FileSelector</code>.
  249. </td>
  250. <td>Yes</td>
  251. </tr>
  252. <tr>
  253. <td>classpath</td>
  254. <td>
  255. The classpath to use in order to load the custom selector class. If
  256. neither <var>classpath</var> nor <var>classpathref</var> are specified, the class will be
  257. loaded from the classpath that Ant uses.
  258. </td>
  259. <td>No</td>
  260. </tr>
  261. <tr>
  262. <td>classpathref</td>
  263. <td>
  264. A reference to a classpath previously defined. If neither <var>classpathref</var>
  265. nor <var>classpath</var> above are specified, the class will be loaded from the classpath
  266. that Ant uses.
  267. </td>
  268. <td>No</td>
  269. </tr>
  270. </table>
  271. <p>
  272. Here is how you use <code>&lt;custom&gt;</code> to use your class as a
  273. selector:
  274. </p>
  275. <pre>
  276. &lt;fileset dir="${mydir}" includes="**/*"&gt;
  277. &lt;custom classname="com.mydomain.MySelector"&gt;
  278. &lt;param name="myattribute" value="myvalue"/&gt;
  279. &lt;/custom&gt;
  280. &lt;/fileset&gt;</pre>
  281. <p>The core selectors that can also be used as custom selectors are</p>
  282. <ul>
  283. <li><a href="selectors.html#containsselect">Contains Selector</a> with
  284. classname <code>org.apache.tools.ant.types.selectors.ContainsSelector</code></li>
  285. <li><a href="selectors.html#dateselect">Date Selector</a> with
  286. classname <code>org.apache.tools.ant.types.selectors.DateSelector</code></li>
  287. <li><a href="selectors.html#depthselect">Depth Selector</a> with
  288. classname <code>org.apache.tools.ant.types.selectors.DepthSelector</code></li>
  289. <li><a href="selectors.html#filenameselect">Filename Selector</a> with
  290. classname <code>org.apache.tools.ant.types.selectors.FilenameSelector</code></li>
  291. <li><a href="selectors.html#sizeselect">Size Selector</a> with
  292. classname <code>org.apache.tools.ant.types.selectors.SizeSelector</code></li>
  293. </ul>
  294. <p>
  295. Here is the example from the Depth Selector section rewritten to use
  296. the selector through <code>&lt;custom&gt;</code>.
  297. </p>
  298. <pre>
  299. &lt;fileset dir="${doc.path}" includes="**/*"&gt;
  300. &lt;custom classname="org.apache.tools.ant.types.selectors.DepthSelector"&gt;
  301. &lt;param name="max" value="1"/&gt;
  302. &lt;/custom&gt;
  303. &lt;/fileset&gt;</pre>
  304. <p>Selects all files in the base directory and one directory below that.</p>
  305. <h3 id="filterreaders">Custom Filter Readers</h3>
  306. <p>
  307. Custom filter readers selectors are datatypes that
  308. implement <code>org.apache.tools.ant.types.filters.ChainableReader</code>.
  309. </p>
  310. <p>
  311. There is only one method required. <code>Reader chain(Reader
  312. reader)</code>. This returns a reader that filters input from the
  313. specified reader.
  314. </p>
  315. <p>
  316. For example a filterreader that removes every second character could
  317. be:
  318. </p>
  319. <pre>
  320. public class RemoveOddCharacters implements ChainableReader {
  321. public Reader chain(Reader reader) {
  322. return new BaseFilterReader(reader) {
  323. int count = 0;
  324. public int read() throws IOException {
  325. while (true) {
  326. int c = in.read();
  327. if (c == -1) {
  328. return c;
  329. }
  330. count++;
  331. if ((count % 2) == 1) {
  332. return c;
  333. }
  334. }
  335. }
  336. }
  337. }
  338. }</pre>
  339. <p>
  340. For line oriented filters it may be easier to
  341. extend <code>ChainableFilterReader</code> an inner class
  342. of <code>org.apache.tools.ant.filters.TokenFilter</code>.
  343. </p>
  344. <p>
  345. For example a filter that appends the line number could be
  346. </p>
  347. <pre>
  348. public class AddLineNumber extends ChainableReaderFilter {
  349. private void lineNumber = 0;
  350. public String filter(String string) {
  351. lineNumber++;
  352. return "" + lineNumber + "\t" + string;
  353. }
  354. }</pre>
  355. </body>
  356. </html>