Clover coverage report - Code Coverage for hivemind release 1.1-alpha-3
Coverage timestamp: Tue Mar 22 2005 09:10:26 EST
file stats: LOC: 237   Methods: 12
NCLOC: 147   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
ConversionDescriptor.java 95.5% 98.2% 100% 97.8%
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.parse;
 16   
 
 17   
 import java.util.ArrayList;
 18   
 import java.util.HashMap;
 19   
 import java.util.Iterator;
 20   
 import java.util.List;
 21   
 import java.util.ListIterator;
 22   
 import java.util.Map;
 23   
 
 24   
 import org.apache.commons.logging.Log;
 25   
 import org.apache.commons.logging.LogFactory;
 26   
 import org.apache.hivemind.Element;
 27   
 import org.apache.hivemind.ErrorHandler;
 28   
 import org.apache.hivemind.schema.AttributeModel;
 29   
 import org.apache.hivemind.schema.ElementModel;
 30   
 import org.apache.hivemind.schema.Rule;
 31   
 import org.apache.hivemind.schema.SchemaProcessor;
 32   
 import org.apache.hivemind.schema.rules.BaseRule;
 33   
 import org.apache.hivemind.schema.rules.CreateObjectRule;
 34   
 import org.apache.hivemind.schema.rules.InvokeParentRule;
 35   
 import org.apache.hivemind.schema.rules.ReadAttributeRule;
 36   
 
 37   
 /**
 38   
  * Descriptor for the <conversion> module descriptor element. This descriptor implements the
 39   
  * {@link Rule}interface and is added as a standard rule to the containing {@link ElementModel}.
 40   
  * When processed it delegates to a {@link CreateObjectRule}, a bunch of {@link ReadAttributeRule},
 41   
  * and finally an {@link InvokeParentRule}.
 42   
  * 
 43   
  * @author Howard Lewis Ship
 44   
  */
 45   
 public class ConversionDescriptor extends BaseRule
 46   
 {
 47   
     private static final Log LOG = LogFactory.getLog(ConversionDescriptor.class);
 48   
 
 49   
     private ErrorHandler _errorHandler;
 50   
 
 51   
     private String _className;
 52   
 
 53   
     private String _parentMethodName = "addElement";
 54   
 
 55   
     private Map _attributeNameMappingMap = new HashMap();
 56   
 
 57   
     /** @since 1.1 */
 58   
     private List _attributeMappings = new ArrayList();
 59   
 
 60   
     private List _rules;
 61   
 
 62   
     private ElementModel _elementModel;
 63   
 
 64  461
     public ConversionDescriptor(ErrorHandler errorHandler, ElementModel elementModel)
 65   
     {
 66  461
         _errorHandler = errorHandler;
 67  461
         _elementModel = elementModel;
 68   
     }
 69   
 
 70   
     /**
 71   
      * @since 1.1
 72   
      */
 73  17
     public List getAttributeMappings()
 74   
     {
 75  17
         return _attributeMappings;
 76   
     }
 77   
 
 78   
     /**
 79   
      * Adds a mapping for an attribute; these come from <map> elements nested within the
 80   
      * <conversion> element. A check for duplicate attribute mappings (that is, duplicated
 81   
      * attribute name), and an error is logged (and the duplicate ignored).
 82   
      */
 83  788
     public void addAttributeMapping(AttributeMappingDescriptor descriptor)
 84   
     {
 85  788
         String attributeName = descriptor.getAttributeName();
 86   
 
 87  788
         AttributeMappingDescriptor existing = (AttributeMappingDescriptor) _attributeNameMappingMap
 88   
                 .get(attributeName);
 89   
 
 90  788
         if (existing != null)
 91   
         {
 92  1
             _errorHandler.error(
 93   
                     LOG,
 94   
                     ParseMessages.dupeAttributeMapping(descriptor, existing),
 95   
                     descriptor.getLocation(),
 96   
                     null);
 97   
 
 98  1
             return;
 99   
         }
 100   
 
 101  787
         _attributeNameMappingMap.put(attributeName, descriptor);
 102   
 
 103  787
         _attributeMappings.add(descriptor);
 104   
     }
 105   
 
 106   
     /**
 107   
      * @since 1.1
 108   
      */
 109  17
     public String getClassName()
 110   
     {
 111  17
         return _className;
 112   
     }
 113   
 
 114  461
     public void setClassName(String string)
 115   
     {
 116  461
         _className = string;
 117   
     }
 118   
 
 119   
     /**
 120   
      * @since 1.1
 121   
      */
 122  17
     public String getParentMethodName()
 123   
     {
 124  17
         return _parentMethodName;
 125   
     }
 126   
 
 127  1
     public void setParentMethodName(String string)
 128   
     {
 129  1
         _parentMethodName = string;
 130   
     }
 131   
 
 132   
     /**
 133   
      * @since 1.1
 134   
      */
 135  1643
     public void begin(SchemaProcessor processor, Element element)
 136   
     {
 137  1643
         for (Iterator i = _rules.iterator(); i.hasNext();)
 138   
         {
 139  7788
             Rule rule = (Rule) i.next();
 140   
 
 141  7788
             rule.begin(processor, element);
 142   
         }
 143   
     }
 144   
 
 145   
     /**
 146   
      * @since 1.1
 147   
      */
 148  1643
     public void end(SchemaProcessor processor, Element element)
 149   
     {
 150  1643
         for (ListIterator i = _rules.listIterator(_rules.size()); i.hasPrevious();)
 151   
         {
 152  7788
             Rule rule = (Rule) i.previous();
 153   
 
 154  7788
             rule.end(processor, element);
 155   
         }
 156   
     }
 157   
 
 158  461
     public void addRulesForModel()
 159   
     {
 160  461
         _rules = new ArrayList();
 161   
 
 162  461
         _rules.add(new CreateObjectRule(_className));
 163   
 
 164  461
         addAttributeRules();
 165   
 
 166  461
         _rules.add(new InvokeParentRule(_parentMethodName));
 167   
     }
 168   
 
 169  461
     private void addAttributeRules()
 170   
     {
 171  461
         Iterator i = _elementModel.getAttributeModels().iterator();
 172   
 
 173  461
         while (i.hasNext())
 174   
         {
 175  1370
             AttributeModel am = (AttributeModel) i.next();
 176  1370
             String attributeName = am.getName();
 177   
 
 178  1370
             AttributeMappingDescriptor amd = (AttributeMappingDescriptor) _attributeNameMappingMap
 179   
                     .get(attributeName);
 180   
 
 181  1370
             if (amd == null)
 182   
             {
 183  584
                 _rules.add(new ReadAttributeRule(attributeName,
 184   
                         constructPropertyName(attributeName), null, getLocation()));
 185   
             }
 186   
             else
 187   
             {
 188  786
                 String propertyName = amd.getPropertyName();
 189  786
                 if (propertyName == null)
 190  0
                     propertyName = constructPropertyName(attributeName);
 191   
 
 192  786
                 _rules.add(new ReadAttributeRule(attributeName, propertyName, null, amd
 193   
                         .getLocation()));
 194   
 
 195  786
                 _attributeNameMappingMap.remove(attributeName);
 196   
             }
 197   
         }
 198   
 
 199  461
         if (!_attributeNameMappingMap.isEmpty())
 200  1
             _errorHandler.error(LOG, ParseMessages.extraMappings(
 201   
                     _attributeNameMappingMap.keySet(),
 202   
                     _elementModel), _elementModel.getLocation(), null);
 203   
     }
 204   
 
 205  584
     private String constructPropertyName(String attributeName)
 206   
     {
 207  584
         int dashx = attributeName.indexOf('-');
 208  584
         if (dashx < 0)
 209  583
             return attributeName;
 210   
 
 211  1
         int length = attributeName.length();
 212  1
         StringBuffer buffer = new StringBuffer(length);
 213   
 
 214  1
         buffer.append(attributeName.substring(0, dashx));
 215  1
         boolean toUpper = true;
 216   
 
 217  1
         for (int i = dashx + 1; i < length; i++)
 218   
         {
 219  14
             char ch = attributeName.charAt(i);
 220   
 
 221  14
             if (ch == '-')
 222   
             {
 223  1
                 toUpper = true;
 224  1
                 continue;
 225   
             }
 226   
 
 227  13
             if (toUpper)
 228  2
                 ch = Character.toUpperCase(ch);
 229   
 
 230  13
             buffer.append(ch);
 231   
 
 232  13
             toUpper = false;
 233   
         }
 234   
 
 235  1
         return buffer.toString();
 236   
     }
 237   
 }