001: /*
002:
003: This software is OSI Certified Open Source Software.
004: OSI Certified is a certification mark of the Open Source Initiative.
005:
006: The license (Mozilla version 1.0) can be read at the MMBase site.
007: See http://www.MMBase.org/license
008:
009: */
010: package org.mmbase.util;
011:
012: import java.io.*;
013: import java.util.*;
014:
015: import javax.xml.transform.stream.StreamResult;
016: import javax.xml.transform.OutputKeys;
017: import javax.xml.transform.Transformer;
018: import javax.xml.transform.TransformerException;
019: import javax.xml.transform.TransformerFactory;
020: import javax.xml.transform.dom.DOMSource;
021:
022: import org.w3c.dom.Document;
023:
024: /**
025: * Gives an xml-representation of a dir structure with builders
026: * Used by the build script to create documentation for builders.
027: * @since mmbase 1.6
028: * @author Gerard van Enk
029: * @author Pierre van Rooden
030: * @version $Id: BuilderList.java,v 1.11 2007/02/24 21:57:50 nklasens Exp $
031: */
032: public class BuilderList {
033: // logger not used at the moment
034: //private static Logger log = Logging.getLoggerInstance(BuilderList.class.getName());
035:
036: /**
037: * Generates the document and writes it to the result object.
038: * @param result the StreamResult object where to store the configuration'
039: */
040: public void write(Document doc, StreamResult result)
041: throws TransformerException {
042: TransformerFactory tfactory = TransformerFactory.newInstance();
043: tfactory.setURIResolver(new org.mmbase.util.xml.URIResolver(
044: new java.io.File("")));
045: // This creates a transformer that does a simple identity transform,
046: // and thus can be used for all intents and purposes as a serializer.
047: Transformer serializer = tfactory.newTransformer();
048: // sets indent amount for xalan
049: // should be done elsewhere, but where?
050: serializer.setOutputProperty(
051: "{http://xml.apache.org/xslt}indent-amount", "2");
052: // xml output configuration
053: serializer.setOutputProperty(OutputKeys.INDENT, "yes");
054: serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION,
055: "yes");
056: serializer.transform(new DOMSource(doc), result);
057: }
058:
059: /**
060: * Lists all builders within a given path, including builders in sub-paths
061: * @param ipath the path to start searching. The path need be closed with a File.seperator character.
062: */
063: void listBuilders(ResourceLoader config, Writer writer)
064: throws IOException {
065: Set<String> xmls = config.getResourcePaths(
066: ResourceLoader.XML_PATTERN, false);
067: writer.write("<buildertype name=\"" + config.getContext()
068: + "\">\n");
069: Iterator<String> i = xmls.iterator();
070: while (i.hasNext()) {
071: String name = i.next();
072: try {
073: Document document = config.getDocument(name);
074: //only process builder config files
075: if (document.getDocumentElement().getTagName().equals(
076: "builder")) {
077: write(document, new StreamResult(writer));
078: }
079: } catch (Exception e) {
080: }
081: }
082: writer.write("</buildertype>\n");
083: Iterator<String> j = config.getChildContexts(null, false)
084: .iterator();
085: while (j.hasNext()) {
086: String sub = j.next();
087: if ("CVS".equals(sub))
088: continue;
089: listBuilders(config.getChildResourceLoader(sub), writer);
090: }
091:
092: }
093:
094: /**
095: * Main method can be called from an Ant build file and will return
096: * the xml with a listing of all the builders
097: *
098: * @param args base dir to start with, it's possible to use more than one dir seperated by ;
099: */
100: public static void main(String[] args)
101: throws UnsupportedEncodingException, IOException {
102: if (args.length != 0) {
103: BuilderList bulList = new BuilderList();
104: Writer s = new OutputStreamWriter(System.out, "UTF-8");
105: s.write("<builders>\n");
106: String[] builderDirs = args[0].split(";");
107: for (String element : builderDirs) {
108: ResourceLoader config = ResourceLoader
109: .getConfigurationRoot().getChildResourceLoader(
110: element);
111: bulList.listBuilders(config, s);
112: }
113: s.write("</builders>\n");
114: s.flush();
115: } else {
116: System.out
117: .println("usage: java BuilderList <basedirwithbuilderconfig>");
118: }
119: }
120: }
|