Search in sources :

Example 11 with XMLContext

use of org.eclipse.persistence.oxm.XMLContext in project eclipselink by eclipse-ee4j.

the class SecondarySQLTestSuite method buildSessions.

@Override
public void buildSessions() {
    XRDynamicClassLoader xrdecl = new XRDynamicClassLoader(parentClassLoader);
    DatasourceLogin login = new DatabaseLogin();
    login.setUserName(username);
    login.setPassword(password);
    ((DatabaseLogin) login).setConnectionString(url);
    ((DatabaseLogin) login).setDriverClassName(DATABASE_PLATFORM);
    Platform platform = builder.getDatabasePlatform();
    ConversionManager conversionManager = platform.getConversionManager();
    if (conversionManager != null) {
        conversionManager.setLoader(xrdecl);
    }
    login.setDatasourcePlatform(platform);
    ((DatabaseLogin) login).bindAllParameters();
    ((DatabaseLogin) login).setUsesStreamsForBinding(true);
    Project orProject = null;
    if (DBWS_OR_STREAM.size() != 0) {
        MetadataProcessor processor = new MetadataProcessor(new XRPersistenceUnitInfo(xrdecl), new DatabaseSessionImpl(login), xrdecl, false, true, false, false, false, null, null);
        processor.setMetadataSource(new JPAMetadataSource(xrdecl, new StringReader(DBWS_OR_STREAM.toString())));
        PersistenceUnitProcessor.processORMetadata(processor, true, PersistenceUnitProcessor.Mode.ALL);
        processor.addNamedQueries();
        orProject = processor.getProject().getProject();
    } else {
        orProject = new Project();
    }
    orProject.setName(builder.getProjectName().concat(OR_PRJ_SUFFIX));
    orProject.setDatasourceLogin(login);
    DatabaseSession databaseSession = orProject.createDatabaseSession();
    if ("off".equalsIgnoreCase(builder.getLogLevel())) {
        databaseSession.dontLogMessages();
    } else {
        databaseSession.setLogLevel(AbstractSessionLog.translateStringToLoggingLevel(builder.getLogLevel()));
    }
    xrService.setORSession(databaseSession);
    orProject.convertClassNamesToClasses(xrdecl);
    Project oxProject = null;
    Map<String, OXMMetadataSource> metadataMap = new HashMap<String, OXMMetadataSource>();
    StreamSource xml = new StreamSource(new StringReader(DBWS_OX_STREAM.toString()));
    try {
        JAXBContext jc = JAXBContext.newInstance(XmlBindingsModel.class);
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        JAXBElement<XmlBindingsModel> jaxbElt = unmarshaller.unmarshal(xml, XmlBindingsModel.class);
        XmlBindingsModel model = jaxbElt.getValue();
        for (XmlBindings xmlBindings : model.getBindingsList()) {
            metadataMap.put(xmlBindings.getPackageName(), new OXMMetadataSource(xmlBindings));
        }
    } catch (JAXBException jaxbex) {
        jaxbex.printStackTrace();
    }
    Map<String, Map<String, OXMMetadataSource>> properties = new HashMap<String, Map<String, OXMMetadataSource>>();
    properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, metadataMap);
    try {
        org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext jCtx = org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContextFactory.createContextFromOXM(parentClassLoader, properties);
        oxProject = jCtx.getXMLContext().getSession(0).getProject();
    } catch (JAXBException e) {
        e.printStackTrace();
    }
    ((XMLLogin) oxProject.getDatasourceLogin()).setPlatformClassName(DOM_PLATFORM_CLASSNAME);
    ((XMLLogin) oxProject.getDatasourceLogin()).setEqualNamespaceResolvers(false);
    prepareDescriptors(oxProject, orProject, xrdecl);
    ProjectHelper.fixOROXAccessors(orProject, oxProject);
    xrService.setORSession(databaseSession);
    xrService.setXMLContext(new XMLContext(oxProject));
    xrService.setOXSession(xrService.getXMLContext().getSession(0));
}
Also used : XmlBindings(org.eclipse.persistence.jaxb.xmlmodel.XmlBindings) Platform(org.eclipse.persistence.internal.databaseaccess.Platform) XMLPlatform(org.eclipse.persistence.platform.xml.XMLPlatform) DatabaseSession(org.eclipse.persistence.sessions.DatabaseSession) HashMap(java.util.HashMap) XRDynamicClassLoader(org.eclipse.persistence.internal.xr.XRDynamicClassLoader) XMLLogin(org.eclipse.persistence.oxm.XMLLogin) JAXBContext(jakarta.xml.bind.JAXBContext) StringReader(java.io.StringReader) ConversionManager(org.eclipse.persistence.internal.helper.ConversionManager) XmlBindingsModel(org.eclipse.persistence.internal.xr.XmlBindingsModel) Unmarshaller(jakarta.xml.bind.Unmarshaller) XMLUnmarshaller(org.eclipse.persistence.oxm.XMLUnmarshaller) DatasourceLogin(org.eclipse.persistence.sessions.DatasourceLogin) XMLContext(org.eclipse.persistence.oxm.XMLContext) StreamSource(javax.xml.transform.stream.StreamSource) JAXBException(jakarta.xml.bind.JAXBException) DatabaseLogin(org.eclipse.persistence.sessions.DatabaseLogin) Project(org.eclipse.persistence.sessions.Project) DBWSBuilderModelProject(org.eclipse.persistence.tools.dbws.DBWSBuilderModelProject) DatabaseSessionImpl(org.eclipse.persistence.internal.sessions.DatabaseSessionImpl) MetadataProcessor(org.eclipse.persistence.internal.jpa.metadata.MetadataProcessor) Map(java.util.Map) HashMap(java.util.HashMap)

