org.eclipse.xsd_2.7.1.v20120130-0943

dSimpleTypeDefinition, eDataType);
      
      handleFacets(xsdSimpleTypeDefinition, eDataType);
      handleTypeArguments(xsdSimpleTypeDefinition, eDataType);
      
      String constraints = getEcoreAttribute(xsdSimpleTypeDefinition, "constraints");
      if (constraints != null) {
        EcoreUtil.setAnnotation(eDataType, "http://www.eclipse.org/emf/2002/Ecore", "constraints", constraints);
      }
      if ("false".equals(getEcoreAttribute(xsdSimpleTypeDefinition, "serializable"))) {
        eDataType.setSerializable(false);
      }
      return eDataType;
    }
    EEnum eEnum = computeEEnum(xsdSimpleTypeDefinition);
    if (eEnum != null) {
      return eEnum;
    }
    XSDSimpleTypeDefinition baseTypeDefinition = xsdSimpleTypeDefinition.getBaseTypeDefinition();
    if (baseTypeDefinition != null)
    {
      EDataType eDataType = EcoreFactory.eINSTANCE.createEDataType();
      setAnnotations(eDataType, xsdSimpleTypeDefinition);
      
      xsdComponentToEModelElementMap.put(xsdSimpleTypeDefinition, eDataType);
      
      String name = getEcoreAttribute(xsdSimpleTypeDefinition, "name");
      if (name == null) {
        name = validAliasName(xsdSimpleTypeDefinition, true);
      }
      eDataType.setName(name);
      extendedMetaData.setName(eDataType, xsdSimpleTypeDefinition.getAliasName());
      
      EPackage ePackage = getEPackage(xsdSimpleTypeDefinition);
      addToSortedList(ePackage.getEClassifiers(), eDataType);
      if (baseTypeDefinition.getVariety() != xsdSimpleTypeDefinition.getVariety())
      {
        if (xsdSimpleTypeDefinition.getVariety() == XSDVariety.LIST_LITERAL)
        {
          EDataType itemEDataType = getEDataType(xsdSimpleTypeDefinition.getItemTypeDefinition());
          extendedMetaData.setItemType(eDataType, itemEDataType);
          eDataType.setInstanceTypeName("java.util.List");
        }
        else
        {
          String instanceClassName = null;
          List<EDataType> memberTypes = new ArrayList();
          for (XSDSimpleTypeDefinition memberTypeDefinition : xsdSimpleTypeDefinition.getMemberTypeDefinitions())
          {
            EDataType memberEDataType = getEDataType(memberTypeDefinition);
            memberTypes.add(memberEDataType);
            String memberInstanceClassName = memberEDataType.getInstanceTypeName();
            if ((memberInstanceClassName == null) && ((memberEDataType instanceof EEnum))) {
              memberInstanceClassName = "org.eclipse.emf.common.util.Enumerator";
            }
            if (instanceClassName == null) {
              instanceClassName = memberInstanceClassName;
            } else if (instanceClassName != memberInstanceClassName) {
              instanceClassName = "java.lang.Object";
            }
          }
          extendedMetaData.setMemberTypes(eDataType, memberTypes);
          eDataType.setInstanceTypeName(instanceClassName);
        }
      }
      else
      {
        EDataType baseEDataType = getEDataType(baseTypeDefinition);
        String instanceClassName = getInstanceClassName(xsdSimpleTypeDefinition, baseEDataType);
        if ((xsdSimpleTypeDefinition.getName() != null) && 
          (XSDConstants.isOrIsDerivedFrom(baseTypeDefinition, xsdSimpleTypeDefinition.getName(), xsdSimpleTypeDefinition.getTargetNamespace())))
        {
          eDataType.setInstanceTypeName(
            instanceClassName == null ? 
            "java.lang.String" : 
            instanceClassName);
        }
        else
        {
          extendedMetaData.setBaseType(eDataType, baseEDataType);
          eDataType.setInstanceTypeName(
            instanceClassName == null ? 
            "org.eclipse.emf.common.util.Enumerator" : 
            instanceClassName);
        }
      }
      checkForPrimitive(xsdSimpleTypeDefinition, eDataType);
      handleFacets(xsdSimpleTypeDefinition, eDataType);
      handleTypeArguments(xsdSimpleTypeDefinition, eDataType);
      
      String constraints = getEcoreAttribute(xsdSimpleTypeDefinition, "constraints");
      if (constraints != null) {
        EcoreUtil.setAnnotation(eDataType, "http://www.eclipse.org/emf/2002/Ecore", "constraints", constraints);
      }
      if ("false".equals(getEcoreAttribute(xsdSimpleTypeDefinition, "serializable"))) {
        eDataType.setSerializable(false);
      }
      return eDataType;
    }
    return (EDataType)getBuiltInEClassifier(rootSchema.getSchemaForSchemaNamespace(), "anySimpleType");
  }
  
  protected String getInstanceClassName(XSDTypeDefinition xsdTypeDefinition, EDataType baseEDataType)
  {
    return baseEDataType.getInstanceTypeName();
  }
  
  protected String validAliasName(XSDTypeDefinition xsdTypeDefinition, boolean isUpperCase)
  {
    return validName(xsdTypeDefinition.getAliasName(), isUpperCase);
  }
  
  protected static boolean canSupportNull(EDataType eDataType)
  {
    return (!(eDataType instanceof EEnum)) && (PRIMITIVES.indexOf(eDataType.getInstanceTypeName()) == -1);
  }
  
  protected void checkForPrimitive(XSDSimpleTypeDefinition xsdSimpleTypeDefinition, EDataType eDataType)
  {
    if ("http://www.eclipse.org/emf/2002/Ecore".equals(xsdSimpleTypeDefinition.getTargetNamespace()))
    {
      String wrapperType = (String)ECORE_PRIMITIVE_TYPES.get(eDataType.getName());
      if (wrapperType != null)
      {
        XSDSimpleTypeDefinition wrapperTypeDefinition = xsdSimpleTypeDefinition.resolveSimpleTypeDefinition(wrapperType);
        if (wrapperTypeDefinition.getContainer() != null)
        {
          EDataType eDataTypeObject = (EDataType)getEClassifier(wrapperTypeDefinition);
          extendedMetaData.setName(eDataTypeObject, eDataType.getName() + ":Object");
          extendedMetaData.setBaseType(eDataTypeObject, eDataType);
          typeToTypeObjectMap.put(eDataType, eDataTypeObject);
          return;
        }
      }
    }
    checkForPrimitive(eDataType);
  }
  
  protected void checkForPrimitive(EDataType eDataType)
  {
    int index = PRIMITIVES.indexOf(eDataType.getInstanceTypeName());
    if ((index != -1) || ((eDataType instanceof EEnum)))
    {
      EDataType eDataTypeObject = EcoreFactory.eINSTANCE.createEDataType();
      eDataTypeObject.setName(eDataType.getName() + "Object");
      if (index != -1) {
        eDataTypeObject.setInstanceTypeName(PRIMITIVE_WRAPPERS[index]);
      } else {
        eDataTypeObject.setInstanceTypeName("org.eclipse.emf.common.util.Enumerator");
      }
      extendedMetaData.setName(eDataTypeObject, extendedMetaData.getName(eDataType) + ":Object");
      addToSortedList(eDataType.getEPackage().getEClassifiers(), eDataTypeObject);
      extendedMetaData.setBaseType(eDataTypeObject, eDataType);
      
      typeToTypeObjectMap.put(eDataType, eDataTypeObject);
    }
  }
  
  protected void handleFacets(final XSDSimpleTypeDefinition xsdSimpleTypeDefinition, final EDataType eDataType)
  {
    final List<String> enumeration = new ArrayList();
    final List<String> pattern = new ArrayList();
    for (XSDFacet xsdFacet : xsdSimpleTypeDefinition.getFacetContents())
    {
      String ignore = getEcoreAttribute(xsdFacet, "ignore");
      if (!"true".equalsIgnoreCase(ignore)) {
        new XSDSwitch()
        {
          public Object caseXSDEnumerationFacet(XSDEnumerationFacet xsdEnumerationFacet)
          {
            List<?> values = xsdEnumerationFacet.getValue();
            if (!values.isEmpty())
            {
              String lexicalValue = xsdSimpleTypeDefinition.getNormalizedLiteral(xsdEnumerationFacet.getLexicalValue());
              Object value = values.get(0);
              if ((value instanceof List))
              {
                List<?> valueItems = (List)value;
                String[] lexicalItems = lexicalValue.split("[ \t\n\r]");
                StringBuilder composedValue = new StringBuilder();
                int i = 0;
                for (int size = Math.min(valueItems.size(), lexicalItems.length); i < size; i++)
                {
                  Object item = valueItems.get(i);
                  String lexicalItem = lexicalItems[i];
                  if ((item instanceof QName)) {
                    lexicalItem = item.toString();
                  }
                  if (i > 0) {
                    composedValue.append(' ');
                  }
                  composedValue.append(lexicalItem);
                }
                enumeration.add(composedValue.toString());
              }
              else
              {
                if ((value instanceof QName)) {
                  lexicalValue = value.toString();
                }
                enumeration.add(lexicalValue);
              }
            }
            return this;
          }
          
          public Object caseXSDFractionDigitsFacet(XSDFractionDigitsFacet xsdFractionDigitsFacet)
          {
            extendedMetaData.setFractionDigitsFacet(eDataType, xsdFractionDigitsFacet.getValue());
            return this;
          }
          
          public Object caseXSDLengthFacet(XSDLengthFacet xsdLengthFacet)
          {
            extendedMetaData.setLengthFacet(eDataType, xsdLengthFacet.getValue());
            return this;
          }
          
          public Object caseXSDMaxExclusiveFacet(XSDMaxExclusiveFacet xsdMaxExclusiveFacet)
          {
            extendedMetaData.setMaxExclusiveFacet(eDataType, xsdMaxExclusiveFacet.getLexicalValue());
            return this;
          }
          
          public Object caseXSDMaxInclusiveFacet(XSDMaxInclusiveFacet xsdMaxInclusiveFacet)
          {
            extendedMetaData.setMaxInclusiveFacet(eDataType, xsdMaxInclusiveFacet.getLexicalValue());
            return this;
          }
          
          public Object caseXSDMaxLengthFacet(XSDMaxLengthFacet xsdMaxLengthFacet)
          {
            extendedMetaData.setMaxLengthFacet(eDataType, xsdMaxLengthFacet.getValue());
            return this;
          }
          
          public Object caseXSDMinExclusiveFacet(XSDMinExclusiveFacet xsdMinExclusiveFacet)
          {
            extendedMetaData.setMinExclusiveFacet(eDataType, xsdMinExclusiveFacet.getLexicalValue());
            return this;
          }
          
          public Object caseXSDMinInclusiveFacet(XSDMinInclusiveFacet xsdMinInclusiveFacet)
          {
            extendedMetaData.setMinInclusiveFacet(eDataType, xsdMinInclusiveFacet.getLexicalValue());
            return this;
          }
          
          public Object caseXSDMinLengthFacet(XSDMinLengthFacet xsdMinLengthFacet)
          {
            extendedMetaData.setMinLengthFacet(eDataType, xsdMinLengthFacet.getValue());
            return this;
          }
          
          public Object caseXSDPatternFacet(XSDPatternFacet xsdPatternFacet)
          {
            pattern.add(xsdPatternFacet.getLexicalValue());
            return this;
          }
          
          public Object caseXSDTotalDigitsFacet(XSDTotalDigitsFacet xsdTotalDigitsFacet)
          {
            extendedMetaData.setTotalDigitsFacet(eDataType, xsdTotalDigitsFacet.getValue());
            return this;
          }
          
          public Object caseXSDWhiteSpaceFacet(XSDWhiteSpaceFacet xsdWhiteSpaceFacet)
          {
            extendedMetaData.setWhiteSpaceFacet(eDataType, xsdWhiteSpaceFacet.getValue().getValue() + 1);
            return this;
          }
        }.doSwitch(xsdFacet);
      }
    }
    extendedMetaData.setEnumerationFacet(eDataType, enumeration);
    extendedMetaData.setPatternFacet(eDataType, pattern);
  }
  
  void handleTypeArguments(XSDTypeDefinition xsdTypeDefinition, EClassifier eClassifier)
  {
    XSDAnnotation xsdAnnotation = xsdTypeDefinition.getAnnotation();
    if (xsdAnnotation != null)
    {
      List<Element> applicationInformationList = xsdAnnotation.getApplicationInformation("http://www.eclipse.org/emf/2002/Ecore");
      for (Element applicationInformation : applicationInformationList) {
        if ("typeParameters".equals(applicationInformation.getAttributeNS("http://www.eclipse.org/emf/2002/Ecore", "key"))) {
          for (Element typeParameter : getElements(applicationInformation, "typeParameter"))
          {
            ETypeParameter eTypeParameter = EcoreFactory.eINSTANCE.createETypeParameter();
            String typeParameterName = typeParameter.getAttributeNS(null, "name");
            eTypeParameter.setName(typeParameterName);
            eClassifier.getETypeParameters().add(eTypeParameter);
            eTypeParameter.getEBounds().addAll(getGenericTypes(xsdTypeDefinition, typeParameter, eClassifier, typeParameter.getAttributeNS(null, "bounds")));
          }
        }
      }
    }
  }
  
  protected EEnum computeEEnum(XSDSimpleTypeDefinition xsdSimpleTypeDefinition)
  {
    if ((!xsdSimpleTypeDefinition.getEnumerationFacets().isEmpty()) && (!"false".equals(getEcoreAttribute(xsdSimpleTypeDefinition, "enum"))))
    {
      for (Object enumerator : xsdSimpleTypeDefinition.getEffectiveEnumerationFacet().getValue())
      {
        if ((enumerator instanceof QName)) {
          return null;
        }
        if ((enumerator instanceof List)) {
          for (Object item : (List)enumerator) {
            if ((item instanceof QName)) {
              return null;
            }
          }
        }
      }
      EEnum eEnum = EcoreFactory.eINSTANCE.createEEnum();
      setAnnotations(eEnum, xsdSimpleTypeDefinition);
      
      String name = getEcoreAttribute(xsdSimpleTypeDefinition, "name");
      if (name == null) {
        name = validAliasName(xsdSimpleTypeDefinition, true);
      }
      eEnum.setName(name);
      extendedMetaData.setName(eEnum, xsdSimpleTypeDefinition.getAliasName());
      
      EPackage ePackage = getEPackage(xsdSimpleTypeDefinition);
      addToSortedList(ePackage.getEClassifiers(), eEnum);
      
      String constraints = getEcoreAttribute(xsdSimpleTypeDefinition, "constraints");
      if (constraints != null) {
        EcoreUtil.setAnnotation(eEnum, "http://www.eclipse.org/emf/2002/Ecore", "constraints", constraints);
      }
      for (ListIterator<XSDEnumerationFacet> i = xsdSimpleTypeDefinition.getEnumerationFacets().listIterator(); i.hasNext();)
      {
        XSDEnumerationFacet xsdEnumerationFacet = (XSDEnumerationFacet)i.next();
        if (!"true".equalsIgnoreCase(getEcoreAttribute(xsdEnumerationFacet, "ignore")))
        {
          String literal = xsdSimpleTypeDefinition.getNormalizedLiteral(xsdEnumerationFacet.getLexicalValue());
          if ((literal != null) && (eEnum.getEEnumLiteralByLiteral(literal) == null))
          {
            EEnumLiteral eEnumLiteral = EcoreFactory.eINSTANCE.createEEnumLiteral();
            setAnnotations(eEnumLiteral, xsdEnumerationFacet);
            String literalName = getEcoreAttribute(xsdEnumerationFacet, "name");
            if (literalName == null) {
              literalName = validName(literal, 0, "_");
            }
            eEnumLiteral.setName(literalName);
            int value = i.previousIndex();
            String valueLiteral = getEcoreAttribute(xsdEnumerationFacet, "value");
            if (valueLiteral != null) {
              value = Integer.parseInt(valueLiteral);
            }
            eEnumLiteral.setValue(value);
            if (!literalName.equals(literal)) {
              eEnumLiteral.setLiteral(literal);
            }
            eEnum.getELiterals().add(eEnumLiteral);
            map(xsdEnumerationFacet, eEnumLiteral);
          }
        }
      }
      checkForPrimitive(eEnum);
      
      return eEnum;
    }
    return null;
  }
  
  public EClass computeEClass(XSDComplexTypeDefinition xsdComplexTypeDefinition)
  {
    if (xsdComplexTypeDefinition == null) {
      return (EClass)getBuiltInEClassifier(rootSchema.getSchemaForSchemaNamespace(), "anyType");
    }
    if (XSDConstants.isSchemaForSchemaNamespace(xsdComplexTypeDefinition.getTargetNamespace()))
    {
      String name = xsdComplexTypeDefinition.getName();
      if (name != null)
      {
        EClass result = (EClass)getBuiltInEClassifier(xsdComplexTypeDefinition.getTargetNamespace(), name);
        if (result != null) {
          return result;
        }
      }
    }
    else
    {
      if (xsdComplexTypeDefinition.getContainer() == null) {
        return (EClass)getBuiltInEClassifier(rootSchema.getSchemaForSchemaNamespace(), "anyType");
      }
      if (reuseRegisteredPackages)
      {
        EClass eClass = (EClass)extendedMetaData.getType(xsdComplexTypeDefinition.getTargetNamespace(), xsdComplexTypeDefinition.getName());
        if (eClass != null) {
          return eClass;
        }
      }
    }
    EClass eClass = EcoreFactory.eINSTANCE.createEClass();
    setAnnotations(eClass, xsdComplexTypeDefinition);
    
    xsdComponentToEModelElementMap.put(xsdComplexTypeDefinition, eClass);
    if ("true".equals(getEcoreAttribute(xsdComplexTypeDefinition, "interface"))) {
      eClass.setInterface(true);
    }
    String instanceClassName = getEcoreAttribute(xsdComplexTypeDefinition, "instanceClass");
    if (instanceClassName != null) {
      eClass.setInstanceTypeName(instanceClassName.replace('{', '<').replace('}', '>'));
    }
    String aliasName = getEcoreAttribute(xsdComplexTypeDefinition, "name");
    if (aliasName == null) {
      aliasName = validAliasName(xsdComplexTypeDefinition, true);
    }
    eClass.setName(aliasName);
    extendedMetaData.setName(eClass, xsdComplexTypeDefinition.getAliasName());
    
    String constraints = getEcoreAttribute(xsdComplexTypeDefinition, "constraints");
    if (constraints != null) {
      EcoreUtil.setAnnotation(eClass, "http://www.eclipse.org/emf/2002/Ecore", "constraints", constraints);
    }
    EPackage ePackage = getEPackage(xsdComplexTypeDefinition);
    addToSortedList(ePackage.getEClassifiers(), eClass);
    if (xsdComplexTypeDefinition.isAbstract()) {
      eClass.setAbstract(true);
    }
    handleTypeArguments(xsdComplexTypeDefinition, eClass);
    
    EClass baseClass = null;
    XSDTypeDefinition baseTypeDefinition = xsdComplexTypeDefinition.getBaseTypeDefinition();
    if (!baseTypeDefinition.isCircular())
    {
      EClassifier baseType = getEClassifier(baseTypeDefinition);
      if (((baseType instanceof EClass)) && (baseType != EcorePackage.Literals.EOBJECT))
      {
        EGenericType genericSuperType = 
          getGenericType(xsdComplexTypeDefinition, xsdComplexTypeDefinition.getElement(), eClass, getEcoreAttribute(xsdComplexTypeDefinition, "extends"));
        if ((genericSuperType != null) && ((genericSuperType.getEClassifier() instanceof EClass)))
        {
          eClass.getEGenericSuperTypes().add(genericSuperType);
          baseClass = (EClass)genericSuperType.getEClassifier();
        }
        else
        {
          eClass.getESuperTypes().add(baseClass = (EClass)baseType);
        }
      }
    }
    else if ((XSDConstants.isURType(baseTypeDefinition)) && (xsdComplexTypeDefinition.getDerivationMethod() == XSDDerivationMethod.EXTENSION_LITERAL))
    {
      eClass.getESuperTypes().add(baseClass = XMLTypePackage.Literals.ANY_TYPE);
    }
    boolean isRestriction = 
      (!eClass.getESuperTypes().isEmpty()) && 
      (xsdComplexTypeDefinition.getDerivationMethod() == XSDDerivationMethod.RESTRICTION_LITERAL);
    for (EGenericType eGenericType : getGenericTypes(xsdComplexTypeDefinition, xsdComplexTypeDefinition.getElement(), eClass, getEcoreAttribute(xsdComplexTypeDefinition, "implements")))
    {
      EClassifier eClassifier = eGenericType.getEClassifier();
      if ((eClassifier != null) && ((eClassifier instanceof EClass)) && (eClassifier != EcorePackage.Literals.EOBJECT)) {
        eClass.getEGenericSuperTypes().add(eGenericType);
      }
    }
    EffectiveOccurrence effectiveOccurrence;
    XSDTerm xsdTerm;
    boolean isRedundant;
    if (xsdComplexTypeDefinition.getContentTypeCategory() == XSDContentTypeCategory.SIMPLE_LITERAL)
    {
      extendedMetaData.setContentKind(eClass, 2);
      if ((!"SimpleAnyType".equals(eClass.getName())) || (!"http://www.eclipse.org/emf/2003/XMLType".equals(eClass.getEPackage().getNsURI()))) {
        if (eClass.getEAllStructuralFeatures().isEmpty())
        {
          XSDComplexTypeContent xsdComplexTypeContent = xsdComplexTypeDefinition.getContent();
          String name = getEcoreAttribute(xsdComplexTypeContent, "name");
          if (name == null) {
            name = "value";
          }
          createFeature(
            eClass, 
            null, 
            name, 
            xsdComplexTypeContent, 
            false);
        }
        else if (!isRestriction)
        {
          XSDSimpleTypeDefinition xsdSimpleTypeDefinition = xsdComplexTypeDefinition.getSimpleType();
          getEClassifier(xsdSimpleTypeDefinition);
        }
      }
    }
    else
    {
      EStructuralFeature globalGroup = null;
      boolean isMixed = xsdComplexTypeDefinition.getContentTypeCategory() == XSDContentTypeCategory.MIXED_LITERAL;
      String featureMapName = getEcoreAttribute(xsdComplexTypeDefinition, "featureMap");
      if (eClass.getESuperTypes().isEmpty() ? 
        "true".equals(getEcoreAttribute(xsdComplexTypeDefinition, "mixed")) : 
        extendedMetaData.getMixedFeature((EClass)eClass.getESuperTypes().get(0)) != null) {
        isMixed = true;
      }
      extendedMetaData.setContentKind(
        eClass, 
        
        xsdComplexTypeDefinition.getContentTypeCategory() == XSDContentTypeCategory.EMPTY_LITERAL ? 
        1 : isMixed ? 3 : 
        4);
      if (isMixed)
      {
        EStructuralFeature mixedFeature = extendedMetaData.getMixedFeature(eClass);
        if (mixedFeature == null)
        {
          if (featureMapName == null) {
            featureMapName = "mixed";
          }
          mixedFeature = 
            createFeature(
            eClass, 
            featureMapName, 
            EcorePackage.Literals.EFEATURE_MAP_ENTRY, 
            null, 
            0, 
            -1);
          extendedMetaData.setName(mixedFeature, ":mixed");
        }
      }
      else
      {
        globalGroup = extendedMetaData.getElement(eClass, null, ":group");
        if ((globalGroup == null) && (featureMapName != null) && (eClass.getESuperTypes().isEmpty()))
        {
          globalGroup = 
            createFeature(
            eClass, 
            featureMapName, 
            EcorePackage.Literals.EFEATURE_MAP_ENTRY, 
            null, 
            0, 
            -1);
          extendedMetaData.setName(globalGroup, ":group");
          extendedMetaData.setFeatureKind(globalGroup, 6);
        }
      }
      if (xsdComplexTypeDefinition.getContent() != null)
      {
        Map<XSDModelGroup, EStructuralFeature> groups = new HashMap();
        List<EffectiveOccurrence> particleInformation = collectParticles((XSDParticle)xsdComplexTypeDefinition.getContent());
        for (Iterator localIterator2 = particleInformation.iterator(); localIterator2.hasNext();)
        {
          effectiveOccurrence = (EffectiveOccurrence)localIterator2.next();
          
          XSDParticle xsdParticle = xsdParticle;
          EStructuralFeature group = (EStructuralFeature)groups.get(xsdModelGroup);
          xsdTerm = xsdParticle.getTerm();
          EStructuralFeature eStructuralFeature = null;
          String name = getEcoreAttribute(xsdParticle, "name");
          if ((xsdTerm instanceof XSDModelGroup))
          {
            if (!isRestriction)
            {
              XSDModelGroup xsdModelGroup = (XSDModelGroup)xsdTerm;
              if (name == null)
              {
                name = getEcoreAttribute(xsdParticle, "featureMap");
                if (name == null)
                {
                  name = getEcoreAttribute(xsdModelGroup, "name");
                  if (name == null)
                  {
                    name = getEcoreAttribute(xsdModelGroup, "featureMap");
                    if (name == null) {
                      if ((xsdModelGroup.getContainer() instanceof XSDModelGroupDefinition))
                      {
                        XSDModelGroupDefinition xsdModelGroupDefinition = (XSDModelGroupDefinition)xsdModelGroup.getContainer();
                        name = getEcoreAttribute(xsdModelGroupDefinition, "name");
                        if (name == null) {
                          name = validName(xsdModelGroupDefinition.getName(), true);
                        }
                      }
                      else
                      {
                        name = "group";
                      }
                    }
                  }
                }
              }
              eStructuralFeature = 
                createFeature(
                eClass, 
                name, 
                EcorePackage.Literals.EFEATURE_MAP_ENTRY, 
                xsdParticle, 
                0, 
                -1);
              groups.put(xsdModelGroup, eStructuralFeature);
              extendedMetaData.setName(eStructuralFeature, name + ":" + eClass.getEAllStructuralFeatures().indexOf(eStructuralFeature));
            }
          }
          else if ((xsdTerm instanceof XSDWildcard))
          {
            if (!isRestriction)
            {
              if (name == null)
              {
                name = getEcoreAttribute(xsdTerm, "name");
                if (name == null) {
                  name = "any";
                }
              }
              eStructuralFeature = 
                createFeature(
                eClass, 
                name, 
                EcorePackage.Literals.EFEATURE_MAP_ENTRY, 
                xsdParticle, 
                minOccurs, 
                maxOccurs);
            }
          }
          else
          {
            XSDElementDeclaration xsdElementDeclaration = (XSDElementDeclaration)xsdTerm;
            
            isRedundant = false;
            if (isRestriction)
            {
              isRedundant = 
                extendedMetaData.getElement(
                baseClass, xsdElementDeclaration.getTargetNamespace(), xsdElementDeclaration.getName()) != null;
              if (!isRedundant) {
                group = 
                  extendedMetaData.getElementWildcardAffiliation(
                  baseClass, xsdElementDeclaration.getTargetNamespace(), xsdElementDeclaration.getName());
              }
            }
            if (!isRedundant)
            {
              if (name == null)
              {
                name = getEcoreAttribute(xsdElementDeclaration, "name");
                if (name == null) {
                  name = validName(xsdElementDeclaration.getName(), true);
                }
              }
              String groupName = getEcoreAttribute(xsdParticle, "featureMap");
              if (groupName == null) {
                groupName = getEcoreAttribute(xsdElementDeclaration, "featureMap");
              }
              if ((!"".equals(groupName)) && (
                (groupName != null) || (
                ((xsdElementDeclaration.isAbstract()) || 
                (xsdElementDeclaration.getSubstitutionGroup().size() > 1)) && 
                (!"true".equals(getEcoreAttribute(xsdParticle.getSchema().getElement(), "ignoreSubstitutionGroups"))))))
              {
                if (groupName == null) {
                  groupName = name + "Group";
                }
                eStructuralFeature = 
                  createFeature(
                  eClass, 
                  groupName, 
                  EcorePackage.Literals.EFEATURE_MAP_ENTRY, 
                  xsdParticle, 
                  minOccurs, 
                  maxOccurs);
                
                eStructuralFeature.setChangeable(true);
                
                extendedMetaData.setFeatureKind(eStructuralFeature, 6);
                extendedMetaData.setName(eStructuralFeature, xsdElementDeclaration.getName() + ":group");
                if (group != null)
                {
                  extendedMetaData.setGroup(eStructuralFeature, group);
                  eStructuralFeature.setDerived(true);
                  eStructuralFeature.setTransient(true);
                  eStructuralFeature.setVolatile(true);
                }
                else if (isMixed)
                {
                  eStructuralFeature.setDerived(true);
                  eStructuralFeature.setTransient(true);
                  eStructuralFeature.setVolatile(true);
                }
                else if (globalGroup != null)
                {
                  extendedMetaData.setGroup(eStructuralFeature, globalGroup);
                  eStructuralFeature.setDerived(true);
                  eStructuralFeature.setTransient(true);
                  eStructuralFeature.setVolatile(true);
                }
                group = eStructuralFeature;
              }
              eStructuralFeature = 
                createFeature(eClass, xsdElementDeclaration, name, xsdParticle, minOccurs, maxOccurs);
              if ((!eStructuralFeature.isChangeable()) && (group == null) && (getEcoreAttribute(xsdParticle, xsdElementDeclaration, "changeable") == null)) {
                eStructuralFeature.setChangeable(true);
              }
            }
          }
          if (eStructuralFeature != null) {
            if (group != null)
            {
              extendedMetaData.setGroup(eStructuralFeature, group);
              eStructuralFeature.setDerived(true);
              eStructuralFeature.setTransient(true);
              eStructuralFeature.setVolatile(true);
            }
            else if (isMixed)
            {
              eStructuralFeature.setDerived(true);
              eStructuralFeature.setTransient(true);
              eStructuralFeature.setVolatile(true);
            }
            else if (globalGroup != null)
            {
              extendedMetaData.setGroup(eStructuralFeature, globalGroup);
              eStructuralFeature.setDerived(true);
              eStructuralFeature.setTransient(true);
              eStructuralFeature.setVolatile(true);
            }
          }
        }
      }
    }
    XSDWildcard baseXSDWildcard = null;
    Object baseAttributeUses = Collections.emptyList();
    Map<String, XSDAttributeUse> baseAttributeURIs = new HashMap();
    XSDAttributeUse xsdAttributeUse;
    if ((baseTypeDefinition instanceof XSDComplexTypeDefinition))
    {
      XSDComplexTypeDefinition complexBaseTypeDefinition = (XSDComplexTypeDefinition)baseTypeDefinition;
      baseXSDWildcard = complexBaseTypeDefinition.getAttributeWildcard();
      baseAttributeUses = complexBaseTypeDefinition.getAttributeUses();
      for (effectiveOccurrence = ((Collection)baseAttributeUses).iterator(); effectiveOccurrence.hasNext();)
      {
        xsdAttributeUse = (XSDAttributeUse)effectiveOccurrence.next();
        
        baseAttributeURIs.put(xsdAttributeUse.getAttributeDeclaration().getURI(), xsdAttributeUse);
      }
    }
    for (XSDAttributeUse xsdAttributeUse : getAttributeUses(xsdComplexTypeDefinition))
    {
      XSDAttributeDeclaration xsdAttributeDeclaration = xsdAttributeUse.getAttributeDeclaration();
      if (baseAttributeURIs.remove(xsdAttributeDeclaration.getURI()) == null)
      {
        String name = getEcoreAttribute(xsdAttributeUse, "name");
        if (name == null) {
          name = getEcoreAttribute(xsdAttributeDeclaration, "name");
        }
        if (name == null) {
          name = validName(xsdAttributeDeclaration.getName(), true);
        }
        EStructuralFeature eStructuralFeature = 
          createFeature(eClass, xsdAttributeDeclaration, name, xsdAttributeUse, xsdAttributeUse.isRequired());
        if (isRestriction)
        {
          EStructuralFeature attributeWildcardEStructuralFeature = 
            extendedMetaData.getAttributeWildcardAffiliation(
            baseClass, xsdAttributeDeclaration.getTargetNamespace(), xsdAttributeDeclaration.getName());
          if (attributeWildcardEStructuralFeature != null)
          {
            extendedMetaData.setGroup(eStructuralFeature, attributeWildcardEStructuralFeature);
            eStructuralFeature.setDerived(true);
            eStructuralFeature.setTransient(true);
            eStructuralFeature.setVolatile(true);
          }
        }
      }
    }
    XSDWildcard xsdWildcard = xsdComplexTypeDefinition.getAttributeWildcard();
    if (((xsdWildcard != null) && (baseXSDWildcard != xsdWildcard)) || (XSDConstants.isURType(xsdComplexTypeDefinition))) {
      if ((!isRestriction) || (XSDConstants.isURType(xsdComplexTypeDefinition)))
      {
        String name = getEcoreAttribute(xsdWildcard, "name");
        if (name == null) {
          name = "anyAttribute";
        }
        createFeature(
          eClass, 
          name, 
          EcorePackage.Literals.EFEATURE_MAP_ENTRY, 
          xsdWildcard, 
          0, 
          -1);
      }
    }
    XSDSimpleTypeDefinition baseSimpleContentType;
    if (isRestriction)
    {
      int baseContentKind = extendedMetaData.getContentKind((EClass)eClass.getESuperTypes().get(0));
      if ((baseContentKind == 3) && 
        (xsdComplexTypeDefinition.getContentTypeCategory() == XSDContentTypeCategory.SIMPLE_LITERAL))
      {
        XSDSimpleTypeDefinition contentType = xsdComplexTypeDefinition.getSimpleType();
        extendedMetaData.setContentKind(eClass, 2);
        boolean isSimpleAnyType = ("SimpleAnyType".equals(eClass.getName())) && ("http://www.eclipse.org/emf/2003/XMLType".equals(eClass.getEPackage().getNsURI()));
        if (isSimpleAnyType)
        {
          EStructuralFeature eStructuralFeature = 
            createFeature(
            eClass, 
            "rawValue", 
            getEClassifier(xsdComplexTypeDefinition.getSchema().resolveTypeDefinition("string")), 
            null, 
            0, 
            1);
          eStructuralFeature.setDerived(true);
          eStructuralFeature.setTransient(true);
          eStructuralFeature.setVolatile(true);
        }
        baseSimpleContentType = contentType.getBaseTypeDefinition();
        EStructuralFeature eStructuralFeature = 
          createFeature(
          eClass, 
          "value", 
          getEClassifier(isSimpleAnyType ? xsdComplexTypeDefinition.getSchema().resolveTypeDefinition("anySimpleType") : baseSimpleContentType), 
          null, 
          0, 
          1);
        eStructuralFeature.setDerived(true);
        eStructuralFeature.setTransient(true);
        eStructuralFeature.setVolatile(true);
        if (isSimpleAnyType)
        {
          eStructuralFeature = 
            createFeature(
            eClass, 
            "instanceType", 
            EcorePackage.Literals.EDATA_TYPE, 
            null, 
            1, 
            1);
          
          ((EReference)eStructuralFeature).setResolveProxies(false);
        }
      }
      else
      {
        extendedMetaData.setContentKind(eClass, baseContentKind);
      }
    }
    XSDAnnotation xsdAnnotation = xsdComplexTypeDefinition.getAnnotation();
    if (xsdAnnotation != null)
    {
      List<Element> applicationInformationList = xsdAnnotation.getApplicationInformation("http://www.eclipse.org/emf/2002/Ecore");
      for (Element applicationInformation : applicationInformationList) {
        if ("operations".equals(applicationInformation.getAttributeNS("http://www.eclipse.org/emf/2002/Ecore", "key"))) {
          for (Element operation : getElements(applicationInformation, "operation"))
          {
            EOperation eOperation = EcoreFactory.eINSTANCE.createEOperation();
            String operationName = operation.getAttributeNS(null, "name");
            eOperation.setName(operationName);
            
            eClass.getEOperations().add(eOperation);
            ETypeParameter eTypeParameter;
            for (Element typeParameter : getElements(operation, "typeParameter"))
            {
              eTypeParameter = EcoreFactory.eINSTANCE.createETypeParameter();
              String typeParameterName = typeParameter.getAttributeNS(null, "name");
              eTypeParameter.setName(typeParameterName);
              eOperation.getETypeParameters().add(eTypeParameter);
              eTypeParameter.getEBounds().addAll(getGenericTypes(xsdComplexTypeDefinition, typeParameter, eOperation, typeParameter.getAttributeNS(null, "bounds")));
            }
            EGenericType returnType = getGenericType(xsdComplexTypeDefinition, operation, eOperation, operation.getAttributeNS(null, "type"));
            if (returnType != null) {
              eOperation.setEGenericType(returnType);
            }
            eOperation.getEGenericExceptions().addAll(getGenericTypes(xsdComplexTypeDefinition, operation, eOperation, operation.getAttributeNS(null, "exception")));
            eOperation.getEGenericExceptions().addAll(getGenericTypes(xsdComplexTypeDefinition, operation, eOperation, operation.getAttributeNS(null, "exceptions")));
            for (Element parameter : getElements(operation, "parameter"))
            {
              EParameter eParameter = EcoreFactory.eINSTANCE.createEParameter();
              String paramaterName = parameter.getAttributeNS(null, "name");
              eParameter.setName(paramaterName);
              
              EGenericType parameterType = getGenericType(xsdComplexTypeDefinition, operation, eOperation, parameter.getAttributeNS(null, "type"));
              eParameter.setEGenericType(parameterType);
              
              populateETypedElement(eParameter, parameter);
              eOperation.getEParameters().add(eParameter);
            }
            List<Element> body = getElements(operation, "body");
            if (!body.isEmpty()) {
              EcoreUtil.setAnnotation(eOperation, "http://www.eclipse.org/emf/2002/GenModel", "body", getText((Element)body.get(0)));
            }
            populateETypedElement(eOperation, operation);
          }
        }
      }
    }
    return eClass;
  }
  
  protected void populateETypedElement(ETypedElement eTypedElement, Element element)
  {
    String lowerBound = element.getAttributeNS(null, "lowerBound");
    if (!"".equals(lowerBound)) {
      eTypedElement.setLowerBound(Integer.parseInt(lowerBound));
    }
    String upperBound = element.getAttributeNS(null, "upperBound");
    if (!"".equals(upperBound)) {
      eTypedElement.setUpperBound(Integer.parseInt(upperBound));
    }
    if ("false".equals(element.getAttributeNS(null, "unique"))) {
      eTypedElement.setUnique(false);
    }
    if ("false".equals(element.getAttributeNS(null, "ordered"))) {
      eTypedElement.setOrdered(false);
    }
    for (Element annotation : getElements(element, "annotation"))
    {
      EAnnotation eAnnotation = EcoreFactory.eINSTANCE.createEAnnotation();
      if (annotation.hasAttributeNS(null, "source")) {
        eAnnotation.setSource(annotation.getAttributeNS(null, "source"));
      }
      for (Element detail : getElements(annotation, "detail")) {
        eAnnotation.getDetails().put(
          detail.hasAttributeNS(null, "key") ? detail.getAttributeNS(null, "key") : null, 
          getText(detail));
      }
      eTypedElement.getEAnnotations().add(eAnnotation);
    }
  }
  
  private String getText(Element element)
  {
    StringBuffer text = new StringBuffer();
    for (Node node = element.getFirstChild(); node != null; node = node.getNextSibling()) {
      switch (node.getNodeType())
      {
      case 3: 
      case 4: 
        text.append(node.getNodeValue());
      }
    }
    return text.toString();
  }
  
  private List<Element> getElements(Element element, String localName)
  {
    List<Element> result = new ArrayList();
    for (Node node = element.getFirstChild(); node != null; node = node.getNextSibling()) {
      if (node.getNodeType() == 1)
      {
        Element child = (Element)node;
        if ((localName.equals(child.getLocalName())) && (child.getNamespaceURI() == null)) {
          result.add(child);
        }
      }
    }
    return result;
  }
  
  p
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49

Further reading...

For more information on Java 1.5 Tiger, you may find Java 1.5 Tiger, A developer's Notebook by D. Flanagan and B. McLaughlin from O'Reilly of interest.

New!JAR listings


Copyright 2006-2019. Infinite Loop Ltd