KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > util > BuilderList


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.util;
11
12 import java.io.*;
13 import java.util.*;
14
15 import javax.xml.parsers.DocumentBuilder JavaDoc;
16 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
17 import javax.xml.transform.stream.StreamResult JavaDoc;
18 import javax.xml.transform.OutputKeys JavaDoc;
19 import javax.xml.transform.Transformer JavaDoc;
20 import javax.xml.transform.TransformerException JavaDoc;
21 import javax.xml.transform.TransformerFactory JavaDoc;
22 import javax.xml.transform.dom.DOMSource JavaDoc;
23
24 import org.w3c.dom.Document JavaDoc;
25
26
27 /**
28  * Gives an xml-representation of a dir structure with builders
29  * Used by the build script to create documentation for builders.
30  * @since mmbase 1.6
31  * @author Gerard van Enk
32  * @author Pierre van Rooden
33  * @version $Id: BuilderList.java,v 1.8 2006/07/18 12:25:42 michiel Exp $
34  */

35 public class BuilderList {
36     // logger not used at the moment
37
//private static Logger log = Logging.getLoggerInstance(BuilderList.class.getName());
38

39    /**
40      * Generates the document and writes it to the result object.
41      * @param result the StreamResult object where to store the configuration'
42      */

43     public void write(Document JavaDoc doc, StreamResult JavaDoc result) throws IOException, TransformerException JavaDoc {
44         TransformerFactory JavaDoc tfactory = TransformerFactory.newInstance();
45         tfactory.setURIResolver(new org.mmbase.util.xml.URIResolver(new java.io.File JavaDoc("")));
46         // This creates a transformer that does a simple identity transform,
47
// and thus can be used for all intents and purposes as a serializer.
48
Transformer JavaDoc serializer = tfactory.newTransformer();
49         // sets indent amount for xalan
50
// should be done elsewhere, but where?
51
serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
52         // xml output configuration
53
serializer.setOutputProperty(OutputKeys.INDENT, "yes");
54         serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
55         serializer.transform(new DOMSource JavaDoc(doc), result);
56     }
57
58     /**
59      * Lists all builders within a given path, including builders in sub-paths
60      * @param ipath the path to start searching. The path need be closed with a File.seperator character.
61      */

62     void listBuilders(ResourceLoader config, Writer writer) throws IOException {
63         Set xmls = config.getResourcePaths(ResourceLoader.XML_PATTERN, false);
64         writer.write("<buildertype name=\"" + config.getContext() + "\">\n");
65         Iterator i = xmls.iterator();
66         while (i.hasNext()) {
67             String JavaDoc name = (String JavaDoc) i.next();
68             try {
69                 Document JavaDoc document = config.getDocument(name);
70                 //only process builder config files
71
if (document.getDocumentElement().getTagName().equals("builder")) {
72                     write(document, new StreamResult JavaDoc(writer));
73                 }
74             } catch (Exception JavaDoc e) {
75             }
76         }
77         writer.write("</buildertype>\n");
78         Iterator j = config.getChildContexts(null, false).iterator();
79         while (j.hasNext()) {
80             String JavaDoc sub = (String JavaDoc) j.next();
81             if ("CVS".equals(sub)) continue;
82             listBuilders(config.getChildResourceLoader(sub), writer);
83         }
84
85
86     }
87
88     /**
89      * Main method can be called from an Ant build file and will return
90      * the xml with a listing of all the builders
91      *
92      * @param args base dir to start with, it's possible to use more than one dir seperated by ;
93      */

94     public static void main(String JavaDoc[] args) throws UnsupportedEncodingException, IOException {
95         if (args.length != 0) {
96             BuilderList bulList = new BuilderList();
97             Writer s = new OutputStreamWriter(System.out, "UTF-8");
98             s.write("<builders>\n");
99             String JavaDoc[] builderDirs = args[0].split(";");
100             for (int i = 0; i < builderDirs.length ; i++) {
101                 ResourceLoader config = ResourceLoader.getConfigurationRoot().getChildResourceLoader(builderDirs[i]);
102                 bulList.listBuilders(config, s);
103             }
104             s.write("</builders>\n");
105             s.flush();
106         } else {
107             System.out.println("usage: java BuilderList <basedirwithbuilderconfig>");
108         }
109     }
110 }
111
Popular Tags