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.

AntStructure.java 13 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 2000-2003 The Apache Software Foundation. All rights
  5. * reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if
  20. * any, must include the following acknowlegement:
  21. * "This product includes software developed by the
  22. * Apache Software Foundation (http://www.apache.org/)."
  23. * Alternately, this acknowlegement may appear in the software itself,
  24. * if and wherever such third-party acknowlegements normally appear.
  25. *
  26. * 4. The names "Ant" and "Apache Software
  27. * Foundation" must not be used to endorse or promote products derived
  28. * from this software without prior written permission. For written
  29. * permission, please contact apache@apache.org.
  30. *
  31. * 5. Products derived from this software may not be called "Apache"
  32. * nor may "Apache" appear in their names without prior written
  33. * permission of the Apache Group.
  34. *
  35. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46. * SUCH DAMAGE.
  47. * ====================================================================
  48. *
  49. * This software consists of voluntary contributions made by many
  50. * individuals on behalf of the Apache Software Foundation. For more
  51. * information on the Apache Software Foundation, please see
  52. * <http://www.apache.org/>.
  53. */
  54. package org.apache.tools.ant.taskdefs;
  55. import java.io.File;
  56. import java.io.FileOutputStream;
  57. import java.io.FileWriter;
  58. import java.io.IOException;
  59. import java.io.OutputStreamWriter;
  60. import java.io.PrintWriter;
  61. import java.io.UnsupportedEncodingException;
  62. import java.util.Enumeration;
  63. import java.util.Hashtable;
  64. import java.util.Vector;
  65. import org.apache.tools.ant.BuildException;
  66. import org.apache.tools.ant.IntrospectionHelper;
  67. import org.apache.tools.ant.Task;
  68. import org.apache.tools.ant.TaskContainer;
  69. import org.apache.tools.ant.types.EnumeratedAttribute;
  70. import org.apache.tools.ant.types.Reference;
  71. /**
  72. * Creates a partial DTD for Ant from the currently known tasks.
  73. *
  74. * @author Stefan Bodewig
  75. *
  76. * @version $Revision$
  77. *
  78. * @since Ant 1.1
  79. *
  80. * @ant.task category="xml"
  81. */
  82. public class AntStructure extends Task {
  83. private final String lSep = System.getProperty("line.separator");
  84. private final String BOOLEAN = "%boolean;";
  85. private final String TASKS = "%tasks;";
  86. private final String TYPES = "%types;";
  87. private Hashtable visited = new Hashtable();
  88. private File output;
  89. /**
  90. * The output file.
  91. */
  92. public void setOutput(File output) {
  93. this.output = output;
  94. }
  95. /**
  96. * Build the antstructure DTD.
  97. *
  98. * @exception BuildException if the DTD cannot be written.
  99. */
  100. public void execute() throws BuildException {
  101. if (output == null) {
  102. throw new BuildException("output attribute is required", getLocation());
  103. }
  104. PrintWriter out = null;
  105. try {
  106. try {
  107. out = new PrintWriter(new OutputStreamWriter(new FileOutputStream(output), "UTF8"));
  108. } catch (UnsupportedEncodingException ue) {
  109. /*
  110. * Plain impossible with UTF8, see
  111. * http://java.sun.com/products/jdk/1.2/docs/guide/internat/encoding.doc.html
  112. *
  113. * fallback to platform specific anyway.
  114. */
  115. out = new PrintWriter(new FileWriter(output));
  116. }
  117. printHead(out, getProject().getTaskDefinitions().keys(),
  118. getProject().getDataTypeDefinitions().keys());
  119. printTargetDecl(out);
  120. Enumeration dataTypes = getProject().getDataTypeDefinitions().keys();
  121. while (dataTypes.hasMoreElements()) {
  122. String typeName = (String) dataTypes.nextElement();
  123. printElementDecl(out, typeName,
  124. (Class) getProject().getDataTypeDefinitions().get(typeName));
  125. }
  126. Enumeration tasks = getProject().getTaskDefinitions().keys();
  127. while (tasks.hasMoreElements()) {
  128. String taskName = (String) tasks.nextElement();
  129. printElementDecl(out, taskName,
  130. (Class) getProject().getTaskDefinitions().get(taskName));
  131. }
  132. } catch (IOException ioe) {
  133. throw new BuildException("Error writing "
  134. + output.getAbsolutePath(), ioe, getLocation());
  135. } finally {
  136. if (out != null) {
  137. out.close();
  138. }
  139. visited.clear();
  140. }
  141. }
  142. /**
  143. * Prints the header of the generated output.
  144. *
  145. * <p>Basically this prints the XML declaration, defines some
  146. * entities and the project element.</p>
  147. */
  148. private void printHead(PrintWriter out, Enumeration tasks,
  149. Enumeration types) {
  150. out.println("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
  151. out.println("<!ENTITY % boolean \"(true|false|on|off|yes|no)\">");
  152. out.print("<!ENTITY % tasks \"");
  153. boolean first = true;
  154. while (tasks.hasMoreElements()) {
  155. String taskName = (String) tasks.nextElement();
  156. if (!first) {
  157. out.print(" | ");
  158. } else {
  159. first = false;
  160. }
  161. out.print(taskName);
  162. }
  163. out.println("\">");
  164. out.print("<!ENTITY % types \"");
  165. first = true;
  166. while (types.hasMoreElements()) {
  167. String typeName = (String) types.nextElement();
  168. if (!first) {
  169. out.print(" | ");
  170. } else {
  171. first = false;
  172. }
  173. out.print(typeName);
  174. }
  175. out.println("\">");
  176. out.println("");
  177. out.print("<!ELEMENT project (target | ");
  178. out.print(TASKS);
  179. out.print(" | ");
  180. out.print(TYPES);
  181. out.println(")*>");
  182. out.println("<!ATTLIST project");
  183. out.println(" name CDATA #IMPLIED");
  184. out.println(" default CDATA #IMPLIED");
  185. out.println(" basedir CDATA #IMPLIED>");
  186. out.println("");
  187. }
  188. /**
  189. * Prints the definition for the target element.
  190. */
  191. private void printTargetDecl(PrintWriter out) {
  192. out.print("<!ELEMENT target (");
  193. out.print(TASKS);
  194. out.print(" | ");
  195. out.print(TYPES);
  196. out.println(")*>");
  197. out.println("");
  198. out.println("<!ATTLIST target");
  199. out.println(" id ID #IMPLIED");
  200. out.println(" name CDATA #REQUIRED");
  201. out.println(" if CDATA #IMPLIED");
  202. out.println(" unless CDATA #IMPLIED");
  203. out.println(" depends CDATA #IMPLIED");
  204. out.println(" description CDATA #IMPLIED>");
  205. out.println("");
  206. }
  207. /**
  208. * Print the definition for a given element.
  209. */
  210. private void printElementDecl(PrintWriter out, String name, Class element)
  211. throws BuildException {
  212. if (visited.containsKey(name)) {
  213. return;
  214. }
  215. visited.put(name, "");
  216. IntrospectionHelper ih = null;
  217. try {
  218. ih = IntrospectionHelper.getHelper(element);
  219. } catch (Throwable t) {
  220. /*
  221. * XXX - failed to load the class properly.
  222. *
  223. * should we print a warning here?
  224. */
  225. return;
  226. }
  227. StringBuffer sb = new StringBuffer("<!ELEMENT ");
  228. sb.append(name).append(" ");
  229. if (org.apache.tools.ant.types.Reference.class.equals(element)) {
  230. sb.append("EMPTY>").append(lSep);
  231. sb.append("<!ATTLIST ").append(name);
  232. sb.append(lSep).append(" id ID #IMPLIED");
  233. sb.append(lSep).append(" refid IDREF #IMPLIED");
  234. sb.append(">").append(lSep);
  235. out.println(sb);
  236. return;
  237. }
  238. Vector v = new Vector();
  239. if (ih.supportsCharacters()) {
  240. v.addElement("#PCDATA");
  241. }
  242. if (TaskContainer.class.isAssignableFrom(element)) {
  243. v.addElement(TASKS);
  244. }
  245. Enumeration enum = ih.getNestedElements();
  246. while (enum.hasMoreElements()) {
  247. v.addElement(enum.nextElement());
  248. }
  249. if (v.isEmpty()) {
  250. sb.append("EMPTY");
  251. } else {
  252. sb.append("(");
  253. final int count = v.size();
  254. for (int i = 0; i < count; i++) {
  255. if (i != 0) {
  256. sb.append(" | ");
  257. }
  258. sb.append(v.elementAt(i));
  259. }
  260. sb.append(")");
  261. if (count > 1 || !v.elementAt(0).equals("#PCDATA")) {
  262. sb.append("*");
  263. }
  264. }
  265. sb.append(">");
  266. out.println(sb);
  267. sb = new StringBuffer("<!ATTLIST ");
  268. sb.append(name);
  269. sb.append(lSep).append(" id ID #IMPLIED");
  270. enum = ih.getAttributes();
  271. while (enum.hasMoreElements()) {
  272. String attrName = (String) enum.nextElement();
  273. if ("id".equals(attrName)) {
  274. continue;
  275. }
  276. sb.append(lSep).append(" ").append(attrName).append(" ");
  277. Class type = ih.getAttributeType(attrName);
  278. if (type.equals(java.lang.Boolean.class)
  279. || type.equals(java.lang.Boolean.TYPE)) {
  280. sb.append(BOOLEAN).append(" ");
  281. } else if (Reference.class.isAssignableFrom(type)) {
  282. sb.append("IDREF ");
  283. } else if (EnumeratedAttribute.class.isAssignableFrom(type)) {
  284. try {
  285. EnumeratedAttribute ea =
  286. (EnumeratedAttribute) type.newInstance();
  287. String[] values = ea.getValues();
  288. if (values == null
  289. || values.length == 0
  290. || !areNmtokens(values)) {
  291. sb.append("CDATA ");
  292. } else {
  293. sb.append("(");
  294. for (int i = 0; i < values.length; i++) {
  295. if (i != 0) {
  296. sb.append(" | ");
  297. }
  298. sb.append(values[i]);
  299. }
  300. sb.append(") ");
  301. }
  302. } catch (InstantiationException ie) {
  303. sb.append("CDATA ");
  304. } catch (IllegalAccessException ie) {
  305. sb.append("CDATA ");
  306. }
  307. } else {
  308. sb.append("CDATA ");
  309. }
  310. sb.append("#IMPLIED");
  311. }
  312. sb.append(">").append(lSep);
  313. out.println(sb);
  314. final int count = v.size();
  315. for (int i = 0; i < count; i++) {
  316. String nestedName = (String) v.elementAt(i);
  317. if (!"#PCDATA".equals(nestedName)
  318. && !TASKS.equals(nestedName)
  319. && !TYPES.equals(nestedName)) {
  320. printElementDecl(out, nestedName, ih.getElementType(nestedName));
  321. }
  322. }
  323. }
  324. /**
  325. * Does this String match the XML-NMTOKEN production?
  326. */
  327. protected boolean isNmtoken(String s) {
  328. final int length = s.length();
  329. for (int i = 0; i < length; i++) {
  330. char c = s.charAt(i);
  331. // XXX - we are ommitting CombiningChar and Extender here
  332. if (!Character.isLetterOrDigit(c)
  333. && c != '.' && c != '-' && c != '_' && c != ':') {
  334. return false;
  335. }
  336. }
  337. return true;
  338. }
  339. /**
  340. * Do the Strings all match the XML-NMTOKEN production?
  341. *
  342. * <p>Otherwise they are not suitable as an enumerated attribute,
  343. * for example.</p>
  344. */
  345. protected boolean areNmtokens(String[] s) {
  346. for (int i = 0; i < s.length; i++) {
  347. if (!isNmtoken(s[i])) {
  348. return false;
  349. }
  350. }
  351. return true;
  352. }
  353. }