Search in sources :

Example 26 with XMLContext

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

the class XMLChoiceCollectionWithGroupingElementTestCases method testReadDeploymentXML.

public void testReadDeploymentXML() {
    try {
        // Read the deploymentXML-file.xml back in with XMLProjectReader
        Project newProject = XMLProjectReader.read(DEPLOYMENT_XML_RESOURCE, Thread.currentThread().getContextClassLoader());
        XMLContext ctx = new XMLContext(newProject);
        XMLUnmarshaller unmarshaller = ctx.createUnmarshaller();
        InputStream instream = ClassLoader.getSystemResourceAsStream(XML_RESOURCE);
        Employee emp = (Employee) unmarshaller.unmarshal(instream);
        instream.close();
        Object[] choices = emp.choice.toArray();
        assertTrue("Choice collection did not unmarshal properly", (choices != null && choices.length > 0));
    } catch (Exception x) {
        x.printStackTrace();
        fail("Deployment XML read test failed");
    }
}
Also used : Project(org.eclipse.persistence.sessions.Project) XMLContext(org.eclipse.persistence.oxm.XMLContext) InputStream(java.io.InputStream) XMLUnmarshaller(org.eclipse.persistence.oxm.XMLUnmarshaller)

Example 27 with XMLContext

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

the class XMLProjectReader method readObjectPersistenceRuntimeFormat.

/**
 * Read a project in the format of an ObjectPersistenceRuntimeXMLProject.
 * This could include a TopLink 11.1.1 project or a TopLink 10.1.3 project
 */
public static Project readObjectPersistenceRuntimeFormat(Document document, ClassLoader classLoader, Project opmProject) {
    XMLLogin xmlLogin = new XMLLogin();
    xmlLogin.setDatasourcePlatform(new org.eclipse.persistence.oxm.platform.DOMPlatform());
    opmProject.setDatasourceLogin(xmlLogin);
    // Create the OPM project.
    if (classLoader != null) {
        xmlLogin.getDatasourcePlatform().getConversionManager().setLoader(classLoader);
    }
    // Marshal OPM format.
    XMLContext context = new XMLContext(opmProject);
    context.getSession(Project.class).getEventManager().addListener(new MissingDescriptorListener());
    XMLUnmarshaller unmarshaller = context.createUnmarshaller();
    Project project = (Project) unmarshaller.unmarshal(document);
    // Set the project's class loader.
    if ((classLoader != null) && (project.getDatasourceLogin() != null)) {
        project.getDatasourceLogin().getDatasourcePlatform().getConversionManager().setLoader(classLoader);
    }
    return project;
}
Also used : EclipseLinkObjectPersistenceRuntimeXMLProject(org.eclipse.persistence.internal.sessions.factories.EclipseLinkObjectPersistenceRuntimeXMLProject) Project(org.eclipse.persistence.sessions.Project) ObjectPersistenceRuntimeXMLProject(org.eclipse.persistence.internal.sessions.factories.ObjectPersistenceRuntimeXMLProject) XMLContext(org.eclipse.persistence.oxm.XMLContext) XMLLogin(org.eclipse.persistence.oxm.XMLLogin) MissingDescriptorListener(org.eclipse.persistence.internal.sessions.factories.MissingDescriptorListener) XMLUnmarshaller(org.eclipse.persistence.oxm.XMLUnmarshaller)

Example 28 with XMLContext

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

the class XMLSessionConfigLoader method buildSessionConfigs.

private boolean buildSessionConfigs(SessionManager sessionManager, ClassLoader loader, Document document, Project project) {
    // No errors occurred, unmasrshal the document which will return a
    // SessionConfigs containing 0 or more SessionConfigs and send
    // them through the factory to create actual Sessions
    XMLContext context = new XMLContext(project);
    XMLUnmarshaller unmarshaller = context.createUnmarshaller();
    SessionConfigs configs = (SessionConfigs) unmarshaller.unmarshal(document);
    SessionsFactory factory = new SessionsFactory();
    Map<String, Session> sessions = factory.buildSessionConfigs(configs, loader);
    for (Map.Entry<String, Session> entry : sessions.entrySet()) {
        // Only add the session if missing.
        if (!sessionManager.getSessions().containsKey(entry.getKey())) {
            sessionManager.addSession(entry.getKey(), entry.getValue());
        }
    }
    return true;
}
Also used : SessionConfigs(org.eclipse.persistence.internal.sessions.factories.model.SessionConfigs) XMLContext(org.eclipse.persistence.oxm.XMLContext) SessionsFactory(org.eclipse.persistence.internal.sessions.factories.SessionsFactory) XMLUnmarshaller(org.eclipse.persistence.oxm.XMLUnmarshaller) Map(java.util.Map) Session(org.eclipse.persistence.sessions.Session)

