Search in sources :

Example 1 with SchemaModelProject

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

the class SimpleTableWithNestedSQLTestSuite method checkWSDL.

@Test
public void checkWSDL() throws WSDLException {
    DBWS_BUILDER_XML_USERNAME = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<dbws-builder xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">" + "<properties>" + "<property name=\"projectName\">simpletable2</property>" + "<property name=\"logLevel\">off</property>" + "<property name=\"username\">";
    DBWS_BUILDER_XML_PASSWORD = "</property><property name=\"password\">";
    DBWS_BUILDER_XML_URL = "</property><property name=\"url\">";
    DBWS_BUILDER_XML_DRIVER = "</property><property name=\"driver\">";
    DBWS_BUILDER_XML_PLATFORM = "</property><property name=\"platformClassname\">";
    DBWS_BUILDER_XML_MAIN = "</property>" + "</properties>" + "<table " + "schemaPattern=\"%\" " + "tableNamePattern=\"simpletable2\" " + ">" + "<sql " + "name=\"findByName\" " + "isCollection=\"true\" " + "returnType=\"simpletable2Type\" " + ">" + "<text><![CDATA[select * from SIMPLETABLE2 where NAME like ?]]></text>" + "<binding name=\"NAME\" type=\"xsd:string\"/>" + "</sql>" + "</table>" + "</dbws-builder>";
    String username = System.getProperty(DATABASE_USERNAME_KEY, DEFAULT_DATABASE_USERNAME);
    String password = System.getProperty(DATABASE_PASSWORD_KEY, DEFAULT_DATABASE_PASSWORD);
    String url = System.getProperty(DATABASE_URL_KEY, DEFAULT_DATABASE_URL);
    String builderString = DBWS_BUILDER_XML_USERNAME + username + DBWS_BUILDER_XML_PASSWORD + password + DBWS_BUILDER_XML_URL + url + DBWS_BUILDER_XML_DRIVER + DATABASE_DRIVER + DBWS_BUILDER_XML_PLATFORM + DATABASE_PLATFORM + DBWS_BUILDER_XML_MAIN;
    XMLContext context = new XMLContext(new DBWSBuilderModelProject());
    XMLUnmarshaller unmarshaller = context.createUnmarshaller();
    DBWSBuilderModel builderModel = (DBWSBuilderModel) unmarshaller.unmarshal(new StringReader(builderString));
    DBWSBuilder builder = new DBWSBuilder();
    builder.quiet = true;
    // builder.setLogLevel(SessionLog.FINE_LABEL);
    builder.setLogLevel(SessionLog.OFF_LABEL);
    builder.setPlatformClassname(DATABASE_PLATFORM);
    builder.properties = builderModel.properties;
    builder.getProperties().put(SESSIONS_FILENAME_KEY, NO_SESSIONS_FILENAME);
    builder.operations = builderModel.operations;
    builder.setPackager(new JSR109WebServicePackager(null, "WebServiceTestPackager", noArchive) {

        @Override
        public void start() {
        // do nothing - don't have to verify existence of 'stageDir' when
        // all the streams are in-memory
        }
    });
    ByteArrayOutputStream dbwsServiceStream = new ByteArrayOutputStream();
    ByteArrayOutputStream wsdlStream = new ByteArrayOutputStream();
    builder.build(__nullStream, __nullStream, dbwsServiceStream, __nullStream, __nullStream, __nullStream, __nullStream, wsdlStream, __nullStream, __nullStream, __nullStream, __nullStream, null);
    // for the nested sql operation 'findByName'
    try {
        StringWriter sw = new StringWriter();
        StreamSource wsdlStreamSource = new StreamSource(new StringReader(wsdlStream.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);
        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()));
    } catch (Exception e) {
        fail(e.getMessage());
    }
}
Also used : Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) XMLContext(org.eclipse.persistence.oxm.XMLContext) SchemaModelProject(org.eclipse.persistence.internal.oxm.schema.SchemaModelProject) StreamSource(javax.xml.transform.stream.StreamSource) Schema(org.eclipse.persistence.internal.oxm.schema.model.Schema) Element(org.eclipse.persistence.internal.oxm.schema.model.Element) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SQLException(java.sql.SQLException) WSDLException(javax.wsdl.WSDLException) DBWSBuilder(org.eclipse.persistence.tools.dbws.DBWSBuilder) JSR109WebServicePackager(org.eclipse.persistence.tools.dbws.JSR109WebServicePackager) StringWriter(java.io.StringWriter) DBWSBuilderModel(org.eclipse.persistence.tools.dbws.DBWSBuilderModel) StringReader(java.io.StringReader) DBWSBuilderModelProject(org.eclipse.persistence.tools.dbws.DBWSBuilderModelProject) XMLUnmarshaller(org.eclipse.persistence.oxm.XMLUnmarshaller) ComplexType(org.eclipse.persistence.internal.oxm.schema.model.ComplexType) Test(org.junit.Test)

