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