Example 12 with XMLContext

use of org.eclipse.persistence.oxm.XMLContext 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 13 with XMLContext

use of org.eclipse.persistence.oxm.XMLContext in project eclipselink by eclipse-ee4j.

the class DBWSTestSuite method setUp.

/**
 * This method should be used when sessions xml is to be generated and written out
 * to DBWS_SESSION_STREAM.
 *
 * @param stageDir sessions xml will be generated and written out if non-null
 */
public static void setUp(String stageDir, boolean useLogger) throws WSDLException {
    comparer.setIgnoreOrder(true);
    if (builder == null) {
        builder = new DBWSBuilder();
    }
    DBWS_SERVICE_STREAM = new ByteArrayOutputStream();
    DBWS_SCHEMA_STREAM = new ByteArrayOutputStream();
    DBWS_SESSION_STREAM = new ByteArrayOutputStream();
    DBWS_OR_STREAM = new ByteArrayOutputStream();
    DBWS_OX_STREAM = new ByteArrayOutputStream();
    DBWS_WSDL_STREAM = new ByteArrayOutputStream();
    final String username = System.getProperty(DATABASE_USERNAME_KEY, DEFAULT_DATABASE_USERNAME);
    final String password = System.getProperty(DATABASE_PASSWORD_KEY, DEFAULT_DATABASE_PASSWORD);
    final 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));
    builder.quiet = true;
    builder.setPlatformClassname(DATABASE_PLATFORM);
    builder.properties = builderModel.properties;
    builder.operations = builderModel.operations;
    XRPackager xrPackager = 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
        }
    };
    xrPackager.setDBWSBuilder(builder);
    builder.setPackager(xrPackager);
    builder.setPackager(xrPackager);
    dbwsLogger = null;
    if (useLogger) {
        dbwsLogger = new DBWSLogger("DBWSTestLogger", null);
    }
    if (stageDir == null) {
        builder.getProperties().put(SESSIONS_FILENAME_KEY, NO_SESSIONS_FILENAME);
        builder.build(DBWS_SCHEMA_STREAM, __nullStream, DBWS_SERVICE_STREAM, DBWS_OR_STREAM, DBWS_OX_STREAM, __nullStream, __nullStream, __nullStream, __nullStream, __nullStream, __nullStream, __nullStream, dbwsLogger);
    } else {
        xrPackager.setStageDir(new File(stageDir));
        builder.build(DBWS_SCHEMA_STREAM, DBWS_SESSION_STREAM, DBWS_SERVICE_STREAM, DBWS_OR_STREAM, DBWS_OX_STREAM, __nullStream, __nullStream, DBWS_WSDL_STREAM, __nullStream, __nullStream, __nullStream, __nullStream, dbwsLogger);
    }
    XRServiceFactory factory = new XRServiceFactory() {

        @Override
        public XRServiceAdapter buildService(XRServiceModel xrServiceModel) {
            parentClassLoader = this.getClass().getClassLoader();
            xrSchemaStream = new ByteArrayInputStream(DBWS_SCHEMA_STREAM.toByteArray());
            return super.buildService(xrServiceModel);
        }

        @Override
        public void buildSessions() {
            XRDynamicClassLoader xrdecl = new XRDynamicClassLoader(parentClassLoader);
            DatasourceLogin login = new DatabaseLogin();
            login.setUserName(username);
            login.setPassword(password);
            ((DatabaseLogin) login).setConnectionString(url);
            ((DatabaseLogin) login).setDriverClassName(DATABASE_PLATFORM);
            Platform platform = builder.getDatabasePlatform();
            ConversionManager conversionManager = platform.getConversionManager();
            if (conversionManager != null) {
                conversionManager.setLoader(xrdecl);
            }
            login.setDatasourcePlatform(platform);
            ((DatabaseLogin) login).bindAllParameters();
            Project orProject = null;
            if (DBWS_OR_STREAM.size() != 0) {
                MetadataProcessor processor = new MetadataProcessor(new XRPersistenceUnitInfo(xrdecl), new DatabaseSessionImpl(login), xrdecl, false, true, false, false, false, null, null);
                processor.setMetadataSource(new JPAMetadataSource(xrdecl, new StringReader(DBWS_OR_STREAM.toString())));
                PersistenceUnitProcessor.processORMetadata(processor, true, PersistenceUnitProcessor.Mode.ALL);
                processor.addNamedQueries();
                orProject = processor.getProject().getProject();
            } else {
                orProject = new Project();
            }
            orProject.setName(builder.getProjectName().concat(OR_PRJ_SUFFIX));
            orProject.setDatasourceLogin(login);
            DatabaseSession databaseSession = orProject.createDatabaseSession();
            if ("off".equalsIgnoreCase(builder.getLogLevel())) {
                databaseSession.dontLogMessages();
            } else {
                databaseSession.setLogLevel(AbstractSessionLog.translateStringToLoggingLevel(builder.getLogLevel()));
            }
            xrService.setORSession(databaseSession);
            orProject.convertClassNamesToClasses(xrdecl);
            Project oxProject = null;
            if (DBWS_OX_STREAM.size() != 0) {
                Map<String, OXMMetadataSource> metadataMap = new HashMap<String, OXMMetadataSource>();
                StreamSource xml = new StreamSource(new StringReader(DBWS_OX_STREAM.toString()));
                try {
                    JAXBContext jc = JAXBContext.newInstance(XmlBindingsModel.class);
                    Unmarshaller unmarshaller = jc.createUnmarshaller();
                    JAXBElement<XmlBindingsModel> jaxbElt = unmarshaller.unmarshal(xml, XmlBindingsModel.class);
                    XmlBindingsModel model = jaxbElt.getValue();
                    for (XmlBindings xmlBindings : model.getBindingsList()) {
                        metadataMap.put(xmlBindings.getPackageName(), new OXMMetadataSource(xmlBindings));
                    }
                } catch (JAXBException jaxbex) {
                    jaxbex.printStackTrace();
                }
                Map<String, Map<String, OXMMetadataSource>> properties = new HashMap<String, Map<String, OXMMetadataSource>>();
                properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, metadataMap);
                try {
                    org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext jCtx = org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContextFactory.createContextFromOXM(xrdecl, properties);
                    oxProject = jCtx.getXMLContext().getSession(0).getProject();
                } catch (JAXBException e) {
                    e.printStackTrace();
                }
            } else {
                oxProject = new Project();
            }
            oxProject.setName(builder.getProjectName().concat(OX_PRJ_SUFFIX));
            login = (DatasourceLogin) oxProject.getDatasourceLogin();
            if (login != null) {
                platform = login.getDatasourcePlatform();
                if (platform != null) {
                    conversionManager = platform.getConversionManager();
                    if (conversionManager != null) {
                        conversionManager.setLoader(xrdecl);
                    }
                }
            }
            prepareDescriptors(oxProject, orProject, xrdecl);
            ProjectHelper.fixOROXAccessors(orProject, oxProject);
            xrService.setORSession(orProject.createDatabaseSession());
            xrService.getORSession().dontLogMessages();
            xrService.setXMLContext(new XMLContext(oxProject));
            xrService.setOXSession(xrService.getXMLContext().getSession(0));
        }
    };
    context = new XMLContext(new DBWSModelProject());
    unmarshaller = context.createUnmarshaller();
    DBWSModel model = (DBWSModel) unmarshaller.unmarshal(new StringReader(DBWS_SERVICE_STREAM.toString()));
    xrService = factory.buildService(model);
}
Also used : XRPackager(org.eclipse.persistence.tools.dbws.XRPackager) XmlBindings(org.eclipse.persistence.jaxb.xmlmodel.XmlBindings) Platform(org.eclipse.persistence.internal.databaseaccess.Platform) XMLPlatform(org.eclipse.persistence.platform.xml.XMLPlatform) DatabaseSession(org.eclipse.persistence.sessions.DatabaseSession) HashMap(java.util.HashMap) XRDynamicClassLoader(org.eclipse.persistence.internal.xr.XRDynamicClassLoader) JAXBContext(jakarta.xml.bind.JAXBContext) DBWSBuilder(org.eclipse.persistence.tools.dbws.DBWSBuilder) DBWSModelProject(org.eclipse.persistence.dbws.DBWSModelProject) JSR109WebServicePackager(org.eclipse.persistence.tools.dbws.JSR109WebServicePackager) StringReader(java.io.StringReader) ConversionManager(org.eclipse.persistence.internal.helper.ConversionManager) XmlBindingsModel(org.eclipse.persistence.internal.xr.XmlBindingsModel) XMLUnmarshaller(org.eclipse.persistence.oxm.XMLUnmarshaller) Unmarshaller(jakarta.xml.bind.Unmarshaller) XMLUnmarshaller(org.eclipse.persistence.oxm.XMLUnmarshaller) DatasourceLogin(org.eclipse.persistence.sessions.DatasourceLogin) DBWSModel(org.eclipse.persistence.dbws.DBWSModel) XMLContext(org.eclipse.persistence.oxm.XMLContext) StreamSource(javax.xml.transform.stream.StreamSource) JAXBException(jakarta.xml.bind.JAXBException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DatabaseLogin(org.eclipse.persistence.sessions.DatabaseLogin) DBWSBuilderModelProject(org.eclipse.persistence.tools.dbws.DBWSBuilderModelProject) Project(org.eclipse.persistence.sessions.Project) DBWSModelProject(org.eclipse.persistence.dbws.DBWSModelProject) XRServiceFactory(org.eclipse.persistence.internal.xr.XRServiceFactory) ByteArrayInputStream(java.io.ByteArrayInputStream) DatabaseSessionImpl(org.eclipse.persistence.internal.sessions.DatabaseSessionImpl) DBWSBuilderModel(org.eclipse.persistence.tools.dbws.DBWSBuilderModel) XRServiceModel(org.eclipse.persistence.internal.xr.XRServiceModel) DBWSBuilderModelProject(org.eclipse.persistence.tools.dbws.DBWSBuilderModelProject) File(java.io.File) MetadataProcessor(org.eclipse.persistence.internal.jpa.metadata.MetadataProcessor) Map(java.util.Map) HashMap(java.util.HashMap)

Example 14 with XMLContext

use of org.eclipse.persistence.oxm.XMLContext 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 15 with XMLContext

use of org.eclipse.persistence.oxm.XMLContext in project eclipselink by eclipse-ee4j.

the class SDOTypesGenerator method getSchema.

/**
 * Return a Schema for the given Source object.
 *
 * Since this method is called recursively, and the SchemaResolverWrapper is stateful,
 * the resolver wrapper must be created outside of this method.
 *
 * @param xsdSource
 * @param schemaResolverWrapper wraps the schema resolver to be used to resolve imports/includes
 * @return
 */
public Schema getSchema(Source xsdSource, SchemaResolverWrapper schemaResolverWrapper, boolean closeStream) {
    xsdSource = schemaResolverWrapper.resolveSchema(xsdSource);
    XMLContext context = new XMLContext(getSchemaProject());
    XMLUnmarshaller unmarshaller = context.createUnmarshaller();
    unmarshaller.setEntityResolver(schemaResolverWrapper.getSchemaResolver());
    Schema schema = (Schema) unmarshaller.unmarshal(xsdSource);
    if (closeStream) {
        closeSource(xsdSource);
    }
    // populate Imports
    java.util.List<Import> imports = schema.getImports();
    Iterator<Import> iter = imports.iterator();
    while (iter.hasNext()) {
        Import nextImport = iter.next();
        Source referencedSchema = getReferencedSchema(xsdSource, nextImport.getNamespace(), nextImport.getSchemaLocation(), schemaResolverWrapper);
        if (referencedSchema != null) {
            Schema importedSchema = getSchema(referencedSchema, schemaResolverWrapper, true);
            nextImport.setSchema(importedSchema);
        }
    }
    // populate includes
    java.util.List<Include> includes = schema.getIncludes();
    Iterator<Include> includesIter = includes.iterator();
    while (includesIter.hasNext()) {
        Include nextInclude = includesIter.next();
        Source referencedSchema = getReferencedSchema(xsdSource, schema.getTargetNamespace(), nextInclude.getSchemaLocation(), schemaResolverWrapper);
        if (referencedSchema != null) {
            Schema includedSchema = getSchema(referencedSchema, schemaResolverWrapper, true);
            nextInclude.setSchema(includedSchema);
        }
    }
    return schema;
}
Also used : Import(org.eclipse.persistence.internal.oxm.schema.model.Import) XMLContext(org.eclipse.persistence.oxm.XMLContext) Schema(org.eclipse.persistence.internal.oxm.schema.model.Schema) Include(org.eclipse.persistence.internal.oxm.schema.model.Include) XMLUnmarshaller(org.eclipse.persistence.oxm.XMLUnmarshaller) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source)

