Clover coverage report - Code Coverage for hivemind release 1.1-alpha-1
Coverage timestamp: Tue Jan 18 2005 07:55:08 EST
file stats: LOC: 275   Methods: 20
NCLOC: 175   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
SchemaProcessorImpl.java 96.2% 98.6% 100% 98.3%
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.impl;
 16   
 
 17   
 import java.util.ArrayList;
 18   
 import java.util.HashMap;
 19   
 import java.util.List;
 20   
 import java.util.Map;
 21   
 
 22   
 import org.apache.hivemind.Element;
 23   
 import org.apache.hivemind.ErrorLog;
 24   
 import org.apache.hivemind.internal.Module;
 25   
 import org.apache.hivemind.schema.ElementModel;
 26   
 import org.apache.hivemind.schema.Schema;
 27   
 import org.apache.hivemind.schema.SchemaProcessor;
 28   
 import org.apache.hivemind.schema.Translator;
 29   
 
 30   
 /**
 31   
  * Used to assemble all the {@link org.apache.hivemind.Contribution}s contributed to an
 32   
  * {@link org.apache.hivemind.ConfigurationPoint}while converting the XML (represented as
 33   
  * {@link org.apache.hivemind.Element}s into Java objects.
 34   
  * 
 35   
  * @author Howard Lewis Ship
 36   
  */
 37   
 public final class SchemaProcessorImpl implements SchemaProcessor
 38   
 {
 39   
     private ErrorLog _errorLog;
 40   
 
 41   
     private Schema _schema;
 42   
 
 43   
     /**
 44   
      * The assembled elements that will be contributed into the ConfigurationPoint.
 45   
      */
 46   
     private List _elements = new ArrayList();
 47   
 
 48   
     private boolean _canElementsBeMapped;
 49   
 
 50   
     private Map _mappedElements = new HashMap();
 51   
 
 52   
     private List _stack = new ArrayList();
 53   
 
 54   
     private Module _module;
 55   
 
 56   
     /**
 57   
      * Map on element name to {@link SchemaElement}.
 58   
      */
 59   
     private Map _elementMap = new HashMap();
 60   
 
 61   
     /**
 62   
      * Used to track the nesting of elements.
 63   
      */
 64   
     private List _elementStack = new ArrayList();
 65   
 
 66  859
     public SchemaProcessorImpl(ErrorLog errorLog, Schema schema)
 67   
     {
 68  859
         _errorLog = errorLog;
 69  859
         _schema = schema;
 70  859
         _stack.add(this);
 71   
 
 72  859
         if (_schema != null)
 73   
         {
 74  851
             List l = _schema.getElementModel();
 75   
 
 76  851
             int count = l.size();
 77  851
             for (int i = 0; i < count; i++)
 78   
             {
 79  863
                 ElementModel model = (ElementModel) l.get(i);
 80  863
                 _elementMap.put(model.getElementName(), new SchemaElement(this, model));
 81   
             }
 82   
 
 83  851
             _canElementsBeMapped = schema.canInstancesBeKeyed();
 84   
         }
 85   
     }
 86   
 
 87   
     /**
 88   
      * Invoked over reflection by the {@link org.apache.hivemind.schema.rules.InvokeParentRule}.
 89   
      */
 90  2503
     public void addElement(Object element)
 91   
     {
 92  2503
         _elements.add(element);
 93   
 
 94  2503
         if (_canElementsBeMapped)
 95   
         {
 96  411
             String key = peekElement().getAttributeValue(_activeElement.getKeyAttribute());
 97   
 
 98  411
             _mappedElements.put(key, element);
 99   
         }
 100   
     }
 101   
 
 102  854
     public List getElements()
 103   
     {
 104  854
         return _elements;
 105   
     }
 106   
 
 107  104
     public Map getMappedElements()
 108   
     {
 109  104
         if (_canElementsBeMapped)
 110  104
             return _mappedElements;
 111   
 
 112  0
         return null;
 113   
     }
 114   
 
 115  5227
     public void push(Object object)
 116   
     {
 117  5227
         _stack.add(object);
 118   
     }
 119   
 
 120  5228
     public Object pop()
 121   
     {
 122  5228
         if (_stack.isEmpty())
 123  1
             throw new ArrayIndexOutOfBoundsException(ImplMessages.schemaStackViolation(this));
 124   
 
 125  5227
         return _stack.remove(_stack.size() - 1);
 126   
     }
 127   
 
 128  10287
     public Object peek()
 129   
     {
 130  10287
         return peek(0);
 131   
     }
 132   
 
 133  15516
     public Object peek(int depth)
 134   
     {
 135  15516
         int count = _stack.size();
 136   
 
 137  15516
         int position = count - 1 - depth;
 138   
 
 139  15516
         if (position < 0)
 140  1
             throw new ArrayIndexOutOfBoundsException(ImplMessages.schemaStackViolation(this));
 141   
 
 142  15515
         return _stack.get(count - 1 - depth);
 143   
     }
 144   
 
 145  20791
     public Module getContributingModule()
 146   
     {
 147  20791
         return _module;
 148   
     }
 149   
 
 150  11
     public String getElementPath()
 151   
     {
 152  11
         StringBuffer buffer = new StringBuffer();
 153  11
         int count = _elementStack.size();
 154   
 
 155  11
         for (int i = 0; i < count; i++)
 156   
         {
 157  10
             if (i > 0)
 158  1
                 buffer.append('/');
 159   
 
 160  10
             buffer.append(((Element) _elementStack.get(i)).getElementName());
 161   
         }
 162   
 
 163  11
         return buffer.toString();
 164   
     }
 165   
 
 166  2902
     private void pushElement(Element element)
 167   
     {
 168  2902
         _elementStack.add(element);
 169   
     }
 170   
 
 171  411
     private Element peekElement()
 172   
     {
 173  411
         return (Element) _elementStack.get(_elementStack.size() - 1);
 174   
     }
 175   
 
 176  2899
     private void popElement()
 177   
     {
 178  2899
         _elementStack.remove(_elementStack.size() - 1);
 179   
     }
 180   
 
 181   
     /**
 182   
      * Processes a single extension.
 183   
      */
 184  864
     public void process(List elements, Module contributingModule)
 185   
     {
 186  864
         if (elements == null)
 187  14
             return;
 188   
 
 189  850
         if (_schema == null)
 190   
         {
 191  3
             _elements.addAll(elements);
 192  3
             return;
 193   
         }
 194   
 
 195  847
         _module = contributingModule;
 196   
 
 197  847
         int count = elements.size();
 198   
 
 199  847
         for (int i = 0; i < count; i++)
 200   
         {
 201  2507
             Element e = (Element) elements.get(i);
 202   
 
 203  2507
             processRootElement(e);
 204   
         }
 205   
 
 206  844
         _module = null;
 207   
     }
 208   
 
 209  2507
     private void processRootElement(Element element)
 210   
     {
 211  2507
         String name = element.getElementName();
 212   
 
 213  2507
         SchemaElement schemaElement = (SchemaElement) _elementMap.get(name);
 214   
 
 215  2507
         processElement(element, schemaElement);
 216   
     }
 217   
 
 218   
     private SchemaElement _activeElement;
 219   
 
 220  2902
     private void processElement(Element element, SchemaElement schemaElement)
 221   
     {
 222  2902
         pushElement(element);
 223   
 
 224  2902
         if (schemaElement == null)
 225  1
             _errorLog
 226   
                     .error(ImplMessages.unknownElement(this, element), element.getLocation(), null);
 227   
         else
 228   
         {
 229  2901
             SchemaElement prior = _activeElement;
 230   
 
 231  2901
             schemaElement.validateAttributes(element);
 232   
 
 233  2899
             _activeElement = schemaElement;
 234   
 
 235  2899
             schemaElement.fireBegin(element);
 236   
 
 237  2898
             processNestedElements(element, schemaElement);
 238   
 
 239  2898
             schemaElement.fireEnd(element);
 240   
 
 241  2898
             _activeElement = prior;
 242   
         }
 243   
 
 244  2899
         popElement();
 245   
     }
 246   
 
 247  2898
     private void processNestedElements(Element element, SchemaElement schemaElement)
 248   
     {
 249  2898
         List l = element.getElements();
 250  2898
         int count = l.size();
 251   
 
 252  2898
         for (int i = 0; i < count; i++)
 253   
         {
 254  395
             Element nested = (Element) l.get(i);
 255  395
             String name = nested.getElementName();
 256   
 
 257  395
             processElement(nested, schemaElement.getNestedElement(name));
 258   
         }
 259   
     }
 260   
 
 261  21
     public Translator getContentTranslator()
 262   
     {
 263  21
         return _activeElement.getContentTranslator();
 264   
     }
 265   
 
 266  5187
     public Translator getAttributeTranslator(String attributeName)
 267   
     {
 268  5187
         return _activeElement.getAttributeTranslator(attributeName);
 269   
     }
 270   
 
 271  5190
     public Translator getTranslator(String translator)
 272   
     {
 273  5190
         return getContributingModule().getTranslator(translator);
 274   
     }
 275   
 }