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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. printHead(out);
  97. Vector tasks = new Vector();
  98. Enumeration enum = project.getTaskDefinitions().keys();
  99. while (enum.hasMoreElements()) {
  100. String taskName = (String) enum.nextElement();
  101. tasks.addElement(taskName);
  102. }
  103. printTargetDecl(out, tasks);
  104. for (int i=0; i<tasks.size(); i++) {
  105. String taskName = (String) tasks.elementAt(i);
  106. printElementDecl(out, taskName,
  107. (Class) project.getTaskDefinitions().get(taskName));
  108. }
  109. printTail(out);
  110. } catch (IOException ioe) {
  111. throw new BuildException("Error writing "+output.getAbsolutePath(),
  112. ioe, location);
  113. } finally {
  114. if (out != null) {
  115. out.close();
  116. }
  117. }
  118. }
  119. private void printHead(PrintWriter out) {
  120. out.println("<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>");
  121. out.println("<!ENTITY % boolean \"(true|false|on|off|yes|no)\">");
  122. out.println("");
  123. out.println("<!ELEMENT project (target | property | taskdef)*>");
  124. out.println("<!ATTLIST project");
  125. out.println(" name CDATA #REQUIRED");
  126. out.println(" default CDATA #REQUIRED");
  127. out.println(" basedir CDATA #IMPLIED>");
  128. out.println("");
  129. }
  130. private void printTargetDecl(PrintWriter out, Vector tasks) {
  131. out.print("<!ELEMENT target (");
  132. for (int i=0; i<tasks.size(); i++) {
  133. String taskName = (String) tasks.elementAt(i);
  134. if (i > 0) {
  135. out.print(" | ");
  136. }
  137. out.print(taskName);
  138. }
  139. out.println(")*>");
  140. out.println("");
  141. out.println("<!ATTLIST target");
  142. out.println(" id ID #IMPLIED");
  143. out.println(" name CDATA #REQUIRED");
  144. out.println(" if CDATA #IMPLIED");
  145. out.println(" unless CDATA #IMPLIED");
  146. out.println(" depends CDATA #IMPLIED>");
  147. out.println("");
  148. }
  149. private void printElementDecl(PrintWriter out, String name, Class element)
  150. throws BuildException {
  151. if (visited.containsKey(name)) {
  152. return;
  153. }
  154. visited.put(name, "");
  155. IntrospectionHelper ih = IntrospectionHelper.getHelper(element);
  156. StringBuffer sb = new StringBuffer("<!ELEMENT ");
  157. sb.append(name).append(" ");
  158. if (org.apache.tools.ant.types.Reference.class.equals(element)) {
  159. sb.append("EMPTY>").append(lSep);
  160. sb.append("<!ATTLIST ").append(name);
  161. sb.append(lSep).append(" id ID #IMPLIED");
  162. sb.append(lSep).append(" refid IDREF #IMPLIED");
  163. sb.append(">").append(lSep);
  164. out.println(sb);
  165. return;
  166. }
  167. Vector v = new Vector();
  168. if (ih.supportsCharacters()) {
  169. v.addElement("#PCDATA");
  170. }
  171. Enumeration enum = ih.getNestedElements();
  172. while (enum.hasMoreElements()) {
  173. v.addElement((String) enum.nextElement());
  174. }
  175. if (v.isEmpty()) {
  176. sb.append("EMPTY");
  177. } else {
  178. sb.append("(");
  179. for (int i=0; i<v.size(); i++) {
  180. if (i != 0) {
  181. sb.append(" | ");
  182. }
  183. sb.append(v.elementAt(i));
  184. }
  185. sb.append(")");
  186. if (v.size() > 1 || !v.elementAt(0).equals("#PCDATA")) {
  187. sb.append("*");
  188. }
  189. }
  190. sb.append(">");
  191. out.println(sb);
  192. sb.setLength(0);
  193. sb.append("<!ATTLIST ").append(name);
  194. sb.append(lSep).append(" id ID #IMPLIED");
  195. enum = ih.getAttributes();
  196. while (enum.hasMoreElements()) {
  197. String attrName = (String) enum.nextElement();
  198. sb.append(lSep).append(" ").append(attrName).append(" ");
  199. Class type = ih.getAttributeType(attrName);
  200. if (type.equals(java.lang.Boolean.class) ||
  201. type.equals(java.lang.Boolean.TYPE)) {
  202. sb.append("%boolean; ");
  203. } else if (org.apache.tools.ant.types.Reference.class.isAssignableFrom(type)) {
  204. sb.append("IDREF ");
  205. } else if (org.apache.tools.ant.types.EnumeratedAttribute.class.isAssignableFrom(type)) {
  206. try {
  207. EnumeratedAttribute ea =
  208. (EnumeratedAttribute)type.newInstance();
  209. String[] values = ea.getValues();
  210. if (values == null || values.length == 0) {
  211. sb.append("CDATA ");
  212. } else {
  213. sb.append("(");
  214. for (int i=0; i < values.length; i++) {
  215. if (i != 0) {
  216. sb.append(" | ");
  217. }
  218. sb.append(values[i]);
  219. }
  220. sb.append(") ");
  221. }
  222. } catch (InstantiationException ie) {
  223. sb.append("CDATA ");
  224. } catch (IllegalAccessException ie) {
  225. sb.append("CDATA ");
  226. }
  227. } else {
  228. sb.append("CDATA ");
  229. }
  230. sb.append("#IMPLIED");
  231. }
  232. sb.append(">").append(lSep);
  233. out.println(sb);
  234. for (int i=0; i<v.size(); i++) {
  235. String nestedName = (String) v.elementAt(i);
  236. if (!"#PCDATA".equals(nestedName)) {
  237. printElementDecl(out, nestedName, ih.getElementType(nestedName));
  238. }
  239. }
  240. }
  241. private void printTail(PrintWriter out) {}
  242. }