Aggregations

XMLContext (org.eclipse.persistence.oxm.XMLContext)170 Project (org.eclipse.persistence.sessions.Project)80 XMLMarshaller (org.eclipse.persistence.oxm.XMLMarshaller)68 Document (org.w3c.dom.Document)57 StringReader (java.io.StringReader)56 Test (org.junit.Test)41 ObjectPersistenceWorkbenchXMLProject (org.eclipse.persistence.internal.sessions.factories.ObjectPersistenceWorkbenchXMLProject)40 XMLUnmarshaller (org.eclipse.persistence.oxm.XMLUnmarshaller)39 PLSQLStoredProcedureCall (org.eclipse.persistence.platform.database.oracle.plsql.PLSQLStoredProcedureCall)36 InputStream (java.io.InputStream)30 NonJDBCTestHelper.buildTestProject (org.eclipse.persistence.testing.tests.nonJDBC.NonJDBCTestHelper.buildTestProject)30 NonJDBCTestHelper.buildWorkbenchXMLProject (org.eclipse.persistence.testing.tests.nonJDBC.NonJDBCTestHelper.buildWorkbenchXMLProject)30 DataModifyQuery (org.eclipse.persistence.queries.DataModifyQuery)21 SchemaModelProject (org.eclipse.persistence.internal.oxm.schema.SchemaModelProject)18 XMLDescriptor (org.eclipse.persistence.oxm.XMLDescriptor)17 XMLLogin (org.eclipse.persistence.oxm.XMLLogin)17 StringWriter (java.io.StringWriter)15 StreamSource (javax.xml.transform.stream.StreamSource)15 JAXBException (jakarta.xml.bind.JAXBException)14 Map (java.util.Map)14