Clover coverage report - Code Coverage for hivemind release 1.0-beta-1
Coverage timestamp: Sat Jul 3 2004 09:41:37 EDT
file stats: LOC: 216   Methods: 9
NCLOC: 133   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
SchemaElement.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.HashSet;
 20   
 import java.util.Iterator;
 21   
 import java.util.List;
 22   
 import java.util.Map;
 23   
 import java.util.Set;
 24   
 
 25   
 import org.apache.hivemind.ApplicationRuntimeException;
 26   
 import org.apache.hivemind.Attribute;
 27   
 import org.apache.hivemind.Element;
 28   
 import org.apache.hivemind.schema.AttributeModel;
 29   
 import org.apache.hivemind.schema.ElementModel;
 30   
 import org.apache.hivemind.schema.Rule;
 31   
 import org.apache.hivemind.schema.SchemaProcessor;
 32   
 import org.apache.hivemind.schema.Translator;
 33   
 import org.apache.hivemind.schema.rules.NullTranslator;
 34   
 
 35   
 /**
 36   
  * A wrapper around {@link org.apache.hivemind.schema.ElementModel} used
 37   
  * by {@link org.apache.hivemind.impl.SchemaProcessorImpl}.
 38   
  *
 39   
  * @author Howard Lewis Ship
 40   
  */
 41   
 final class SchemaElement
 42   
 {
 43   
     private SchemaProcessor _processor;
 44   
     private ElementModel _model;
 45   
     private List _requiredAttributes;
 46   
     private Set _knownAttributes;
 47   
     private Map _nestedElements;
 48   
     /**
 49   
      * Keyed on attribute name, value is string (possibly null) used to access a translator.
 50   
      */
 51   
     private Map _attributeTranslators = new HashMap();
 52   
 
 53  3951
     SchemaElement(SchemaProcessor processor, ElementModel model)
 54   
     {
 55  3951
         _processor = processor;
 56  3951
         _model = model;
 57   
 
 58  3951
         _requiredAttributes = new ArrayList();
 59  3951
         _knownAttributes = new HashSet();
 60   
 
 61  3951
         List attributeModels = model.getAttributeModels();
 62  3951
         int count = attributeModels.size();
 63   
 
 64  3951
         for (int i = 0; i < count; i++)
 65   
         {
 66  4022
             AttributeModel am = (AttributeModel) attributeModels.get(i);
 67   
 
 68  4022
             String name = am.getName();
 69   
 
 70  4022
             _knownAttributes.add(name);
 71   
 
 72  4022
             if (am.isRequired())
 73  2291
                 _requiredAttributes.add(name);
 74   
 
 75  4022
             _attributeTranslators.put(name, am.getTranslator());
 76   
         }
 77   
     }
 78   
 
 79   
     /**
 80   
      * Returns a {@link SchemaElement} for a nested element, or
 81   
      * null if no such element exists.
 82   
      */
 83  217
     SchemaElement getNestedElement(String elementName)
 84   
     {
 85  217
         if (_nestedElements == null)
 86  202
             buildNestedElements();
 87   
 
 88  217
         return (SchemaElement) _nestedElements.get(elementName);
 89   
     }
 90   
 
 91  202
     private void buildNestedElements()
 92   
     {
 93  202
         _nestedElements = new HashMap();
 94   
 
 95  202
         List l = _model.getElementModel();
 96  202
         int count = l.size();
 97   
 
 98  202
         for (int i = 0; i < count; i++)
 99   
         {
 100  3372
             ElementModel nested = (ElementModel) l.get(i);
 101   
 
 102  3372
             SchemaElement nestedElement = new SchemaElement(_processor, nested);
 103   
 
 104   
             // TODO: Check for duplicates here, or at parse!
 105   
 
 106  3372
             _nestedElements.put(nested.getElementName(), nestedElement);
 107   
         }
 108   
 
 109   
     }
 110   
 
 111   
     /**
 112   
      * Validates the attributes of the element; checks that all
 113   
      * required attributes are present and that all attributes are defined.
 114   
      * Validation errors result in logged error messages.
 115   
      * 
 116   
      */
 117  1825
     void validateAttributes(Element element)
 118   
     {
 119  1825
         List l = element.getAttributes();
 120  1825
         int count = l.size();
 121  1825
         Set required = new HashSet(_requiredAttributes);
 122  1825
         List errors = new ArrayList();
 123   
 
 124  1825
         for (int i = 0; i < count; i++)
 125   
         {
 126  3300
             Attribute a = (Attribute) l.get(i);
 127  3300
             String name = a.getName();
 128   
 
 129  3300
             if (!_knownAttributes.contains(name))
 130  1
                 errors.add(ImplMessages.unknownAttribute(name));
 131   
 
 132  3300
             required.remove(name);
 133   
 
 134   
         }
 135   
 
 136  1825
         Iterator it = required.iterator();
 137   
 
 138  1825
         while (it.hasNext())
 139   
         {
 140  1
             String name = (String) it.next();
 141  1
             errors.add(ImplMessages.missingAttribute(name));
 142   
         }
 143   
 
 144  1825
         count = errors.size();
 145   
 
 146  1825
         if (count == 0)
 147  1824
             return;
 148   
 
 149  1
         StringBuffer buffer = new StringBuffer();
 150   
 
 151  1
         buffer.append(ImplMessages.elementErrors(_processor, element));
 152   
 
 153  1
         for (int i = 0; i < count; i++)
 154   
         {
 155  2
             buffer.append(' ');
 156  2
             buffer.append(errors.get(i).toString());
 157   
         }
 158   
 
 159  1
         throw new ApplicationRuntimeException(buffer.toString(), element.getLocation(), null);
 160   
     }
 161   
 
 162  1824
     void fireBegin(Element element)
 163   
     {
 164  1824
         List rules = _model.getRules();
 165  1824
         int count = rules.size();
 166   
 
 167  1824
         for (int i = 0; i < count; i++)
 168   
         {
 169  10265
             Rule r = (Rule) rules.get(i);
 170   
 
 171  10265
             r.begin(_processor, element);
 172   
 
 173   
         }
 174   
     }
 175   
 
 176  1823
     void fireEnd(Element element)
 177   
     {
 178  1823
         List rules = _model.getRules();
 179  1823
         int count = rules.size();
 180   
 
 181  1823
         for (int i = count - 1; i >= 0; i--)
 182   
         {
 183  10261
             Rule r = (Rule) rules.get(i);
 184   
 
 185  10261
             r.end(_processor, element);
 186   
 
 187   
         }
 188   
     }
 189   
 
 190   
     private Translator _nullTranslator = new NullTranslator();
 191   
     private Translator _contentTranslator;
 192   
 
 193  15
     public Translator getContentTranslator()
 194   
     {
 195  15
         if (_contentTranslator == null)
 196  10
             _contentTranslator = getTranslator(_model.getContentTranslator());
 197   
 
 198  15
         return _contentTranslator;
 199   
     }
 200   
 
 201  3309
     private Translator getTranslator(String translator)
 202   
     {
 203  3309
         if (translator == null)
 204  4
             return _nullTranslator;
 205   
 
 206  3305
         return _processor.getTranslator(translator);
 207   
     }
 208   
 
 209  3299
     public Translator getAttributeTranslator(String attributeName)
 210   
     {
 211  3299
         String translator = (String) _attributeTranslators.get(attributeName);
 212   
 
 213  3299
         return getTranslator(translator);
 214   
     }
 215   
 }
 216