You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CompileXMLFiles.java 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 1999 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. *
  55. */
  56. /** This class is an extension of Ant, a script utility from
  57. jakarta.apache.org.
  58. It takes a couple of xml files conforming to the xml-site dtd and
  59. writes them all into one xml file, deleting any reference to
  60. the proprietary protocol sbk. The configFile determines what files
  61. are read in what sequence.
  62. */
  63. // Ant
  64. import org.apache.tools.ant.Task;
  65. import org.apache.tools.ant.BuildException;
  66. // SAX
  67. import org.xml.sax.Parser;
  68. import org.xml.sax.InputSource;
  69. import org.xml.sax.SAXException;
  70. import org.xml.sax.SAXParseException;
  71. import org.xml.sax.Locator;
  72. import org.xml.sax.AttributeList;
  73. // Java
  74. import java.io.*;
  75. import java.util.*;
  76. import java.net.URL;
  77. public class CompileXMLFiles extends Task
  78. implements org.xml.sax.EntityResolver,
  79. org.xml.sax.DTDHandler,
  80. org.xml.sax.DocumentHandler,
  81. org.xml.sax.ErrorHandler {
  82. private String configFile, outFile;
  83. private String [] filenameList;
  84. private String filenames;
  85. private Vector files = new Vector();
  86. //sets name of configuration file, which must
  87. //be an xml file conforming to the book.dtd used by xml-site
  88. public void setConfigFile(String configFile) {
  89. this.configFile = configFile;
  90. }
  91. public void setOutFile(String outFile) {
  92. this.outFile = outFile;
  93. }
  94. //main method of this task
  95. public void execute () throws BuildException {
  96. boolean errors = false;
  97. if (!(new File(configFile).exists())) {
  98. errors = true;
  99. System.err.println("Task CompileXMLFiles - ERROR: config file " + configFile + " is missing.");
  100. }
  101. Parser parser = createParser();
  102. if (parser == null) {
  103. System.err.println("Task CompileXMLFiles - ERROR: Unable to create SAX parser");
  104. errors = true;
  105. }
  106. parser.setDocumentHandler(this);
  107. try {
  108. parser.parse(CompileXMLFiles.fileInputSource(configFile));
  109. } catch (SAXException e) {
  110. System.out.println(e);
  111. } catch (IOException ioe) {
  112. System.out.println(ioe);
  113. }
  114. } //end: execute()
  115. /*the following methods belong to the sax parser and implement the Document Handler*/
  116. public InputSource resolveEntity (String publicId, String systemId)
  117. throws SAXException
  118. {
  119. return null;
  120. }
  121. public void notationDecl (String name, String publicId, String systemId)
  122. {
  123. // no op
  124. }
  125. public void unparsedEntityDecl (String name, String publicId,
  126. String systemId, String notationName)
  127. {
  128. // no op
  129. }
  130. public void setDocumentLocator (Locator locator)
  131. {
  132. // no op
  133. }
  134. public void startDocument ()
  135. throws SAXException
  136. {
  137. // no op
  138. }
  139. /* After the cnfiguration file has been parsed all files which
  140. have been collected in the ArrayList files are concatinated
  141. and written to a new (temporary) file */
  142. public void endDocument ()
  143. throws SAXException
  144. {
  145. String line, filename;
  146. BufferedReader in;
  147. Enumeration iterator = files.elements();
  148. try {
  149. BufferedWriter out =
  150. new BufferedWriter (new FileWriter("compileXMLFiles-tmp.xml"));
  151. out.write("<?xml version=\"1.0\"?>\n" +
  152. "<!DOCTYPE documentation [\n" +
  153. "<!ENTITY nbsp \" \">\n" +
  154. "]>\n<documentation>");
  155. while (iterator.hasMoreElements()) {
  156. filename = (String) iterator.nextElement();
  157. in = new BufferedReader(new FileReader(filename));
  158. while ((line = in.readLine()) != null) {
  159. //kill the lines pointing to the sbk protocol and the xml declaration
  160. if (line.indexOf("<!DOCTYPE ")!= -1 || line.indexOf("<?xml ")!= -1 ) {
  161. line = "";
  162. }
  163. out.write(line+"\n");
  164. }
  165. out.flush();
  166. }
  167. out.write("\n</documentation>");
  168. out.close();
  169. } catch (Exception e) {
  170. System.out.println(e);
  171. }
  172. }
  173. public void startElement (String name, AttributeList atts)
  174. throws SAXException {
  175. String id, label, source;
  176. if (name.equals("document") || name.equals("entry")) {
  177. source = atts.getValue("source");
  178. files.addElement(source);
  179. }
  180. }
  181. public void endElement (String name)
  182. throws SAXException
  183. {
  184. // no op
  185. }
  186. public void characters (char ch[], int start, int length)
  187. throws SAXException
  188. {
  189. // no op
  190. }
  191. public void ignorableWhitespace (char ch[], int start, int length)
  192. throws SAXException
  193. {
  194. // no op
  195. }
  196. public void processingInstruction (String target, String data)
  197. throws SAXException
  198. {
  199. // no op
  200. }
  201. public void warning (SAXParseException e)
  202. throws SAXException
  203. {
  204. // no op
  205. }
  206. public void error (SAXParseException e)
  207. throws SAXException
  208. {
  209. // no op
  210. }
  211. public void fatalError (SAXParseException e)
  212. throws SAXException
  213. {
  214. throw e;
  215. }
  216. /* ------------------------*/
  217. /**
  218. * creates a SAX parser, using the value of org.xml.sax.parser
  219. * defaulting to org.apache.xerces.parsers.SAXParser
  220. *
  221. * @return the created SAX parser
  222. */
  223. static Parser createParser() {
  224. String parserClassName = System.getProperty("org.xml.sax.parser");
  225. if (parserClassName == null) {
  226. parserClassName = "org.apache.xerces.parsers.SAXParser";
  227. }
  228. System.err.println("using SAX parser " + parserClassName);
  229. try {
  230. return (Parser) Class.forName(parserClassName).newInstance();
  231. } catch (ClassNotFoundException e) {
  232. System.err.println("Could not find " + parserClassName);
  233. } catch (InstantiationException e) {
  234. System.err.println("Could not instantiate " + parserClassName);
  235. } catch (IllegalAccessException e) {
  236. System.err.println("Could not access " + parserClassName);
  237. } catch (ClassCastException e) {
  238. System.err.println(parserClassName + " is not a SAX driver");
  239. }
  240. return null;
  241. }
  242. /**
  243. * create an InputSource from a file name
  244. *
  245. * @param filename the name of the file
  246. * @return the InputSource created
  247. */
  248. protected static InputSource fileInputSource(String filename) {
  249. /* this code adapted from James Clark's in XT */
  250. File file = new File(filename);
  251. String path = file.getAbsolutePath();
  252. String fSep = System.getProperty("file.separator");
  253. if (fSep != null && fSep.length() == 1)
  254. path = path.replace(fSep.charAt(0), '/');
  255. if (path.length() > 0 && path.charAt(0) != '/')
  256. path = '/' + path;
  257. try {
  258. return new InputSource(new URL("file", null, path).toString());
  259. } catch (java.net.MalformedURLException e) {
  260. throw new Error("unexpected MalformedURLException");
  261. }
  262. }
  263. }