Clover coverage report - Code Coverage for hivemind release 1.1-beta-2
Coverage timestamp: Tue Jun 28 2005 10:28:23 EDT
file stats: LOC: 295   Methods: 21
NCLOC: 189   Classes: 1
 
 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  1036 public SchemaProcessorImpl(ErrorLog errorLog, Schema schema)
 67    {
 68  1036 _errorLog = errorLog;
 69  1036 _schema = schema;
 70  1036 _stack.add(this);
 71   
 72  1036 if (_schema != null)
 73    {
 74  1022 List l = _schema.getElementModel();
 75   
 76  1022 int count = l.size();
 77  1022 for (int i = 0; i < count; i++)
 78    {
 79  1034 ElementModel model = (ElementModel) l.get(i);
 80  1034 _elementMap.put(model.getElementName(), new SchemaElement(this, model));
 81    }
 82   
 83  1022 _canElementsBeMapped = schema.canInstancesBeKeyed();
 84    }
 85    }
 86   
 87    /**
 88    * Invoked over reflection by the {@link org.apache.hivemind.schema.rules.InvokeParentRule}.
 89    */
 90  3221 public void addElement(Object element)
 91    {
 92  3221 _elements.add(element);
 93   
 94  3221 if (_canElementsBeMapped)
 95    {
 96  608 Element currentElement = peekElement();
 97  608 String keyAttribute = _activeElement.getKeyAttribute();
 98   
 99  608 String expandedKey = getContributingModule().expandSymbols(
 100    currentElement.getAttributeValue(keyAttribute),
 101    currentElement.getLocation());
 102   
 103  608 Translator t = getAttributeTranslator(keyAttribute);
 104   
 105  608 Object finalValue = t.translate(
 106    getContributingModule(),
 107    Object.class,
 108    expandedKey,
 109    currentElement.getLocation());
 110   
 111  608 _mappedElements.put(finalValue, element);
 112    }
 113    }
 114   
 115  1031 public List getElements()
 116    {
 117  1031 return _elements;
 118    }
 119   
 120  123 public Map getMappedElements()
 121    {
 122  123 if (_canElementsBeMapped)
 123  123 return _mappedElements;
 124   
 125  0 return null;
 126    }
 127   
 128  6589 public void push(Object object)
 129    {
 130  6589 _stack.add(object);
 131    }
 132   
 133  6590 public Object pop()
 134    {
 135  6590 if (_stack.isEmpty())
 136  1 throw new ArrayIndexOutOfBoundsException(ImplMessages.schemaStackViolation(this));
 137   
 138  6589 return _stack.remove(_stack.size() - 1);
 139    }
 140   
 141  12850 public Object peek()
 142    {
 143  12850 return peek(0);
 144    }
 145   
 146  19441 public Object peek(int depth)
 147    {
 148  19441 int count = _stack.size();
 149   
 150  19441 int position = count - 1 - depth;
 151   
 152  19441 if (position < 0)
 153  1 throw new ArrayIndexOutOfBoundsException(ImplMessages.schemaStackViolation(this));
 154   
 155  19440 return _stack.get(count - 1 - depth);
 156    }
 157   
 158  24225 public Module getContributingModule()
 159    {
 160  24225 return _contributingModule;
 161    }
 162   
 163    /** @since 1.1 */
 164   
 165  5858 public Module getDefiningModule()
 166    {
 167  5858 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  3700 private void pushElement(Element element)
 187    {
 188  3700 _elementStack.add(element);
 189    }
 190   
 191  608 private Element peekElement()
 192    {
 193  608 return (Element) _elementStack.get(_elementStack.size() - 1);
 194    }
 195   
 196  3697 private void popElement()
 197    {
 198  3697 _elementStack.remove(_elementStack.size() - 1);
 199    }
 200   
 201    /**
 202    * Processes a single extension.
 203    */
 204  1042 public void process(List elements, Module contributingModule)
 205    {
 206  1042 if (elements == null)
 207  20 return;
 208   
 209  1022 if (_schema == null)
 210    {
 211  3 _elements.addAll(elements);
 212  3 return;
 213    }
 214   
 215  1019 _contributingModule = contributingModule;
 216   
 217  1019 int count = elements.size();
 218   
 219  1019 for (int i = 0; i < count; i++)
 220    {
 221  3225 Element e = (Element) elements.get(i);
 222   
 223  3225 processRootElement(e);
 224    }
 225   
 226  1016 _contributingModule = null;
 227    }
 228   
 229  3225 private void processRootElement(Element element)
 230    {
 231  3225 String name = element.getElementName();
 232   
 233  3225 SchemaElement schemaElement = (SchemaElement) _elementMap.get(name);
 234   
 235  3225 processElement(element, schemaElement);
 236    }
 237   
 238    private SchemaElement _activeElement;
 239   
 240  3700 private void processElement(Element element, SchemaElement schemaElement)
 241    {
 242  3700 pushElement(element);
 243   
 244  3700 if (schemaElement == null)
 245  1 _errorLog
 246    .error(ImplMessages.unknownElement(this, element), element.getLocation(), null);
 247    else
 248    {
 249  3699 SchemaElement prior = _activeElement;
 250   
 251  3699 schemaElement.validateAttributes(element);
 252   
 253  3697 _activeElement = schemaElement;
 254   
 255  3697 schemaElement.fireBegin(element);
 256   
 257  3696 processNestedElements(element, schemaElement);
 258   
 259  3696 schemaElement.fireEnd(element);
 260   
 261  3696 _activeElement = prior;
 262    }
 263   
 264  3697 popElement();
 265    }
 266   
 267  3696 private void processNestedElements(Element element, SchemaElement schemaElement)
 268    {
 269  3696 List l = element.getElements();
 270  3696 int count = l.size();
 271   
 272  3696 for (int i = 0; i < count; i++)
 273    {
 274  475 Element nested = (Element) l.get(i);
 275  475 String name = nested.getElementName();
 276   
 277  475 processElement(nested, schemaElement.getNestedElement(name));
 278    }
 279    }
 280   
 281  21 public Translator getContentTranslator()
 282    {
 283  21 return _activeElement.getContentTranslator();
 284    }
 285   
 286  7147 public Translator getAttributeTranslator(String attributeName)
 287    {
 288  7147 return _activeElement.getAttributeTranslator(attributeName);
 289    }
 290   
 291  8250 public Translator getTranslator(String translator)
 292    {
 293  8250 return getContributingModule().getTranslator(translator);
 294    }
 295    }