Search in sources :

Example 11 with SchemaModelProject

use of org.eclipse.persistence.internal.oxm.schema.SchemaModelProject in project eclipselink by eclipse-ee4j.

the class WSDLGenerator method createInlineSchema.

/**
 * Build the inline schema that will make up the content of the
 * {@code <wsdl:types>} section of the WSDL.
 */
@SuppressWarnings("unchecked")
private org.w3c.dom.Element createInlineSchema() {
    SchemaModelProject project = new SchemaModelProject();
    XMLContext context = new XMLContext(project);
    XMLMarshaller marshaller = context.createMarshaller();
    XMLDescriptor descriptor = (XMLDescriptor) project.getClassDescriptor(Schema.class);
    if (descriptor.getNamespaceResolver() == null) {
        descriptor.setNamespaceResolver(new NamespaceResolver());
    }
    descriptor.getNamespaceResolver().put(NS_TNS_PREFIX, serviceNameSpace);
    descriptor.getNamespaceResolver().put(NS_SCHEMA_PREFIX, W3C_XML_SCHEMA_NS_URI);
    if (hasAttachments) {
        descriptor.getNamespaceResolver().put(WSI_SWAREF_PREFIX, WSI_SWAREF_URI);
    }
    Schema schema = new Schema();
    schema.setTargetNamespace(serviceNameSpace);
    schema.setElementFormDefault(true);
    Import parent = new Import();
    parent.setNamespace(importedSchemaNameSpace);
    parent.setSchemaLocation(DBWS_SCHEMA_XML);
    schema.getImports().add(parent);
    if (hasAttachments) {
        Import ref = new Import();
        ref.setNamespace(WSI_SWAREF_URI);
        // ref.setSchemaLocation("http://ws-i.org/profiles/basic/1.1/swaref.xsd"); // later version
        ref.setSchemaLocation(WSI_SWAREF_XSD_FILE);
        schema.getImports().add(ref);
    }
    boolean requireFaultTypeEmptyResponse = false;
    for (Operation op : xrServiceModel.getOperationsList()) {
        String opName = op.getName();
        ComplexType requestType = new ComplexType();
        // may need to extract tableNameAlias from operation name - everything after first
        // underscore '_' character (for cases where a QueryOperation is not user defined)
        // i.e. String tableNameAlias = opName.substring(opName.indexOf('_') + 1);
        requestType.setName(opName + REQUEST_SUFFIX + TYPE_SUFFIX);
        Sequence requestSequence = null;
        if (op.getParameters().size() > 0) {
            requestSequence = new Sequence();
            for (Parameter p : op.getParameters()) {
                Element arg = new Element();
                if (p.isOptional()) {
                    arg.setMinOccurs("0");
                }
                arg.setName(p.getName());
                if (THE_INSTANCE_NAME.equals(p.getName())) {
                    ComplexType nestedComplexType = new ComplexType();
                    Sequence nestedSequence = new Sequence();
                    nestedComplexType.setSequence(nestedSequence);
                    Element nestedElement = new Element();
                    nestedElement.setRef(TARGET_NAMESPACE_PREFIX + ":" + p.getType().getLocalPart());
                    // here we may need to use 'tableNameAlias' instead, i.e.
                    // nestedElement.setRef(TARGET_NAMESPACE_PREFIX + ":" + tableNameAlias);
                    nestedSequence.addElement(nestedElement);
                    arg.setComplexType(nestedComplexType);
                } else {
                    arg.setName(p.getName());
                    if (p.getType().getNamespaceURI().equals(W3C_XML_SCHEMA_NS_URI)) {
                        arg.setType(NS_SCHEMA_PREFIX + ":" + p.getType().getLocalPart());
                    } else if (p.getType().getNamespaceURI().equals(importedSchemaNameSpace)) {
                        arg.setType(TARGET_NAMESPACE_PREFIX + ":" + p.getType().getLocalPart());
                    } else {
                        arg.setType(p.getType().getLocalPart());
                    }
                }
                requestSequence.addElement(arg);
            }
            requestType.setSequence(requestSequence);
        }
        schema.addTopLevelComplexTypes(requestType);
        Element requestElement = new Element();
        requestElement.setName(op.getName());
        requestElement.setType(NS_TNS_PREFIX + ":" + requestType.getName());
        schema.addTopLevelElement(requestElement);
        // build response message based on operation type
        if (op instanceof QueryOperation) {
            QueryOperation q = (QueryOperation) op;
            ComplexType responseType = new ComplexType();
            responseType.setName(op.getName() + RESPONSE_SUFFIX + TYPE_SUFFIX);
            Sequence responseSequence = new Sequence();
            Element result = new Element();
            result.setName("result");
            if (q.isAttachment()) {
                result.setType(WSI_SWAREF_PREFIX + ":" + WSI_SWAREF);
            } else if (q.isSimpleXMLFormat() || q.getResultType().equals(new QName(W3C_XML_SCHEMA_NS_URI, "any"))) {
                ComplexType anyType = new ComplexType();
                Sequence anySequence = new Sequence();
                anySequence.addAny(new Any());
                anyType.setSequence(anySequence);
                result.setComplexType(anyType);
            } else {
                if (q.getResultType().getNamespaceURI().equals(W3C_XML_SCHEMA_NS_URI)) {
                    result.setType(NS_SCHEMA_PREFIX + ":" + q.getResultType().getLocalPart());
                } else {
                    ComplexType nestedComplexType = new ComplexType();
                    Sequence nestedSequence = new Sequence();
                    nestedComplexType.setSequence(nestedSequence);
                    Element nestedElement = new Element();
                    // may need if/else based on if the operation is/isn't user defined, i.e.
                    // if (!q.isUserDefined()) {
                    // nestedElement.setRef(TARGET_NAMESPACE_PREFIX + ":" + tableNameAlias);
                    // } else {
                    // nestedElement.setRef(TARGET_NAMESPACE_PREFIX + ":" + q.getResultType().getLocalPart());
                    // }
                    nestedElement.setRef(TARGET_NAMESPACE_PREFIX + ":" + q.getResultType().getLocalPart());
                    nestedElement.setMinOccurs("0");
                    if (q.isCollection()) {
                        nestedElement.setMaxOccurs("unbounded");
                    }
                    nestedSequence.addElement(nestedElement);
                    result.setComplexType(nestedComplexType);
                }
            }
            responseSequence.addElement(result);
            responseType.setSequence(responseSequence);
            schema.addTopLevelComplexTypes(responseType);
            Element responseElement = new Element();
            responseElement.setName(op.getName() + RESPONSE_SUFFIX);
            responseElement.setType(NS_TNS_PREFIX + ":" + responseType.getName());
            schema.addTopLevelElement(responseElement);
        } else {
            requireFaultTypeEmptyResponse = true;
        }
    }
    if (requireFaultTypeEmptyResponse) {
        // <element name="EmptyResponse">
        // <xsd:complexType/>
        // </element>
        Element emptyResponseElement = new Element();
        emptyResponseElement.setName(EMPTY_RESPONSE);
        ComplexType emptyResponseComplexType = new ComplexType();
        emptyResponseElement.setComplexType(emptyResponseComplexType);
        schema.addTopLevelElement(emptyResponseElement);
        // <xsd:element name="FaultType">
        // <xsd:complexType>
        // <xsd:sequence>
        // <xsd:element name="faultCode" type="xsd:string"/>
        // <xsd:element name="faultString" type="xsd:string"/>
        // </xsd:sequence>
        // </xsd:complexType>
        // </element>
        Element elementFaultType = new Element();
        elementFaultType.setName(FAULT_SUFFIX + TYPE_SUFFIX);
        ComplexType faultComplexType = new ComplexType();
        elementFaultType.setComplexType(faultComplexType);
        Sequence nestedSequence = new Sequence();
        faultComplexType.setSequence(nestedSequence);
        Element faultCodeElement = new Element();
        faultCodeElement.setName("faultCode");
        faultCodeElement.setMinOccurs("1");
        faultCodeElement.setType(NS_SCHEMA_PREFIX + ":string");
        nestedSequence.addElement(faultCodeElement);
        Element faultStringElement = new Element();
        faultStringElement.setMinOccurs("1");
        faultStringElement.setName("faultString");
        faultStringElement.setType(NS_SCHEMA_PREFIX + ":string");
        nestedSequence.addElement(faultStringElement);
        schema.addTopLevelElement(elementFaultType);
    }
    return marshaller.objectToXML(schema).getDocumentElement();
}
Also used : Import(org.eclipse.persistence.internal.oxm.schema.model.Import) SchemaModelProject(org.eclipse.persistence.internal.oxm.schema.SchemaModelProject) XMLContext(org.eclipse.persistence.oxm.XMLContext) XMLMarshaller(org.eclipse.persistence.oxm.XMLMarshaller) QName(javax.xml.namespace.QName) Schema(org.eclipse.persistence.internal.oxm.schema.model.Schema) Element(org.eclipse.persistence.internal.oxm.schema.model.Element) ExtensibilityElement(javax.wsdl.extensions.ExtensibilityElement) QueryOperation(org.eclipse.persistence.internal.xr.QueryOperation) SOAP12Operation(javax.wsdl.extensions.soap12.SOAP12Operation) Operation(org.eclipse.persistence.internal.xr.Operation) BindingOperation(javax.wsdl.BindingOperation) SOAPOperation(javax.wsdl.extensions.soap.SOAPOperation) Sequence(org.eclipse.persistence.internal.oxm.schema.model.Sequence) Any(org.eclipse.persistence.internal.oxm.schema.model.Any) XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) NamespaceResolver(org.eclipse.persistence.oxm.NamespaceResolver) Parameter(org.eclipse.persistence.internal.xr.Parameter) ComplexType(org.eclipse.persistence.internal.oxm.schema.model.ComplexType) QueryOperation(org.eclipse.persistence.internal.xr.QueryOperation)

