Coverage Report - uk.co.badgersinfoil.asxsd.XSDUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
XSDUtils
77% 
89% 
3.222
 
 1  
 /*
 2  
  * Copyright (c) 2006-2007 David Holroyd
 3  
  */
 4  
 
 5  
 package uk.co.badgersinfoil.asxsd;
 6  
 
 7  
 import java.util.List;
 8  
 import org.eclipse.xsd.XSDComplexTypeDefinition;
 9  
 import org.eclipse.xsd.XSDCompositor;
 10  
 import org.eclipse.xsd.XSDConcreteComponent;
 11  
 import org.eclipse.xsd.XSDElementDeclaration;
 12  
 import org.eclipse.xsd.XSDModelGroup;
 13  
 import org.eclipse.xsd.XSDNamedComponent;
 14  
 import org.eclipse.xsd.XSDParticle;
 15  
 import org.eclipse.xsd.XSDParticleContent;
 16  
 import org.eclipse.xsd.XSDTypeDefinition;
 17  
 
 18  
 
 19  0
 public class XSDUtils {
 20  
 
 21  
         public static final String SCHEMA_NAMESPACE = "http://www.w3.org/2001/XMLSchema";
 22  
 
 23  
         /**
 24  
          * returns true if the given particle's maxOccurs attribute is either
 25  
          * 'unbounded', or an integer greater than 1.
 26  
          */
 27  
         public static boolean isMultiplyOccuring(XSDParticle part) {
 28  154
                 int max = part.getMaxOccurs();
 29  154
                 return max == -1 || max > 1;  // -1 : unbounded
 30  
         }
 31  
 
 32  
         public static boolean isRequiredToAppear(XSDParticle part) {
 33  2
                 return part.getMinOccurs() != 0;
 34  
         }
 35  
 
 36  
         public static boolean isXSDAnyType(XSDNamedComponent named) {
 37  57
                 return "anyType".equals(named.getName())
 38  
                     && named.getTargetNamespace().equals(SCHEMA_NAMESPACE);
 39  
         }
 40  
 
 41  
         /**
 42  
          * If the given element appears to be implementing a list-container
 43  
          * design pattern then return the definition of the element repeated in
 44  
          * the list
 45  
          */
 46  
         public static XSDElementDeclaration getElementIfContainerForList(XSDElementDeclaration decl) {
 47  760
                 XSDTypeDefinition typeDef = decl.getTypeDefinition();
 48  760
                 return getElementIfContainerForList(typeDef);
 49  
         }
 50  
 
 51  
         public static XSDElementDeclaration getElementIfContainerForList(XSDTypeDefinition typeDef) {
 52  2212
                 if (typeDef == null || !(typeDef instanceof XSDComplexTypeDefinition)) {
 53  1784
                         return null;
 54  
                 }
 55  428
                 XSDComplexTypeDefinition ctypeDef = (XSDComplexTypeDefinition)typeDef;
 56  428
                 if (!ctypeDef.getAttributeUses().isEmpty()) {
 57  
                         // can't treat the element as an array if it also has
 58  
                         // to host properties due to attributes,
 59  112
                         return null;
 60  
                 }
 61  
                 // look for the definition pattern which allows documents like:
 62  
                 // ...<eggs><egg/><egg/><egg/></eggs>...
 63  
                 // so that we can add a 'eggs' array to the defining class,
 64  
                 // rather than creating a useless 'Eggs' class which just holds
 65  
                 // the array.
 66  
                 // TODO: check for attrs on container,
 67  316
                 XSDParticle ctype = ctypeDef.getComplexType();
 68  316
                 if (ctype == null) {
 69  
                         // no element content
 70  0
                         return null;
 71  
                 }
 72  316
                 XSDParticleContent particleContent = ctype.getContent();
 73  316
                 if (particleContent instanceof XSDModelGroup) {
 74  316
                         XSDModelGroup modelGroup = (XSDModelGroup)particleContent;
 75  316
                         if (modelGroup.getCompositor().equals(XSDCompositor.SEQUENCE_LITERAL)) {
 76  298
                                 List particles = modelGroup.getParticles();
 77  298
                                 if (particles.size() == 1) {
 78  154
                                         XSDParticle part = (XSDParticle)particles.get(0);
 79  154
                                         XSDParticleContent partContent = part.getContent();
 80  154
                                         if (partContent instanceof XSDElementDeclaration) {
 81  154
                                                 if (XSDUtils.isMultiplyOccuring(part)) {
 82  49
                                                         return (XSDElementDeclaration)partContent;
 83  
                                                 }
 84  
                                         }
 85  
                                 }
 86  
                         }
 87  
                 }
 88  
                 
 89  267
                 return null;
 90  
         }
 91  
 
 92  
         public static boolean typeIsA(XSDTypeDefinition type, String namespace, String name) {
 93  0
                 return (type.hasNameAndTargetNamespace(name, namespace))
 94  
                     || (type.getBaseType()!=null && type.getBaseType()!=type && typeIsA(type.getBaseType(), namespace, name));
 95  
         }
 96  
 
 97  
         /**
 98  
          * Produces a string representing the location of the given element of
 99  
          * the schema within the schema definition, for debugging.
 100  
          */
 101  
         public static String path(XSDConcreteComponent component) {
 102  1
                 if (component == null) {
 103  0
                         throw new IllegalArgumentException("component must not be null");
 104  
                 }
 105  1
                 StringBuffer result = new StringBuffer(toString(component));
 106  1
                 while ((component = component.getContainer()) != null) {
 107  0
                         result.insert(0, "/");
 108  0
                         result.insert(0, toString(component));
 109  
                 }
 110  1
                 return result.toString();
 111  
         }
 112  
 
 113  
         private static String toString(XSDConcreteComponent component) {
 114  1
                 String result = component.getClass().getName();
 115  1
                 result = result.substring(result.lastIndexOf('.')+1);
 116  1
                 result = result.substring(0, result.length()-4);  // strip "Impl"
 117  1
                 if (component instanceof XSDNamedComponent) {
 118  0
                         XSDNamedComponent named = (XSDNamedComponent)component;
 119  0
                         if (named.getName() != null) {
 120  0
                                 return result + "[" + named.getName() + "]";
 121  
                         }
 122  
                 }
 123  1
                 if (component instanceof XSDModelGroup) {
 124  0
                         XSDModelGroup group = (XSDModelGroup)component;
 125  0
                         return group.getCompositor().getName();
 126  
                 }
 127  1
                 return result;
 128  
         }
 129  
 
 130  
         public static boolean isAnonymous(XSDTypeDefinition typeDef) {
 131  650
                 return typeDef.getName() == null;
 132  
         }
 133  
 }