Clover coverage report - Code Coverage for hivemind release 1.0-beta-2
Coverage timestamp: Sun Aug 1 2004 14:03:45 EDT
file stats: LOC: 246   Methods: 18
NCLOC: 160   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 100% 100% 100% 100%
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.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.commons.logging.Log;
 23   
 import org.apache.hivemind.Element;
 24   
 import org.apache.hivemind.ErrorHandler;
 25   
 import org.apache.hivemind.internal.Module;
 26   
 import org.apache.hivemind.schema.ElementModel;
 27   
 import org.apache.hivemind.schema.Schema;
 28   
 import org.apache.hivemind.schema.SchemaProcessor;
 29   
 import org.apache.hivemind.schema.Translator;
 30   
 
 31   
 /**
 32   
  * Used to assemble all the {@link org.apache.hivemind.Contribution}s
 33   
  * contributed to an {@link org.apache.hivemind.ConfigurationPoint} while
 34   
  * converting the XML (represented as {@link org.apache.hivemind.Element}s
 35   
  * into Java objects.
 36   
  *
 37   
  * @author Howard Lewis Ship
 38   
  */
 39   
 public final class SchemaProcessorImpl implements SchemaProcessor
 40   
 {
 41   
     private ErrorHandler _errorHandler;
 42   
     private Log _log;
 43   
     private Schema _schema;
 44   
 
 45   
     /**
 46   
      * The assembled elements that will be contributed into
 47   
      * the ConfigurationPoint.
 48   
      */
 49   
     private List _elements = new ArrayList();
 50   
     private List _stack = new ArrayList();
 51   
     private Module _module;
 52   
 
 53   
     /**
 54   
      * Map on element name to {@link SchemaElement}.
 55   
      */
 56   
     private Map _elementMap = new HashMap();
 57   
 
 58   
     /**
 59   
      * Used to track the nesting of elements.
 60   
      */
 61   
     private List _elementStack = new ArrayList();
 62   
 
 63  531
     public SchemaProcessorImpl(ErrorHandler errorHandler, Log log, Schema schema)
 64   
     {
 65  531
         _errorHandler = errorHandler;
 66  531
         _log = log;
 67  531
         _schema = schema;
 68  531
         _stack.add(this);
 69   
 
 70  531
         if (_schema != null)
 71   
         {
 72   
 
 73  525
             List l = _schema.getElementModel();
 74  525
             int count = l.size();
 75  525
             for (int i = 0; i < count; i++)
 76   
             {
 77  616
                 ElementModel model = (ElementModel) l.get(i);
 78  616
                 _elementMap.put(model.getElementName(), new SchemaElement(this, model));
 79   
             }
 80   
         }
 81   
     }
 82   
 
 83  1628
     public void addElement(Object element)
 84   
     {
 85  1628
         _elements.add(element);
 86   
     }
 87   
 
 88  529
     public List getElements()
 89   
     {
 90  529
         return _elements;
 91   
     }
 92   
 
 93  2996
     public void push(Object object)
 94   
     {
 95  2996
         _stack.add(object);
 96   
     }
 97   
 
 98  2995
     public Object pop()
 99   
     {
 100  2995
         return _stack.remove(_stack.size() - 1);
 101   
     }
 102   
 
 103  6505
     public Object peek()
 104   
     {
 105  6505
         return peek(0);
 106   
     }
 107   
 
 108  9503
     public Object peek(int depth)
 109   
     {
 110  9503
         int count = _stack.size();
 111   
 
 112  9503
         return _stack.get(count - 1 - depth);
 113   
     }
 114   
 
 115  13370
     public Module getContributingModule()
 116   
     {
 117  13370
         return _module;
 118   
     }
 119   
 
 120  9
     public String getElementPath()
 121   
     {
 122  9
         StringBuffer buffer = new StringBuffer();
 123  9
         int count = _elementStack.size();
 124   
 
 125  9
         for (int i = 0; i < count; i++)
 126   
         {
 127  11
             if (i > 0)
 128  2
                 buffer.append('/');
 129   
 
 130  11
             buffer.append(_elementStack.get(i).toString());
 131   
         }
 132   
 
 133  9
         return buffer.toString();
 134   
     }
 135   
 
 136  1864
     private void pushElement(String elementName)
 137   
     {
 138  1864
         _elementStack.add(elementName);
 139   
     }
 140   
 
 141  1862
     private void popElement()
 142   
     {
 143  1862
         _elementStack.remove(_elementStack.size() - 1);
 144   
     }
 145   
 
 146   
     /**
 147   
      * Processes a single extension.
 148   
      */
 149  534
     public void process(List elements, Module contributingModule)
 150   
     {
 151  534
         if (elements == null)
 152  10
             return;
 153   
 
 154  524
         if (_schema == null)
 155   
         {
 156  2
             _elements.addAll(elements);
 157  2
             return;
 158   
         }
 159   
 
 160  522
         _module = contributingModule;
 161   
 
 162  522
         int count = elements.size();
 163   
 
 164  522
         for (int i = 0; i < count; i++)
 165   
         {
 166  1631
             Element e = (Element) elements.get(i);
 167   
 
 168  1631
             processRootElement(e);
 169   
         }
 170   
 
 171  520
         _module = null;
 172   
     }
 173   
 
 174  1631
     private void processRootElement(Element element)
 175   
     {
 176  1631
         String name = element.getElementName();
 177   
 
 178  1631
         SchemaElement schemaElement = (SchemaElement) _elementMap.get(name);
 179   
 
 180  1631
         processElement(element, schemaElement);
 181   
     }
 182   
 
 183   
     private SchemaElement _activeElement;
 184   
 
 185  1864
     private void processElement(Element element, SchemaElement schemaElement)
 186   
     {
 187  1864
         String name = element.getElementName();
 188   
 
 189  1864
         pushElement(name);
 190   
 
 191  1864
         if (schemaElement == null)
 192  1
             _errorHandler.error(
 193   
                 _log,
 194   
                 ImplMessages.unknownElement(this, element),
 195   
                 element.getLocation(),
 196   
                 null);
 197   
         else
 198   
         {
 199  1863
             SchemaElement prior = _activeElement;
 200   
 
 201  1863
             schemaElement.validateAttributes(element);
 202   
 
 203  1862
             _activeElement = schemaElement;
 204   
 
 205  1862
             schemaElement.fireBegin(element);
 206   
 
 207  1861
             processNestedElements(element, schemaElement);
 208   
 
 209  1861
             schemaElement.fireEnd(element);
 210   
 
 211  1861
             _activeElement = prior;
 212   
         }
 213   
 
 214  1862
         popElement();
 215   
     }
 216   
 
 217  1861
     private void processNestedElements(Element element, SchemaElement schemaElement)
 218   
     {
 219  1861
         List l = element.getElements();
 220  1861
         int count = l.size();
 221   
 
 222  1861
         for (int i = 0; i < count; i++)
 223   
         {
 224  233
             Element nested = (Element) l.get(i);
 225  233
             String name = nested.getElementName();
 226   
 
 227  233
             processElement(nested, schemaElement.getNestedElement(name));
 228   
         }
 229   
     }
 230   
 
 231  15
     public Translator getContentTranslator()
 232   
     {
 233  15
         return _activeElement.getContentTranslator();
 234   
     }
 235   
 
 236  3574
     public Translator getAttributeTranslator(String attributeName)
 237   
     {
 238  3574
         return _activeElement.getAttributeTranslator(attributeName);
 239   
     }
 240   
 
 241  3580
     public Translator getTranslator(String translator)
 242   
     {
 243  3580
         return getContributingModule().getTranslator(translator);
 244   
     }
 245   
 }
 246