Example 12 with SchemaModelProject

use of org.eclipse.persistence.internal.oxm.schema.SchemaModelProject in project eclipselink by eclipse-ee4j.

the class SchemaGeneratorTestCase method addTransformerToSchema.

@Test
public void addTransformerToSchema() {
    InputStream inputStream = ClassLoader.getSystemResourceAsStream(BINDINGS_DOC);
    HashMap<String, Source> metadataSourceMap = new HashMap<>();
    metadataSourceMap.put("org.eclipse.persistence.testing.jaxb.externalizedmetadata.mappings.xmltransformation", new StreamSource(inputStream));
    Map<String, Object> properties = new HashMap<>();
    properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, metadataSourceMap);
    Map<String, XmlBindings> bindings = JAXBContextFactory.getXmlBindingsFromProperties(properties, Thread.currentThread().getContextClassLoader());
    JavaModelInputImpl jModelInput = new JavaModelInputImpl(DOMAIN_CLASSES, new JavaModelImpl(new JaxbClassLoader(Thread.currentThread().getContextClassLoader(), DOMAIN_CLASSES)));
    Helper helper = new Helper(jModelInput.getJavaModel());
    Generator generator = new Generator(jModelInput, bindings, Thread.currentThread().getContextClassLoader(), "", false);
    TypeInfo typeInfo = generator.getAnnotationsProcessor().getTypeInfos().get(EMPLOYEE_CLASS_NAME);
    Property normalHoursProperty = typeInfo.getProperties().get(XML_TRANSFORMATION_PROPERTY_NAME);
    ChildSchemaGenerator childSchemaGenerator = new ChildSchemaGenerator(helper);
    TransformerPropertyBuilder transformerPropertyBuilder = childSchemaGenerator.getTransformerPropertyBuilder(normalHoursProperty, typeInfo);
    java.util.List<Property> props = transformerPropertyBuilder.buildProperties();
    // Indirect call of org.eclipse.persistence.jaxb.compiler.SchemaGenerator.addTransformerToSchema(.....) method.
    List<Schema> schemas = (List) generator.generateSchema();
    XMLContext context = new XMLContext(new SchemaModelProject());
    XMLMarshaller marshaller = context.createMarshaller();
    final StringWriter stringWriter = new StringWriter();
    marshaller.marshal(schemas.get(0), stringWriter);
    String outputSchema = stringWriter.toString();
    // Verify that XML-Transformation property is added to XML schema
    for (Property property : props) {
        assertTrue(outputSchema.contains(property.getPropertyName()));
    }
}
Also used : XmlBindings(org.eclipse.persistence.jaxb.xmlmodel.XmlBindings) JavaModelImpl(org.eclipse.persistence.jaxb.javamodel.reflection.JavaModelImpl) HashMap(java.util.HashMap) XMLMarshaller(org.eclipse.persistence.oxm.XMLMarshaller) Schema(org.eclipse.persistence.internal.oxm.schema.model.Schema) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) ChildSchemaGenerator(org.eclipse.persistence.testing.moxy.unit.jaxb.compiler.builder.ChildSchemaGenerator) StringWriter(java.io.StringWriter) TransformerPropertyBuilder(org.eclipse.persistence.jaxb.compiler.builder.TransformerPropertyBuilder) List(java.util.List) Property(org.eclipse.persistence.jaxb.compiler.Property) JaxbClassLoader(org.eclipse.persistence.internal.jaxb.JaxbClassLoader) XMLContext(org.eclipse.persistence.oxm.XMLContext) SchemaModelProject(org.eclipse.persistence.internal.oxm.schema.SchemaModelProject) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) TypeInfo(org.eclipse.persistence.jaxb.compiler.TypeInfo) JavaModelInputImpl(org.eclipse.persistence.jaxb.javamodel.reflection.JavaModelInputImpl) Generator(org.eclipse.persistence.jaxb.compiler.Generator) ChildSchemaGenerator(org.eclipse.persistence.testing.moxy.unit.jaxb.compiler.builder.ChildSchemaGenerator) Test(org.junit.Test)

