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

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