You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by dk...@apache.org on 2006/08/28 20:54:46 UTC
svn commit: r437778 [14/42] - in /incubator/tuscany/java: ./ buildtools/
buildtools/src/main/resources/ das/ das/rdb/
das/rdb/src/main/java/org/apache/tuscany/das/rdb/
das/rdb/src/main/java/org/apache/tuscany/das/rdb/generator/impl/
das/rdb/src/main/ja...
Modified: incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/SchemaBuilder.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/SchemaBuilder.java?rev=437778&r1=437777&r2=437778&view=diff
==============================================================================
--- incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/SchemaBuilder.java (original)
+++ incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/SchemaBuilder.java Mon Aug 28 11:53:49 2006
@@ -1,767 +1,767 @@
-package org.apache.tuscany.sdo.helper;
-
-import java.util.Hashtable;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Locale;
-import java.util.Map;
-import java.util.Vector;
-
-import javax.xml.namespace.QName;
-
-import org.apache.tuscany.sdo.util.SDOUtil;
-import org.eclipse.xsd.XSDAttributeDeclaration;
-import org.eclipse.xsd.XSDAttributeUse;
-import org.eclipse.xsd.XSDComplexTypeDefinition;
-import org.eclipse.xsd.XSDCompositor;
-import org.eclipse.xsd.XSDConstraint;
-import org.eclipse.xsd.XSDDerivationMethod;
-import org.eclipse.xsd.XSDElementDeclaration;
-import org.eclipse.xsd.XSDFactory;
-import org.eclipse.xsd.XSDForm;
-import org.eclipse.xsd.XSDImport;
-import org.eclipse.xsd.XSDInclude;
-import org.eclipse.xsd.XSDModelGroup;
-import org.eclipse.xsd.XSDParticle;
-import org.eclipse.xsd.XSDProcessContents;
-import org.eclipse.xsd.XSDSchema;
-import org.eclipse.xsd.XSDSchemaContent;
-import org.eclipse.xsd.XSDSimpleTypeDefinition;
-import org.eclipse.xsd.XSDTypeDefinition;
-import org.eclipse.xsd.XSDWildcard;
-
-import commonj.sdo.Property;
-import commonj.sdo.Type;
-import commonj.sdo.helper.XSDHelper;
-
-public class SchemaBuilder extends SDOAnnotations
-{
- public static final String DEFAULT_SCHEMA_LOCATION = "";
- public static final String NAME_SPACE_PREFIX = "stn_";
- private static int prefixCount = 1;
-
- //public static final String MIXED = "mixed";
- //public static final String GROUP = "group";
- public static final String EFEATURE_MAP_ENTRY = "EFeatureMapEntry";
-
- private Map schemaMap = null;
- protected Map targetNamespacePrefixMap = new Hashtable();
- protected Map schemaLocationMap = null;
- protected TypeTable typeTable = null;
- protected XSDFactory xsdFactory = XSDFactory.eINSTANCE;
-
-
- protected SchemaBuilder(Map schemaMap,
- Map nsPrefixMap,
- TypeTable typeTable,
- Map schemaLocMap )
- {
- this.schemaMap = schemaMap;
- this.targetNamespacePrefixMap = nsPrefixMap;
- this.typeTable = typeTable;
- this.schemaLocationMap = schemaLocMap;
- }
-
-
-
-
- private QName addAttribute2ComplexType(String targetNamespace,
- XSDComplexTypeDefinition complexType,
- Property aProperty)
- {
- QName attributeSchemaType = null;
- String prefix = null;
-
- try
- {
- attributeSchemaType = buildSchema(aProperty.getType());
- }
- catch ( IllegalArgumentException e )
- {
- //schema cannot be generated for this type as there exists an xsd already
- //so include that original XSD
- attributeSchemaType = new QName(aProperty.getType().getURI(),
- aProperty.getType().getName(),
- generatePrefix());
- if ( aProperty.getType().isDataType() )
- {
- typeTable.addSimpleSchemaType(aProperty.getType().getName(), attributeSchemaType);
-
- XSDSimpleTypeDefinition simpleType = xsdFactory.createXSDSimpleTypeDefinition();
- simpleType.setName(aProperty.getType().getName());
- simpleType.setTargetNamespace(aProperty.getType().getURI());
- typeTable.addXSDTypeDef(attributeSchemaType.getNamespaceURI(),
- attributeSchemaType.getLocalPart(),
- simpleType);
- }
- else
- {
- typeTable.addComplexSchemaType(aProperty.getType().getURI(),
- aProperty.getType().getName(),
- attributeSchemaType);
-
- XSDComplexTypeDefinition extComplexType = xsdFactory.createXSDComplexTypeDefinition();
- extComplexType.setName(aProperty.getType().getName());
- extComplexType.setTargetNamespace(aProperty.getType().getURI());
- typeTable.addXSDTypeDef(attributeSchemaType.getNamespaceURI(),
- attributeSchemaType.getLocalPart(),
- extComplexType);
- }
- includeExtXSD(aProperty.getType());
- }
- //ensure than an import is done rightaway so that the right prefixes will be used by the
- //attribute whose type is set as 'this type'. Otherwise when setting the type for the attribute
- //there will be a duplicate prefix (like Q1 or Q2... ) that will be created
- prefix = addImports((XSDSchema)schemaMap.get(targetNamespace), attributeSchemaType);
-
- XSDAttributeDeclaration attribute = xsdFactory.createXSDAttributeDeclaration();
- attribute.setName(aProperty.getName());
- XSDAttributeUse orderDateAttributeUse = xsdFactory.createXSDAttributeUse();
- orderDateAttributeUse.setContent(attribute);
- complexType.getAttributeContents().add(orderDateAttributeUse);
- attribute.updateElement();
-
- if ( aProperty.getType().isDataType() )
- {
- attribute.setTypeDefinition((XSDSimpleTypeDefinition)typeTable.getXSDTypeDef(attributeSchemaType.getNamespaceURI(),
- attributeSchemaType.getLocalPart()));
-
- }
- else
- {
- attribute.setTypeDefinition((XSDSimpleTypeDefinition)typeTable.getXSDTypeDef(
- typeTable.getSimpleSchemaTypeName("URI").getNamespaceURI(),
- typeTable.getSimpleSchemaTypeName("URI").getLocalPart()));
-
- }
-
- if ( aProperty.getDefault() != null )
- {
- attribute.setConstraint(XSDConstraint.DEFAULT_LITERAL);
- attribute.setLexicalValue(aProperty.getDefault().toString());
- }
-
- addAnnotations(attribute, aProperty );
- if ( !aProperty.getType().isDataType() )
- {
- String value = prefix + COLON + attributeSchemaType.getLocalPart();
- attribute.getElement().setAttribute(PROPERTY_TYPE, value);
- }
-
- return attributeSchemaType;
- }
-
- private QName addElement2ComplexType(String targetNamespace,
- XSDComplexTypeDefinition complexType,
- Property aProperty)
- {
- String prefix = null;
- QName elementSchemaType = null;
- try
- {
- elementSchemaType = buildSchema(aProperty.getType());
- }
- catch ( IllegalArgumentException e )
- {
- //schema cannot be generated for this type as there exists an xsd already
- //so include that original XSD
- elementSchemaType = new QName(aProperty.getType().getURI(),
- aProperty.getType().getName(),
- generatePrefix());
- if ( aProperty.getType().isDataType() )
- {
- typeTable.addSimpleSchemaType(aProperty.getType().getName(), elementSchemaType);
-
- XSDSimpleTypeDefinition simpleType = xsdFactory.createXSDSimpleTypeDefinition();
- simpleType.setName(aProperty.getType().getName());
- simpleType.setTargetNamespace(aProperty.getType().getURI());
- typeTable.addXSDTypeDef(elementSchemaType.getNamespaceURI(),
- elementSchemaType.getLocalPart(),
- simpleType);
- }
- else
- {
- typeTable.addComplexSchemaType(aProperty.getType().getURI(),
- aProperty.getType().getName(),
- elementSchemaType);
- XSDComplexTypeDefinition extComplexType = xsdFactory.createXSDComplexTypeDefinition();
- extComplexType.setName(aProperty.getType().getName());
- extComplexType.setTargetNamespace(aProperty.getType().getURI());
- typeTable.addXSDTypeDef(elementSchemaType.getNamespaceURI(),
- elementSchemaType.getLocalPart(),
- extComplexType);
- }
- includeExtXSD(aProperty.getType());
- }
-
- //ensure than an import is done rightaway so that the right prefixes will be used by the
- //element whose type is set as 'this type'. Otherwise when setting the type for the element
- //there will be a duplicate prefix (like Q1 or Q2... ) that will be created
- prefix = addImports((XSDSchema)schemaMap.get(targetNamespace), elementSchemaType);
-
- //XmlSchemaElement element = new XmlSchemaElement();
- XSDElementDeclaration element = xsdFactory.createXSDElementDeclaration();
- element.setName(aProperty.getName());
-
- XSDParticle aParticle = xsdFactory.createXSDParticle();
- aParticle.setContent(element);
-
- ((XSDModelGroup)((XSDParticle)complexType.getContent()).getContent()).
- getContents().add(aParticle);
-
- element.updateElement();
-
- if ( aProperty.isMany() )
- {
- aParticle.setMaxOccurs(-1);
- aParticle.setMinOccurs(0);
-
- }
-
- if ( aProperty.isContainment() )
- {
- element.setTypeDefinition(typeTable.getXSDTypeDef(elementSchemaType.getNamespaceURI(),
- elementSchemaType.getLocalPart()));
- }
- else
- {
- if ( !aProperty.getType().isDataType() )
- {
- QName qName = typeTable.getSimpleSchemaTypeName("URI");
- element.setTypeDefinition(typeTable.getXSDTypeDef(qName.getNamespaceURI(),
- qName.getLocalPart()));
- }
- }
-
- addAnnotations(element, aProperty);
- if ( !aProperty.isContainment() && !aProperty.getType().isDataType() )
- {
- String value = prefix + COLON + elementSchemaType.getLocalPart();
- element.getElement().setAttribute(PROPERTY_TYPE, value);
- }
- return elementSchemaType;
-
- }
-
- private void addAnnotations(XSDSchemaContent xsdContent, Property aProperty)
- {
- if ( !aProperty.getAliasNames().isEmpty() )
- {
- addAliasNamesAnnotation(xsdContent, aProperty.getAliasNames());
- }
-
- if ( aProperty.isReadOnly() )
- {
- xsdContent.getElement().setAttribute(READ_ONLY, Boolean.toString(aProperty.isReadOnly()));
- }
-
- if ( aProperty.getOpposite() != null )
- {
- xsdContent.getElement().setAttribute(OPPOSITE_PROPERTY, aProperty.getOpposite().getName());
- }
- }
-
-
- private QName buildComplexSchemaTypeContents(String targetNamespace,
- XSDComplexTypeDefinition complexType,
- Type dataType)
- {
- //clipProperties(dataType);
- List properties = dataType.getDeclaredProperties();
- Iterator iterator = properties.iterator();
- Property aProperty;
- QName propertySchemaTypeName = null;
-
- while ( iterator.hasNext() )
- {
- aProperty = (Property)iterator.next();
- if ( aProperty.isContainment() || aProperty.isMany() || !aProperty.getType().isDataType() )
- {
- propertySchemaTypeName = addElement2ComplexType(targetNamespace, complexType, aProperty);
- }
- else
- {
- propertySchemaTypeName = addAttribute2ComplexType(targetNamespace, complexType, aProperty);
- }
-
- /*if ( !EFEATURE_MAP_ENTRY.equals(aProperty.getType().getName()) )
- {
- addContents2ComplexType(targetNamespace, complexType, aProperty);
- }*/
- }
-
- return propertySchemaTypeName;
-
- }
-
- public QName buildComplexSchemaType(Type dataType)
- {
- //this is called from buildSchema only if isXSD(dataType) is false
- QName complexSchemaTypeName = null ;
- if ( !dataType.isDataType() &&
- (complexSchemaTypeName = typeTable.getComplexSchemaTypeName(dataType.getURI(), dataType.getName())) == null )
- {
- XSDSchema xmlSchema = getXmlSchema(dataType);
- String targetNamespace = dataType.getURI();
- String targetNamespacePrefix = (String)targetNamespacePrefixMap.get(targetNamespace);
-
- complexSchemaTypeName = new QName(targetNamespace,
- dataType.getName(),
- targetNamespacePrefix);
-
- XSDComplexTypeDefinition complexType = xsdFactory.createXSDComplexTypeDefinition();
- complexType.setName(dataType.getName());
- complexType.setTargetNamespace(targetNamespace);
- complexType.setAbstract(dataType.isAbstract());
-
- xmlSchema.getTypeDefinitions().add(complexType);
- xmlSchema.getContents().add(complexType);
-
- complexType.updateElement();
-
- addAnnotations(complexType, dataType);
-
- handleBaseExtn(xmlSchema, dataType, complexType);
- handleSDOSequence(dataType, complexType);
- handleSDOOpenType(dataType, complexType);
-
- //add before constructing the contents because a content element could
- //possibly be of type 'complexType'.
- typeTable.addComplexSchemaType(dataType.getURI(), dataType.getName(), complexSchemaTypeName);
- typeTable.addXSDTypeDef(dataType.getURI(), dataType.getName(), complexType);
-
- //now compose the contents for this complex type
- buildComplexSchemaTypeContents(targetNamespace, complexType, dataType);
-
- //finally create a global element for this type
- createGlobalElement(xmlSchema, complexType, complexSchemaTypeName);
- }
-
- return complexSchemaTypeName;
- }
-
- private void addAnnotations(XSDTypeDefinition xsdType, Type dataType)
- {
- if ( dataType.isAbstract() )
- {
- if ( xsdType instanceof XSDComplexTypeDefinition )
- {
- ((XSDComplexTypeDefinition)xsdType).setAbstract(dataType.isAbstract());
- }
- else
- {
- xsdType.getElement().setAttribute(ABSTRACT_TYPE,
- Boolean.toString(dataType.isAbstract()));
- }
- }
-
- //add alias names if it exists
- addAliasNamesAnnotation(xsdType,
- dataType.getAliasNames());
-
- //add instanceClass annotation
- if ( dataType.getInstanceClass() != null )
- {
- xsdType.getElement().setAttribute(INSTANCE_CLASS, dataType.getInstanceClass().getName());
- }
- }
-
-
- private QName buildSimpleSchemaType(Type dataType)
- {
- QName simpleSchemaTypeName = null;
- if ( dataType.isDataType() &&
- (simpleSchemaTypeName = typeTable.getSimpleSchemaTypeName(dataType.getName()) ) == null )
- {
- XSDSchema xmlSchema = getXmlSchema(dataType);
- XSDSimpleTypeDefinition simpleType = xsdFactory.createXSDSimpleTypeDefinition();
- //set the name
- simpleType.setName(dataType.getName());
- simpleType.setTargetNamespace(dataType.getURI());
- //set abstract=true if abstract
- simpleSchemaTypeName = new QName(dataType.getURI(),
- dataType.getName(),
- (String)targetNamespacePrefixMap.get(dataType.getURI()));
- xmlSchema.getContents().add(simpleType);
- simpleType.updateElement();
-
- addAnnotations(simpleType, dataType);
-
- if ( !dataType.getBaseTypes().isEmpty() )
- {
- Type baseType = (Type)dataType.getBaseTypes().get(0);
-
- QName baseSchemaType = null;
-
- try
- {
- baseSchemaType = buildSchema(baseType);
- }
- catch ( IllegalArgumentException e )
- {
- //this means that the base type comes from a original xsd and hence not generated
- baseSchemaType = new QName(baseType.getURI(),
- baseType.getName(),
- generatePrefix());
-
- typeTable.addSimpleSchemaType(baseType.getName(), baseSchemaType);
-
- XSDSimpleTypeDefinition baseTypeDef = xsdFactory.createXSDSimpleTypeDefinition();
- baseTypeDef.setName(baseType.getName());
- baseTypeDef.setTargetNamespace(baseType.getURI());
- typeTable.addXSDTypeDef(baseType.getURI(), baseType.getName(), baseTypeDef);
-
- //include external XSD locations
- includeExtXSD(baseType);
- }
-
- simpleType.setBaseTypeDefinition((XSDSimpleTypeDefinition)typeTable.
- getXSDTypeDef(baseSchemaType.getNamespaceURI(),baseSchemaType.getLocalPart()));
- addImports(xmlSchema, baseSchemaType);
- }
-
-
- typeTable.addSimpleSchemaType(dataType.getName(), simpleSchemaTypeName);
- typeTable.addXSDTypeDef(dataType.getURI(), dataType.getName(), simpleType);
- }
- return simpleSchemaTypeName;
- }
-
- private void includeExtXSD(Type dataType)
- {
- //now we know there is a type for which the xsd must come from outside
- //create a schema for the namespace of this type and add an include in it for
- //the xsd that is defined externally
- XSDSchema xmlSchema = getXmlSchema(dataType);
-
- //ideally there could be more than one external schema defintions for a namespace
- //and hence schemalocations will be a list of locations
- //List schemaLocations = (List)schemaLocationMap.get(dataType.getURI());
-
- //since as per the specs the input to XSDHelper is a map of <String, String> allowing
- //only one schemalocation for a namespace. So for now this single location will be
- //picked up and put into a list
- List schemaLocations = new Vector();
- if ( schemaLocationMap.get(dataType.getURI()) != null )
- {
- schemaLocations.add(schemaLocationMap.get(dataType.getURI()));
- }
-
- if ( schemaLocations.size() <= 0 )
- {
- schemaLocations.add(DEFAULT_SCHEMA_LOCATION);
- }
-
- Object schemaContent = null;
- Iterator includesIterator = xmlSchema.getContents().iterator();
- Iterator schemaLocIterator = schemaLocations.iterator();
- String aSchemaLocation = null;
- boolean includeExists = false;
- //include all external schema locations
- while ( schemaLocIterator.hasNext() )
- {
- aSchemaLocation = (String)schemaLocIterator.next();
- while ( includesIterator.hasNext() )
- {
- schemaContent = includesIterator.next();
- if ( schemaContent instanceof XSDInclude )
- {
- if ( !includeExists && aSchemaLocation.equals(
- ((XSDInclude)schemaContent).getSchemaLocation()
- ))
- {
- includeExists = true;
- }
- }
- }
-
- if ( !includeExists )
- {
- XSDInclude includeElement = xsdFactory.createXSDInclude();
- includeElement.setSchemaLocation(aSchemaLocation);
- xmlSchema.getContents().add(0, includeElement);
- }
- }
- }
-
- protected QName buildSchema(Type dataType) throws IllegalArgumentException
- {
- QName schemaTypeName = null;
-
- if ( dataType.isDataType() )
- {
- schemaTypeName = typeTable.getSimpleSchemaTypeName(dataType.getName());
- }
- else
- {
- schemaTypeName = typeTable.getComplexSchemaTypeName(dataType.getURI(), dataType.getName());
- }
-
- //attempt to generate only if we have not done it already..i.e the type is
- //not found in the typetable
- if ( schemaTypeName == null )
- {
- XSDHelper xsdHelper = SDOUtil.createXSDHelper(SDOUtil.createTypeHelper());
-
- if ( !xsdHelper.isXSD( dataType ) )
- {
- if ( dataType.isDataType() )
- {
- schemaTypeName = buildSimpleSchemaType(dataType);
- }
- else
- {
- schemaTypeName = buildComplexSchemaType(dataType);
- }
- }
- else
- {
- throw new IllegalArgumentException("Cannot generate XSD since SDO Type '" +
- dataType.getName() + "' was orginally generated from XSD. Use original XSD");
- }
- }
- return schemaTypeName;
- }
-
-
- private XSDSchema getXmlSchema(Type dataType)
- {
- XSDSchema xmlSchema = null;
-
- if ((xmlSchema = (XSDSchema) schemaMap.get(dataType.getURI())) == null)
- {
- String targetNamespacePrefix = generatePrefix();
-
- xmlSchema = xsdFactory.createXSDSchema();
- xmlSchema.setTargetNamespace(dataType.getURI());
- xmlSchema.setAttributeFormDefault(XSDForm.QUALIFIED_LITERAL);
- xmlSchema.setElementFormDefault(XSDForm.QUALIFIED_LITERAL);
-
- targetNamespacePrefixMap.put(dataType.getURI(), targetNamespacePrefix);
- schemaMap.put(dataType.getURI(), xmlSchema);
-
- xmlSchema.getQNamePrefixToNamespaceMap().put(TypeTable.XS_URI_PREFIX, TypeTable.XML_SCHEMA_URI);
- xmlSchema.setSchemaForSchemaQNamePrefix(TypeTable.XS_URI_PREFIX);
-
- xmlSchema.getQNamePrefixToNamespaceMap().put(targetNamespacePrefix, dataType.getURI());
- //xmlSchema.setSchemaForSchemaQNamePrefix(targetNamespacePrefix);
-
- addSDONamespaces(xmlSchema);
- addPackageAnnotation(xmlSchema, dataType);
- }
- return xmlSchema;
- }
-
-
- private void addSDONamespaces(XSDSchema xmlSchema)
- {
- xmlSchema.getQNamePrefixToNamespaceMap().put(COMMONJ_SDO_NS_PREFIX, COMMONJ_SDO_NS);
- //xmlSchema.setSchemaForSchemaQNamePrefix(COMMONJ_SDO_NS_PREFIX);
-
- xmlSchema.getQNamePrefixToNamespaceMap().put(SDO_JAVA_NS_PREFIX, SDO_JAVA_NS);
- //xmlSchema.setSchemaForSchemaQNamePrefix(SDO_JAVA_NS_PREFIX);
- }
-
-
- /**
- * JAM convert first name of an attribute into UpperCase as an example if
- * there is a instance variable called foo in a bean , then Jam give that as
- * Foo so this method is to correct that error
- *
- * @param wrongName
- * @return the right name, using english as the locale for case conversion
- */
- public static String getCorrectName(String wrongName)
- {
- if (wrongName.length() > 1) {
- return wrongName.substring(0, 1).toLowerCase(Locale.ENGLISH)
- + wrongName.substring(1, wrongName.length());
- } else {
- return wrongName.substring(0, 1).toLowerCase(Locale.ENGLISH);
- }
- }
-
- private String addImports(XSDSchema xmlSchema, QName schemaTypeName)
- {
- String prefix = null;
- Iterator iterator = xmlSchema.getQNamePrefixToNamespaceMap().keySet().iterator();
- while ( iterator.hasNext() )
- {
- prefix = (String)iterator.next();
-
- if ( schemaTypeName.getNamespaceURI().equals(
- xmlSchema.getQNamePrefixToNamespaceMap().get(prefix)) )
- {
- return prefix;
- }
- }
-
- //the following lines are executed only if a prefix was not found which implies that the
- //schemaTypeName was not imported earlier and also it does not belong to the targetnamespace
- XSDImport importElement = xsdFactory.createXSDImport();
- importElement.setNamespace(schemaTypeName.getNamespaceURI());
- xmlSchema.getContents().add(0, importElement);
- prefix = schemaTypeName.getPrefix();
- if ( prefix == null || prefix.length() <= 0 )
- {
- prefix = generatePrefix();
- }
- xmlSchema.getQNamePrefixToNamespaceMap().put(prefix, schemaTypeName.getNamespaceURI());
-
- return prefix;
- }
-
- private void handleSDOSequence(Type datatype, XSDComplexTypeDefinition complexType)
- {
- if ( datatype.isSequenced() )
- {
- complexType.setMixed(true);
- XSDModelGroup choice = xsdFactory.createXSDModelGroup();
- choice.setCompositor(XSDCompositor.CHOICE_LITERAL);
- XSDParticle aParticle = xsdFactory.createXSDParticle();
- aParticle.setContent(choice);
- aParticle.setMaxOccurs(-1);
- complexType.setContent(aParticle);
- }
- else
- {
- //hack to handle group property as choice
- /*if ( getPropertyStartsWithName(datatype.getDeclaredProperties(), GROUP).size() > 0 )
- {
- XmlSchemaChoice choice = new XmlSchemaChoice();
- choice.setMaxOccurs(Long.MAX_VALUE);
- complexType.setParticle(choice);
- }
- else*/
- {
- XSDModelGroup sequence = xsdFactory.createXSDModelGroup();
- sequence.setCompositor(XSDCompositor.SEQUENCE_LITERAL);
- XSDParticle aParticle = xsdFactory.createXSDParticle();
- aParticle.setContent(sequence);
- complexType.setContent(aParticle);
- }
- }
- }
-
- private void handleSDOOpenType(Type datatype, XSDComplexTypeDefinition complexType)
- {
- if ( datatype.isOpen() /*&&
- getPropertyStartsWithName(datatype.getDeclaredProperties(), GROUP).size() <= 0 */)
- {
- XSDWildcard elementWildcard = xsdFactory.createXSDWildcard();
- elementWildcard.getLexicalNamespaceConstraint().add("##other");
- elementWildcard.setProcessContents(XSDProcessContents.LAX_LITERAL);
- // Create a particle to hold the wildcard.
- XSDParticle wildcardParticle = xsdFactory.createXSDParticle();
- wildcardParticle.setContent(elementWildcard);
- wildcardParticle.setMaxOccurs(-1);
- ((XSDModelGroup)((XSDParticle)complexType.getContent()).getContent()).
- getContents().add(wildcardParticle);
-
- XSDWildcard attributeWildcard = xsdFactory.createXSDWildcard();
- attributeWildcard.getLexicalNamespaceConstraint().add("##other");
- attributeWildcard.setProcessContents(XSDProcessContents.LAX_LITERAL);
- complexType.setAttributeWildcard(attributeWildcard);
- }
- }
-
- private void handleBaseExtn(XSDSchema xmlSchema,
- Type datatype,
- XSDComplexTypeDefinition complexType)
- {
- if ( datatype.getBaseTypes().size() > 0 )
- {
- Type baseType = (Type)datatype.getBaseTypes().get(0);
- QName baseSchemaType = null;
-
- try
- {
- baseSchemaType = buildSchema(baseType);
- }
- catch ( IllegalArgumentException e )
- {
- //schema cannot be generated for this type as there exists an xsd already
- //so include that original XSD
- baseSchemaType = new QName(baseType.getURI(),
- baseType.getName(),
- generatePrefix());
-
- XSDSimpleTypeDefinition baseTypeDef = xsdFactory.createXSDSimpleTypeDefinition();
- baseTypeDef.setName(baseType.getName());
- baseTypeDef.setTargetNamespace(baseType.getURI());
- typeTable.addXSDTypeDef(baseType.getURI(), baseType.getName(), baseTypeDef);
-
- includeExtXSD(baseType);
- }
-
- complexType.setDerivationMethod(XSDDerivationMethod.EXTENSION_LITERAL);
-
- if ( baseType.isDataType() )
- {
- XSDSimpleTypeDefinition anonymousSimpleTypeDefinition
- = xsdFactory.createXSDSimpleTypeDefinition();
- anonymousSimpleTypeDefinition.setBaseTypeDefinition((XSDSimpleTypeDefinition)typeTable.
- getXSDTypeDef(baseSchemaType.getNamespaceURI(),baseSchemaType.getLocalPart()));
- complexType.setContent(anonymousSimpleTypeDefinition);
- }
- else
- {
- complexType.setBaseTypeDefinition((XSDSimpleTypeDefinition)typeTable.
- getXSDTypeDef(baseSchemaType.getNamespaceURI(),baseSchemaType.getLocalPart()));
-
- }
-
- addImports(xmlSchema, baseSchemaType);
- }
- }
-
- private String formGlobalElementName(String typeName)
- {
- String firstChar = typeName.substring(0,1);
- return typeName.replaceFirst(firstChar, firstChar.toLowerCase());
- }
-
- private void createGlobalElement(XSDSchema xmlSchema,
- XSDComplexTypeDefinition complexType,
- QName schemaElementName )
- {
- XSDElementDeclaration globalElement = xsdFactory.createXSDElementDeclaration();
- globalElement.setTargetNamespace(xmlSchema.getTargetNamespace());
- globalElement.setName(formGlobalElementName(complexType.getName()));
- globalElement.setTypeDefinition
- (typeTable.getXSDTypeDef(schemaElementName.getNamespaceURI(),
- schemaElementName.getLocalPart()));
- xmlSchema.getContents().add(globalElement);
- xmlSchema.getElementDeclarations().add(globalElement);
- }
-
- private void addAliasNamesAnnotation(XSDSchemaContent typeDef,
- List aliasNames)
- {
- if ( !aliasNames.isEmpty() )
- {
- StringBuffer sb = new StringBuffer();
- Iterator iterator = aliasNames.iterator();
- while ( iterator.hasNext() )
- {
- sb.append(iterator.next());
- }
- typeDef.getElement().setAttribute(ALIAS_NAMES, sb.toString());
- }
- }
-
- private void addPackageAnnotation(XSDSchema xmlSchema, Type dataType)
- {
- if ( dataType.getInstanceClass() != null )
- {
- xmlSchema.updateElement();
- xmlSchema.getElement().setAttribute(JAVA_PACKAGE,
- dataType.getInstanceClass().getPackage().getName());
- }
- }
-
- private String generatePrefix()
- {
- return NAME_SPACE_PREFIX + prefixCount++;
- }
-}
+package org.apache.tuscany.sdo.helper;
+
+import java.util.Hashtable;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.Vector;
+
+import javax.xml.namespace.QName;
+
+import org.apache.tuscany.sdo.util.SDOUtil;
+import org.eclipse.xsd.XSDAttributeDeclaration;
+import org.eclipse.xsd.XSDAttributeUse;
+import org.eclipse.xsd.XSDComplexTypeDefinition;
+import org.eclipse.xsd.XSDCompositor;
+import org.eclipse.xsd.XSDConstraint;
+import org.eclipse.xsd.XSDDerivationMethod;
+import org.eclipse.xsd.XSDElementDeclaration;
+import org.eclipse.xsd.XSDFactory;
+import org.eclipse.xsd.XSDForm;
+import org.eclipse.xsd.XSDImport;
+import org.eclipse.xsd.XSDInclude;
+import org.eclipse.xsd.XSDModelGroup;
+import org.eclipse.xsd.XSDParticle;
+import org.eclipse.xsd.XSDProcessContents;
+import org.eclipse.xsd.XSDSchema;
+import org.eclipse.xsd.XSDSchemaContent;
+import org.eclipse.xsd.XSDSimpleTypeDefinition;
+import org.eclipse.xsd.XSDTypeDefinition;
+import org.eclipse.xsd.XSDWildcard;
+
+import commonj.sdo.Property;
+import commonj.sdo.Type;
+import commonj.sdo.helper.XSDHelper;
+
+public class SchemaBuilder extends SDOAnnotations
+{
+ public static final String DEFAULT_SCHEMA_LOCATION = "";
+ public static final String NAME_SPACE_PREFIX = "stn_";
+ private static int prefixCount = 1;
+
+ //public static final String MIXED = "mixed";
+ //public static final String GROUP = "group";
+ public static final String EFEATURE_MAP_ENTRY = "EFeatureMapEntry";
+
+ private Map schemaMap = null;
+ protected Map targetNamespacePrefixMap = new Hashtable();
+ protected Map schemaLocationMap = null;
+ protected TypeTable typeTable = null;
+ protected XSDFactory xsdFactory = XSDFactory.eINSTANCE;
+
+
+ protected SchemaBuilder(Map schemaMap,
+ Map nsPrefixMap,
+ TypeTable typeTable,
+ Map schemaLocMap )
+ {
+ this.schemaMap = schemaMap;
+ this.targetNamespacePrefixMap = nsPrefixMap;
+ this.typeTable = typeTable;
+ this.schemaLocationMap = schemaLocMap;
+ }
+
+
+
+
+ private QName addAttribute2ComplexType(String targetNamespace,
+ XSDComplexTypeDefinition complexType,
+ Property aProperty)
+ {
+ QName attributeSchemaType = null;
+ String prefix = null;
+
+ try
+ {
+ attributeSchemaType = buildSchema(aProperty.getType());
+ }
+ catch ( IllegalArgumentException e )
+ {
+ //schema cannot be generated for this type as there exists an xsd already
+ //so include that original XSD
+ attributeSchemaType = new QName(aProperty.getType().getURI(),
+ aProperty.getType().getName(),
+ generatePrefix());
+ if ( aProperty.getType().isDataType() )
+ {
+ typeTable.addSimpleSchemaType(aProperty.getType().getName(), attributeSchemaType);
+
+ XSDSimpleTypeDefinition simpleType = xsdFactory.createXSDSimpleTypeDefinition();
+ simpleType.setName(aProperty.getType().getName());
+ simpleType.setTargetNamespace(aProperty.getType().getURI());
+ typeTable.addXSDTypeDef(attributeSchemaType.getNamespaceURI(),
+ attributeSchemaType.getLocalPart(),
+ simpleType);
+ }
+ else
+ {
+ typeTable.addComplexSchemaType(aProperty.getType().getURI(),
+ aProperty.getType().getName(),
+ attributeSchemaType);
+
+ XSDComplexTypeDefinition extComplexType = xsdFactory.createXSDComplexTypeDefinition();
+ extComplexType.setName(aProperty.getType().getName());
+ extComplexType.setTargetNamespace(aProperty.getType().getURI());
+ typeTable.addXSDTypeDef(attributeSchemaType.getNamespaceURI(),
+ attributeSchemaType.getLocalPart(),
+ extComplexType);
+ }
+ includeExtXSD(aProperty.getType());
+ }
+ //ensure than an import is done rightaway so that the right prefixes will be used by the
+ //attribute whose type is set as 'this type'. Otherwise when setting the type for the attribute
+ //there will be a duplicate prefix (like Q1 or Q2... ) that will be created
+ prefix = addImports((XSDSchema)schemaMap.get(targetNamespace), attributeSchemaType);
+
+ XSDAttributeDeclaration attribute = xsdFactory.createXSDAttributeDeclaration();
+ attribute.setName(aProperty.getName());
+ XSDAttributeUse orderDateAttributeUse = xsdFactory.createXSDAttributeUse();
+ orderDateAttributeUse.setContent(attribute);
+ complexType.getAttributeContents().add(orderDateAttributeUse);
+ attribute.updateElement();
+
+ if ( aProperty.getType().isDataType() )
+ {
+ attribute.setTypeDefinition((XSDSimpleTypeDefinition)typeTable.getXSDTypeDef(attributeSchemaType.getNamespaceURI(),
+ attributeSchemaType.getLocalPart()));
+
+ }
+ else
+ {
+ attribute.setTypeDefinition((XSDSimpleTypeDefinition)typeTable.getXSDTypeDef(
+ typeTable.getSimpleSchemaTypeName("URI").getNamespaceURI(),
+ typeTable.getSimpleSchemaTypeName("URI").getLocalPart()));
+
+ }
+
+ if ( aProperty.getDefault() != null )
+ {
+ attribute.setConstraint(XSDConstraint.DEFAULT_LITERAL);
+ attribute.setLexicalValue(aProperty.getDefault().toString());
+ }
+
+ addAnnotations(attribute, aProperty );
+ if ( !aProperty.getType().isDataType() )
+ {
+ String value = prefix + COLON + attributeSchemaType.getLocalPart();
+ attribute.getElement().setAttribute(PROPERTY_TYPE, value);
+ }
+
+ return attributeSchemaType;
+ }
+
+ private QName addElement2ComplexType(String targetNamespace,
+ XSDComplexTypeDefinition complexType,
+ Property aProperty)
+ {
+ String prefix = null;
+ QName elementSchemaType = null;
+ try
+ {
+ elementSchemaType = buildSchema(aProperty.getType());
+ }
+ catch ( IllegalArgumentException e )
+ {
+ //schema cannot be generated for this type as there exists an xsd already
+ //so include that original XSD
+ elementSchemaType = new QName(aProperty.getType().getURI(),
+ aProperty.getType().getName(),
+ generatePrefix());
+ if ( aProperty.getType().isDataType() )
+ {
+ typeTable.addSimpleSchemaType(aProperty.getType().getName(), elementSchemaType);
+
+ XSDSimpleTypeDefinition simpleType = xsdFactory.createXSDSimpleTypeDefinition();
+ simpleType.setName(aProperty.getType().getName());
+ simpleType.setTargetNamespace(aProperty.getType().getURI());
+ typeTable.addXSDTypeDef(elementSchemaType.getNamespaceURI(),
+ elementSchemaType.getLocalPart(),
+ simpleType);
+ }
+ else
+ {
+ typeTable.addComplexSchemaType(aProperty.getType().getURI(),
+ aProperty.getType().getName(),
+ elementSchemaType);
+ XSDComplexTypeDefinition extComplexType = xsdFactory.createXSDComplexTypeDefinition();
+ extComplexType.setName(aProperty.getType().getName());
+ extComplexType.setTargetNamespace(aProperty.getType().getURI());
+ typeTable.addXSDTypeDef(elementSchemaType.getNamespaceURI(),
+ elementSchemaType.getLocalPart(),
+ extComplexType);
+ }
+ includeExtXSD(aProperty.getType());
+ }
+
+ //ensure than an import is done rightaway so that the right prefixes will be used by the
+ //element whose type is set as 'this type'. Otherwise when setting the type for the element
+ //there will be a duplicate prefix (like Q1 or Q2... ) that will be created
+ prefix = addImports((XSDSchema)schemaMap.get(targetNamespace), elementSchemaType);
+
+ //XmlSchemaElement element = new XmlSchemaElement();
+ XSDElementDeclaration element = xsdFactory.createXSDElementDeclaration();
+ element.setName(aProperty.getName());
+
+ XSDParticle aParticle = xsdFactory.createXSDParticle();
+ aParticle.setContent(element);
+
+ ((XSDModelGroup)((XSDParticle)complexType.getContent()).getContent()).
+ getContents().add(aParticle);
+
+ element.updateElement();
+
+ if ( aProperty.isMany() )
+ {
+ aParticle.setMaxOccurs(-1);
+ aParticle.setMinOccurs(0);
+
+ }
+
+ if ( aProperty.isContainment() )
+ {
+ element.setTypeDefinition(typeTable.getXSDTypeDef(elementSchemaType.getNamespaceURI(),
+ elementSchemaType.getLocalPart()));
+ }
+ else
+ {
+ if ( !aProperty.getType().isDataType() )
+ {
+ QName qName = typeTable.getSimpleSchemaTypeName("URI");
+ element.setTypeDefinition(typeTable.getXSDTypeDef(qName.getNamespaceURI(),
+ qName.getLocalPart()));
+ }
+ }
+
+ addAnnotations(element, aProperty);
+ if ( !aProperty.isContainment() && !aProperty.getType().isDataType() )
+ {
+ String value = prefix + COLON + elementSchemaType.getLocalPart();
+ element.getElement().setAttribute(PROPERTY_TYPE, value);
+ }
+ return elementSchemaType;
+
+ }
+
+ private void addAnnotations(XSDSchemaContent xsdContent, Property aProperty)
+ {
+ if ( !aProperty.getAliasNames().isEmpty() )
+ {
+ addAliasNamesAnnotation(xsdContent, aProperty.getAliasNames());
+ }
+
+ if ( aProperty.isReadOnly() )
+ {
+ xsdContent.getElement().setAttribute(READ_ONLY, Boolean.toString(aProperty.isReadOnly()));
+ }
+
+ if ( aProperty.getOpposite() != null )
+ {
+ xsdContent.getElement().setAttribute(OPPOSITE_PROPERTY, aProperty.getOpposite().getName());
+ }
+ }
+
+
+ private QName buildComplexSchemaTypeContents(String targetNamespace,
+ XSDComplexTypeDefinition complexType,
+ Type dataType)
+ {
+ //clipProperties(dataType);
+ List properties = dataType.getDeclaredProperties();
+ Iterator iterator = properties.iterator();
+ Property aProperty;
+ QName propertySchemaTypeName = null;
+
+ while ( iterator.hasNext() )
+ {
+ aProperty = (Property)iterator.next();
+ if ( aProperty.isContainment() || aProperty.isMany() || !aProperty.getType().isDataType() )
+ {
+ propertySchemaTypeName = addElement2ComplexType(targetNamespace, complexType, aProperty);
+ }
+ else
+ {
+ propertySchemaTypeName = addAttribute2ComplexType(targetNamespace, complexType, aProperty);
+ }
+
+ /*if ( !EFEATURE_MAP_ENTRY.equals(aProperty.getType().getName()) )
+ {
+ addContents2ComplexType(targetNamespace, complexType, aProperty);
+ }*/
+ }
+
+ return propertySchemaTypeName;
+
+ }
+
+ public QName buildComplexSchemaType(Type dataType)
+ {
+ //this is called from buildSchema only if isXSD(dataType) is false
+ QName complexSchemaTypeName = null ;
+ if ( !dataType.isDataType() &&
+ (complexSchemaTypeName = typeTable.getComplexSchemaTypeName(dataType.getURI(), dataType.getName())) == null )
+ {
+ XSDSchema xmlSchema = getXmlSchema(dataType);
+ String targetNamespace = dataType.getURI();
+ String targetNamespacePrefix = (String)targetNamespacePrefixMap.get(targetNamespace);
+
+ complexSchemaTypeName = new QName(targetNamespace,
+ dataType.getName(),
+ targetNamespacePrefix);
+
+ XSDComplexTypeDefinition complexType = xsdFactory.createXSDComplexTypeDefinition();
+ complexType.setName(dataType.getName());
+ complexType.setTargetNamespace(targetNamespace);
+ complexType.setAbstract(dataType.isAbstract());
+
+ xmlSchema.getTypeDefinitions().add(complexType);
+ xmlSchema.getContents().add(complexType);
+
+ complexType.updateElement();
+
+ addAnnotations(complexType, dataType);
+
+ handleBaseExtn(xmlSchema, dataType, complexType);
+ handleSDOSequence(dataType, complexType);
+ handleSDOOpenType(dataType, complexType);
+
+ //add before constructing the contents because a content element could
+ //possibly be of type 'complexType'.
+ typeTable.addComplexSchemaType(dataType.getURI(), dataType.getName(), complexSchemaTypeName);
+ typeTable.addXSDTypeDef(dataType.getURI(), dataType.getName(), complexType);
+
+ //now compose the contents for this complex type
+ buildComplexSchemaTypeContents(targetNamespace, complexType, dataType);
+
+ //finally create a global element for this type
+ createGlobalElement(xmlSchema, complexType, complexSchemaTypeName);
+ }
+
+ return complexSchemaTypeName;
+ }
+
+ private void addAnnotations(XSDTypeDefinition xsdType, Type dataType)
+ {
+ if ( dataType.isAbstract() )
+ {
+ if ( xsdType instanceof XSDComplexTypeDefinition )
+ {
+ ((XSDComplexTypeDefinition)xsdType).setAbstract(dataType.isAbstract());
+ }
+ else
+ {
+ xsdType.getElement().setAttribute(ABSTRACT_TYPE,
+ Boolean.toString(dataType.isAbstract()));
+ }
+ }
+
+ //add alias names if it exists
+ addAliasNamesAnnotation(xsdType,
+ dataType.getAliasNames());
+
+ //add instanceClass annotation
+ if ( dataType.getInstanceClass() != null )
+ {
+ xsdType.getElement().setAttribute(INSTANCE_CLASS, dataType.getInstanceClass().getName());
+ }
+ }
+
+
+ private QName buildSimpleSchemaType(Type dataType)
+ {
+ QName simpleSchemaTypeName = null;
+ if ( dataType.isDataType() &&
+ (simpleSchemaTypeName = typeTable.getSimpleSchemaTypeName(dataType.getName()) ) == null )
+ {
+ XSDSchema xmlSchema = getXmlSchema(dataType);
+ XSDSimpleTypeDefinition simpleType = xsdFactory.createXSDSimpleTypeDefinition();
+ //set the name
+ simpleType.setName(dataType.getName());
+ simpleType.setTargetNamespace(dataType.getURI());
+ //set abstract=true if abstract
+ simpleSchemaTypeName = new QName(dataType.getURI(),
+ dataType.getName(),
+ (String)targetNamespacePrefixMap.get(dataType.getURI()));
+ xmlSchema.getContents().add(simpleType);
+ simpleType.updateElement();
+
+ addAnnotations(simpleType, dataType);
+
+ if ( !dataType.getBaseTypes().isEmpty() )
+ {
+ Type baseType = (Type)dataType.getBaseTypes().get(0);
+
+ QName baseSchemaType = null;
+
+ try
+ {
+ baseSchemaType = buildSchema(baseType);
+ }
+ catch ( IllegalArgumentException e )
+ {
+ //this means that the base type comes from a original xsd and hence not generated
+ baseSchemaType = new QName(baseType.getURI(),
+ baseType.getName(),
+ generatePrefix());
+
+ typeTable.addSimpleSchemaType(baseType.getName(), baseSchemaType);
+
+ XSDSimpleTypeDefinition baseTypeDef = xsdFactory.createXSDSimpleTypeDefinition();
+ baseTypeDef.setName(baseType.getName());
+ baseTypeDef.setTargetNamespace(baseType.getURI());
+ typeTable.addXSDTypeDef(baseType.getURI(), baseType.getName(), baseTypeDef);
+
+ //include external XSD locations
+ includeExtXSD(baseType);
+ }
+
+ simpleType.setBaseTypeDefinition((XSDSimpleTypeDefinition)typeTable.
+ getXSDTypeDef(baseSchemaType.getNamespaceURI(),baseSchemaType.getLocalPart()));
+ addImports(xmlSchema, baseSchemaType);
+ }
+
+
+ typeTable.addSimpleSchemaType(dataType.getName(), simpleSchemaTypeName);
+ typeTable.addXSDTypeDef(dataType.getURI(), dataType.getName(), simpleType);
+ }
+ return simpleSchemaTypeName;
+ }
+
+ private void includeExtXSD(Type dataType)
+ {
+ //now we know there is a type for which the xsd must come from outside
+ //create a schema for the namespace of this type and add an include in it for
+ //the xsd that is defined externally
+ XSDSchema xmlSchema = getXmlSchema(dataType);
+
+ //ideally there could be more than one external schema defintions for a namespace
+ //and hence schemalocations will be a list of locations
+ //List schemaLocations = (List)schemaLocationMap.get(dataType.getURI());
+
+ //since as per the specs the input to XSDHelper is a map of <String, String> allowing
+ //only one schemalocation for a namespace. So for now this single location will be
+ //picked up and put into a list
+ List schemaLocations = new Vector();
+ if ( schemaLocationMap.get(dataType.getURI()) != null )
+ {
+ schemaLocations.add(schemaLocationMap.get(dataType.getURI()));
+ }
+
+ if ( schemaLocations.size() <= 0 )
+ {
+ schemaLocations.add(DEFAULT_SCHEMA_LOCATION);
+ }
+
+ Object schemaContent = null;
+ Iterator includesIterator = xmlSchema.getContents().iterator();
+ Iterator schemaLocIterator = schemaLocations.iterator();
+ String aSchemaLocation = null;
+ boolean includeExists = false;
+ //include all external schema locations
+ while ( schemaLocIterator.hasNext() )
+ {
+ aSchemaLocation = (String)schemaLocIterator.next();
+ while ( includesIterator.hasNext() )
+ {
+ schemaContent = includesIterator.next();
+ if ( schemaContent instanceof XSDInclude )
+ {
+ if ( !includeExists && aSchemaLocation.equals(
+ ((XSDInclude)schemaContent).getSchemaLocation()
+ ))
+ {
+ includeExists = true;
+ }
+ }
+ }
+
+ if ( !includeExists )
+ {
+ XSDInclude includeElement = xsdFactory.createXSDInclude();
+ includeElement.setSchemaLocation(aSchemaLocation);
+ xmlSchema.getContents().add(0, includeElement);
+ }
+ }
+ }
+
+ protected QName buildSchema(Type dataType) throws IllegalArgumentException
+ {
+ QName schemaTypeName = null;
+
+ if ( dataType.isDataType() )
+ {
+ schemaTypeName = typeTable.getSimpleSchemaTypeName(dataType.getName());
+ }
+ else
+ {
+ schemaTypeName = typeTable.getComplexSchemaTypeName(dataType.getURI(), dataType.getName());
+ }
+
+ //attempt to generate only if we have not done it already..i.e the type is
+ //not found in the typetable
+ if ( schemaTypeName == null )
+ {
+ XSDHelper xsdHelper = SDOUtil.createXSDHelper(SDOUtil.createTypeHelper());
+
+ if ( !xsdHelper.isXSD( dataType ) )
+ {
+ if ( dataType.isDataType() )
+ {
+ schemaTypeName = buildSimpleSchemaType(dataType);
+ }
+ else
+ {
+ schemaTypeName = buildComplexSchemaType(dataType);
+ }
+ }
+ else
+ {
+ throw new IllegalArgumentException("Cannot generate XSD since SDO Type '" +
+ dataType.getName() + "' was orginally generated from XSD. Use original XSD");
+ }
+ }
+ return schemaTypeName;
+ }
+
+
+ private XSDSchema getXmlSchema(Type dataType)
+ {
+ XSDSchema xmlSchema = null;
+
+ if ((xmlSchema = (XSDSchema) schemaMap.get(dataType.getURI())) == null)
+ {
+ String targetNamespacePrefix = generatePrefix();
+
+ xmlSchema = xsdFactory.createXSDSchema();
+ xmlSchema.setTargetNamespace(dataType.getURI());
+ xmlSchema.setAttributeFormDefault(XSDForm.QUALIFIED_LITERAL);
+ xmlSchema.setElementFormDefault(XSDForm.QUALIFIED_LITERAL);
+
+ targetNamespacePrefixMap.put(dataType.getURI(), targetNamespacePrefix);
+ schemaMap.put(dataType.getURI(), xmlSchema);
+
+ xmlSchema.getQNamePrefixToNamespaceMap().put(TypeTable.XS_URI_PREFIX, TypeTable.XML_SCHEMA_URI);
+ xmlSchema.setSchemaForSchemaQNamePrefix(TypeTable.XS_URI_PREFIX);
+
+ xmlSchema.getQNamePrefixToNamespaceMap().put(targetNamespacePrefix, dataType.getURI());
+ //xmlSchema.setSchemaForSchemaQNamePrefix(targetNamespacePrefix);
+
+ addSDONamespaces(xmlSchema);
+ addPackageAnnotation(xmlSchema, dataType);
+ }
+ return xmlSchema;
+ }
+
+
+ private void addSDONamespaces(XSDSchema xmlSchema)
+ {
+ xmlSchema.getQNamePrefixToNamespaceMap().put(COMMONJ_SDO_NS_PREFIX, COMMONJ_SDO_NS);
+ //xmlSchema.setSchemaForSchemaQNamePrefix(COMMONJ_SDO_NS_PREFIX);
+
+ xmlSchema.getQNamePrefixToNamespaceMap().put(SDO_JAVA_NS_PREFIX, SDO_JAVA_NS);
+ //xmlSchema.setSchemaForSchemaQNamePrefix(SDO_JAVA_NS_PREFIX);
+ }
+
+
+ /**
+ * JAM convert first name of an attribute into UpperCase as an example if
+ * there is a instance variable called foo in a bean , then Jam give that as
+ * Foo so this method is to correct that error
+ *
+ * @param wrongName
+ * @return the right name, using english as the locale for case conversion
+ */
+ public static String getCorrectName(String wrongName)
+ {
+ if (wrongName.length() > 1) {
+ return wrongName.substring(0, 1).toLowerCase(Locale.ENGLISH)
+ + wrongName.substring(1, wrongName.length());
+ } else {
+ return wrongName.substring(0, 1).toLowerCase(Locale.ENGLISH);
+ }
+ }
+
+ private String addImports(XSDSchema xmlSchema, QName schemaTypeName)
+ {
+ String prefix = null;
+ Iterator iterator = xmlSchema.getQNamePrefixToNamespaceMap().keySet().iterator();
+ while ( iterator.hasNext() )
+ {
+ prefix = (String)iterator.next();
+
+ if ( schemaTypeName.getNamespaceURI().equals(
+ xmlSchema.getQNamePrefixToNamespaceMap().get(prefix)) )
+ {
+ return prefix;
+ }
+ }
+
+ //the following lines are executed only if a prefix was not found which implies that the
+ //schemaTypeName was not imported earlier and also it does not belong to the targetnamespace
+ XSDImport importElement = xsdFactory.createXSDImport();
+ importElement.setNamespace(schemaTypeName.getNamespaceURI());
+ xmlSchema.getContents().add(0, importElement);
+ prefix = schemaTypeName.getPrefix();
+ if ( prefix == null || prefix.length() <= 0 )
+ {
+ prefix = generatePrefix();
+ }
+ xmlSchema.getQNamePrefixToNamespaceMap().put(prefix, schemaTypeName.getNamespaceURI());
+
+ return prefix;
+ }
+
+ private void handleSDOSequence(Type datatype, XSDComplexTypeDefinition complexType)
+ {
+ if ( datatype.isSequenced() )
+ {
+ complexType.setMixed(true);
+ XSDModelGroup choice = xsdFactory.createXSDModelGroup();
+ choice.setCompositor(XSDCompositor.CHOICE_LITERAL);
+ XSDParticle aParticle = xsdFactory.createXSDParticle();
+ aParticle.setContent(choice);
+ aParticle.setMaxOccurs(-1);
+ complexType.setContent(aParticle);
+ }
+ else
+ {
+ //hack to handle group property as choice
+ /*if ( getPropertyStartsWithName(datatype.getDeclaredProperties(), GROUP).size() > 0 )
+ {
+ XmlSchemaChoice choice = new XmlSchemaChoice();
+ choice.setMaxOccurs(Long.MAX_VALUE);
+ complexType.setParticle(choice);
+ }
+ else*/
+ {
+ XSDModelGroup sequence = xsdFactory.createXSDModelGroup();
+ sequence.setCompositor(XSDCompositor.SEQUENCE_LITERAL);
+ XSDParticle aParticle = xsdFactory.createXSDParticle();
+ aParticle.setContent(sequence);
+ complexType.setContent(aParticle);
+ }
+ }
+ }
+
+ private void handleSDOOpenType(Type datatype, XSDComplexTypeDefinition complexType)
+ {
+ if ( datatype.isOpen() /*&&
+ getPropertyStartsWithName(datatype.getDeclaredProperties(), GROUP).size() <= 0 */)
+ {
+ XSDWildcard elementWildcard = xsdFactory.createXSDWildcard();
+ elementWildcard.getLexicalNamespaceConstraint().add("##other");
+ elementWildcard.setProcessContents(XSDProcessContents.LAX_LITERAL);
+ // Create a particle to hold the wildcard.
+ XSDParticle wildcardParticle = xsdFactory.createXSDParticle();
+ wildcardParticle.setContent(elementWildcard);
+ wildcardParticle.setMaxOccurs(-1);
+ ((XSDModelGroup)((XSDParticle)complexType.getContent()).getContent()).
+ getContents().add(wildcardParticle);
+
+ XSDWildcard attributeWildcard = xsdFactory.createXSDWildcard();
+ attributeWildcard.getLexicalNamespaceConstraint().add("##other");
+ attributeWildcard.setProcessContents(XSDProcessContents.LAX_LITERAL);
+ complexType.setAttributeWildcard(attributeWildcard);
+ }
+ }
+
+ private void handleBaseExtn(XSDSchema xmlSchema,
+ Type datatype,
+ XSDComplexTypeDefinition complexType)
+ {
+ if ( datatype.getBaseTypes().size() > 0 )
+ {
+ Type baseType = (Type)datatype.getBaseTypes().get(0);
+ QName baseSchemaType = null;
+
+ try
+ {
+ baseSchemaType = buildSchema(baseType);
+ }
+ catch ( IllegalArgumentException e )
+ {
+ //schema cannot be generated for this type as there exists an xsd already
+ //so include that original XSD
+ baseSchemaType = new QName(baseType.getURI(),
+ baseType.getName(),
+ generatePrefix());
+
+ XSDSimpleTypeDefinition baseTypeDef = xsdFactory.createXSDSimpleTypeDefinition();
+ baseTypeDef.setName(baseType.getName());
+ baseTypeDef.setTargetNamespace(baseType.getURI());
+ typeTable.addXSDTypeDef(baseType.getURI(), baseType.getName(), baseTypeDef);
+
+ includeExtXSD(baseType);
+ }
+
+ complexType.setDerivationMethod(XSDDerivationMethod.EXTENSION_LITERAL);
+
+ if ( baseType.isDataType() )
+ {
+ XSDSimpleTypeDefinition anonymousSimpleTypeDefinition
+ = xsdFactory.createXSDSimpleTypeDefinition();
+ anonymousSimpleTypeDefinition.setBaseTypeDefinition((XSDSimpleTypeDefinition)typeTable.
+ getXSDTypeDef(baseSchemaType.getNamespaceURI(),baseSchemaType.getLocalPart()));
+ complexType.setContent(anonymousSimpleTypeDefinition);
+ }
+ else
+ {
+ complexType.setBaseTypeDefinition((XSDSimpleTypeDefinition)typeTable.
+ getXSDTypeDef(baseSchemaType.getNamespaceURI(),baseSchemaType.getLocalPart()));
+
+ }
+
+ addImports(xmlSchema, baseSchemaType);
+ }
+ }
+
+ private String formGlobalElementName(String typeName)
+ {
+ String firstChar = typeName.substring(0,1);
+ return typeName.replaceFirst(firstChar, firstChar.toLowerCase());
+ }
+
+ private void createGlobalElement(XSDSchema xmlSchema,
+ XSDComplexTypeDefinition complexType,
+ QName schemaElementName )
+ {
+ XSDElementDeclaration globalElement = xsdFactory.createXSDElementDeclaration();
+ globalElement.setTargetNamespace(xmlSchema.getTargetNamespace());
+ globalElement.setName(formGlobalElementName(complexType.getName()));
+ globalElement.setTypeDefinition
+ (typeTable.getXSDTypeDef(schemaElementName.getNamespaceURI(),
+ schemaElementName.getLocalPart()));
+ xmlSchema.getContents().add(globalElement);
+ xmlSchema.getElementDeclarations().add(globalElement);
+ }
+
+ private void addAliasNamesAnnotation(XSDSchemaContent typeDef,
+ List aliasNames)
+ {
+ if ( !aliasNames.isEmpty() )
+ {
+ StringBuffer sb = new StringBuffer();
+ Iterator iterator = aliasNames.iterator();
+ while ( iterator.hasNext() )
+ {
+ sb.append(iterator.next());
+ }
+ typeDef.getElement().setAttribute(ALIAS_NAMES, sb.toString());
+ }
+ }
+
+ private void addPackageAnnotation(XSDSchema xmlSchema, Type dataType)
+ {
+ if ( dataType.getInstanceClass() != null )
+ {
+ xmlSchema.updateElement();
+ xmlSchema.getElement().setAttribute(JAVA_PACKAGE,
+ dataType.getInstanceClass().getPackage().getName());
+ }
+ }
+
+ private String generatePrefix()
+ {
+ return NAME_SPACE_PREFIX + prefixCount++;
+ }
+}
Propchange: incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/SchemaBuilder.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/SchemaBuilder.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Modified: incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/TypeTable.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/TypeTable.java?rev=437778&r1=437777&r2=437778&view=diff
==============================================================================
--- incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/TypeTable.java (original)
+++ incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/TypeTable.java Mon Aug 28 11:53:49 2006
@@ -1,235 +1,235 @@
-package org.apache.tuscany.sdo.helper;
-
-import java.util.ArrayList;
-import java.util.Hashtable;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Vector;
-
-import javax.xml.namespace.QName;
-
-//import org.apache.axiom.om.OMElement;
-import org.eclipse.xsd.XSDFactory;
-import org.eclipse.xsd.XSDTypeDefinition;
-import org.w3c.dom.Element;
-
-
-
-public class TypeTable
-{
- public static final String XML_SCHEMA_URI = "http://www.w3.org/2001/XMLSchema";
- public static final String XS_URI_PREFIX = "xs";
- public static final QName XS_QNAME = new QName(XML_SCHEMA_URI, "schema", XS_URI_PREFIX);
- public static final String DELIMITER = "#";
-
- private Hashtable simpleXSDTypes;
- private Hashtable complexXSDTypes;
- private Hashtable xsdTypeDefs;
-
- public static String asQualifiedName(String uri, String typeName)
- {
- return (uri + DELIMITER + typeName);
- }
-
-
- public TypeTable()
- {
- simpleXSDTypes = new Hashtable();
- complexXSDTypes = new Hashtable();
- xsdTypeDefs = new Hashtable();
- populateSimpleXSDTypes();
- populateStdSDOTypes();
- }
-
- private void populateStdSDOTypes()
- {
- simpleXSDTypes.put("Boolean",
- new QName(XML_SCHEMA_URI, "boolean", XS_URI_PREFIX));
- simpleXSDTypes.put("Byte",
- new QName(XML_SCHEMA_URI, "byte", XS_URI_PREFIX));
- simpleXSDTypes.put("Bytes",
- new QName(XML_SCHEMA_URI, "hexBinary", XS_URI_PREFIX));
- simpleXSDTypes.put("Character",
- new QName(XML_SCHEMA_URI, "string", XS_URI_PREFIX));
- simpleXSDTypes.put("DataObject",
- new QName(XML_SCHEMA_URI, "anyType", XS_URI_PREFIX));
- simpleXSDTypes.put("Date",
- new QName(XML_SCHEMA_URI, "dateTime", XS_URI_PREFIX));
- simpleXSDTypes.put("Day",
- new QName(XML_SCHEMA_URI, "gDay", XS_URI_PREFIX));
- simpleXSDTypes.put("Decimal",
- new QName(XML_SCHEMA_URI, "decimal", XS_URI_PREFIX));
- simpleXSDTypes.put("Double",
- new QName(XML_SCHEMA_URI, "double", XS_URI_PREFIX));
- simpleXSDTypes.put("Duration",
- new QName(XML_SCHEMA_URI, "duration", XS_URI_PREFIX));
- simpleXSDTypes.put("Float",
- new QName(XML_SCHEMA_URI, "float", XS_URI_PREFIX));
- simpleXSDTypes.put("Int",
- new QName(XML_SCHEMA_URI, "int", XS_URI_PREFIX));
- simpleXSDTypes.put("Integer",
- new QName(XML_SCHEMA_URI, "integer", XS_URI_PREFIX));
- simpleXSDTypes.put("Long",
- new QName(XML_SCHEMA_URI, "long", XS_URI_PREFIX));
- simpleXSDTypes.put("Month",
- new QName(XML_SCHEMA_URI, "gMonth", XS_URI_PREFIX));
- simpleXSDTypes.put("monthDay",
- new QName(XML_SCHEMA_URI, "gMonthDay", XS_URI_PREFIX));
- simpleXSDTypes.put("Object",
- new QName(XML_SCHEMA_URI, "anySimpleType", XS_URI_PREFIX));
- simpleXSDTypes.put("Short",
- new QName(XML_SCHEMA_URI, "short", XS_URI_PREFIX));
- simpleXSDTypes.put("String",
- new QName(XML_SCHEMA_URI, "string", XS_URI_PREFIX));
- simpleXSDTypes.put("Strings",
- new QName(XML_SCHEMA_URI, "string", XS_URI_PREFIX));
- simpleXSDTypes.put("Time",
- new QName(XML_SCHEMA_URI, "time", XS_URI_PREFIX));
- simpleXSDTypes.put("Year",
- new QName(XML_SCHEMA_URI, "gYear", XS_URI_PREFIX));
- simpleXSDTypes.put("YearMonth",
- new QName(XML_SCHEMA_URI, "gYearMonth", XS_URI_PREFIX));
- simpleXSDTypes.put("YearMonthDay",
- new QName(XML_SCHEMA_URI, "date", XS_URI_PREFIX));
- simpleXSDTypes.put("URI",
- new QName(XML_SCHEMA_URI, "anyURI", XS_URI_PREFIX));
- }
-
- private void populateSimpleXSDTypes() {
- //todo pls use the types from org.apache.ws.commons.schema.constants.Constants
- simpleXSDTypes.put("int",
- new QName(XML_SCHEMA_URI, "int", XS_URI_PREFIX));
- simpleXSDTypes.put("java.lang.String",
- new QName(XML_SCHEMA_URI, "string", XS_URI_PREFIX));
- simpleXSDTypes.put("boolean",
- new QName(XML_SCHEMA_URI, "boolean", XS_URI_PREFIX));
- simpleXSDTypes.put("float",
- new QName(XML_SCHEMA_URI, "float", XS_URI_PREFIX));
- simpleXSDTypes.put("double",
- new QName(XML_SCHEMA_URI, "double", XS_URI_PREFIX));
- simpleXSDTypes.put("short",
- new QName(XML_SCHEMA_URI, "short", XS_URI_PREFIX));
- simpleXSDTypes.put("long",
- new QName(XML_SCHEMA_URI, "long", XS_URI_PREFIX));
- simpleXSDTypes.put("byte",
- new QName(XML_SCHEMA_URI, "byte", XS_URI_PREFIX));
- simpleXSDTypes.put("char",
- new QName(XML_SCHEMA_URI, "anyType", XS_URI_PREFIX));
- simpleXSDTypes.put("java.lang.Integer",
- new QName(XML_SCHEMA_URI, "int", XS_URI_PREFIX));
- simpleXSDTypes.put("java.lang.Double",
- new QName(XML_SCHEMA_URI, "double", XS_URI_PREFIX));
- simpleXSDTypes.put("java.lang.Float",
- new QName(XML_SCHEMA_URI, "float", XS_URI_PREFIX));
- simpleXSDTypes.put("java.lang.Long",
- new QName(XML_SCHEMA_URI, "long", XS_URI_PREFIX));
- simpleXSDTypes.put("java.lang.Character",
- new QName(XML_SCHEMA_URI, "anyType", XS_URI_PREFIX));
- simpleXSDTypes.put("java.lang.Boolean",
- new QName(XML_SCHEMA_URI, "boolean", XS_URI_PREFIX));
- simpleXSDTypes.put("java.lang.Byte",
- new QName(XML_SCHEMA_URI, "byte", XS_URI_PREFIX));
- simpleXSDTypes.put("java.lang.Short",
- new QName(XML_SCHEMA_URI, "short", XS_URI_PREFIX));
- simpleXSDTypes.put("java.util.Date",
- new QName(XML_SCHEMA_URI, "dateTime", XS_URI_PREFIX));
- simpleXSDTypes.put("java.util.Calendar",
- new QName(XML_SCHEMA_URI, "dateTime", XS_URI_PREFIX));
- simpleXSDTypes.put("java.lang.Object",
- new QName(XML_SCHEMA_URI, "anyType", XS_URI_PREFIX));
- simpleXSDTypes.put("java.math.BigDecimal",
- new QName(XML_SCHEMA_URI, "decimal", XS_URI_PREFIX));
-
- // Any types
- simpleXSDTypes.put(Element.class.getName(),
- new QName(XML_SCHEMA_URI, "anyType", XS_URI_PREFIX));
- simpleXSDTypes.put(ArrayList.class.getName(),
- new QName(XML_SCHEMA_URI, "anyType", XS_URI_PREFIX));
- simpleXSDTypes.put(Vector.class.getName(),
- new QName(XML_SCHEMA_URI, "anyType", XS_URI_PREFIX));
- simpleXSDTypes.put(List.class.getName(),
- new QName(XML_SCHEMA_URI, "anyType", XS_URI_PREFIX));
- }
-
- public QName getStdSdoType(String typename)
- {
- return (QName) simpleXSDTypes.get(typename);
- }
-
- public QName getComplexSchemaTypeName(String sdoURI, String sdoTypeName)
- {
- return (QName) complexXSDTypes.get(asQualifiedName(sdoURI, sdoTypeName));
- }
-
- public boolean isSimpleType(String typeName)
- {
- Iterator keys = simpleXSDTypes.keySet().iterator();
- while (keys.hasNext()) {
- String s = (String) keys.next();
- if (s.equals(typeName)) {
- return true;
- }
- }
- return false;
- }
-
- public QName getSimpleSchemaTypeName(String typename)
- {
- return (QName) simpleXSDTypes.get(typename);
- }
-
- public void addSimpleSchemaType(String typeName, QName schemaType)
- {
- simpleXSDTypes.put(typeName, schemaType);
- }
-
-
- public void addComplexSchemaType(String namespaceURI, String name, QName schemaType)
- {
- complexXSDTypes.put(asQualifiedName(namespaceURI, name), schemaType);
- }
-
-
- public QName getQNamefortheType(String namespaceURI, String typeName)
- {
- if ( XML_SCHEMA_URI.equals(namespaceURI))
- {
- return getSimpleSchemaTypeName(typeName);
- }
- else
- {
- return getComplexSchemaTypeName(namespaceURI, typeName);
- }
- }
-
- public void addXSDTypeDef(String namespaceURI, String typeName, XSDTypeDefinition aTypeDef)
- {
- if ( namespaceURI != null && typeName != null && aTypeDef != null )
- {
- xsdTypeDefs.put(asQualifiedName(namespaceURI, typeName), aTypeDef);
- }
- }
-
- public XSDTypeDefinition getXSDTypeDef(String namespaceURI, String typeName)
- {
- XSDTypeDefinition typeDef = null;
- if ( namespaceURI != null && typeName != null )
- {
- if ( XML_SCHEMA_URI.equals(namespaceURI) )
- {
- if ( ( typeDef = (XSDTypeDefinition)xsdTypeDefs.get(asQualifiedName(namespaceURI, typeName)) ) == null )
- {
- typeDef = XSDFactory.eINSTANCE.createXSDSimpleTypeDefinition();
- typeDef.setName(typeName);
- typeDef.setTargetNamespace(namespaceURI);
- addXSDTypeDef(namespaceURI, typeName, typeDef);
- }
- }
- else
- {
- typeDef = (XSDTypeDefinition)xsdTypeDefs.get(asQualifiedName(namespaceURI, typeName));
- }
- }
- return typeDef;
- }
-}
+package org.apache.tuscany.sdo.helper;
+
+import java.util.ArrayList;
+import java.util.Hashtable;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Vector;
+
+import javax.xml.namespace.QName;
+
+//import org.apache.axiom.om.OMElement;
+import org.eclipse.xsd.XSDFactory;
+import org.eclipse.xsd.XSDTypeDefinition;
+import org.w3c.dom.Element;
+
+
+
+public class TypeTable
+{
+ public static final String XML_SCHEMA_URI = "http://www.w3.org/2001/XMLSchema";
+ public static final String XS_URI_PREFIX = "xs";
+ public static final QName XS_QNAME = new QName(XML_SCHEMA_URI, "schema", XS_URI_PREFIX);
+ public static final String DELIMITER = "#";
+
+ private Hashtable simpleXSDTypes;
+ private Hashtable complexXSDTypes;
+ private Hashtable xsdTypeDefs;
+
+ public static String asQualifiedName(String uri, String typeName)
+ {
+ return (uri + DELIMITER + typeName);
+ }
+
+
+ public TypeTable()
+ {
+ simpleXSDTypes = new Hashtable();
+ complexXSDTypes = new Hashtable();
+ xsdTypeDefs = new Hashtable();
+ populateSimpleXSDTypes();
+ populateStdSDOTypes();
+ }
+
+ private void populateStdSDOTypes()
+ {
+ simpleXSDTypes.put("Boolean",
+ new QName(XML_SCHEMA_URI, "boolean", XS_URI_PREFIX));
+ simpleXSDTypes.put("Byte",
+ new QName(XML_SCHEMA_URI, "byte", XS_URI_PREFIX));
+ simpleXSDTypes.put("Bytes",
+ new QName(XML_SCHEMA_URI, "hexBinary", XS_URI_PREFIX));
+ simpleXSDTypes.put("Character",
+ new QName(XML_SCHEMA_URI, "string", XS_URI_PREFIX));
+ simpleXSDTypes.put("DataObject",
+ new QName(XML_SCHEMA_URI, "anyType", XS_URI_PREFIX));
+ simpleXSDTypes.put("Date",
+ new QName(XML_SCHEMA_URI, "dateTime", XS_URI_PREFIX));
+ simpleXSDTypes.put("Day",
+ new QName(XML_SCHEMA_URI, "gDay", XS_URI_PREFIX));
+ simpleXSDTypes.put("Decimal",
+ new QName(XML_SCHEMA_URI, "decimal", XS_URI_PREFIX));
+ simpleXSDTypes.put("Double",
+ new QName(XML_SCHEMA_URI, "double", XS_URI_PREFIX));
+ simpleXSDTypes.put("Duration",
+ new QName(XML_SCHEMA_URI, "duration", XS_URI_PREFIX));
+ simpleXSDTypes.put("Float",
+ new QName(XML_SCHEMA_URI, "float", XS_URI_PREFIX));
+ simpleXSDTypes.put("Int",
+ new QName(XML_SCHEMA_URI, "int", XS_URI_PREFIX));
+ simpleXSDTypes.put("Integer",
+ new QName(XML_SCHEMA_URI, "integer", XS_URI_PREFIX));
+ simpleXSDTypes.put("Long",
+ new QName(XML_SCHEMA_URI, "long", XS_URI_PREFIX));
+ simpleXSDTypes.put("Month",
+ new QName(XML_SCHEMA_URI, "gMonth", XS_URI_PREFIX));
+ simpleXSDTypes.put("monthDay",
+ new QName(XML_SCHEMA_URI, "gMonthDay", XS_URI_PREFIX));
+ simpleXSDTypes.put("Object",
+ new QName(XML_SCHEMA_URI, "anySimpleType", XS_URI_PREFIX));
+ simpleXSDTypes.put("Short",
+ new QName(XML_SCHEMA_URI, "short", XS_URI_PREFIX));
+ simpleXSDTypes.put("String",
+ new QName(XML_SCHEMA_URI, "string", XS_URI_PREFIX));
+ simpleXSDTypes.put("Strings",
+ new QName(XML_SCHEMA_URI, "string", XS_URI_PREFIX));
+ simpleXSDTypes.put("Time",
+ new QName(XML_SCHEMA_URI, "time", XS_URI_PREFIX));
+ simpleXSDTypes.put("Year",
+ new QName(XML_SCHEMA_URI, "gYear", XS_URI_PREFIX));
+ simpleXSDTypes.put("YearMonth",
+ new QName(XML_SCHEMA_URI, "gYearMonth", XS_URI_PREFIX));
+ simpleXSDTypes.put("YearMonthDay",
+ new QName(XML_SCHEMA_URI, "date", XS_URI_PREFIX));
+ simpleXSDTypes.put("URI",
+ new QName(XML_SCHEMA_URI, "anyURI", XS_URI_PREFIX));
+ }
+
+ private void populateSimpleXSDTypes() {
+ //todo pls use the types from org.apache.ws.commons.schema.constants.Constants
+ simpleXSDTypes.put("int",
+ new QName(XML_SCHEMA_URI, "int", XS_URI_PREFIX));
+ simpleXSDTypes.put("java.lang.String",
+ new QName(XML_SCHEMA_URI, "string", XS_URI_PREFIX));
+ simpleXSDTypes.put("boolean",
+ new QName(XML_SCHEMA_URI, "boolean", XS_URI_PREFIX));
+ simpleXSDTypes.put("float",
+ new QName(XML_SCHEMA_URI, "float", XS_URI_PREFIX));
+ simpleXSDTypes.put("double",
+ new QName(XML_SCHEMA_URI, "double", XS_URI_PREFIX));
+ simpleXSDTypes.put("short",
+ new QName(XML_SCHEMA_URI, "short", XS_URI_PREFIX));
+ simpleXSDTypes.put("long",
+ new QName(XML_SCHEMA_URI, "long", XS_URI_PREFIX));
+ simpleXSDTypes.put("byte",
+ new QName(XML_SCHEMA_URI, "byte", XS_URI_PREFIX));
+ simpleXSDTypes.put("char",
+ new QName(XML_SCHEMA_URI, "anyType", XS_URI_PREFIX));
+ simpleXSDTypes.put("java.lang.Integer",
+ new QName(XML_SCHEMA_URI, "int", XS_URI_PREFIX));
+ simpleXSDTypes.put("java.lang.Double",
+ new QName(XML_SCHEMA_URI, "double", XS_URI_PREFIX));
+ simpleXSDTypes.put("java.lang.Float",
+ new QName(XML_SCHEMA_URI, "float", XS_URI_PREFIX));
+ simpleXSDTypes.put("java.lang.Long",
+ new QName(XML_SCHEMA_URI, "long", XS_URI_PREFIX));
+ simpleXSDTypes.put("java.lang.Character",
+ new QName(XML_SCHEMA_URI, "anyType", XS_URI_PREFIX));
+ simpleXSDTypes.put("java.lang.Boolean",
+ new QName(XML_SCHEMA_URI, "boolean", XS_URI_PREFIX));
+ simpleXSDTypes.put("java.lang.Byte",
+ new QName(XML_SCHEMA_URI, "byte", XS_URI_PREFIX));
+ simpleXSDTypes.put("java.lang.Short",
+ new QName(XML_SCHEMA_URI, "short", XS_URI_PREFIX));
+ simpleXSDTypes.put("java.util.Date",
+ new QName(XML_SCHEMA_URI, "dateTime", XS_URI_PREFIX));
+ simpleXSDTypes.put("java.util.Calendar",
+ new QName(XML_SCHEMA_URI, "dateTime", XS_URI_PREFIX));
+ simpleXSDTypes.put("java.lang.Object",
+ new QName(XML_SCHEMA_URI, "anyType", XS_URI_PREFIX));
+ simpleXSDTypes.put("java.math.BigDecimal",
+ new QName(XML_SCHEMA_URI, "decimal", XS_URI_PREFIX));
+
+ // Any types
+ simpleXSDTypes.put(Element.class.getName(),
+ new QName(XML_SCHEMA_URI, "anyType", XS_URI_PREFIX));
+ simpleXSDTypes.put(ArrayList.class.getName(),
+ new QName(XML_SCHEMA_URI, "anyType", XS_URI_PREFIX));
+ simpleXSDTypes.put(Vector.class.getName(),
+ new QName(XML_SCHEMA_URI, "anyType", XS_URI_PREFIX));
+ simpleXSDTypes.put(List.class.getName(),
+ new QName(XML_SCHEMA_URI, "anyType", XS_URI_PREFIX));
+ }
+
+ public QName getStdSdoType(String typename)
+ {
+ return (QName) simpleXSDTypes.get(typename);
+ }
+
+ public QName getComplexSchemaTypeName(String sdoURI, String sdoTypeName)
+ {
+ return (QName) complexXSDTypes.get(asQualifiedName(sdoURI, sdoTypeName));
+ }
+
+ public boolean isSimpleType(String typeName)
+ {
+ Iterator keys = simpleXSDTypes.keySet().iterator();
+ while (keys.hasNext()) {
+ String s = (String) keys.next();
+ if (s.equals(typeName)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ public QName getSimpleSchemaTypeName(String typename)
+ {
+ return (QName) simpleXSDTypes.get(typename);
+ }
+
+ public void addSimpleSchemaType(String typeName, QName schemaType)
+ {
+ simpleXSDTypes.put(typeName, schemaType);
+ }
+
+
+ public void addComplexSchemaType(String namespaceURI, String name, QName schemaType)
+ {
+ complexXSDTypes.put(asQualifiedName(namespaceURI, name), schemaType);
+ }
+
+
+ public QName getQNamefortheType(String namespaceURI, String typeName)
+ {
+ if ( XML_SCHEMA_URI.equals(namespaceURI))
+ {
+ return getSimpleSchemaTypeName(typeName);
+ }
+ else
+ {
+ return getComplexSchemaTypeName(namespaceURI, typeName);
+ }
+ }
+
+ public void addXSDTypeDef(String namespaceURI, String typeName, XSDTypeDefinition aTypeDef)
+ {
+ if ( namespaceURI != null && typeName != null && aTypeDef != null )
+ {
+ xsdTypeDefs.put(asQualifiedName(namespaceURI, typeName), aTypeDef);
+ }
+ }
+
+ public XSDTypeDefinition getXSDTypeDef(String namespaceURI, String typeName)
+ {
+ XSDTypeDefinition typeDef = null;
+ if ( namespaceURI != null && typeName != null )
+ {
+ if ( XML_SCHEMA_URI.equals(namespaceURI) )
+ {
+ if ( ( typeDef = (XSDTypeDefinition)xsdTypeDefs.get(asQualifiedName(namespaceURI, typeName)) ) == null )
+ {
+ typeDef = XSDFactory.eINSTANCE.createXSDSimpleTypeDefinition();
+ typeDef.setName(typeName);
+ typeDef.setTargetNamespace(namespaceURI);
+ addXSDTypeDef(namespaceURI, typeName, typeDef);
+ }
+ }
+ else
+ {
+ typeDef = (XSDTypeDefinition)xsdTypeDefs.get(asQualifiedName(namespaceURI, typeName));
+ }
+ }
+ return typeDef;
+ }
+}
Propchange: incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/TypeTable.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/TypeTable.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Propchange: incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/XMLStreamHelper.java
------------------------------------------------------------------------------
--- svn:keywords (original)
+++ svn:keywords Mon Aug 28 11:53:49 2006
@@ -1 +1 @@
-Rev,Date
+Rev Date
Propchange: incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/XMLStreamHelperImpl.java
------------------------------------------------------------------------------
--- svn:keywords (original)
+++ svn:keywords Mon Aug 28 11:53:49 2006
@@ -1 +1 @@
-Rev,Date
+Rev Date
---------------------------------------------------------------------
To unsubscribe, e-mail: tuscany-commits-unsubscribe@ws.apache.org
For additional commands, e-mail: tuscany-commits-help@ws.apache.org