Example 13 with SchemaModelProject

use of org.eclipse.persistence.internal.oxm.schema.SchemaModelProject in project eclipselink by eclipse-ee4j.

the class SimpleTableWithNestedSQLTestSuite method validateElementRefType.

@Test
public void validateElementRefType() throws TransformerFactoryConfigurationError, TransformerException {
    StringWriter sw = new StringWriter();
    StreamSource wsdlStreamSource = new StreamSource(new StringReader(DBWS_WSDL_STREAM.toString()));
    Transformer t = TransformerFactory.newInstance().newTransformer(new StreamSource(new StringReader(MATCH_SCHEMA)));
    StreamResult streamResult = new StreamResult(sw);
    t.transform(wsdlStreamSource, streamResult);
    sw.toString();
    SchemaModelProject schemaProject = new SchemaModelProject();
    XMLContext xmlContext2 = new XMLContext(schemaProject);
    XMLUnmarshaller unmarshaller = xmlContext2.createUnmarshaller();
    Schema schema = (Schema) unmarshaller.unmarshal(new StringReader(sw.toString()));
    ComplexType findByNameResponseType = schema.getTopLevelComplexTypes().get(FINDBYNAME_RESPONSETYPE);
    Element result = findByNameResponseType.getSequence().getElements().get(0);
    Element unnamed = result.getComplexType().getSequence().getElements().get(0);
    assertTrue("wrong refType for " + FINDBYNAME_RESPONSETYPE, TABLE_ALIAS.equals(unnamed.getRef()));
}
Also used : Transformer(javax.xml.transform.Transformer) StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) SchemaModelProject(org.eclipse.persistence.internal.oxm.schema.SchemaModelProject) XMLContext(org.eclipse.persistence.oxm.XMLContext) StreamSource(javax.xml.transform.stream.StreamSource) Schema(org.eclipse.persistence.internal.oxm.schema.model.Schema) Element(org.eclipse.persistence.internal.oxm.schema.model.Element) StringReader(java.io.StringReader) XMLUnmarshaller(org.eclipse.persistence.oxm.XMLUnmarshaller) ComplexType(org.eclipse.persistence.internal.oxm.schema.model.ComplexType) Test(org.junit.Test)

