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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 2000-2002 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 "The Jakarta Project", "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 org.apache.tools.ant.BuildException;
  56. import org.apache.tools.ant.IntrospectionHelper;
  57. import org.apache.tools.ant.Task;
  58. import org.apache.tools.ant.TaskContainer;
  59. import org.apache.tools.ant.types.EnumeratedAttribute;
  60. import java.util.Enumeration;
  61. import java.util.Hashtable;
  62. import java.util.Vector;
  63. import java.io.File;
  64. import java.io.PrintWriter;
  65. import java.io.OutputStreamWriter;
  66. import java.io.FileOutputStream;
  67. import java.io.UnsupportedEncodingException;
  68. import java.io.FileWriter;
  69. import java.io.IOException;
  70. /**
  71. * Creates a partial DTD for Ant from the currently known tasks.
  72. *
  73. * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a>
  74. *
  75. * @version $Revision$
  76. */
  77. public class AntStructure extends Task {
  78. private final String lSep = System.getProperty("line.separator");
  79. private final String BOOLEAN = "%boolean;";
  80. private final String TASKS = "%tasks;";
  81. private final String TYPES = "%types;";
  82. private Hashtable visited = new Hashtable();
  83. private File output;
  84. /**
  85. * The output file.
  86. */
  87. public void setOutput(File output) {
  88. this.output = output;
  89. }
  90. public void execute() throws BuildException {
  91. if (output == null) {
  92. throw new BuildException("output attribute is required", location);
  93. }
  94. PrintWriter out = null;
  95. try {
  96. try {
  97. out = new PrintWriter(new OutputStreamWriter(new FileOutputStream(output), "UTF8"));
  98. } catch (UnsupportedEncodingException ue) {
  99. /*
  100. * Plain impossible with UTF8, see
  101. * http://java.sun.com/products/jdk/1.2/docs/guide/internat/encoding.doc.html
  102. *
  103. * fallback to platform specific anyway.
  104. */
  105. out = new PrintWriter(new FileWriter(output));
  106. }
  107. printHead(out, project.getTaskDefinitions().keys(),
  108. project.getDataTypeDefinitions().keys());
  109. printTargetDecl(out);
  110. Enumeration dataTypes = project.getDataTypeDefinitions().keys();
  111. while (dataTypes.hasMoreElements()) {
  112. String typeName = (String) dataTypes.nextElement();
  113. printElementDecl(out, typeName,
  114. (Class) project.getDataTypeDefinitions().get(typeName));
  115. }
  116. Enumeration tasks = project.getTaskDefinitions().keys();
  117. while (tasks.hasMoreElements()) {
  118. String taskName = (String) tasks.nextElement();
  119. printElementDecl(out, taskName,
  120. (Class) project.getTaskDefinitions().get(taskName));
  121. }
  122. printTail(out);
  123. } catch (IOException ioe) {
  124. throw new BuildException("Error writing "+output.getAbsolutePath(),
  125. ioe, location);
  126. } finally {
  127. if (out != null) {
  128. out.close();
  129. }
  130. }
  131. }
  132. private void printHead(PrintWriter out, Enumeration tasks,
  133. Enumeration types) {
  134. out.println("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
  135. out.println("<!ENTITY % boolean \"(true|false|on|off|yes|no)\">");
  136. out.print("<!ENTITY % tasks \"");
  137. boolean first = true;
  138. while (tasks.hasMoreElements()) {
  139. String taskName = (String) tasks.nextElement();
  140. if (!first) {
  141. out.print(" | ");
  142. } else {
  143. first = false;
  144. }
  145. out.print(taskName);
  146. }
  147. out.println("\">");
  148. out.print("<!ENTITY % types \"");
  149. first = true;
  150. while (types.hasMoreElements()) {
  151. String typeName = (String) types.nextElement();
  152. if (!first) {
  153. out.print(" | ");
  154. } else {
  155. first = false;
  156. }
  157. out.print(typeName);
  158. }
  159. out.println("\">");
  160. out.println("");
  161. out.print("<!ELEMENT project (target | property | taskdef | ");
  162. out.print(TYPES);
  163. out.println(")*>");
  164. out.println("<!ATTLIST project");
  165. out.println(" name CDATA #IMPLIED");
  166. out.println(" default CDATA #REQUIRED");
  167. out.println(" basedir CDATA #IMPLIED>");
  168. out.println("");
  169. }
  170. private void printTargetDecl(PrintWriter out) {
  171. out.print("<!ELEMENT target (");
  172. out.print(TASKS);
  173. out.print(" | ");
  174. out.print(TYPES);
  175. out.println(")*>");
  176. out.println("");
  177. out.println("<!ATTLIST target");
  178. out.println(" id ID #IMPLIED");
  179. out.println(" name CDATA #REQUIRED");
  180. out.println(" if CDATA #IMPLIED");
  181. out.println(" unless CDATA #IMPLIED");
  182. out.println(" depends CDATA #IMPLIED");
  183. out.println(" description CDATA #IMPLIED>");
  184. out.println("");
  185. }
  186. private void printElementDecl(PrintWriter out, String name, Class element)
  187. throws BuildException {
  188. if (visited.containsKey(name)) {
  189. return;
  190. }
  191. visited.put(name, "");
  192. IntrospectionHelper ih = null;
  193. try {
  194. ih = IntrospectionHelper.getHelper(element);
  195. } catch (Throwable t) {
  196. /*
  197. * XXX - failed to load the class properly.
  198. *
  199. * should we print a warning here?
  200. */
  201. return;
  202. }
  203. StringBuffer sb = new StringBuffer("<!ELEMENT ");
  204. sb.append(name).append(" ");
  205. if (org.apache.tools.ant.types.Reference.class.equals(element)) {
  206. sb.append("EMPTY>").append(lSep);
  207. sb.append("<!ATTLIST ").append(name);
  208. sb.append(lSep).append(" id ID #IMPLIED");
  209. sb.append(lSep).append(" refid IDREF #IMPLIED");
  210. sb.append(">").append(lSep);
  211. out.println(sb);
  212. return;
  213. }
  214. Vector v = new Vector();
  215. if (ih.supportsCharacters()) {
  216. v.addElement("#PCDATA");
  217. }
  218. if (TaskContainer.class.isAssignableFrom(element)) {
  219. v.addElement(TASKS);
  220. }
  221. Enumeration enum = ih.getNestedElements();
  222. while (enum.hasMoreElements()) {
  223. v.addElement(enum.nextElement());
  224. }
  225. if (v.isEmpty()) {
  226. sb.append("EMPTY");
  227. } else {
  228. sb.append("(");
  229. final int count = v.size();
  230. for (int i = 0; i < count; i++) {
  231. if (i != 0) {
  232. sb.append(" | ");
  233. }
  234. sb.append(v.elementAt(i));
  235. }
  236. sb.append(")");
  237. if (count > 1 || !v.elementAt(0).equals("#PCDATA")) {
  238. sb.append("*");
  239. }
  240. }
  241. sb.append(">");
  242. out.println(sb);
  243. sb.setLength(0);
  244. sb.append("<!ATTLIST ").append(name);
  245. sb.append(lSep).append(" id ID #IMPLIED");
  246. enum = ih.getAttributes();
  247. while (enum.hasMoreElements()) {
  248. String attrName = (String) enum.nextElement();
  249. if ("id".equals(attrName)) {
  250. continue;
  251. }
  252. sb.append(lSep).append(" ").append(attrName).append(" ");
  253. Class type = ih.getAttributeType(attrName);
  254. if (type.equals(java.lang.Boolean.class) ||
  255. type.equals(java.lang.Boolean.TYPE)) {
  256. sb.append(BOOLEAN).append(" ");
  257. } else if (org.apache.tools.ant.types.Reference.class.isAssignableFrom(type)) {
  258. sb.append("IDREF ");
  259. } else if (org.apache.tools.ant.types.EnumeratedAttribute.class.isAssignableFrom(type)) {
  260. try {
  261. EnumeratedAttribute ea =
  262. (EnumeratedAttribute)type.newInstance();
  263. String[] values = ea.getValues();
  264. if (values == null
  265. || values.length == 0
  266. || !areNmtokens(values)) {
  267. sb.append("CDATA ");
  268. } else {
  269. sb.append("(");
  270. for (int i=0; i < values.length; i++) {
  271. if (i != 0) {
  272. sb.append(" | ");
  273. }
  274. sb.append(values[i]);
  275. }
  276. sb.append(") ");
  277. }
  278. } catch (InstantiationException ie) {
  279. sb.append("CDATA ");
  280. } catch (IllegalAccessException ie) {
  281. sb.append("CDATA ");
  282. }
  283. } else {
  284. sb.append("CDATA ");
  285. }
  286. sb.append("#IMPLIED");
  287. }
  288. sb.append(">").append(lSep);
  289. out.println(sb);
  290. final int count = v.size();
  291. for (int i = 0; i < count; i++) {
  292. String nestedName = (String) v.elementAt(i);
  293. if (!"#PCDATA".equals(nestedName) &&
  294. !TASKS.equals(nestedName) &&
  295. !TYPES.equals(nestedName)
  296. ) {
  297. printElementDecl(out, nestedName, ih.getElementType(nestedName));
  298. }
  299. }
  300. }
  301. private void printTail(PrintWriter out) {}
  302. /**
  303. * Does this String match the XML-NMTOKEN production?
  304. */
  305. protected boolean isNmtoken(String s) {
  306. final int length = s.length();
  307. for (int i = 0; i < length; i++) {
  308. char c = s.charAt(i);
  309. // XXX - we are ommitting CombiningChar and Extender here
  310. if (!Character.isLetterOrDigit(c) &&
  311. c != '.' && c != '-' &&
  312. c != '_' && c != ':') {
  313. return false;
  314. }
  315. }
  316. return true;
  317. }
  318. /**
  319. * Do the Strings all match the XML-NMTOKEN production?
  320. *
  321. * <p>Otherwise they are not suitable as an enumerated attribute,
  322. * for example.</p>
  323. */
  324. protected boolean areNmtokens(String[] s) {
  325. for (int i = 0; i < s.length; i++) {
  326. if (!isNmtoken(s[i])) {
  327. return false;
  328. }
  329. }
  330. return true;
  331. }
  332. }