Clover coverage report - Code Coverage for hivemind release 1.1
Coverage timestamp: Tue Oct 25 2005 10:47:07 EDT
file stats: LOC: 224   Methods: 12
NCLOC: 138   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
ConstructRegistry.java 86.4% 93.1% 100% 92.4%
coverage coverage
 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;
 18    import java.io.File;
 19    import java.io.FileOutputStream;
 20    import java.io.IOException;
 21    import java.io.OutputStream;
 22    import java.net.URL;
 23    import java.util.ArrayList;
 24    import java.util.List;
 25   
 26    import org.apache.hivemind.ModuleDescriptorProvider;
 27    import org.apache.hivemind.Resource;
 28    import org.apache.hivemind.impl.DefaultClassResolver;
 29    import org.apache.hivemind.impl.XmlModuleDescriptorProvider;
 30    import org.apache.hivemind.util.FileResource;
 31    import org.apache.hivemind.util.URLResource;
 32    import org.apache.tools.ant.BuildException;
 33    import org.apache.tools.ant.Task;
 34    import org.apache.tools.ant.types.Path;
 35    import org.apache.xml.serialize.OutputFormat;
 36    import org.apache.xml.serialize.XMLSerializer;
 37    import org.w3c.dom.Document;
 38   
 39    /**
 40    * Reads some number of hivemodule deployment descriptors (specified as a fileset) and builds a
 41    * composite registry by simply concatinating them all. The resulting file is suitable for passing
 42    * through an XSLT processor to create documentation.
 43    * <p>
 44    * The resulting XML file does not conform to the hivemind module deployment descriptor schema. The
 45    * following changes occur:
 46    * <ul>
 47    * <li>The outermost element is &lt;registry&gt; (which contains a list of &lt;module&gt;)
 48    * <li>A unique id (unique within the file) is assigned to each &lt;module&gt;,
 49    * &lt;configuration-point&gt;, &lt;service-point&gt;, &lt;contribution&gt;, &tl;schema&gt; and
 50    * &lt;implementation&gt; (this is to make it easier to generate links and anchors)
 51    * <li>Unqualified ids are converted to qualified ids (whereever possible).
 52    * </ul>
 53    *
 54    * @author Howard Lewis Ship
 55    */
 56    public class ConstructRegistry extends Task
 57    {
 58    private File _output;
 59   
 60    private Path _descriptorsPath;
 61   
 62    /**
 63    * List of {@link org.apache.hivemind.Resource} of additional descriptors to parse.
 64    */
 65    private List _resourceQueue = new ArrayList();
 66   
 67  7 public void execute() throws BuildException
 68    {
 69  7 if (_output == null)
 70  1 throw new BuildException("You must specify an output file");
 71   
 72  6 if (_descriptorsPath == null)
 73  1 throw new BuildException("You must specify a set of module descriptors");
 74   
 75  5 long outputStamp = _output.lastModified();
 76   
 77  5 String[] paths = _descriptorsPath.list();
 78  5 int count = paths.length;
 79   
 80  5 boolean needsUpdate = false;
 81   
 82  5 File[] descriptors = new File[count];
 83   
 84  5 for (int i = 0; i < count; i++)
 85    {
 86  10 File f = new File(paths[i]);
 87   
 88  10 if (f.isDirectory())
 89  0 continue;
 90   
 91  10 if (f.lastModified() > outputStamp)
 92  8 needsUpdate = true;
 93   
 94  10 descriptors[i] = f;
 95    }
 96   
 97  5 if (needsUpdate)
 98    {
 99  4 Document registry = constructRegistry(descriptors);
 100   
 101  4 log("Writing registry to " + _output);
 102   
 103  4 writeDocument(registry, _output);
 104    }
 105   
 106    }
 107   
 108  4 private Document constructRegistry(File[] moduleDescriptors) throws BuildException
 109    {
 110  4 try
 111    {
 112  4 enqueue(moduleDescriptors);
 113   
 114  4 ModuleDescriptorProvider provider = new XmlModuleDescriptorProvider(
 115    new DefaultClassResolver(), _resourceQueue);
 116   
 117  4 RegistrySerializer generator = new RegistrySerializer();
 118   
 119  4 generator.addModuleDescriptorProvider(provider);
 120   
 121  4 Document result = generator.createRegistryDocument();
 122   
 123  4 return result;
 124    }
 125    catch (Exception ex)
 126    {
 127  0 throw new BuildException(ex);
 128    }
 129    }
 130   
 131  4 private void enqueue(File[] descriptors) throws IOException
 132    {
 133  4 for (int i = 0; i < descriptors.length; i++)
 134  8 enqueue(descriptors[i]);
 135    }
 136   
 137    /**
 138    * Queues up a single descriptor which may be a raw XML file, or a JAR (containing the XML
 139    * file).
 140    */
 141  8 private void enqueue(File file) throws IOException
 142    {
 143    // This occurs when a bare directory is part of the classpath.
 144   
 145  8 if (file == null)
 146  0 return;
 147   
 148  8 if (file.getName().endsWith(".jar"))
 149    {
 150  2 enqueueJar(file);
 151  2 return;
 152    }
 153   
 154  6 String path = file.getPath().replace('\\', '/');
 155   
 156  6 Resource r = new FileResource(path);
 157   
 158  6 enqueue(r);
 159    }
 160   
 161  7 private void enqueue(Resource resource)
 162    {
 163  7 if (!_resourceQueue.contains(resource))
 164  7 _resourceQueue.add(resource);
 165    }
 166   
 167  2 private void enqueueJar(File jarFile) throws IOException
 168    {
 169  2 URL jarRootURL = new URL("jar:" + jarFile.toURL() + "!/");
 170   
 171  2 Resource jarResource = new URLResource(jarRootURL);
 172   
 173  2 enqueueIfExists(jarResource, XmlModuleDescriptorProvider.HIVE_MODULE_XML);
 174    }
 175   
 176  2 private void enqueueIfExists(Resource jarResource, String path)
 177    {
 178  2 Resource r = jarResource.getRelativeResource(path);
 179   
 180  2 if (r.getResourceURL() != null)
 181  1 enqueue(r);
 182    }
 183   
 184  4 private void writeDocument(Document document, File file) throws BuildException
 185    {
 186  4 try
 187    {
 188  4 OutputStream out = new FileOutputStream(file);
 189  4 BufferedOutputStream buffered = new BufferedOutputStream(out);
 190   
 191  4 writeDocument(document, buffered);
 192   
 193  4 buffered.close();
 194    }
 195    catch (IOException ex)
 196    {
 197  0 throw new BuildException(
 198    "Unable to write registry to " + file + ": " + ex.getMessage(), ex);
 199    }
 200    }
 201   
 202  4 private void writeDocument(Document document, OutputStream out) throws IOException
 203    {
 204  4 XMLSerializer serializer = new XMLSerializer(out, new OutputFormat(document, null, true));
 205  4 serializer.serialize(document);
 206    }
 207   
 208  4 public Path createDescriptors()
 209    {
 210  4 _descriptorsPath = new Path(getProject());
 211  4 return _descriptorsPath;
 212    }
 213   
 214  1 public File getOutput()
 215    {
 216  1 return _output;
 217    }
 218   
 219  5 public void setOutput(File file)
 220    {
 221  5 _output = file;
 222    }
 223   
 224    }