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 14 kB

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