Search in sources :

Example 26 with Schema

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

the class PathbasedMappingTestCases method testSchemaGen.

public void testSchemaGen() throws Exception {
    Schema generatedSchema = null;
    Document tDoc = null;
    Document cDoc = null;
    boolean exception = false;
    String msg = null;
    try {
        SchemaModelGeneratorProperties props = new SchemaModelGeneratorProperties();
        props.addProperty(MYNS, SchemaModelGeneratorProperties.ELEMENT_FORM_QUALIFIED_KEY, false);
        XMLContext xCtx = new XMLContext("org.eclipse.persistence.testing.oxm.schemamodelgenerator.mappings.pathbased", Thread.currentThread().getContextClassLoader());
        Project prj = xCtx.getSession(0).getProject();
        loginProject(prj);
        List<Descriptor> descriptorsToProcess = setupDescriptorList(prj);
        Map<String, Schema> generatedSchemas = sg.generateSchemas(descriptorsToProcess, props);
        generatedSchema = generatedSchemas.get(MYNS);
        assertNotNull("No schema was generated for namespace [" + MYNS + "]", generatedSchema);
        writeSchema(generatedSchema);
        tDoc = getDocument(generatedSchema);
        cDoc = getDocument(XSD_RESOURCE);
        assertNotNull("Schema to Document conversion failed", tDoc);
        assertNotNull("A problem occurred loading the control schema", cDoc);
        assertTrue("Schema comparsion failed", comparer.isSchemaEqual(cDoc, tDoc));
        SchemaFactory sFact = SchemaFactory.newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI);
        File schemaFile = new File(TMP_DIR, "generatedSchema.xsd");
        javax.xml.validation.Schema theSchema = sFact.newSchema(schemaFile);
        Validator validator = theSchema.newValidator();
        StreamSource ss = new StreamSource(new File(Thread.currentThread().getContextClassLoader().getResource(XML_RESOURCE).toURI()));
        validator.validate(ss);
    } catch (Exception ex) {
        exception = true;
        msg = ex.toString();
        ex.printStackTrace();
    }
    assertTrue("Schema validation failed unexpectedly: " + msg, exception == false);
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) XMLContext(org.eclipse.persistence.oxm.XMLContext) Schema(org.eclipse.persistence.internal.oxm.schema.model.Schema) StreamSource(javax.xml.transform.stream.StreamSource) Document(org.w3c.dom.Document) Project(org.eclipse.persistence.sessions.Project) SchemaModelGeneratorProperties(org.eclipse.persistence.internal.oxm.schema.SchemaModelGeneratorProperties) Descriptor(org.eclipse.persistence.internal.oxm.mappings.Descriptor) File(java.io.File) Validator(javax.xml.validation.Validator)

Example 27 with Schema

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

the class NillableSchemaTestCases method runTest.

public void runTest(boolean nillable) throws Exception {
    Schema generatedSchema = null;
    Document tDoc = null;
    Document cDoc = null;
    try {
        boolean setSchemaContext = true;
        boolean setDefaultRootElement = true;
        SchemaModelGeneratorProperties props = new SchemaModelGeneratorProperties();
        props.addProperty(MYNS, SchemaModelGeneratorProperties.ELEMENT_FORM_QUALIFIED_KEY, true);
        Project prj = new NillableTestProject(nillable);
        loginProject(prj);
        List<Descriptor> descriptorsToProcess = setupDescriptorList(prj);
        Map<String, Schema> generatedSchemas = sg.generateSchemas(descriptorsToProcess, props);
        generatedSchema = generatedSchemas.get(MYNS);
        // debugging
        // writeSchema(generatedSchema);
        assertNotNull("No schema was generated for namespace [" + MYNS + "]", generatedSchema);
        tDoc = getDocument(generatedSchema);
        if (nillable) {
            cDoc = getDocument(NILLABLE_RESOURCE);
        } else {
            cDoc = getDocument(NON_NILLABLE_RESOURCE);
        }
    } catch (Exception ex) {
        ex.printStackTrace();
        fail();
    }
    assertNotNull("Schema to Document conversion failed", tDoc);
    assertNotNull("A problem occurred loading the control schema", cDoc);
    assertTrue("Schema comparsion failed", comparer.isSchemaEqual(cDoc, tDoc));
}
Also used : TestProject(org.eclipse.persistence.testing.oxm.schemamodelgenerator.TestProject) Project(org.eclipse.persistence.sessions.Project) Schema(org.eclipse.persistence.internal.oxm.schema.model.Schema) SchemaModelGeneratorProperties(org.eclipse.persistence.internal.oxm.schema.SchemaModelGeneratorProperties) Descriptor(org.eclipse.persistence.internal.oxm.mappings.Descriptor) XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) Document(org.w3c.dom.Document)

