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

For more information, please explore the Attic.

Clover coverage report - Code Coverage for hivemind release 1.2.1
Coverage timestamp: Fri Feb 10 2006 16:33:43 PST
file stats: LOC: 237   Methods: 12
NCLOC: 147   Classes: 1
 
 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  518 public ConversionDescriptor(ErrorHandler errorHandler, ElementModel elementModel)
 65    {
 66  518 _errorHandler = errorHandler;
 67  518 _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  886 public void addAttributeMapping(AttributeMappingDescriptor descriptor)
 84    {
 85  886 String attributeName = descriptor.getAttributeName();
 86   
 87  886 AttributeMappingDescriptor existing = (AttributeMappingDescriptor) _attributeNameMappingMap
 88    .get(attributeName);
 89   
 90  886 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  885 _attributeNameMappingMap.put(attributeName, descriptor);
 102   
 103  885 _attributeMappings.add(descriptor);
 104    }
 105   
 106    /**
 107    * @since 1.1
 108    */
 109  17 public String getClassName()
 110    {
 111  17 return _className;
 112    }
 113   
 114  518 public void setClassName(String string)
 115    {
 116  518 _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  1975 public void begin(SchemaProcessor processor, Element element)
 136    {
 137  1975 for (Iterator i = _rules.iterator(); i.hasNext();)
 138    {
 139  9392 Rule rule = (Rule) i.next();
 140   
 141  9392 rule.begin(processor, element);
 142    }
 143    }
 144   
 145    /**
 146    * @since 1.1
 147    */
 148  1975 public void end(SchemaProcessor processor, Element element)
 149    {
 150  1975 for (ListIterator i = _rules.listIterator(_rules.size()); i.hasPrevious();)
 151    {
 152  9392 Rule rule = (Rule) i.previous();
 153   
 154  9392 rule.end(processor, element);
 155    }
 156    }
 157   
 158  518 public void addRulesForModel()
 159    {
 160  518 _rules = new ArrayList();
 161   
 162  518 _rules.add(new CreateObjectRule(_className));
 163   
 164  518 addAttributeRules();
 165   
 166  518 _rules.add(new InvokeParentRule(_parentMethodName));
 167    }
 168   
 169  518 private void addAttributeRules()
 170    {
 171  518 Iterator i = _elementModel.getAttributeModels().iterator();
 172   
 173  518 while (i.hasNext())
 174    {
 175  1540 AttributeModel am = (AttributeModel) i.next();
 176  1540 String attributeName = am.getName();
 177   
 178  1540 AttributeMappingDescriptor amd = (AttributeMappingDescriptor) _attributeNameMappingMap
 179    .get(attributeName);
 180   
 181  1540 if (amd == null)
 182    {
 183  656 _rules.add(new ReadAttributeRule(attributeName,
 184    constructPropertyName(attributeName), null, getLocation()));
 185    }
 186    else
 187    {
 188  884 String propertyName = amd.getPropertyName();
 189  884 if (propertyName == null)
 190  0 propertyName = constructPropertyName(attributeName);
 191   
 192  884 _rules.add(new ReadAttributeRule(attributeName, propertyName, null, amd
 193    .getLocation()));
 194   
 195  884 _attributeNameMappingMap.remove(attributeName);
 196    }
 197    }
 198   
 199  518 if (!_attributeNameMappingMap.isEmpty())
 200  1 _errorHandler.error(LOG, ParseMessages.extraMappings(
 201    _attributeNameMappingMap.keySet(),
 202    _elementModel), _elementModel.getLocation(), null);
 203    }
 204   
 205  656 private String constructPropertyName(String attributeName)
 206    {
 207  656 int dashx = attributeName.indexOf('-');
 208  656 if (dashx < 0)
 209  655 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    }