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: 190   Methods: 7
NCLOC: 126   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 94.4% 97.8% 100% 97.2%
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.parse;
 16   
 
 17   
 import java.util.HashMap;
 18   
 import java.util.Iterator;
 19   
 import java.util.Map;
 20   
 
 21   
 import org.apache.commons.logging.Log;
 22   
 import org.apache.commons.logging.LogFactory;
 23   
 import org.apache.hivemind.ErrorHandler;
 24   
 import org.apache.hivemind.HiveMind;
 25   
 import org.apache.hivemind.Location;
 26   
 import org.apache.hivemind.impl.BaseLocatable;
 27   
 import org.apache.hivemind.schema.AttributeModel;
 28   
 import org.apache.hivemind.schema.Translator;
 29   
 import org.apache.hivemind.schema.impl.ElementModelImpl;
 30   
 import org.apache.hivemind.schema.rules.CreateObjectRule;
 31   
 import org.apache.hivemind.schema.rules.InvokeParentRule;
 32   
 import org.apache.hivemind.schema.rules.ReadAttributeRule;
 33   
 
 34   
 /**
 35   
  * Descriptor for the <conversion> module descriptor element.
 36   
  *
 37   
  * @author Howard Lewis Ship
 38   
  */
 39   
 public class ConversionDescriptor extends BaseLocatable
 40   
 {
 41   
     private static final Log LOG = LogFactory.getLog(ConversionDescriptor.class);
 42   
 
 43   
     private ErrorHandler _errorHandler;
 44   
     private ElementModelImpl _elementModel;
 45   
     private String _defaultTranslator;
 46   
 
 47   
     private String _className;
 48   
     private String _parentMethodName = "addElement";
 49   
     private Map _attributeMappings = new HashMap();
 50   
 
 51  320
     public ConversionDescriptor(
 52   
         ErrorHandler errorHandler,
 53   
         ElementModelImpl elementModel,
 54   
         String defaultTranslator,
 55   
         Location location)
 56   
     {
 57  320
         _errorHandler = errorHandler;
 58  320
         _elementModel = elementModel;
 59  320
         _defaultTranslator = defaultTranslator;
 60   
 
 61  320
         setLocation(location);
 62   
     }
 63   
 
 64   
     /**
 65   
      * Adds a mapping for an attribute; these come from <map>
 66   
      * elements nested within the <conversion> element.  A check
 67   
      * for duplicate attribute mappings (that is, duplicated attribute name),
 68   
      * and an error is logged (and the duplicate ignored).
 69   
      */
 70  476
     public void addAttributeMapping(AttributeMappingDescriptor descriptor)
 71   
     {
 72  476
         String attributeName = descriptor.getAttributeName();
 73   
 
 74  476
         AttributeMappingDescriptor existing =
 75   
             (AttributeMappingDescriptor) _attributeMappings.get(attributeName);
 76   
 
 77  476
         if (existing != null)
 78   
         {
 79  1
             _errorHandler.error(
 80   
                 LOG,
 81   
                 ParseMessages.dupeAttributeMapping(descriptor, existing),
 82   
                 descriptor.getLocation(),
 83   
                 null);
 84   
 
 85  1
             return;
 86   
         }
 87   
 
 88  475
         _attributeMappings.put(attributeName, descriptor);
 89   
     }
 90   
 
 91  320
     public void setClassName(String string)
 92   
     {
 93  320
         _className = string;
 94   
     }
 95   
 
 96  1
     public void setParentMethodName(String string)
 97   
     {
 98  1
         _parentMethodName = string;
 99   
     }
 100   
 
 101   
     /**
 102   
      * Invoked once all <map> elements have been processed; this creates
 103   
      * {@link org.apache.hivemind.schema.Rule}s that are added
 104   
      * to the {@link ElementModelImpl}.
 105   
      */
 106  320
     public void addRulesForModel()
 107   
     {
 108  320
         _elementModel.addRule(new CreateObjectRule(_className));
 109   
 
 110  320
         addAttributeRules();
 111   
 
 112  320
         _elementModel.addRule(new InvokeParentRule(_parentMethodName));
 113   
     }
 114   
 
 115  320
     private void addAttributeRules()
 116   
     {
 117  320
         Iterator i = _elementModel.getAttributeModels().iterator();
 118   
 
 119  320
         while (i.hasNext())
 120   
         {
 121  878
             AttributeModel am = (AttributeModel) i.next();
 122  878
             String attributeName = am.getName();
 123   
 
 124  878
             AttributeMappingDescriptor amd =
 125   
                 (AttributeMappingDescriptor) _attributeMappings.get(attributeName);
 126   
 
 127  878
             if (amd == null)
 128   
             {
 129  404
                 _elementModel.addRule(
 130   
                     new ReadAttributeRule(
 131   
                         attributeName,
 132   
                         constructPropertyName(attributeName),
 133   
                         null,
 134   
                         getLocation()));
 135   
             }
 136   
             else
 137   
             {
 138  474
                 String propertyName = amd.getPropertyName();
 139  474
                 if (propertyName == null)
 140  0
                     propertyName = constructPropertyName(attributeName);
 141   
 
 142  474
                 _elementModel.addRule(
 143   
                     new ReadAttributeRule(attributeName, propertyName, null, amd.getLocation()));
 144   
 
 145  474
                 _attributeMappings.remove(attributeName);
 146   
             }
 147   
         }
 148   
 
 149  320
         if (!_attributeMappings.isEmpty())
 150  1
             _errorHandler.error(
 151   
                 LOG,
 152   
                 ParseMessages.extraMappings(_attributeMappings.keySet(), _elementModel),
 153   
                 _elementModel.getLocation(),
 154   
                 null);
 155   
     }
 156   
 
 157  404
     private String constructPropertyName(String attributeName)
 158   
     {
 159  404
         int dashx = attributeName.indexOf('-');
 160  404
         if (dashx < 0)
 161  403
             return attributeName;
 162   
 
 163  1
         int length = attributeName.length();
 164  1
         StringBuffer buffer = new StringBuffer(length);
 165   
 
 166  1
         buffer.append(attributeName.substring(0, dashx));
 167  1
         boolean toUpper = true;
 168   
 
 169  1
         for (int i = dashx + 1; i < length; i++)
 170   
         {
 171  14
             char ch = attributeName.charAt(i);
 172   
 
 173  14
             if (ch == '-')
 174   
             {
 175  1
                 toUpper = true;
 176  1
                 continue;
 177   
             }
 178   
 
 179  13
             if (toUpper)
 180  2
                 ch = Character.toUpperCase(ch);
 181   
 
 182  13
             buffer.append(ch);
 183   
 
 184  13
             toUpper = false;
 185   
         }
 186   
 
 187  1
         return buffer.toString();
 188   
     }
 189   
 }
 190