Example 14 with SchemaModelProject

use of org.eclipse.persistence.internal.oxm.schema.SchemaModelProject in project eclipselink by eclipse-ee4j.

the class SDOSchemaGenerator method generate.

/**
 * <p>Method to generate an XSD. Note the following:<ul>
 * <li> All types must have same URI
 * <li> Referenced types in same URI will also be generated in schema
 * <li> Includes will never be generated
 * <li> Imports will be generated for referenced types in other URIs
 * </ul>
 * @param types The list of commonj.sdo.Type objects to generate the XSD from
 * @param aNamespaceToSchemaLocation map of namespaces to schemaLocations
 * used for getting the value of the schemaLocation attribute of generated imports and includes
 * @return String The generated XSD.
 */
public String generate(List types, Map<String, String> aNamespaceToSchemaLocation) {
    if ((types == null) || (types.size() == 0)) {
        throw new IllegalArgumentException("No Schema was generated from null or empty list of types.");
    }
    String uri = null;
    namespaceToSchemaLocation = aNamespaceToSchemaLocation;
    Type firstType = (Type) types.get(0);
    if (firstType == null) {
        throw new IllegalArgumentException("No Schema was generated from a list of types containing null elements");
    } else {
        uri = firstType.getURI();
    }
    allTypes = types;
    generateSchema(uri, types);
    // Now we have a built schema model
    Project p = new SchemaModelProject();
    Vector<Namespace> namespaces = generatedSchema.getNamespaceResolver().getNamespaces();
    for (int i = 0; i < namespaces.size(); i++) {
        Namespace next = namespaces.get(i);
        ((XMLDescriptor) p.getDescriptor(Schema.class)).getNamespaceResolver().put(next.getPrefix(), next.getNamespaceURI());
    }
    XMLLogin login = new XMLLogin();
    login.setDatasourcePlatform(new DOMPlatform());
    p.setDatasourceLogin(login);
    XMLContext context = new XMLContext(p);
    XMLMarshaller marshaller = context.createMarshaller();
    StringWriter generatedSchemaWriter = new StringWriter();
    marshaller.marshal(generatedSchema, generatedSchemaWriter);
    return generatedSchemaWriter.toString();
}
Also used : DOMPlatform(org.eclipse.persistence.oxm.platform.DOMPlatform) SchemaModelProject(org.eclipse.persistence.internal.oxm.schema.SchemaModelProject) XMLContext(org.eclipse.persistence.oxm.XMLContext) XMLMarshaller(org.eclipse.persistence.oxm.XMLMarshaller) XMLLogin(org.eclipse.persistence.oxm.XMLLogin) Namespace(org.eclipse.persistence.internal.oxm.Namespace) Project(org.eclipse.persistence.sessions.Project) SchemaModelProject(org.eclipse.persistence.internal.oxm.schema.SchemaModelProject) Type(commonj.sdo.Type) SDOType(org.eclipse.persistence.sdo.SDOType) StringWriter(java.io.StringWriter)

