Search in sources :

Example 16 with Generator

use of org.eclipse.persistence.jaxb.compiler.Generator in project eclipselink by eclipse-ee4j.

the class SessionEventTestCases method setupContext.

protected JAXBContext setupContext(Class<?>[] classesToBeBound, SessionEventListener sel) throws Exception {
    jakarta.xml.bind.JAXBContext jaxbContext = null;
    XMLContext xmlContext = null;
    JaxbClassLoader loader = new JaxbClassLoader(Thread.currentThread().getContextClassLoader());
    try {
        Generator generator = new Generator(new JavaModelInputImpl(classesToBeBound, new JavaModelImpl(loader)));
        CoreProject proj = generator.generateProject();
        ConversionManager conversionManager = null;
        conversionManager = new ConversionManager();
        conversionManager.setLoader(loader);
        proj.convertClassNamesToClasses(conversionManager.getLoader());
        for (Iterator<ClassDescriptor> descriptorIt = proj.getOrderedDescriptors().iterator(); descriptorIt.hasNext(); ) {
            ClassDescriptor descriptor = descriptorIt.next();
            if (descriptor.getJavaClass() == null) {
                descriptor.setJavaClass(conversionManager.convertClassNameToClass(descriptor.getJavaClassName()));
            }
        }
        xmlContext = new XMLContext((Project) proj, loader, sel);
        jaxbContext = new org.eclipse.persistence.jaxb.JAXBContext(xmlContext, generator, classesToBeBound);
    } catch (Exception ex) {
        throw ex;
    }
    return jaxbContext;
}
Also used : JAXBContext(jakarta.xml.bind.JAXBContext) JavaModelImpl(org.eclipse.persistence.jaxb.javamodel.reflection.JavaModelImpl) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) XMLContext(org.eclipse.persistence.oxm.XMLContext) JAXBException(jakarta.xml.bind.JAXBException) JavaModelInputImpl(org.eclipse.persistence.jaxb.javamodel.reflection.JavaModelInputImpl) CoreProject(org.eclipse.persistence.core.sessions.CoreProject) Project(org.eclipse.persistence.sessions.Project) CoreProject(org.eclipse.persistence.core.sessions.CoreProject) ConversionManager(org.eclipse.persistence.internal.helper.ConversionManager) JaxbClassLoader(org.eclipse.persistence.internal.jaxb.JaxbClassLoader) Generator(org.eclipse.persistence.jaxb.compiler.Generator)

Example 17 with Generator

use of org.eclipse.persistence.jaxb.compiler.Generator in project eclipselink by eclipse-ee4j.

the class JAXBContextTransformationMappingTestCases method testInvalidTransformerClass.

/**
 * Test exception handling:  in this case an invalid transformer class
 * has been set.  An exception should be thrown.
 *
 * Negative test.
 */
public void testInvalidTransformerClass() {
    InputStream inputStream = ClassLoader.getSystemResourceAsStream(METADATA_FILE_BAD_CLASS);
    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);
    int exceptionCount = 0;
    // test exception from MappingsGenerator
    try {
        JAXBContext jaxbContext = JAXBContextFactory.createContext(new Class<?>[] { Employee.class }, properties, Thread.currentThread().getContextClassLoader());
    } catch (JAXBException e) {
        exceptionCount++;
    }
    assertTrue("The expected exception was never thrown.", exceptionCount > 0);
    exceptionCount--;
    // test exception from SchemaGenerator
    try {
        Map<String, XmlBindings> bindings = JAXBContextFactory.getXmlBindingsFromProperties(properties, Thread.currentThread().getContextClassLoader());
        JavaModelInputImpl jModelInput = new JavaModelInputImpl(new Class<?>[] { Employee.class }, new JavaModelImpl(new JaxbClassLoader(Thread.currentThread().getContextClassLoader(), new Class<?>[] { Employee.class })));
        Generator generator = new Generator(jModelInput, bindings, Thread.currentThread().getContextClassLoader(), "", false);
        generator.generateSchema();
    } catch (Exception e) {
        exceptionCount++;
    }
    assertTrue("The expected exception was never thrown.", exceptionCount > 0);
}
Also used : XmlBindings(org.eclipse.persistence.jaxb.xmlmodel.XmlBindings) JavaModelImpl(org.eclipse.persistence.jaxb.javamodel.reflection.JavaModelImpl) HashMap(java.util.HashMap) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) JAXBException(jakarta.xml.bind.JAXBException) JAXBContext(jakarta.xml.bind.JAXBContext) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) JAXBException(jakarta.xml.bind.JAXBException) JavaModelInputImpl(org.eclipse.persistence.jaxb.javamodel.reflection.JavaModelInputImpl) JaxbClassLoader(org.eclipse.persistence.internal.jaxb.JaxbClassLoader) Generator(org.eclipse.persistence.jaxb.compiler.Generator)

Example 18 with Generator

use of org.eclipse.persistence.jaxb.compiler.Generator in project eclipselink by eclipse-ee4j.

the class SchemaGenEmployeeTestCases method xtestAbstractSuperclassVaildDoc.

/**
 * In this test the xsi:type of the root element set to subclass, which is valid.
 */
