Clover coverage report - JBind Project
Coverage timestamp: Fr Mai 28 2004 11:17:36 CEST
file stats: LOC: 258   Methods: 21
NCLOC: 212   Classes: 1
This license of Clover is provided to support the development of JBind only. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover.
 
 Source file Conditionals Statements Methods TOTAL
SchemaElement.java 85% 89,1% 100% 88,6%
 1   
 /*
 2   
  * JBind
 3   
  *
 4   
  * Copyright (c) by Stefan Wachter. All rights reserved.
 5   
  *
 6   
  * Usage, modification, and redistribution is subject to license terms that are
 7   
  * available at 'http://www.jbind.org'. The JBind license is like the
 8   
  * 'Apache Software License V 1.1'.
 9   
  */
 10   
 package org.jbind.xml.schema.element;
 11   
 
 12   
 import java.util.Collections;
 13   
 import java.util.HashMap;
 14   
 import java.util.HashSet;
 15   
 import java.util.Iterator;
 16   
 import java.util.Map;
 17   
 import java.util.Set;
 18   
 
 19   
 import org.jbind.xml.base.IAttribute;
 20   
 import org.jbind.xml.base.IBindingAttributes;
 21   
 import org.jbind.xml.base.INamespaces;
 22   
 import org.jbind.xml.base.IRef;
 23   
 import org.jbind.xml.core.cmp.ISchema;
 24   
 import org.jbind.xml.msg.IConstraintViolations;
 25   
 import org.jbind.xml.msg.XmlException;
 26   
 import org.jbind.xml.msg.XmlMessages;
 27   
 import org.jbind.xml.schema.instantiation.IElemValHelper;
 28   
 import org.jbind.xml.schema.instantiation.IHasTopLevelJobs;
 29   
 import org.jbind.xml.schema.instantiation.ISchemaElement;
 30   
 import org.jbind.xml.schema.instantiation.ITopLevelElement;
 31   
 
 32   
 public class SchemaElement extends OpenAttributes implements ISchemaElement, IOpenAttributes {
 33   
 
 34   
   /**
 35   
    * Maps references to top level elements.
 36   
    */
 37   
   private Map myTopLevelComponents = new HashMap();
 38   
 
 39   
   private String myTargetNamespace = INamespaces.NO;
 40   
 
 41   
   private Set myDefaultBlockTypes = Collections.EMPTY_SET;
 42   
   private Set myDefaultFinalTypes = Collections.EMPTY_SET;
 43   
 
 44   
   private FormType myAttributeFormDefault = FormType.UNQUALIFIED;
 45   
   private FormType myElementFormDefault = FormType.UNQUALIFIED;
 46   
 
 47   
   private ISchemaElement myParentSchemaElement = null;
 48   
 
 49   
   private ISchema mySchema = null;
 50   
 
 51  243
   public SchemaElement(CreationParams aCreationParams, ISchemaElement aParentSchemaElement) {
 52  243
     super(aCreationParams);
 53  243
     myParentSchemaElement = aParentSchemaElement;
 54   
   }
 55   
 
 56  253
   public Iterator iterTopLevelComponents() {
 57  253
     return myTopLevelComponents.values().iterator();
 58   
   }
 59   
 
 60  49
   private boolean hasTopLevelComponents() {
 61  49
     return iterTopLevelComponents().hasNext();
 62   
   }
 63   
 
 64  14
   public ITopLevelElement removeTopLevelElement(IRef aRef) {
 65  14
     return (ITopLevelElement)myTopLevelComponents.remove(aRef);
 66   
   }
 67   
 
 68  204
   public Iterator iterAdditionalSources() {
 69  204
     return iterChildrenByClass(IAdditionalSource.class);
 70   
   }
 71   
 
 72  1095
   public IElement doCreateChild(CreationParams aCreationParams) throws XmlException {
 73  1095
     IElement res = null;
 74  1095
     String componentName = NameUtil.getSchemaComponentName(aCreationParams);
 75  1095
     if ("annotation".equals(componentName)) {
 76  28
       res = new AnnotationElement(aCreationParams);
 77  1067
     } else if ("import".equals(componentName)) {
 78  26
       if (hasTopLevelComponents()) {
 79  0
         throw new XmlException(XmlMessages.notAllowedHere("Import", aCreationParams.myLocation));
 80   
       }
 81  26
       res = new Import(aCreationParams);
 82  1041
     } else if ("include".equals(componentName)) {
 83  10
       if (hasTopLevelComponents()) {
 84  0
         throw new XmlException(XmlMessages.notAllowedHere("Include", aCreationParams.myLocation));
 85   
       }
 86  10
       res = new Include(aCreationParams);
 87  1031
     } else if ("redefine".equals(componentName)) {
 88  13
       if (hasTopLevelComponents()) {
 89  0
         throw new XmlException(XmlMessages.notAllowedHere("Redefine", aCreationParams.myLocation));
 90   
       }
 91  13
       res = new Redefine(aCreationParams);
 92  1018
     } else if ("element".equals(componentName)) {
 93  331
       res = new ElementDeclaration(aCreationParams, false);
 94  687
     } else if ("attribute".equals(componentName)) {
 95  85
       res = new AttributeDeclaration(aCreationParams, false);
 96  602
     } else if ("notation".equals(componentName)) {
 97  4
       res = new Notation(aCreationParams);
 98  598
     } else if ("simpleType".equals(componentName)) {
 99  191
       res = new SimpleTypeDef(aCreationParams, null);
 100  407
     } else if ("complexType".equals(componentName)) {
 101  313
       res = new ComplexTypeDef(aCreationParams, null);
 102  94
     } else if ("group".equals(componentName)) {
 103  33
       res = new ModelGroupDeclaration(aCreationParams);
 104  61
     } else if ("attributeGroup".equals(componentName)) {
 105  61
       res = new AttributeGroupDeclaration(aCreationParams);
 106   
     } else {
 107  0
       res = super.doCreateChild(aCreationParams);
 108   
     }
 109  1095
     return res;
 110   
   }
 111   
 
 112  559
   protected IAttribute doCreateAttribute(ACParams anACParams) throws XmlException {
 113  559
     IAttribute res = null;
 114  559
     String an = NameUtil.getSchemaAttributeName(anACParams);
 115  559
     String upan = NameUtil.getBindingAttributeName(anACParams);
 116  559
     if (IBindingAttributes.PACKAGE.equals(upan)) {
 117  27
       res = new Attribute(anACParams);
 118  532
     } else if (IBindingAttributes.HAS_BEHAVIOUR.equals(upan)) {
 119  61
       res = new BooleanAttribute(anACParams);
 120  471
     } else if (IBindingAttributes.FACTORY_TYPE.equals(upan)) {
 121  2
       res = new Attribute(anACParams);
 122  469
     } else if (IBindingAttributes.TYPE_SUB_PACKAGE.equals(upan)) {
 123  0
       res = new Attribute(anACParams);
 124  469
     } else if (IBindingAttributes.ATTRIBUTE_SUB_PACKAGE.equals(upan)) {
 125  0
       res = new Attribute(anACParams);
 126  469
     } else if (IBindingAttributes.ELEMENT_SUB_PACKAGE.equals(upan)) {
 127  0
       res = new Attribute(anACParams);
 128  469
     } else if (IBindingAttributes.ATTR_GROUP_SUB_PACKAGE.equals(upan)) {
 129  0
       res = new Attribute(anACParams);
 130  469
     } else if (IBindingAttributes.ELEM_GROUP_SUB_PACKAGE.equals(upan)) {
 131  0
       res = new Attribute(anACParams);
 132  469
     } else if ("dataClassPrefix".equals(upan) || "dataClassSuffix".equals(upan) || "dataInterfacePrefix".equals(upan) || "dataInterfaceSuffix".equals(upan) || "behaviourClassPrefix".equals(upan) || "behaviourClassSuffix".equals(upan) || "behaviourInterfacePrefix".equals(upan) || "behaviourInterfaceSuffix".equals(upan)) {
 133  0
       res = new Attribute(anACParams);
 134  469
     } else if ("targetNamespace".equals(an)) {
 135  154
       res = new Attribute(anACParams);
 136  154
       myTargetNamespace = res.getStringValue();
 137  315
     } else if ("blockDefault".equals(an)) {
 138  3
       res = new Attribute(anACParams);
 139  3
       myDefaultBlockTypes = ConstantResolver.getBlockTypes(res.getStringValue(), this);
 140  312
     } else if ("finalDefault".equals(an)) {
 141  0
       res = new Attribute(anACParams);
 142  0
       myDefaultFinalTypes = ConstantResolver.getFinalTypes(res.getStringValue(), this);
 143  312
     } else if ("attributeFormDefault".equals(an)) {
 144  30
       res = new Attribute(anACParams);
 145  30
       myAttributeFormDefault = ConstantResolver.getFormType(res.getStringValue(), this);
 146  282
     } else if ("elementFormDefault".equals(an)) {
 147  146
       res = new Attribute(anACParams);
 148  146
       myElementFormDefault = ConstantResolver.getFormType(res.getStringValue(), this);
 149  136
     } else if ("version".equals(an)) {
 150  2
       res = new Attribute(anACParams);
 151   
     } else {
 152  134
       res = super.doCreateAttribute(anACParams);
 153   
     }
 154  559
     return res;
 155   
   }
 156   
 
 157  951
   public Set getDefaultBlockTypes() {
 158  951
     return myDefaultBlockTypes;
 159   
   }
 160   
 
 161  1686
   public Set getDefaultFinalTypes() {
 162  1686
     return myDefaultFinalTypes;
 163   
   }
 164   
 
 165  573
   public FormType getAttributeFormDefault() {
 166  573
     return myAttributeFormDefault;
 167   
   }
 168   
 
 169  389
   public FormType getElementFormDefault() {
 170  389
     return myElementFormDefault;
 171   
   }
 172   
 
 173  14
   public ITopLevelElement getTopLevelElement(IRef aGlobalRef) {
 174  14
     return (ITopLevelElement)myTopLevelComponents.get(aGlobalRef);
 175   
   }
 176   
 
 177  28
   public ITopLevelElement setTopLevelElement(ITopLevelElement aTopLevelElement) {
 178  28
     return (ITopLevelElement)myTopLevelComponents.put(aTopLevelElement.getGlobalRef(), aTopLevelElement);
 179   
   }
 180   
 
 181  243
   public void validateElement(IElemValHelper aHelper, IConstraintViolations aViolations) {
 182  243
     super.validateElement(aHelper, aViolations);
 183  243
     if (null != myParentSchemaElement) {
 184  20
       String parentTargetNamespace = myParentSchemaElement.getTargetNamespace();
 185  20
       if (!INamespaces.NO.equals(myTargetNamespace) && !myTargetNamespace.equals(parentTargetNamespace)) {
 186  0
         aViolations.add(XmlMessages.invalidTargetNamespace(parentTargetNamespace, myTargetNamespace, myParentSchemaElement.getLocation(), this));
 187   
       }
 188   
     }
 189  243
     if (aViolations.isEmpty()) {
 190  205
       for (Iterator i = iterChildrenByClass(ITopLevelElement.class);
 191  1160
               i.hasNext(); ) {
 192  955
         ITopLevelElement c = (ITopLevelElement)i.next();
 193  955
         if (null != myTopLevelComponents.put(c.getGlobalRef(), c)) {
 194  1
           aViolations.add(XmlMessages.componentNotUnique(c.getGlobalRef(), c));
 195   
         }
 196   
       }
 197   
     }
 198   
   }
 199   
 
 200  204
   public void createJobs(final IHasTopLevelJobs aSchemaJob) {
 201  204
     for (Iterator i = iterAdditionalSources(); i.hasNext(); ) {
 202  46
       IAdditionalSource source = (IAdditionalSource)i.next();
 203  46
       source.createAndAddJob(aSchemaJob);
 204   
     }
 205   
 
 206  204
     for (Iterator i = iterTopLevelComponents(); i.hasNext(); ) {
 207  967
       INamed named = (INamed)i.next();
 208  967
       named.createAndAddJob(aSchemaJob);
 209   
     }
 210   
 
 211   
   }
 212   
 
 213  619
   public boolean isChameleon() {
 214  619
     return (null != myParentSchemaElement) && INamespaces.NO.equals(myTargetNamespace);
 215   
   }
 216   
 
 217  3202
   public String getTargetNamespace() {
 218  3202
     String res = null;
 219  3202
     if (null != myParentSchemaElement) {
 220  150
       res = myParentSchemaElement.getTargetNamespace();
 221   
     } else {
 222  3052
       res = myTargetNamespace;
 223   
     }
 224  3202
     return res;
 225   
   }
 226   
 
 227  1212
   public ISchema getSchema() {
 228  1212
     ISchema res = null;
 229  1212
     if (null != myParentSchemaElement) {
 230  22
       res = myParentSchemaElement.getSchema();
 231   
     } else {
 232  1190
       res = mySchema;
 233   
     }
 234  1212
     return res;
 235   
   }
 236   
 
 237  185
   public void setSchema(ISchema aSchema) {
 238  185
     mySchema = aSchema;
 239   
   }
 240   
 
 241  6486
   public IAttribute getBindingAttribute(String aName) {
 242  6486
     IAttribute res = (IAttribute)getAttributes().getAttribute(INamespaces.JBIND, aName);
 243  6486
     if ((null == res) && (null != myParentSchemaElement)) {
 244  30
       res = myParentSchemaElement.getBindingAttribute(aName);
 245   
     }
 246  6486
     return res;
 247   
   }
 248   
 
 249   
   private Set myIds = null;
 250   
 
 251  280
   public boolean addElementId(String anId) {
 252  280
     if (null == myIds) {
 253  18
       myIds = new HashSet();
 254   
     }
 255  280
     return myIds.add(anId);
 256   
   }
 257   
 }
 258