Search in sources :

Example 21 with XmlBindings

use of org.eclipse.persistence.jaxb.xmlmodel.XmlBindings in project eclipselink by eclipse-ee4j.

the class XmlBindingsGenerator method generateXmlBindings.

/**
 * Generate an XmlBindings instance based on a list of XML descriptors.
 *
 * OXM metadata files are processed on a per package basis, hence it is
 * assumed that the given list of descriptors are from the same package.
 */
public static XmlBindings generateXmlBindings(String packageName, List<XMLDescriptor> descriptors) {
    String defaultNamespace = null;
    Map<String, String> prefixMap = new HashMap<String, String>();
    JavaTypes jTypes = new JavaTypes();
    for (XMLDescriptor xdesc : descriptors) {
        // mappings to the map
        if (xdesc.getNamespaceResolver() != null) {
            if (defaultNamespace == null) {
                defaultNamespace = xdesc.getNamespaceResolver().getDefaultNamespaceURI();
            }
            Map<String, String> preMap = xdesc.getNamespaceResolver().getPrefixesToNamespaces();
            for (String pfx : preMap.keySet()) {
                // ignore mime prefix/url for now
                if (!pfx.equals(XML_MIME_PREFIX)) {
                    prefixMap.put(pfx, preMap.get(pfx));
                }
            }
        }
        // generate a JavaType instance for the XML descriptor
        jTypes.getJavaType().add(generateJavaType(xdesc));
    }
    XmlBindings xmlBindings = null;
    // if there are no JavaTypes, there's nothing to do
    if (jTypes.getJavaType().size() > 0) {
        xmlBindings = new XmlBindings();
        xmlBindings.setJavaTypes(jTypes);
        xmlBindings.setPackageName(packageName);
        // handle XmlSchema
        if (defaultNamespace != null || !prefixMap.isEmpty()) {
            XmlSchema xSchema = new XmlSchema();
            xSchema.setNamespace(defaultNamespace == null ? EMPTY_STRING : defaultNamespace);
            xSchema.setElementFormDefault(XmlNsForm.QUALIFIED);
            // handle XmlNs
            if (!prefixMap.isEmpty()) {
                XmlNs xmlNs;
                for (String pfx : prefixMap.keySet()) {
                    xmlNs = new XmlNs();
                    xmlNs.setNamespaceUri(prefixMap.get(pfx));
                    xmlNs.setPrefix(pfx);
                    xSchema.getXmlNs().add(xmlNs);
                }
            }
            xmlBindings.setXmlSchema(xSchema);
        }
    }
    return xmlBindings;
}
Also used : JavaTypes(org.eclipse.persistence.jaxb.xmlmodel.XmlBindings.JavaTypes) XmlBindings(org.eclipse.persistence.jaxb.xmlmodel.XmlBindings) XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) HashMap(java.util.HashMap) XmlSchema(org.eclipse.persistence.jaxb.xmlmodel.XmlSchema) XmlNs(org.eclipse.persistence.jaxb.xmlmodel.XmlSchema.XmlNs)

Example 22 with XmlBindings

use of org.eclipse.persistence.jaxb.xmlmodel.XmlBindings in project eclipselink by eclipse-ee4j.

the class JAXBContextFactory method processBindingFile.

/**
 * Processing a bindings file and add it to a given Map of package name to binding
 * files.
 *
 * @param originalBindings Map of bindings to be updated
 * @param bindingHandle handle to bindings file
 */
private static Map<String, List<XmlBindings>> processBindingFile(Map<String, List<XmlBindings>> originalBindings, Object bindingHandle, ClassLoader classLoader, Map<String, Object> properties) {
    XmlBindings binding = getXmlBindings(bindingHandle, classLoader, properties);
    if (binding != null) {
        String key = binding.getPackageName();
        if (key.equals(XMLProcessor.DEFAULT)) {
            throw org.eclipse.persistence.exceptions.JAXBException.packageNotSetForBindingException();
        }
        List<XmlBindings> existingBindings = originalBindings.get(key);
        if (existingBindings != null) {
            existingBindings.add(binding);
        } else {
            existingBindings = new ArrayList<XmlBindings>();
            existingBindings.add(binding);
            originalBindings.put(key, existingBindings);
        }
    }
    return originalBindings;
}
Also used : XmlBindings(org.eclipse.persistence.jaxb.xmlmodel.XmlBindings)

Example 23 with XmlBindings

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

Example 24 with XmlBindings

use of org.eclipse.persistence.jaxb.xmlmodel.XmlBindings in project eclipselink by eclipse-ee4j.

the class TransformerPropertyBuilderTestCase method testTransformerPropertyBuilder.

