Clover coverage report - Code Coverage for hivemind release 1.1-beta-1
Coverage timestamp: Thu Apr 28 2005 19:53:41 EDT
file stats: LOC: 295   Methods: 21
NCLOC: 189   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.7% 100% 98.4%
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.internal.Contribution}s contributed to an
 32   
  * {@link org.apache.hivemind.internal.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 _contributingModule;
 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  980
     public SchemaProcessorImpl(ErrorLog errorLog, Schema schema)
 67   
     {
 68  980
         _errorLog = errorLog;
 69  980
         _schema = schema;
 70  980
         _stack.add(this);
 71   
 
 72  980
         if (_schema != null)
 73   
         {
 74  967
             List l = _schema.getElementModel();
 75   
 
 76  967
             int count = l.size();
 77  967
             for (int i = 0; i < count; i++)
 78   
             {
 79  979
                 ElementModel model = (ElementModel) l.get(i);
 80  979
                 _elementMap.put(model.getElementName(), new SchemaElement(this, model));
 81   
             }
 82   
 
 83  967
             _canElementsBeMapped = schema.canInstancesBeKeyed();
 84   
         }
 85   
     }
 86   
 
 87   
     /**
 88   
      * Invoked over reflection by the {@link org.apache.hivemind.schema.rules.InvokeParentRule}.
 89   
      */
 90  2943
     public void addElement(Object element)
 91   
     {
 92  2943
         _elements.add(element);
 93   
 
 94  2943
         if (_canElementsBeMapped)
 95   
         {
 96  578
             Element currentElement = peekElement();
 97  578
             String keyAttribute = _activeElement.getKeyAttribute();
 98   
 
 99  578
             String expandedKey = getContributingModule().expandSymbols(
 100   
                     currentElement.getAttributeValue(keyAttribute),
 101   
                     currentElement.getLocation());
 102   
 
 103  578
             Translator t = getAttributeTranslator(keyAttribute);
 104   
 
 105  578
             Object finalValue = t.translate(
 106   
                     getContributingModule(),
 107   
                     Object.class,
 108   
                     expandedKey,
 109   
                     currentElement.getLocation());
 110   
 
 111  578
             _mappedElements.put(finalValue, element);
 112   
         }
 113   
     }
 114   
 
 115  975
     public List getElements()
 116   
     {
 117  975
         return _elements;
 118   
     }
 119   
 
 120  117
     public Map getMappedElements()
 121   
     {
 122  117
         if (_canElementsBeMapped)
 123  117
             return _mappedElements;
 124   
 
 125  0
         return null;
 126   
     }
 127   
 
 128  6099
     public void push(Object object)
 129   
     {
 130  6099
         _stack.add(object);
 131   
     }
 132   
 
 133  6100
     public Object pop()
 134   
     {
 135  6100
         if (_stack.isEmpty())
 136  1
             throw new ArrayIndexOutOfBoundsException(ImplMessages.schemaStackViolation(this));
 137   
 
 138  6099
         return _stack.remove(_stack.size() - 1);
 139   
     }
 140   
 
 141  11807
     public Object peek()
 142   
     {
 143  11807
         return peek(0);
 144   
     }
 145   
 
 146  17908
     public Object peek(int depth)
 147   
     {
 148  17908
         int count = _stack.size();
 149   
 
 150  17908
         int position = count - 1 - depth;
 151   
 
 152  17908
         if (position < 0)
 153  1
             throw new ArrayIndexOutOfBoundsException(ImplMessages.schemaStackViolation(this));
 154   
 
 155  17907
         return _stack.get(count - 1 - depth);
 156   
     }
 157   
 
 158  22303
     public Module getContributingModule()
 159   
     {
 160  22303
         return _contributingModule;
 161   
     }
 162   
 
 163   
     /** @since 1.1 */
 164   
 
 165  5404
     public Module getDefiningModule()
 166   
     {
 167  5404
         return _schema.getDefiningModule();
 168   
     }
 169   
 
 170  11
     public String getElementPath()
 171   
     {
 172  11
         StringBuffer buffer = new StringBuffer();
 173  11
         int count = _elementStack.size();
 174   
 
 175  11
         for (int i = 0; i < count; i++)
 176   
         {
 177  10
             if (i > 0)
 178  1
                 buffer.append('/');
 179   
 
 180  10
             buffer.append(((Element) _elementStack.get(i)).getElementName());
 181   
         }
 182   
 
 183  11
         return buffer.toString();
 184   
     }
 185   
 
 186  3396
     private void pushElement(Element element)
 187   
     {
 188  3396
         _elementStack.add(element);
 189   
     }
 190   
 
 191  578
     private Element peekElement()
 192   
     {
 193  578
         return (Element) _elementStack.get(_elementStack.size() - 1);
 194   
     }
 195   
 
 196  3393
     private void popElement()
 197   
     {
 198  3393
         _elementStack.remove(_elementStack.size() - 1);
 199   
     }
 200   
 
 201   
     /**
 202   
      * Processes a single extension.
 203   
      */
 204  986
     public void process(List elements, Module contributingModule)
 205   
     {
 206  986
         if (elements == null)
 207  19
             return;
 208   
 
 209  967
         if (_schema == null)
 210   
         {
 211  3
             _elements.addAll(elements);
 212  3
             return;
 213   
         }
 214   
 
 215  964
         _contributingModule = contributingModule;
 216   
 
 217  964
         int count = elements.size();
 218   
 
 219  964
         for (int i = 0; i < count; i++)
 220   
         {
 221  2947
             Element e = (Element) elements.get(i);
 222   
 
 223  2947
             processRootElement(e);
 224   
         }
 225   
 
 226  961
         _contributingModule = null;
 227   
     }
 228   
 
 229  2947
     private void processRootElement(Element element)
 230   
     {
 231  2947
         String name = element.getElementName();
 232   
 
 233  2947
         SchemaElement schemaElement = (SchemaElement) _elementMap.get(name);
 234   
 
 235  2947
         processElement(element, schemaElement);
 236   
     }
 237   
 
 238   
     private SchemaElement _activeElement;
 239   
 
 240  3396
     private void processElement(Element element, SchemaElement schemaElement)
 241   
     {
 242  3396
         pushElement(element);
 243   
 
 244  3396
         if (schemaElement == null)
 245  1
             _errorLog
 246   
                     .error(ImplMessages.unknownElement(this, element), element.getLocation(), null);
 247   
         else
 248   
         {
 249  3395
             SchemaElement prior = _activeElement;
 250   
 
 251  3395
             schemaElement.validateAttributes(element);
 252   
 
 253  3393
             _activeElement = schemaElement;
 254   
 
 255  3393
             schemaElement.fireBegin(element);
 256   
 
 257  3392
             processNestedElements(element, schemaElement);
 258   
 
 259  3392
             schemaElement.fireEnd(element);
 260   
 
 261  3392
             _activeElement = prior;
 262   
         }
 263   
 
 264  3393
         popElement();
 265   
     }
 266   
 
 267  3392
     private void processNestedElements(Element element, SchemaElement schemaElement)
 268   
     {
 269  3392
         List l = element.getElements();
 270  3392
         int count = l.size();
 271   
 
 272  3392
         for (int i = 0; i < count; i++)
 273   
         {
 274  449
             Element nested = (Element) l.get(i);
 275  449
             String name = nested.getElementName();
 276   
 
 277  449
             processElement(nested, schemaElement.getNestedElement(name));
 278   
         }
 279   
     }
 280   
 
 281  21
     public Translator getContentTranslator()
 282   
     {
 283  21
         return _activeElement.getContentTranslator();
 284   
     }
 285   
 
 286  6551
     public Translator getAttributeTranslator(String attributeName)
 287   
     {
 288  6551
         return _activeElement.getAttributeTranslator(attributeName);
 289   
     }
 290   
 
 291  7600
     public Translator getTranslator(String translator)
 292   
     {
 293  7600
         return getContributingModule().getTranslator(translator);
 294   
     }
 295   
 }