| 1 |
|
|
| 2 |
|
|
| 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 |
|
|
| 25 |
|
|
| 26 |
|
|
| 27 |
|
public static boolean isMultiplyOccuring(XSDParticle part) { |
| 28 |
154 |
int max = part.getMaxOccurs(); |
| 29 |
154 |
return max == -1 || max > 1; |
| 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 |
|
|
| 43 |
|
|
| 44 |
|
|
| 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 |
|
|
| 58 |
|
|
| 59 |
112 |
return null; |
| 60 |
|
} |
| 61 |
|
|
| 62 |
|
|
| 63 |
|
|
| 64 |
|
|
| 65 |
|
|
| 66 |
|
|
| 67 |
316 |
XSDParticle ctype = ctypeDef.getComplexType(); |
| 68 |
316 |
if (ctype == null) { |
| 69 |
|
|
| 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 |
|
|
| 99 |
|
|
| 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); |
| 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 |
|
} |