Example 28 with Schema

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

the class RequiredSchemaTestCases method runTest.

public void runTest(boolean required) throws Exception {
    Schema generatedSchema = null;
    Document tDoc = null;
    Document cDoc = null;
    try {
        boolean setSchemaContext = true;
        boolean setDefaultRootElement = true;
        SchemaModelGeneratorProperties props = new SchemaModelGeneratorProperties();
        props.addProperty(MYNS, SchemaModelGeneratorProperties.ELEMENT_FORM_QUALIFIED_KEY, true);
        Project prj = new RequiredTestProject(required);
        loginProject(prj);
        List<Descriptor> descriptorsToProcess = setupDescriptorList(prj);
        Map<String, Schema> generatedSchemas = sg.generateSchemas(descriptorsToProcess, props);
        generatedSchema = generatedSchemas.get(MYNS);
        // debugging
        // writeSchema(generatedSchema);
        assertNotNull("No schema was generated for namespace [" + MYNS + "]", generatedSchema);
        tDoc = getDocument(generatedSchema);
        if (required) {
            cDoc = getDocument(REQUIRED_RESOURCE);
        } else {
            cDoc = getDocument(NON_REQUIRED_RESOURCE);
        }
    } catch (Exception ex) {
        ex.printStackTrace();
        fail();
    }
    assertNotNull("Schema to Document conversion failed", tDoc);
    assertNotNull("A problem occurred loading the control schema", cDoc);
    assertTrue("Schema comparsion failed", comparer.isSchemaEqual(cDoc, tDoc));
}
Also used : TestProject(org.eclipse.persistence.testing.oxm.schemamodelgenerator.TestProject) Project(org.eclipse.persistence.sessions.Project) Schema(org.eclipse.persistence.internal.oxm.schema.model.Schema) SchemaModelGeneratorProperties(org.eclipse.persistence.internal.oxm.schema.SchemaModelGeneratorProperties) Descriptor(org.eclipse.persistence.internal.oxm.mappings.Descriptor) XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) Document(org.w3c.dom.Document)

Example 29 with Schema

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

the class SchemaModelGenerator method generateSchemas.

/**
 * Generates a Map of EclipseLink schema model Schema objects for a given list of XMLDescriptors.
 * The descriptors are assumed to have been initialized.  One Schema  object will be generated
 * per namespace.
 *
 * @param descriptorsToProcess list of XMLDescriptors which will be used to generate Schema objects
 * @param properties holds a namespace to Properties map containing schema settings, such as elementFormDefault
 * @param additionalGlobalElements a map of QName-Type entries identifying additional global elements to be added
 * @return a map of namespaces to EclipseLink schema model Schema objects
 * @throws DescriptorException if the reference descriptor for a composite mapping is not in the list of descriptors
 * @see Schema
 */