@Test
public void testTransformerPropertyBuilder() {
    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);
    // Indirect call of org.eclipse.persistence.jaxb.compiler.builder.TransformerPropertyBuilder.getPropertyName(...) and org.eclipse.persistence.jaxb.compiler.builder.TransformerPropertyBuilder.buildPropertyFromTransformer(...)
    // Indirect call of org.eclipse.persistence.jaxb.compiler.builder.TransformerPropertyBuilder.getTransformerJavaClass(...) with booth options writeTransformer.isSetTransformerClass() true,false
    List<Property> props = transformerPropertyBuilder.buildProperties();
    assertEquals(2, props.size());
    // Verification of result of org.eclipse.persistence.jaxb.compiler.builder.TransformerPropertyBuilder.getPropertyName(...)
    // Verify property names
    assertEquals("start-time", props.get(0).getPropertyName());
    assertEquals("end-time", props.get(1).getPropertyName());
    // Verification of result of org.eclipse.persistence.jaxb.compiler.builder.TransformerPropertyBuilder.getReturnTypeFromTransformer(...)
    // Verify property types
    assertEquals(String.class.getName(), props.get(0).getType().getName());
    assertEquals(String.class.getName(), props.get(1).getType().getName());
    assertEquals(NormalHoursAttributeTransformer.class.getName(), normalHoursProperty.getXmlTransformation().getXmlReadTransformer().getTransformerClass());
    assertEquals(StartTimeTransformer.class.getName(), normalHoursProperty.getXmlTransformation().getXmlWriteTransformer().get(0).getTransformerClass());
    assertEquals(String.class.getName(), normalHoursProperty.getGenericType().getName());
    // Indirect call of org.eclipse.persistence.jaxb.compiler.builder.TransformerPropertyBuilder.getTransformerJavaClass(...) with invalid TransformerClass name
    try {
        normalHoursProperty.getXmlTransformation().getXmlWriteTransformer().get(0).setTransformerClass("xxx.xxx.WrongClassName");
        transformerPropertyBuilder.buildProperties();
        fail("Expected JAXBException.");
    } catch (JAXBException expected) {
        assertEquals(50054, expected.getErrorCode());
    }
}
Also used : XmlBindings(org.eclipse.persistence.jaxb.xmlmodel.XmlBindings) JavaModelImpl(org.eclipse.persistence.jaxb.javamodel.reflection.JavaModelImpl) NormalHoursAttributeTransformer(org.eclipse.persistence.testing.jaxb.externalizedmetadata.mappings.xmltransformation.NormalHoursAttributeTransformer) HashMap(java.util.HashMap) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) JAXBException(org.eclipse.persistence.exceptions.JAXBException) StartTimeTransformer(org.eclipse.persistence.testing.jaxb.externalizedmetadata.mappings.xmltransformation.StartTimeTransformer) TypeInfo(org.eclipse.persistence.jaxb.compiler.TypeInfo) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) JavaModelInputImpl(org.eclipse.persistence.jaxb.javamodel.reflection.JavaModelInputImpl) Helper(org.eclipse.persistence.jaxb.javamodel.Helper) TransformerPropertyBuilder(org.eclipse.persistence.jaxb.compiler.builder.TransformerPropertyBuilder) Property(org.eclipse.persistence.jaxb.compiler.Property) JaxbClassLoader(org.eclipse.persistence.internal.jaxb.JaxbClassLoader) Generator(org.eclipse.persistence.jaxb.compiler.Generator) Test(org.junit.Test)

Example 25 with XmlBindings

use of org.eclipse.persistence.jaxb.xmlmodel.XmlBindings in project eclipselink by eclipse-ee4j.

the class BaseDBWSBuilderHelper method writeOROXProjects.