public void xtestAbstractSuperclassVaildDoc() throws Exception {
    boolean exception = false;
    String msg = null;
    String src = "org/eclipse/persistence/testing/jaxb/schemagen/employee/valid_inheritance.xml";
    File newXsd = new File(tmpdir, "schema1.xsd");
    if (newXsd.exists() && newXsd.isFile() && newXsd.delete()) {
        System.err.println("removed existing: " + newXsd.getAbsolutePath());
    }
    try {
        Class<?>[] jClasses = new Class<?>[] { MyAbstractTestType.class, MyTestSubType.class };
        Generator gen = new Generator(new JavaModelInputImpl(jClasses, new JavaModelImpl(Thread.currentThread().getContextClassLoader())));
        gen.generateSchemaFiles(tmpdir, null);
        SchemaFactory sFact = SchemaFactory.newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Schema theSchema = sFact.newSchema(newXsd);
        Validator validator = theSchema.newValidator();
        StreamSource ss = new StreamSource(new File(src));
        validator.validate(ss);
    } catch (Exception ex) {
        exception = true;
        msg = ex.toString();
    }
    assertFalse("Schema validation failed unexpectedly: " + msg, exception);
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) JavaModelImpl(org.eclipse.persistence.jaxb.javamodel.reflection.JavaModelImpl) Schema(javax.xml.validation.Schema) StreamSource(javax.xml.transform.stream.StreamSource) JavaModelInputImpl(org.eclipse.persistence.jaxb.javamodel.reflection.JavaModelInputImpl) File(java.io.File) Validator(javax.xml.validation.Validator) Generator(org.eclipse.persistence.jaxb.compiler.Generator)

Example 19 with Generator

use of org.eclipse.persistence.jaxb.compiler.Generator in project eclipselink by eclipse-ee4j.

the class JAXBContext method generateSchema.

/**
 * Generate a Schema for this JAXBContext
 *
 * @param outputResolver Class that decides where the schema file (of the given namespace URI) will be written
 * @param additonalGlobalElements Map of additional global elements to be added to the generated XSD.
 * Note that if any QName in this map conflicts with another global element (for example from a TypeMappingInfo object)
 * then the element generated from this map will be the one that is present in the XSD.
 */
public void generateSchema(SchemaOutputResolver outputResolver, Map<QName, Type> additonalGlobalElements) {
    JAXBContextState currentJAXBContextState = contextState;
    if (isRefreshable()) {
        // Recreate context state, to rebuild Generator
        try {
            currentJAXBContextState = newContextState();
        } catch (Exception e) {
            throw JAXBException.exceptionDuringSchemaGeneration(e);
        }
    }
    XMLContext xmlContext = currentJAXBContextState.getXMLContext();
    Generator generator = currentJAXBContextState.getGenerator();
    if (generator == null) {
        SchemaModelGenerator smGen = new SchemaModelGenerator(xmlContext.getOxmConversionManager());
        smGen.generateSchemas(xmlContext.getDescriptors(), null, new JAXBSchemaOutputResolver(outputResolver), additonalGlobalElements);
    } else {
        generator.generateSchemaFiles(outputResolver, additonalGlobalElements);
    }
}
Also used : JAXBSchemaOutputResolver(org.eclipse.persistence.internal.jaxb.JAXBSchemaOutputResolver) XMLContext(org.eclipse.persistence.oxm.XMLContext) SchemaModelGenerator(org.eclipse.persistence.internal.oxm.schema.SchemaModelGenerator) JAXBException(org.eclipse.persistence.exceptions.JAXBException) ConversionException(org.eclipse.persistence.exceptions.ConversionException) PropertyException(jakarta.xml.bind.PropertyException) Generator(org.eclipse.persistence.jaxb.compiler.Generator) SchemaModelGenerator(org.eclipse.persistence.internal.oxm.schema.SchemaModelGenerator) JsonSchemaGenerator(org.eclipse.persistence.internal.jaxb.json.schema.JsonSchemaGenerator)

Example 20 with Generator

use of org.eclipse.persistence.jaxb.compiler.Generator 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)

Aggregations

Generator (org.eclipse.persistence.jaxb.compiler.Generator)27 JavaModelImpl (org.eclipse.persistence.jaxb.javamodel.reflection.JavaModelImpl)26 JavaModelInputImpl (org.eclipse.persistence.jaxb.javamodel.reflection.JavaModelInputImpl)26 StreamSource (javax.xml.transform.stream.StreamSource)18 File (java.io.File)15 Schema (javax.xml.validation.Schema)14 SchemaFactory (javax.xml.validation.SchemaFactory)14 Validator (javax.xml.validation.Validator)14 InputStream (java.io.InputStream)9 JaxbClassLoader (org.eclipse.persistence.internal.jaxb.JaxbClassLoader)9 JAXBException (jakarta.xml.bind.JAXBException)4 HashMap (java.util.HashMap)4 Source (javax.xml.transform.Source)4 CoreProject (org.eclipse.persistence.core.sessions.CoreProject)4 Property (org.eclipse.persistence.jaxb.compiler.Property)4 TypeInfo (org.eclipse.persistence.jaxb.compiler.TypeInfo)4 XmlBindings (org.eclipse.persistence.jaxb.xmlmodel.XmlBindings)4 XMLContext (org.eclipse.persistence.oxm.XMLContext)4 JAXBContext (jakarta.xml.bind.JAXBContext)3 IOException (java.io.IOException)3