public Map<String, Schema> generateSchemas(List<Descriptor> descriptorsToProcess, SchemaModelGeneratorProperties properties, SchemaModelOutputResolver outputResolver, Map<QName, Type> additionalGlobalElements) throws DescriptorException {
    Map<String, Schema> schemas = generateSchemas(descriptorsToProcess, properties, additionalGlobalElements);
    // write out the generates schema(s) via the given output resolver
    Project proj = new SchemaModelProject();
    XMLContext context = new XMLContext(proj);
    XMLMarshaller marshaller = context.createMarshaller();
    Descriptor schemaDescriptor = (Descriptor) proj.getDescriptor(Schema.class);
    int schemaCount = 0;
    for (Entry<String, Schema> entry : schemas.entrySet()) {
        Schema schema = entry.getValue();
        try {
            NamespaceResolver schemaNamespaces = schema.getNamespaceResolver();
            schemaNamespaces.put(Constants.SCHEMA_PREFIX, "http://www.w3.org/2001/XMLSchema");
            schemaDescriptor.setNamespaceResolver(schemaNamespaces);
            javax.xml.transform.Result target = outputResolver.createOutput(schema.getTargetNamespace(), schema.getName());
            marshaller.marshal(schema, target);
            schemaCount++;
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    return schemas;
}
Also used : XMLContext(org.eclipse.persistence.oxm.XMLContext) XMLMarshaller(org.eclipse.persistence.oxm.XMLMarshaller) Schema(org.eclipse.persistence.internal.oxm.schema.model.Schema) IOException(java.io.IOException) Project(org.eclipse.persistence.sessions.Project) Descriptor(org.eclipse.persistence.internal.oxm.mappings.Descriptor) XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) NamespaceResolver(org.eclipse.persistence.internal.oxm.NamespaceResolver)

Example 30 with Schema

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

the class SchemaModelGenerator method handleFragNamespace.

/**
 * This method will generate a global element if required (based in URI and elementFormDefault) and
 * set a reference to it on a given element accordingly.  This method will typically be used for
 * direct mappings.
 */
protected Element handleFragNamespace(XPathFragment frag, HashMap<String, Schema> schemaForNamespace, Schema workingSchema, SchemaModelGeneratorProperties properties, Element element, String schemaTypeString) {
    String fragUri = frag.getNamespaceURI();
    // may need to add a global element
    Schema s = getSchema(fragUri, null, schemaForNamespace, properties);
    String targetNS = workingSchema.getTargetNamespace();
    if ((s.isElementFormDefault() && !fragUri.equals(targetNS)) || (!s.isElementFormDefault() && fragUri.length() > 0)) {
        if (s.getTopLevelElements().get(frag.getLocalName()) == null) {
            Element globalElement = new Element();
            globalElement.setName(frag.getLocalName());
            globalElement.setType(schemaTypeString);
            s.addTopLevelElement(globalElement);
        }
        element = new Element();
        element.setRef(frag.getShortName());
    } else {
        element = buildElement(frag, schemaTypeString, Occurs.ZERO, null);
    }
    return element;
}
Also used : Schema(org.eclipse.persistence.internal.oxm.schema.model.Schema) Element(org.eclipse.persistence.internal.oxm.schema.model.Element)

Aggregations

Schema (org.eclipse.persistence.internal.oxm.schema.model.Schema)44 Descriptor (org.eclipse.persistence.internal.oxm.mappings.Descriptor)18 SchemaModelProject (org.eclipse.persistence.internal.oxm.schema.SchemaModelProject)17 Project (org.eclipse.persistence.sessions.Project)17 XMLDescriptor (org.eclipse.persistence.oxm.XMLDescriptor)16 Element (org.eclipse.persistence.internal.oxm.schema.model.Element)15 ComplexType (org.eclipse.persistence.internal.oxm.schema.model.ComplexType)14 SchemaModelGeneratorProperties (org.eclipse.persistence.internal.oxm.schema.SchemaModelGeneratorProperties)13 XmlVirtualAccessMethodsSchema (org.eclipse.persistence.jaxb.xmlmodel.XmlVirtualAccessMethodsSchema)12 XMLContext (org.eclipse.persistence.oxm.XMLContext)12 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)10 Document (org.w3c.dom.Document)10 QName (javax.xml.namespace.QName)7 StreamSource (javax.xml.transform.stream.StreamSource)7 NamespaceResolver (org.eclipse.persistence.oxm.NamespaceResolver)7 Sequence (org.eclipse.persistence.internal.oxm.schema.model.Sequence)6 TypeDefParticle (org.eclipse.persistence.internal.oxm.schema.model.TypeDefParticle)6 IOException (java.io.IOException)5 StringWriter (java.io.StringWriter)5 XMLMarshaller (org.eclipse.persistence.oxm.XMLMarshaller)5