Example 2 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 aSchemaLocationResolver implementation of the org.eclipse.persistence.sdo.helper.SchemaLocationResolver interface
 * used for getting the value of the schemaLocation attribute of generated imports and includes
 * @return String The generated XSD.
 */
public String generate(List types, SchemaLocationResolver aSchemaLocationResolver) {
    schemaLocationResolver = aSchemaLocationResolver;
    if ((types == null) || (types.size() == 0)) {
        throw new IllegalArgumentException("No Schema was generated from null or empty list of types.");
    }
    String uri = null;
    Type firstType = (Type) types.get(0);
    uri = firstType.getURI();
    allTypes = types;
    generateSchema(uri, types);
    // Now we have a built schema model
    Project p = new SchemaModelProject();
    Vector<Namespace> generatedNamespaces = generatedSchema.getNamespaceResolver().getNamespaces();
    XMLDescriptor desc = ((XMLDescriptor) p.getDescriptor(Schema.class));
    for (int i = 0; i < generatedNamespaces.size(); i++) {
        Namespace next = generatedNamespaces.get(i);
        desc.getNamespaceResolver().put(next.getPrefix(), next.getNamespaceURI());
        if (next.getNamespaceURI().equals(SDOConstants.SDO_URL) || next.getNamespaceURI().equals(SDOConstants.SDOXML_URL) || next.getNamespaceURI().equals(SDOConstants.SDOJAVA_URL)) {
            if (!importExists(generatedSchema.getImports(), next.getNamespaceURI())) {
                Import theImport = new Import();
                theImport.setNamespace(next.getNamespaceURI());
                String schemaLocation = "classpath:/xml/";
                String customLocation = null;
                if (next.getNamespaceURI().equals(SDOConstants.SDO_URL)) {
                    if (schemaLocationResolver != null) {
                        customLocation = schemaLocationResolver.resolveSchemaLocation(firstType, SDOConstants.SDO_BOOLEAN);
                    }
                    if (customLocation != null) {
                        schemaLocation = customLocation;
                    } else {
                        schemaLocation += "sdoModel.xsd";
                    }
                } else if (next.getNamespaceURI().equals(SDOConstants.SDOXML_URL)) {
                    if (schemaLocationResolver != null) {
                        customLocation = schemaLocationResolver.resolveSchemaLocation(firstType, new SDOType(SDOConstants.SDOXML_URL, "XMLInfo"));
                    }
                    if (customLocation != null) {
                        schemaLocation = customLocation;
                    } else {
                        schemaLocation += "sdoXML.xsd";
                    }
                } else if (next.getNamespaceURI().equals(SDOConstants.SDOJAVA_URL)) {
                    if (schemaLocationResolver != null) {
                        customLocation = schemaLocationResolver.resolveSchemaLocation(firstType, SDOConstants.SDO_BOOLEANOBJECT);
                    }
                    if (customLocation != null) {
                        schemaLocation = customLocation;
                    } else {
                        schemaLocation += "sdoJava.xsd";
                    }
                }
                theImport.setSchemaLocation(schemaLocation);
                generatedSchema.getImports().add(theImport);
            }
        }
    }
    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) SDOType(org.eclipse.persistence.sdo.SDOType) 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) XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) StringWriter(java.io.StringWriter)

Example 3 with SchemaModelProject

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

the class XRServiceFactory method loadXMLSchema.

/**
 * <p>INTERNAL:
 * Read and unmarshal <code>XRService</code>'s <code>.xsd</code> file.
 * @param xrSchemaStream Stream resource for the <code>XRService</code>'s <code>.xsd</code> file.
 */
