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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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", "Tomcat", 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.types.EnumeratedAttribute;
  60. import java.util.Enumeration;
  61. import java.util.Hashtable;
  62. import java.util.Vector;
  63. import java.io.*;
  64. /**
  65. * Creates a partial DTD for Ant from the currently known tasks.
  66. *
  67. * @author <a href="mailto:stefan.bodewig@megabit.net">Stefan Bodewig</a>
  68. */
  69. public class AntStructure extends Task {
  70. private final String lSep = System.getProperty("line.separator");
  71. private Hashtable visited = new Hashtable();
  72. private File output;
  73. /**
  74. * The output file.
  75. */
  76. public void setOutput(File output) {
  77. this.output = output;
  78. }
  79. public void execute() throws BuildException {
  80. if (output == null) {
  81. throw new BuildException("output attribute is required", location);
  82. }
  83. PrintWriter out = null;
  84. try {
  85. try {
  86. out = new PrintWriter(new OutputStreamWriter(new FileOutputStream(output), "ISO8859_1"));
  87. } catch (UnsupportedEncodingException ue) {
  88. /*
  89. * Plain impossible with ISO8859_1, see
  90. * http://java.sun.com/products/jdk/1.2/docs/guide/internat/encoding.doc.html
  91. *
  92. * fallback to platform specific anyway.
  93. */
  94. out = new PrintWriter(new FileWriter(output));
  95. }
  96. Enumeration dataTypes = project.getDataTypeDefinitions().keys();
  97. printHead(out, dataTypes);
  98. Vector tasks = new Vector();
  99. Enumeration enum = project.getTaskDefinitions().keys();
  100. while (enum.hasMoreElements()) {
  101. String taskName = (String) enum.nextElement();
  102. tasks.addElement(taskName);
  103. }
  104. printTargetDecl(out, tasks);
  105. 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. for (int i=0; i<tasks.size(); i++) {
  112. String taskName = (String) tasks.elementAt(i);
  113. printElementDecl(out, taskName,
  114. (Class) project.getTaskDefinitions().get(taskName));
  115. }
  116. printTail(out);
  117. } catch (IOException ioe) {
  118. throw new BuildException("Error writing "+output.getAbsolutePath(),
  119. ioe, location);
  120. } finally {
  121. if (out != null) {
  122. out.close();
  123. }
  124. }
  125. }
  126. private void printHead(PrintWriter out, Enumeration enum) {
  127. out.println("<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>");
  128. out.println("<!ENTITY % boolean \"(true|false|on|off|yes|no)\">");
  129. out.println("");
  130. out.print("<!ELEMENT project (target | property | taskdef");
  131. while (enum.hasMoreElements()) {
  132. String typeName = (String) enum.nextElement();
  133. out.print(" | "+typeName);
  134. }
  135. out.println(")*>");
  136. out.println("<!ATTLIST project");
  137. out.println(" name CDATA #REQUIRED");
  138. out.println(" default CDATA #REQUIRED");
  139. out.println(" basedir CDATA #IMPLIED>");
  140. out.println("");
  141. }
  142. private void printTargetDecl(PrintWriter out, Vector tasks) {
  143. out.print("<!ELEMENT target (");
  144. for (int i=0; i<tasks.size(); i++) {
  145. String taskName = (String) tasks.elementAt(i);
  146. if (i > 0) {
  147. out.print(" | ");
  148. }
  149. out.print(taskName);
  150. }
  151. out.println(")*>");
  152. out.println("");
  153. out.println("<!ATTLIST target");
  154. out.println(" id ID #IMPLIED");
  155. out.println(" name CDATA #REQUIRED");
  156. out.println(" if CDATA #IMPLIED");
  157. out.println(" unless CDATA #IMPLIED");
  158. out.println(" depends CDATA #IMPLIED>");
  159. out.println("");
  160. }
  161. private void printElementDecl(PrintWriter out, String name, Class element)
  162. throws BuildException {
  163. if (visited.containsKey(name)) {
  164. return;
  165. }
  166. visited.put(name, "");
  167. IntrospectionHelper ih = IntrospectionHelper.getHelper(element);
  168. StringBuffer sb = new StringBuffer("<!ELEMENT ");
  169. sb.append(name).append(" ");
  170. if (org.apache.tools.ant.types.Reference.class.equals(element)) {
  171. sb.append("EMPTY>").append(lSep);
  172. sb.append("<!ATTLIST ").append(name);
  173. sb.append(lSep).append(" id ID #IMPLIED");
  174. sb.append(lSep).append(" refid IDREF #IMPLIED");
  175. sb.append(">").append(lSep);
  176. out.println(sb);
  177. return;
  178. }
  179. Vector v = new Vector();
  180. if (ih.supportsCharacters()) {
  181. v.addElement("#PCDATA");
  182. }
  183. Enumeration enum = ih.getNestedElements();
  184. while (enum.hasMoreElements()) {
  185. v.addElement((String) enum.nextElement());
  186. }
  187. if (v.isEmpty()) {
  188. sb.append("EMPTY");
  189. } else {
  190. sb.append("(");
  191. for (int i=0; i<v.size(); i++) {
  192. if (i != 0) {
  193. sb.append(" | ");
  194. }
  195. sb.append(v.elementAt(i));
  196. }
  197. sb.append(")");
  198. if (v.size() > 1 || !v.elementAt(0).equals("#PCDATA")) {
  199. sb.append("*");
  200. }
  201. }
  202. sb.append(">");
  203. out.println(sb);
  204. sb.setLength(0);
  205. sb.append("<!ATTLIST ").append(name);
  206. sb.append(lSep).append(" id ID #IMPLIED");
  207. enum = ih.getAttributes();
  208. while (enum.hasMoreElements()) {
  209. String attrName = (String) enum.nextElement();
  210. sb.append(lSep).append(" ").append(attrName).append(" ");
  211. Class type = ih.getAttributeType(attrName);
  212. if (type.equals(java.lang.Boolean.class) ||
  213. type.equals(java.lang.Boolean.TYPE)) {
  214. sb.append("%boolean; ");
  215. } else if (org.apache.tools.ant.types.Reference.class.isAssignableFrom(type)) {
  216. sb.append("IDREF ");
  217. } else if (org.apache.tools.ant.types.EnumeratedAttribute.class.isAssignableFrom(type)) {
  218. try {
  219. EnumeratedAttribute ea =
  220. (EnumeratedAttribute)type.newInstance();
  221. String[] values = ea.getValues();
  222. if (values == null || values.length == 0) {
  223. sb.append("CDATA ");
  224. } else {
  225. sb.append("(");
  226. for (int i=0; i < values.length; i++) {
  227. if (i != 0) {
  228. sb.append(" | ");
  229. }
  230. sb.append(values[i]);
  231. }
  232. sb.append(") ");
  233. }
  234. } catch (InstantiationException ie) {
  235. sb.append("CDATA ");
  236. } catch (IllegalAccessException ie) {
  237. sb.append("CDATA ");
  238. }
  239. } else {
  240. sb.append("CDATA ");
  241. }
  242. sb.append("#IMPLIED");
  243. }
  244. sb.append(">").append(lSep);
  245. out.println(sb);
  246. for (int i=0; i<v.size(); i++) {
  247. String nestedName = (String) v.elementAt(i);
  248. if (!"#PCDATA".equals(nestedName)) {
  249. printElementDecl(out, nestedName, ih.getElementType(nestedName));
  250. }
  251. }
  252. }
  253. private void printTail(PrintWriter out) {}
  254. }