KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > ant > ConstructRegistry


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.hivemind.ant;
16
17 import java.io.BufferedOutputStream JavaDoc;
18 import java.io.File JavaDoc;
19 import java.io.FileOutputStream JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.OutputStream JavaDoc;
22 import java.net.URL JavaDoc;
23
24 import org.apache.hivemind.Resource;
25 import org.apache.hivemind.definition.RegistryDefinition;
26 import org.apache.hivemind.impl.RegistryBuilder;
27 import org.apache.hivemind.impl.XmlModuleReader;
28 import org.apache.hivemind.impl.HivemoduleProvider;
29 import org.apache.hivemind.util.FileResource;
30 import org.apache.hivemind.util.URLResource;
31 import org.apache.tools.ant.BuildException;
32 import org.apache.tools.ant.Task;
33 import org.apache.tools.ant.types.Path;
34 import org.apache.xml.serialize.OutputFormat;
35 import org.apache.xml.serialize.XMLSerializer;
36 import org.w3c.dom.Document JavaDoc;
37
38 /**
39  * Reads some number of hivemodule deployment descriptors (specified as a fileset) and builds a
40  * composite registry by simply concatinating them all. The resulting file is suitable for passing
41  * through an XSLT processor to create documentation.
42  * <p>
43  * The resulting XML file does not conform to the hivemind module deployment descriptor schema. The
44  * following changes occur:
45  * <ul>
46  * <li>The outermost element is &lt;registry&gt; (which contains a list of &lt;module&gt;)
47  * <li>A unique id (unique within the file) is assigned to each &lt;module&gt;,
48  * &lt;configuration-point&gt;, &lt;service-point&gt;, &lt;contribution&gt;, &tl;schema&gt; and
49  * &lt;implementation&gt; (this is to make it easier to generate links and anchors)
50  * <li>Unqualified ids are converted to qualified ids (whereever possible).
51  * </ul>
52  *
53  * Note: Construct registry is only partially functional in the moment.
54  * Reason: The generation now works on the definition api which doesn't provide
55  * data (for example Elements) which has been available in previous versions.
56  * Nevertheless its still xml specific (schemas) and to break this dependency one
57  * had to invent a plugin mechanism that allows the different formats (xml, annotations)
58  * to register specific renderers for the custom metadata.
59  *
60  * @author Howard Lewis Ship
61  */

62 public class ConstructRegistry extends Task
63 {
64     private File JavaDoc _output;
65
66     private Path _modulesPath;
67
68     public void execute() throws BuildException
69     {
70         if (_output == null)
71             throw new BuildException("You must specify an output file");
72
73         if (_modulesPath == null)
74             throw new BuildException("You must specify a set of modules");
75
76         RegistryDefinition registryDefinition = buildRegistry();
77         Document JavaDoc registry = constructRegistryDocument(registryDefinition);
78
79         log("Writing registry to " + _output);
80
81         writeDocument(registry, _output);
82
83     }
84
85     private RegistryDefinition buildRegistry()
86     {
87         String JavaDoc[] paths = _modulesPath.list();
88         int count = paths.length;
89         
90         File JavaDoc[] modules = new File JavaDoc[count];
91
92         for (int i = 0; i < count; i++)
93         {
94             File JavaDoc f = new File JavaDoc(paths[i]);
95
96             if (f.isDirectory())
97                 continue;
98
99             modules[i] = f;
100         }
101         RegistryBuilder builder = new RegistryBuilder();
102         buildRegistry(modules, builder.getRegistryDefinition());
103         return builder.getRegistryDefinition();
104     }
105     
106     private void buildRegistry(File JavaDoc[] modules, RegistryDefinition definition) throws BuildException
107     {
108         try
109         {
110             for (int i = 0; i < modules.length; i++)
111                 enqueue(modules[i], definition);
112         }
113         catch (Exception JavaDoc ex)
114         {
115             throw new BuildException(ex);
116         }
117     }
118
119     /**
120      * Queues up a single descriptor which may be a raw XML file, or a JAR (containing the XML
121      * file).
122      */

123     private void enqueue(File JavaDoc file, RegistryDefinition definition) throws IOException JavaDoc
124     {
125         // This occurs when a bare directory is part of the classpath.
126

127         if (file == null)
128             return;
129
130         if (file.getName().endsWith(".jar"))
131         {
132             enqueueJar(file, definition);
133             return;
134         }
135
136         String JavaDoc path = file.getPath().replace('\\', '/');
137
138         Resource r = new FileResource(path);
139
140         enqueue(r, definition);
141     }
142     
143     private void enqueueJar(File JavaDoc jarFile, RegistryDefinition definition) throws IOException JavaDoc
144     {
145         URL JavaDoc jarRootURL = new URL JavaDoc("jar:" + jarFile.toURL() + "!/" + HivemoduleProvider.HIVE_MODULE_XML);
146
147         Resource jarResource = new URLResource(jarRootURL);
148         enqueue(jarResource, definition);
149     }
150     
151     private void enqueue(Resource resource, RegistryDefinition definition)
152     {
153         if (resource.getResourceURL() != null) {
154             XmlModuleReader reader = new XmlModuleReader(definition);
155             reader.readModule(resource);
156         }
157     }
158
159     private Document JavaDoc constructRegistryDocument(RegistryDefinition registryDefinition) throws BuildException
160     {
161         try
162         {
163             RegistrySerializer generator = new RegistrySerializer();
164
165             Document JavaDoc result = generator.createRegistryDocument(registryDefinition);
166
167             return result;
168         }
169         catch (Exception JavaDoc ex)
170         {
171             throw new BuildException(ex);
172         }
173     }
174
175     private void writeDocument(Document JavaDoc document, File JavaDoc file) throws BuildException
176     {
177         try
178         {
179             OutputStream JavaDoc out = new FileOutputStream JavaDoc(file);
180             BufferedOutputStream JavaDoc buffered = new BufferedOutputStream JavaDoc(out);
181
182             writeDocument(document, buffered);
183
184             buffered.close();
185         }
186         catch (IOException JavaDoc ex)
187         {
188             throw new BuildException(
189                     "Unable to write registry to " + file + ": " + ex.getMessage(), ex);
190         }
191     }
192
193     private void writeDocument(Document JavaDoc document, OutputStream JavaDoc out) throws IOException JavaDoc
194     {
195         XMLSerializer serializer = new XMLSerializer(out, new OutputFormat(document, null, true));
196         serializer.serialize(document);
197     }
198
199     public File JavaDoc getOutput()
200     {
201         return _output;
202     }
203
204     public void setOutput(File JavaDoc file)
205     {
206         _output = file;
207     }
208
209     public Path createModules()
210     {
211         _modulesPath = new Path(getProject());
212         return _modulesPath;
213     }
214
215 }
Popular Tags