Example 15 with SchemaModelProject

use of org.eclipse.persistence.internal.oxm.schema.SchemaModelProject in project eclipselink by eclipse-ee4j.

the class BaseDBWSBuilderHelper method writeSchema.

public void writeSchema(OutputStream dbwsSchemaStream) {
    if (!isNullStream(dbwsSchemaStream)) {
        SchemaModelProject schemaProject = new SchemaModelProject();
        if (dbwsBuilder.getSchema().getNamespaceResolver().resolveNamespacePrefix(WSI_SWAREF_PREFIX) != null) {
            XMLDescriptor descriptor = (XMLDescriptor) schemaProject.getClassDescriptor(Schema.class);
            descriptor.getNamespaceResolver().put(WSI_SWAREF_PREFIX, WSI_SWAREF_URI);
        }
        if (dbwsBuilder.getSchema().getNamespaceResolver().resolveNamespacePrefix(XML_MIME_PREFIX) != null) {
            XMLDescriptor descriptor = (XMLDescriptor) schemaProject.getClassDescriptor(Schema.class);
            descriptor.getNamespaceResolver().put(XML_MIME_PREFIX, XML_MIME_URL);
        }
        XMLContext context = new XMLContext(schemaProject);
        XMLMarshaller marshaller = context.createMarshaller();
        marshaller.marshal(dbwsBuilder.getSchema(), dbwsSchemaStream);
        dbwsBuilder.getPackager().closeSchemaStream(dbwsSchemaStream);
    }
}
Also used : XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) SchemaModelProject(org.eclipse.persistence.internal.oxm.schema.SchemaModelProject) XMLContext(org.eclipse.persistence.oxm.XMLContext) XMLMarshaller(org.eclipse.persistence.oxm.XMLMarshaller) Schema(org.eclipse.persistence.internal.oxm.schema.model.Schema)

Aggregations

SchemaModelProject (org.eclipse.persistence.internal.oxm.schema.SchemaModelProject)15 XMLContext (org.eclipse.persistence.oxm.XMLContext)15 XMLMarshaller (org.eclipse.persistence.oxm.XMLMarshaller)11 Schema (org.eclipse.persistence.internal.oxm.schema.model.Schema)9 Project (org.eclipse.persistence.sessions.Project)9 StringWriter (java.io.StringWriter)8 Namespace (org.eclipse.persistence.internal.oxm.Namespace)6 NamespaceResolver (org.eclipse.persistence.oxm.NamespaceResolver)5 Vector (java.util.Vector)4 StreamSource (javax.xml.transform.stream.StreamSource)4 XMLDescriptor (org.eclipse.persistence.oxm.XMLDescriptor)4 XMLUnmarshaller (org.eclipse.persistence.oxm.XMLUnmarshaller)4 StringReader (java.io.StringReader)3 Transformer (javax.xml.transform.Transformer)3 StreamResult (javax.xml.transform.stream.StreamResult)3 ComplexType (org.eclipse.persistence.internal.oxm.schema.model.ComplexType)3 Element (org.eclipse.persistence.internal.oxm.schema.model.Element)3 Test (org.junit.Test)3 Type (commonj.sdo.Type)2 File (java.io.File)2