/** * Gets the "default" attribute value for the given XSDFeature, if there is none then it returns * an empty string. * * @param xsdElem * @return the default value string */ public static String getDefaultValue(XSDFeature xsdElem) { XSDConstraint constraint = null; if (xsdElem instanceof XSDAttributeDeclaration) { // attribute declarations store their default values in // their containers (attribute uses) XSDAttributeUse use = (XSDAttributeUse) xsdElem.getContainer(); if (use.isSetConstraint()) { constraint = use.getConstraint(); } if (constraint != null && constraint.equals(XSDConstraint.DEFAULT_LITERAL)) { if (use.getLexicalValue() != null) { return use.getLexicalValue(); } return EMPTY_STRING; } } else if (xsdElem instanceof XSDElementDeclaration) { if (xsdElem.isSetConstraint()) constraint = xsdElem.getConstraint(); if (constraint != null && constraint.equals(XSDConstraint.DEFAULT_LITERAL)) { if (xsdElem.getLexicalValue() != null) { return xsdElem.getLexicalValue(); } return EMPTY_STRING; } } return EMPTY_STRING; }
/** * Gets the "maxOccurs" attribute value for the given XSDFeature, if there is none then it returns * the default 1. * * @param xsdElem * @return max occurs */ public static int getMaxOccurs(XSDFeature xsdElem) { int max = 1; // not a particle means an attribute use. attributes are maxed at 1. if (!(xsdElem.eContainer() instanceof XSDParticle)) return max; XSDParticle particle = (XSDParticle) xsdElem.eContainer(); if (particle.isSetMaxOccurs()) max = particle.getMaxOccurs(); return max; }
/** * Gets the "minOccurs" attribute value for the given XSDFeature, if there is none then it returns * the default 1. * * @param xsdElem * @return min occurs */ public static int getMinOccurs(XSDFeature xsdElem) { if (xsdElem.eContainer() instanceof XSDAttributeUse) { return (((XSDAttributeUse) xsdElem.eContainer()).getUse() == XSDAttributeUseCategory.REQUIRED_LITERAL ? 1 : 0); } XSDParticle particle = (XSDParticle) xsdElem.eContainer(); int min = 1; if (particle.isSetMinOccurs()) min = particle.getMinOccurs(); return min; }
/** * Given an element, return its type, or null if it does not have a type. This is slightly more * complicated than just calling getType() since an element may not have a type at all, it may * reference another element. * * @param feature * @return the type of the element */ public static XSDTypeDefinition getResolvedType(XSDFeature feature) { // Special case of elements referencing stale XSD complex types if (feature instanceof XSDElementDeclaration && ((XSDElementDeclaration) feature).getTypeDefinition() instanceof XSDComplexTypeDefinition) { XSDElementDeclaration element = (XSDElementDeclaration) feature; // We have a type, but types can be proxies, and proxies can become // stale if the referenced // type changes, so before we return it, re-resolve the proxy and // then return it XSDComplexTypeDefinition oldType = (XSDComplexTypeDefinition) element.getTypeDefinition(); EObject newType = EcoreUtil.resolve(element.getTypeDefinition(), element); if (oldType != newType) { // We only return the resolved type if the name and the namespace has not changed. Changing // the name // and namespace is essentially an unresolved BO. String oldName = oldType.getName(); String newName = ((XSDTypeDefinition) newType).getName(); String oldTNS = oldType.getTargetNamespace(); String newTNS = ((XSDTypeDefinition) newType).getTargetNamespace(); if (((oldName == newName) || (oldName != null && oldName.equals(newName))) && ((oldTNS == newTNS) || (oldTNS != null && oldTNS.equals(newTNS)))) element.setTypeDefinition((XSDTypeDefinition) newType); } return element.getTypeDefinition(); } else if (feature.getType() != null) { return feature.getType(); } else if (feature.getResolvedFeature() != null && feature.getResolvedFeature().getType() != null) { // We reference another element return feature.getResolvedFeature().getType(); } else { return null; } }