Clover coverage report - Code Coverage for hivemind release 1.0-beta-1
Coverage timestamp: Sat Jul 3 2004 09:41:37 EDT
file stats: LOC: 402   Methods: 20
NCLOC: 263   Classes: 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
 
 Source file Conditionals Statements Methods TOTAL
ConstructRegistry.java 94% 96.7% 100% 96.3%
coverage coverage
 1   
 //  Copyright 2004 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.BufferedInputStream;
 18   
 import java.io.BufferedOutputStream;
 19   
 import java.io.File;
 20   
 import java.io.FileOutputStream;
 21   
 import java.io.IOException;
 22   
 import java.io.InputStream;
 23   
 import java.io.OutputStream;
 24   
 import java.net.URL;
 25   
 import java.util.ArrayList;
 26   
 import java.util.List;
 27   
 
 28   
 import javax.xml.parsers.DocumentBuilder;
 29   
 import javax.xml.parsers.DocumentBuilderFactory;
 30   
 import javax.xml.parsers.ParserConfigurationException;
 31   
 
 32   
 import org.apache.hivemind.Resource;
 33   
 import org.apache.hivemind.impl.RegistryBuilder;
 34   
 import org.apache.hivemind.sdl.SDLDocumentAdaptor;
 35   
 import org.apache.hivemind.util.FileResource;
 36   
 import org.apache.hivemind.util.URLResource;
 37   
 import org.apache.tools.ant.BuildException;
 38   
 import org.apache.tools.ant.Task;
 39   
 import org.apache.tools.ant.types.Path;
 40   
 import org.apache.xml.serialize.OutputFormat;
 41   
 import org.apache.xml.serialize.XMLSerializer;
 42   
 import org.w3c.dom.Document;
 43   
 import org.w3c.dom.Element;
 44   
 import org.w3c.dom.NamedNodeMap;
 45   
 import org.w3c.dom.Node;
 46   
 import org.xml.sax.InputSource;
 47   
 import org.xml.sax.SAXException;
 48   
 import org.xml.sax.SAXParseException;
 49   
 
 50   
 /**
 51   
  * Reads some number of hivemodule deployment descriptors (specified as a fileset)
 52   
  * and builds a composite registry by simply concatinating them all. The resulting
 53   
  * file is suitable for passing through an XSLT processor to create documentation.
 54   
  * 
 55   
  * <p>
 56   
  * The resulting XML file does not conform to the hivemind module deployment
 57   
  * descriptor schema.  The following changes occur:
 58   
  * <ul>
 59   
  * <li>The outermost element is &lt;registry&gt; (which contains a list of &lt;module&gt;)
 60   
  * <li>A unique id (unique within the file) is assigned to each &lt;module&gt;, 
 61   
  * &lt;configuration-point&gt;, 
 62   
  * &lt;service-point&gt;, &lt;contribution&gt;, &tl;schema&gt; and &lt;implementation&gt; (this is
 63   
  * to make it easier to generate links and anchors)
 64   
  * <li>Unqualified ids are converted to qualified ids (whereever possible).
 65   
  * </ul>
 66   
  *
 67   
  * 
 68   
  * @author Howard Lewis Ship
 69   
  */
 70   
 public class ConstructRegistry extends Task
 71   
 {
 72   
     private int _uid = 1;
 73   
 
 74   
     private File _output;
 75   
     private Path _descriptorsPath;
 76   
 
 77   
     /**
 78   
      * List of {@link org.apache.hivemind.Resource} of additional
 79   
      * descriptors to parse.
 80   
      */
 81   
     private List _resourceQueue = new ArrayList();
 82   
 
 83  8
     public void execute() throws BuildException
 84   
     {
 85  8
         if (_output == null)
 86  1
             throw new BuildException("You must specify an output file");
 87   
 
 88  7
         if (_descriptorsPath == null)
 89  1
             throw new BuildException("You must specify a set of module descriptors");
 90   
 
 91  6
         long outputStamp = _output.lastModified();
 92   
 
 93  6
         String[] paths = _descriptorsPath.list();
 94  6
         int count = paths.length;
 95   
 
 96  6
         boolean needsUpdate = false;
 97   
 
 98  6
         File[] descriptors = new File[count];
 99   
 
 100  6
         for (int i = 0; i < count; i++)
 101   
         {
 102  12
             File f = new File(paths[i]);
 103   
 
 104  12
             if (f.lastModified() > outputStamp)
 105  10
                 needsUpdate = true;
 106   
 
 107  12
             descriptors[i] = f;
 108   
         }
 109   
 
 110  6
         if (needsUpdate)
 111   
         {
 112  5
             Document registry = constructRegistry(descriptors);
 113   
 
 114  5
             log("Writing registry to " + _output);
 115   
 
 116  5
             writeDocument(registry, _output);
 117   
         }
 118   
 
 119   
     }
 120   
 
 121  5
     private DocumentBuilder getBuilder() throws ParserConfigurationException
 122   
     {
 123  5
         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
 124   
 
 125  5
         factory.setIgnoringComments(true);
 126   
 
 127  5
         return factory.newDocumentBuilder();
 128   
     }
 129   
 
 130  5
     private Document constructRegistry(File[] moduleDescriptors) throws BuildException
 131   
     {
 132  5
         try
 133   
         {
 134  5
             DocumentBuilder builder = getBuilder();
 135   
 
 136  5
             Document result = builder.newDocument();
 137   
 
 138  5
             Element registry = result.createElement("registry");
 139   
 
 140  5
             result.appendChild(registry);
 141   
 
 142  5
             enqueue(moduleDescriptors);
 143   
 
 144  5
             while (!_resourceQueue.isEmpty())
 145   
             {
 146  9
                 Resource r = (Resource) _resourceQueue.remove(0);
 147   
 
 148  9
                 processResource(r, builder, registry);
 149   
             }
 150   
 
 151  5
             return result;
 152   
         }
 153   
         catch (Exception ex)
 154   
         {
 155  0
             throw new BuildException(ex);
 156   
         }
 157   
     }
 158   
 
 159  5
     private void enqueue(File[] descriptors) throws IOException
 160   
     {
 161  5
         for (int i = 0; i < descriptors.length; i++)
 162  10
             enqueue(descriptors[i]);
 163   
     }
 164   
 
 165   
     /**
 166   
      * Queues up a single descriptor which may be a raw XML or SDL
 167   
      * file, or a JAR (containing XML or SDL files).
 168   
      */
 169  10
     private void enqueue(File file) throws IOException
 170   
     {
 171  10
         if (file.getName().endsWith(".jar"))
 172   
         {
 173  2
             enqueueJar(file);
 174  2
             return;
 175   
         }
 176   
 
 177  8
         Resource r = new FileResource(file.getPath());
 178   
 
 179  8
         enqueue(r);
 180   
     }
 181   
 
 182  9
     private void enqueue(Resource resource)
 183   
     {
 184  9
         _resourceQueue.add(resource);
 185   
     }
 186   
 
 187  2
     private void enqueueJar(File jarFile) throws IOException
 188   
     {
 189  2
         URL jarRootURL = new URL("jar:" + jarFile.toURL() + "!/");
 190   
 
 191  2
         Resource jarResource = new URLResource(jarRootURL);
 192   
 
 193  2
         enqueueIfExists(jarResource, RegistryBuilder.HIVE_MODULE_SDL);
 194  2
         enqueueIfExists(jarResource, RegistryBuilder.HIVE_MODULE_XML);
 195   
     }
 196   
 
 197  4
     private void enqueueIfExists(Resource jarResource, String path)
 198   
     {
 199  4
         Resource r = jarResource.getRelativeResource(path);
 200   
 
 201  4
         if (r.getResourceURL() != null)
 202  1
             enqueue(r);
 203   
     }
 204  9
     private void processResource(
 205   
         Resource descriptor,
 206   
         DocumentBuilder builder,
 207   
         Element registryElement)
 208   
         throws SAXException, IOException
 209   
     {
 210  9
         log("Reading " + descriptor);
 211   
 
 212  9
         Document module = parse(builder, descriptor);
 213   
 
 214  9
         Element e = module.getDocumentElement();
 215   
 
 216  9
         prepareModuleForInclusion(e);
 217   
 
 218  9
         Document d = (Document) registryElement.getParentNode();
 219   
 
 220  9
         Node eCopy = d.importNode(e, true);
 221   
 
 222  9
         registryElement.appendChild(eCopy);
 223   
 
 224   
     }
 225   
 
 226  9
     private Document parse(DocumentBuilder builder, Resource descriptor)
 227   
         throws SAXException, IOException
 228   
     {
 229  9
         String path = descriptor.getPath();
 230   
 
 231  9
         if (path.endsWith(".sdl"))
 232  7
             return parseSDL(builder, descriptor);
 233   
 
 234  2
         return parseXML(builder, descriptor);
 235   
     }
 236   
 
 237  7
     private Document parseSDL(DocumentBuilder builder, Resource descriptor)
 238   
         throws SAXParseException, IOException
 239   
     {
 240  7
         Document result = builder.newDocument();
 241  7
         SDLDocumentAdaptor parser = new SDLDocumentAdaptor(result);
 242   
 
 243  7
         parser.parse(descriptor);
 244   
 
 245  7
         return result;
 246   
     }
 247   
 
 248  2
     private Document parseXML(DocumentBuilder builder, Resource descriptor)
 249   
         throws SAXException, IOException
 250   
     {
 251  2
         URL resourceURL = descriptor.getResourceURL();
 252   
 
 253  2
         InputStream rawStream = resourceURL.openStream();
 254  2
         InputStream stream = new BufferedInputStream(rawStream);
 255   
 
 256  2
         InputSource source = new InputSource(stream);
 257   
 
 258  2
         try
 259   
         {
 260  2
             return builder.parse(source);
 261   
         }
 262   
         finally
 263   
         {
 264  2
             stream.close();
 265   
         }
 266   
     }
 267   
 
 268  5
     private void writeDocument(Document document, File file) throws BuildException
 269   
     {
 270  5
         try
 271   
         {
 272  5
             OutputStream out = new FileOutputStream(file);
 273  5
             BufferedOutputStream buffered = new BufferedOutputStream(out);
 274   
 
 275  5
             writeDocument(document, buffered);
 276   
 
 277  5
             buffered.close();
 278   
         }
 279   
         catch (IOException ex)
 280   
         {
 281  0
             throw new BuildException(
 282   
                 "Unable to write registry to " + file + ": " + ex.getMessage(),
 283   
                 ex);
 284   
         }
 285   
     }
 286   
 
 287  9
     private void prepareModuleForInclusion(Element module)
 288   
     {
 289  9
         NamedNodeMap attributes = module.getAttributes();
 290   
 
 291  9
         String moduleId = attributes.getNamedItem("id").getNodeValue();
 292   
 
 293  9
         for (int i = attributes.getLength() - 1; i >= 0; i--)
 294   
         {
 295  18
             Node attr = attributes.item(i);
 296   
 
 297  18
             String name = attr.getNodeName();
 298   
 
 299  18
             if (name.indexOf(':') > 0)
 300  0
                 attributes.removeNamedItem(name);
 301   
         }
 302   
 
 303  9
         module.setAttribute("uid", Integer.toString(_uid++));
 304   
 
 305  9
         Node node = module.getFirstChild();
 306   
 
 307  9
         while (node != null)
 308   
         {
 309  75
             if (node instanceof Element)
 310   
             {
 311  71
                 Element e = (Element) node;
 312   
 
 313  71
                 e.setAttribute("uid", Integer.toString(_uid++));
 314   
 
 315  71
                 String name = e.getTagName();
 316   
 
 317  71
                 if (name.equals("service-point")
 318   
                     || name.equals("configuration-point")
 319   
                     || name.equals("schema"))
 320  60
                     qualify(moduleId, e, "id");
 321   
 
 322  71
                 if (name.equals("configuration-point"))
 323  15
                     qualify(moduleId, e, "schema-id");
 324   
 
 325  71
                 if (name.equals("service-point"))
 326  30
                     qualify(moduleId, e, "parameters-schema-id");
 327   
 
 328   
                 // Expand local ids to fully qualified ids in extension and extend-service
 329   
 
 330  71
                 if (name.equals("contribution"))
 331  7
                     qualify(moduleId, e, "configuration-id");
 332   
 
 333  71
                 if (name.equals("implementation"))
 334  0
                     qualify(moduleId, e, "service-id");
 335   
 
 336  71
                 if (name.equals("service-point") || name.equals("implementation"))
 337  30
                     qualifyServiceIds(moduleId, e);
 338   
 
 339   
                 // TODO: submodule!
 340   
 
 341   
             }
 342   
 
 343  75
             node = node.getNextSibling();
 344   
         }
 345   
     }
 346   
 
 347  130
     private void qualify(String moduleId, Element element, String attributeName)
 348   
     {
 349  130
         String id = element.getAttribute(attributeName);
 350   
 
 351  130
         if (id == null || id.trim().length() == 0)
 352  26
             return;
 353   
 
 354  104
         if (id.indexOf('.') < 0)
 355  104
             element.setAttribute(attributeName, moduleId + "." + id);
 356   
     }
 357   
 
 358  30
     private void qualifyServiceIds(String moduleId, Element element)
 359   
     {
 360  30
         Node node = element.getFirstChild();
 361   
 
 362  30
         while (node != null)
 363   
         {
 364  64
             if (node instanceof Element)
 365   
             {
 366  59
                 Element e = (Element) node;
 367   
 
 368  59
                 String name = e.getTagName();
 369   
 
 370  59
                 if (name.equals("invoke-factory") || name.equals("interceptor"))
 371  18
                     qualify(moduleId, e, "service-id");
 372   
 
 373   
             }
 374   
 
 375  64
             node = node.getNextSibling();
 376   
         }
 377   
     }
 378   
 
 379  5
     private void writeDocument(Document document, OutputStream out) throws IOException
 380   
     {
 381  5
         XMLSerializer serializer = new XMLSerializer(out, new OutputFormat(document, null, true));
 382  5
         serializer.serialize(document);
 383   
     }
 384   
 
 385  5
     public Path createDescriptors()
 386   
     {
 387  5
         _descriptorsPath = new Path(project);
 388  5
         return _descriptorsPath;
 389   
     }
 390   
 
 391  1
     public File getOutput()
 392   
     {
 393  1
         return _output;
 394   
     }
 395   
 
 396  6
     public void setOutput(File file)
 397   
     {
 398  6
         _output = file;
 399   
     }
 400   
 
 401   
 }
 402