2009/04/15 - Apache HiveMind has been retired.

For more information, please explore the Attic.

Clover coverage report - Code Coverage for hivemind-lib release 1.2.1
Coverage timestamp: Fri Feb 10 2006 16:34:07 PST
file stats: LOC: 142   Methods: 9
NCLOC: 88   Classes: 2
 
 Source file Conditionals Statements Methods TOTAL
HiveMindBuilder.java 91.7% 90% 77.8% 88.2%
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.lib.groovy;
 16   
 17    import java.util.HashMap;
 18    import java.util.Map;
 19   
 20    import groovy.xml.SAXBuilder;
 21   
 22    import org.xml.sax.Attributes;
 23    import org.xml.sax.ContentHandler;
 24    import org.xml.sax.Locator;
 25    import org.xml.sax.helpers.AttributesImpl;
 26   
 27    /**
 28    * The HiveMindBuilder is a <a href="http://groovy.codehaus.org/GroovyMarkup">groovy markup builder
 29    * </a> which can be used to define HiveMind
 30    * {@link org.apache.hivemind.parse.ModuleDescriptor module descriptors} using a Groovy script. A
 31    * single Groovy script must only define one module descriptor.
 32    * <p>
 33    * The markup in the Groovy script is equivalent to the XML markup for module descriptors. The only
 34    * difference being that any dashes in element names and attribute names (which would confuse the
 35    * Groovy parser) are replaced by a camelCase notation. So for example
 36    * <code>configuration-point</code> becomes <code>configurationPoint</code> in a Groovy script.
 37    *
 38    * @since 1.1
 39    * @author Knut Wannheden
 40    */
 41    public class HiveMindBuilder extends SAXBuilder
 42    {
 43    public static final Locator GROOVY_LOCATOR = new GroovyLocator();
 44   
 45    private static final Map CAMEL_TO_HYPHEN_MAP = new HashMap();
 46   
 47  4 public HiveMindBuilder(ContentHandler parser)
 48    {
 49  4 super(parser);
 50   
 51  4 parser.setDocumentLocator(GROOVY_LOCATOR);
 52    }
 53   
 54  20 protected void nodeCompleted(Object parent, Object node)
 55    {
 56  20 super.nodeCompleted(parent, getHyphenatedName(node.toString()));
 57    }
 58   
 59  20 protected void doStartElement(Object name, Attributes attributes)
 60    {
 61  20 super.doStartElement(
 62    getHyphenatedName(name.toString()),
 63    getHyphenatedAttributes(attributes));
 64    }
 65   
 66  68 private String getHyphenatedName(String name)
 67    {
 68  68 String hyphenatedName = (String) CAMEL_TO_HYPHEN_MAP.get(name);
 69   
 70  68 if (hyphenatedName == null)
 71    {
 72  11 char[] chars = name.toCharArray();
 73   
 74  11 StringBuffer hyphenated = new StringBuffer();
 75   
 76  11 for (int i = 0; i < name.length(); i++)
 77    {
 78  97 if (Character.isUpperCase(chars[i]))
 79  4 hyphenated.append('-').append(Character.toLowerCase(chars[i]));
 80    else
 81  93 hyphenated.append(chars[i]);
 82    }
 83   
 84  11 hyphenatedName = hyphenated.toString();
 85   
 86  11 CAMEL_TO_HYPHEN_MAP.put(name, hyphenatedName);
 87    }
 88   
 89  68 return hyphenatedName;
 90    }
 91   
 92  20 private Attributes getHyphenatedAttributes(Attributes attributes)
 93    {
 94  20 AttributesImpl result = (AttributesImpl) attributes;
 95   
 96  20 for (int i = 0; i < result.getLength(); i++)
 97    {
 98  28 result.setLocalName(i, getHyphenatedName(result.getLocalName(i)));
 99    }
 100   
 101  20 return result;
 102    }
 103   
 104    private static class GroovyLocator implements Locator
 105    {
 106  0 public String getPublicId()
 107    {
 108  0 return null;
 109    }
 110   
 111  0 public String getSystemId()
 112    {
 113  0 return null;
 114    }
 115   
 116  19 public int getLineNumber()
 117    {
 118  19 try
 119    {
 120  19 throw new Throwable();
 121    }
 122    catch (Throwable t)
 123    {
 124  19 StackTraceElement[] trace = t.getStackTrace();
 125   
 126  19 for (int i = 0; i < trace.length; i++)
 127    {
 128  1330 String fileName = trace[i].getFileName();
 129  1330 if (fileName != null && fileName.endsWith(".groovy"))
 130  0 return trace[i].getLineNumber();
 131    }
 132    }
 133   
 134  19 return -1;
 135    }
 136   
 137  19 public int getColumnNumber()
 138    {
 139  19 return -1;
 140    }
 141    }
 142    }