Example 29 with XMLContext

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

the class XMLSessionConfigLoader method loadConfigsForMappingWorkbench.

/**
 * INTERNAL:
 * This method is to be used to load config objects for the Mapping Workbench
 * only. Do not call this method.
 */
public SessionConfigs loadConfigsForMappingWorkbench(ClassLoader loader, boolean validate) {
    Document document = loadDocument(loader, validate);
    if (getExceptionStore().isEmpty()) {
        if (document.getDocumentElement().getTagName().equals("sessions")) {
            // No errors occurred, unmarshal the document which will return a
            // SessionConfigs containing 0 or more SessionConfigs
            XMLContext context = new XMLContext(getProject());
            XMLUnmarshaller unmarshaller = context.createUnmarshaller();
            return (SessionConfigs) unmarshaller.unmarshal(document);
        } else {
            // 9.0.4 session.xml or invalid xml format.
            throw SessionLoaderException.InvalidSessionXML();
        }
    } else {
        if (document.getDocumentElement().getTagName().equals("toplink-sessions")) {
            // No errors occurred, unmarshal the document which will return a
            // SessionConfigs containing 0 or more SessionConfigs
            XMLContext context = new XMLContext(new XMLSessionConfigToplinkProject());
            XMLUnmarshaller unmarshaller = context.createUnmarshaller();
            return (SessionConfigs) unmarshaller.unmarshal(document);
        } else {
            // Throw the exceptions we encountered
            throw SessionLoaderException.finalException(getExceptionStore());
        }
    }
}
Also used : SessionConfigs(org.eclipse.persistence.internal.sessions.factories.model.SessionConfigs) XMLContext(org.eclipse.persistence.oxm.XMLContext) XMLUnmarshaller(org.eclipse.persistence.oxm.XMLUnmarshaller) Document(org.w3c.dom.Document) XMLSessionConfigToplinkProject(org.eclipse.persistence.internal.sessions.factories.XMLSessionConfigToplinkProject)

Example 30 with XMLContext

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

the class XMLMarshallerCreateTestCases method testInvalidMappingTypeRelational.

public void testInvalidMappingTypeRelational() {
    Project project = new Project();
    XMLDescriptor descriptor = new XMLDescriptor();
    descriptor.setJavaClass(Car.class);
    descriptor.setDefaultRootElement("test");
    DirectToFieldMapping mapping = new DirectToFieldMapping();
    mapping.setAttributeName("license");
    mapping.setFieldName("license");
    descriptor.addMapping(mapping);
    project.addDescriptor(descriptor);
    try {
        XMLContext context = new XMLContext(project);
    } catch (DescriptorException exception) {
        assertTrue("An invalid mapping type exception should have been caught but wasn't.", exception.getErrorCode() == DescriptorException.INVALID_MAPPING_TYPE);
        return;
    } catch (IntegrityException exception) {
        Vector exceptions = exception.getIntegrityChecker().getCaughtExceptions();
        assertTrue("Too many exceptions were found...should have been 1.", exceptions.size() == 1);
        Exception e = (Exception) exceptions.elementAt(0);
        assertTrue("A DescriptorException should have been caught but wasn't.", e.getClass() == DescriptorException.class);
        assertTrue("An invalid mapping type exception should have been caught but wasn't.", ((DescriptorException) e).getErrorCode() == DescriptorException.INVALID_MAPPING_TYPE);
        return;
    }
    assertTrue("An invalid mapping type exception should have been caught but wasn't.", false);
}
Also used : DirectToFieldMapping(org.eclipse.persistence.mappings.DirectToFieldMapping) Project(org.eclipse.persistence.sessions.Project) XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) XMLContext(org.eclipse.persistence.oxm.XMLContext) DescriptorException(org.eclipse.persistence.exceptions.DescriptorException) IntegrityException(org.eclipse.persistence.exceptions.IntegrityException) Vector(java.util.Vector) XMLMarshalException(org.eclipse.persistence.exceptions.XMLMarshalException) DescriptorException(org.eclipse.persistence.exceptions.DescriptorException) IntegrityException(org.eclipse.persistence.exceptions.IntegrityException) ValidationException(org.eclipse.persistence.exceptions.ValidationException)

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