public void writeOROXProjects(OutputStream dbwsOrStream, OutputStream dbwsOxStream) {
    Project orProject = dbwsBuilder.getOrProject();
    Project oxProject = dbwsBuilder.getOxProject();
    boolean writeORProject = false;
    if (hasTables() || dbwsBuilder.hasBuildSqlOperations()) {
        writeORProject = true;
    }
    if (!writeORProject) {
        // check for any named queries - SimpleXMLFormatProject sometimes need them
        if (orProject.getQueries().size() > 0) {
            writeORProject = true;
        } else // check for ObjectRelationalDataTypeDescriptor's - Advanced JDBC object/varray types
        if (orProject.getDescriptors().size() > 0) {
            Collection<ClassDescriptor> descriptors = orProject.getDescriptors().values();
            for (ClassDescriptor desc : descriptors) {
                if (desc.isObjectRelationalDataTypeDescriptor()) {
                    writeORProject = true;
                    break;
                }
            }
        }
    }
    if ((writeORProject || !dbwsBuilder.xrServiceModel.getOperations().isEmpty()) && !isNullStream(dbwsOrStream)) {
        XMLContext context = new XMLContext(workbenchXMLProject);
        context.getSession(orProject).getEventManager().addListener(new MissingDescriptorListener());
        XMLEntityMappings mappings = XmlEntityMappingsGenerator.generateXmlEntityMappings(orProject, complextypes, crudOps);
        if (mappings != null) {
            XMLEntityMappingsWriter.write(mappings, dbwsOrStream);
        }
    }
    if (!isNullStream(dbwsOxStream)) {
        boolean writeOXProject = false;
        if (hasTables() || dbwsBuilder.hasBuildSqlOperations()) {
            writeOXProject = true;
        }
        if (!writeOXProject) {
            // check for any named queries - SimpleXMLFormatProject's sometimes need them
            if (orProject.getQueries().size() > 0) {
                writeOXProject = true;
            } else // check for ObjectRelationalDataTypeDescriptor's - Advanced JDBC object/varray types
            if (orProject.getDescriptors().size() > 0) {
                Collection<ClassDescriptor> descriptors = orProject.getDescriptors().values();
                for (ClassDescriptor desc : descriptors) {
                    if (desc.isObjectRelationalDataTypeDescriptor()) {
                        writeOXProject = true;
                        break;
                    }
                }
            }
        }
        if (writeOXProject) {
            List<XmlBindings> xmlBindingsList = XmlBindingsGenerator.generateXmlBindings(oxProject.getOrderedDescriptors());
            if (xmlBindingsList.size() > 0) {
                XmlBindingsModel model = new XmlBindingsModel();
                model.setBindingsList(xmlBindingsList);
                try {
                    JAXBContext jc = JAXBContext.newInstance(XmlBindingsModel.class);
                    Marshaller marshaller = jc.createMarshaller();
                    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
                    marshaller.marshal(model, dbwsOxStream);
                } catch (JAXBException jaxbEx) {
                    throw new DBWSException(OXM_MARSHAL_EX_MSG, jaxbEx);
                }
            }
        }
    }
    dbwsBuilder.getPackager().closeOrStream(dbwsOrStream);
    dbwsBuilder.getPackager().closeOxStream(dbwsOxStream);
}
Also used : XmlBindings(org.eclipse.persistence.jaxb.xmlmodel.XmlBindings) Marshaller(jakarta.xml.bind.Marshaller) XMLMarshaller(org.eclipse.persistence.oxm.XMLMarshaller) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) XMLContext(org.eclipse.persistence.oxm.XMLContext) XMLEntityMappings(org.eclipse.persistence.internal.jpa.metadata.xml.XMLEntityMappings) JAXBException(jakarta.xml.bind.JAXBException) JAXBContext(jakarta.xml.bind.JAXBContext) MissingDescriptorListener(org.eclipse.persistence.internal.sessions.factories.MissingDescriptorListener) Project(org.eclipse.persistence.sessions.Project) SchemaModelProject(org.eclipse.persistence.internal.oxm.schema.SchemaModelProject) DBWSModelProject(org.eclipse.persistence.dbws.DBWSModelProject) ObjectPersistenceWorkbenchXMLProject(org.eclipse.persistence.internal.sessions.factories.ObjectPersistenceWorkbenchXMLProject) SimpleXMLFormatProject(org.eclipse.persistence.internal.xr.sxf.SimpleXMLFormatProject) DBWSException(org.eclipse.persistence.exceptions.DBWSException) Collection(java.util.Collection) PLSQLCollection(org.eclipse.persistence.platform.database.oracle.plsql.PLSQLCollection) XmlBindingsModel(org.eclipse.persistence.internal.xr.XmlBindingsModel)

Aggregations

XmlBindings (org.eclipse.persistence.jaxb.xmlmodel.XmlBindings)30 HashMap (java.util.HashMap)18 JAXBException (jakarta.xml.bind.JAXBException)16 StreamSource (javax.xml.transform.stream.StreamSource)16 JAXBContext (jakarta.xml.bind.JAXBContext)13 Unmarshaller (jakarta.xml.bind.Unmarshaller)12 Map (java.util.Map)11 StringReader (java.io.StringReader)10 XMLContext (org.eclipse.persistence.oxm.XMLContext)10 Project (org.eclipse.persistence.sessions.Project)10 XmlBindingsModel (org.eclipse.persistence.internal.xr.XmlBindingsModel)9 InputStream (java.io.InputStream)8 Platform (org.eclipse.persistence.internal.databaseaccess.Platform)8 ConversionManager (org.eclipse.persistence.internal.helper.ConversionManager)8 MetadataProcessor (org.eclipse.persistence.internal.jpa.metadata.MetadataProcessor)8 DatabaseSessionImpl (org.eclipse.persistence.internal.sessions.DatabaseSessionImpl)8 XRDynamicClassLoader (org.eclipse.persistence.internal.xr.XRDynamicClassLoader)8 XMLPlatform (org.eclipse.persistence.platform.xml.XMLPlatform)8 DatabaseLogin (org.eclipse.persistence.sessions.DatabaseLogin)8 DatabaseSession (org.eclipse.persistence.sessions.DatabaseSession)8