public void loadXMLSchema(InputStream xrSchemaStream) {
    SchemaModelProject schemaProject = new SchemaModelProject();
    XMLContext xmlContext = new XMLContext(schemaProject);
    XMLUnmarshaller unmarshaller = xmlContext.createUnmarshaller();
    Schema schema;
    try {
        schema = (Schema) unmarshaller.unmarshal(xrSchemaStream);
    } catch (XMLMarshalException e) {
        xmlContext.getSession().getSessionLog().log(SessionLog.WARNING, SessionLog.DBWS, "dbws_xml_schema_read_error", e.getLocalizedMessage());
        throw new DBWSException(OXM_PROCESSING_SCH, e);
    }
    NamespaceResolver nr = schema.getNamespaceResolver();
    String targetNamespace = schema.getTargetNamespace();
    nr.put(TARGET_NAMESPACE_PREFIX, targetNamespace);
    xrService.schema = schema;
    xrService.schemaNamespace = targetNamespace;
}
Also used : DBWSException(org.eclipse.persistence.exceptions.DBWSException) SchemaModelProject(org.eclipse.persistence.internal.oxm.schema.SchemaModelProject) XMLContext(org.eclipse.persistence.oxm.XMLContext) Schema(org.eclipse.persistence.internal.oxm.schema.model.Schema) NamespaceResolver(org.eclipse.persistence.oxm.NamespaceResolver) XMLUnmarshaller(org.eclipse.persistence.oxm.XMLUnmarshaller) XMLMarshalException(org.eclipse.persistence.exceptions.XMLMarshalException)

Example 4 with SchemaModelProject

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

the class GenerateSchemaTestCases method compareSchemaStrings.

/**
 * Compares a schema model Schema (as a string) against a string representation
 * of an XML schema.
 */
protected boolean compareSchemaStrings(Schema generatedSchema, String controlSchema) {
    Project p = new SchemaModelProject();
    Vector namespaces = generatedSchema.getNamespaceResolver().getNamespaces();
    for (int i = 0; i < namespaces.size(); i++) {
        Namespace next = (Namespace) namespaces.get(i);
        ((XMLDescriptor) p.getDescriptor(Schema.class)).getNamespaceResolver().put(next.getPrefix(), next.getNamespaceURI());
    }
    XMLContext context = new XMLContext(p);
    XMLMarshaller marshaller = context.createMarshaller();
    StringWriter generatedSchemaWriter = new StringWriter();
    marshaller.marshal(generatedSchema, generatedSchemaWriter);
    return generatedSchemaWriter.toString().equals(controlSchema);
}
Also used : Project(org.eclipse.persistence.sessions.Project) SchemaModelProject(org.eclipse.persistence.internal.oxm.schema.SchemaModelProject) StringWriter(java.io.StringWriter) SchemaModelProject(org.eclipse.persistence.internal.oxm.schema.SchemaModelProject) XMLContext(org.eclipse.persistence.oxm.XMLContext) XMLMarshaller(org.eclipse.persistence.oxm.XMLMarshaller) Vector(java.util.Vector) Namespace(org.eclipse.persistence.internal.oxm.Namespace)

Example 5 with SchemaModelProject

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

the class GenerateSchemaTestCases method writeSchema.

/**
 * Write the given schema to the T_WORK folder.
 */
protected void writeSchema(Schema generatedSchema) {
    try {
        Project p = new SchemaModelProject();
        Vector namespaces = generatedSchema.getNamespaceResolver().getNamespaces();
        for (int i = 0; i < namespaces.size(); i++) {
            Namespace next = (Namespace) namespaces.get(i);
            ((XMLDescriptor) p.getDescriptor(Schema.class)).getNamespaceResolver().put(next.getPrefix(), next.getNamespaceURI());
        }
        XMLContext context = new XMLContext(p);
        XMLMarshaller marshaller = context.createMarshaller();
        FileWriter generatedSchemaWriter = new FileWriter(new File(TMP_DIR, "generatedSchema.xsd"));
        marshaller.marshal(generatedSchema, generatedSchemaWriter);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}
Also used : Project(org.eclipse.persistence.sessions.Project) SchemaModelProject(org.eclipse.persistence.internal.oxm.schema.SchemaModelProject) SchemaModelProject(org.eclipse.persistence.internal.oxm.schema.SchemaModelProject) XMLContext(org.eclipse.persistence.oxm.XMLContext) XMLMarshaller(org.eclipse.persistence.oxm.XMLMarshaller) FileWriter(java.io.FileWriter) Vector(java.util.Vector) File(java.io.File) Namespace(org.eclipse.persistence.internal.oxm.Namespace)

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