Search in sources :

Example 1 with XMLTransformationMapping

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

the class MappingsGenerator method generateTransformationMapping.

/**
 * Generate an XMLTransformationMapping based on a given Property.
 */
public TransformationMapping generateTransformationMapping(Property property, Descriptor descriptor, NamespaceInfo namespace) {
    TransformationMapping<AbstractSession, AttributeAccessor, ContainerPolicy, ClassDescriptor, DatabaseField, XMLTransformationRecord, XMLRecord> mapping = new XMLTransformationMapping();
    if (property.isMethodProperty()) {
        if (property.getGetMethodName() == null) {
            // handle case of set with no get method
            String paramTypeAsString = property.getType().getName();
            mapping.setAttributeAccessor(new JAXBSetMethodAttributeAccessor(paramTypeAsString, helper.getClassLoader()));
            mapping.setSetMethodName(property.getSetMethodName());
        } else if (property.getSetMethodName() == null) {
            mapping.setGetMethodName(property.getGetMethodName());
        } else {
            mapping.setSetMethodName(property.getSetMethodName());
            mapping.setGetMethodName(property.getGetMethodName());
        }
    }
    // handle transformation
    if (property.isSetXmlTransformation()) {
        XmlTransformation xmlTransformation = property.getXmlTransformation();
        mapping.setIsOptional(xmlTransformation.isOptional());
        // handle transformer(s)
        if (xmlTransformation.isSetXmlReadTransformer()) {
            // handle read transformer
            mapping.setAttributeName(property.getPropertyName());
            XmlReadTransformer readTransformer = xmlTransformation.getXmlReadTransformer();
            if (readTransformer.isSetTransformerClass()) {
                mapping.setAttributeTransformerClassName(xmlTransformation.getXmlReadTransformer().getTransformerClass());
            } else {
                mapping.setAttributeTransformation(xmlTransformation.getXmlReadTransformer().getMethod());
            }
        }
        if (xmlTransformation.isSetXmlWriteTransformers()) {
            // handle write transformer(s)
            for (XmlWriteTransformer writeTransformer : xmlTransformation.getXmlWriteTransformer()) {
                if (writeTransformer.isSetTransformerClass()) {
                    mapping.addFieldTransformerClassName(writeTransformer.getXmlPath(), writeTransformer.getTransformerClass());
                } else {
                    mapping.addFieldTransformation(writeTransformer.getXmlPath(), writeTransformer.getMethod());
                }
            }
        }
    }
    return mapping;
}
Also used : ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) JAXBSetMethodAttributeAccessor(org.eclipse.persistence.internal.jaxb.JAXBSetMethodAttributeAccessor) XMLTransformationMapping(org.eclipse.persistence.oxm.mappings.XMLTransformationMapping) XMLRecord(org.eclipse.persistence.oxm.record.XMLRecord) ContainerPolicy(org.eclipse.persistence.internal.queries.ContainerPolicy) XMLTransformationRecord(org.eclipse.persistence.internal.oxm.record.XMLTransformationRecord) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) XmlWriteTransformer(org.eclipse.persistence.jaxb.xmlmodel.XmlTransformation.XmlWriteTransformer) VirtualAttributeAccessor(org.eclipse.persistence.internal.descriptors.VirtualAttributeAccessor) CustomAccessorAttributeAccessor(org.eclipse.persistence.internal.jaxb.CustomAccessorAttributeAccessor) JAXBSetMethodAttributeAccessor(org.eclipse.persistence.internal.jaxb.JAXBSetMethodAttributeAccessor) AttributeAccessor(org.eclipse.persistence.mappings.AttributeAccessor) JAXBArrayAttributeAccessor(org.eclipse.persistence.internal.jaxb.many.JAXBArrayAttributeAccessor) MapValueAttributeAccessor(org.eclipse.persistence.internal.jaxb.many.MapValueAttributeAccessor) MethodAttributeAccessor(org.eclipse.persistence.internal.descriptors.MethodAttributeAccessor) InstanceVariableAttributeAccessor(org.eclipse.persistence.internal.descriptors.InstanceVariableAttributeAccessor) XmlTransformation(org.eclipse.persistence.jaxb.xmlmodel.XmlTransformation) XmlReadTransformer(org.eclipse.persistence.jaxb.xmlmodel.XmlTransformation.XmlReadTransformer) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession)

Example 2 with XMLTransformationMapping

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

the class SDOProperty method buildXMLTransformationMapping.

private DatabaseMapping buildXMLTransformationMapping(String mappingUri) {
    XMLTransformationMapping mapping = new XMLTransformationMapping();
    mapping.setAttributeName(getName());
    String xpath = getQualifiedXPath(mappingUri, true);
    String xpathMinusText;
    int indexOfTextXPath = xpath.lastIndexOf("/text()");
    if (indexOfTextXPath < 0) {
        xpathMinusText = xpath;
    } else {
        xpathMinusText = xpath.substring(0, indexOfTextXPath);
    }
    QNameTransformer transformer = new QNameTransformer(xpath);
    mapping.setAttributeTransformer(transformer);
    mapping.addFieldTransformer(xpath, transformer);
    NamespaceResolver nsr = new NamespaceResolver();
    nsr.put(javax.xml.XMLConstants.XMLNS_ATTRIBUTE, javax.xml.XMLConstants.XMLNS_ATTRIBUTE_NS_URI);
    XMLField field = new XMLField();
    field.setNamespaceResolver(nsr);
    field.setXPath(xpathMinusText + "/@" + javax.xml.XMLConstants.XMLNS_ATTRIBUTE + ":" + QNameTransformer.QNAME_NAMESPACE_PREFIX);
    mapping.addFieldTransformer(field, new NamespaceURITransformer());
    return mapping;
}
Also used : XMLField(org.eclipse.persistence.oxm.XMLField) QNameTransformer(org.eclipse.persistence.sdo.helper.metadata.QNameTransformer) XMLTransformationMapping(org.eclipse.persistence.oxm.mappings.XMLTransformationMapping) NamespaceResolver(org.eclipse.persistence.oxm.NamespaceResolver) NamespaceURITransformer(org.eclipse.persistence.sdo.helper.metadata.NamespaceURITransformer)

Example 3 with XMLTransformationMapping

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

the class TableTypeTestSuite method setUp.

@BeforeClass
public static void setUp() throws WSDLException, SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
    if (conn == null) {
        try {
            conn = buildConnection();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    String ddlCreateProp = System.getProperty(DATABASE_DDL_CREATE_KEY, DEFAULT_DATABASE_DDL_CREATE);
    if ("true".equalsIgnoreCase(ddlCreateProp)) {
        ddlCreate = true;
    }
    String ddlDropProp = System.getProperty(DATABASE_DDL_DROP_KEY, DEFAULT_DATABASE_DDL_DROP);
    if ("true".equalsIgnoreCase(ddlDropProp)) {
        ddlDrop = true;
    }
    String ddlDebugProp = System.getProperty(DATABASE_DDL_DEBUG_KEY, DEFAULT_DATABASE_DDL_DEBUG);
    if ("true".equalsIgnoreCase(ddlDebugProp)) {
        ddlDebug = true;
    }
    if (ddlCreate) {
        runDdl(conn, CREATE_TABLETYPE_TABLE, ddlDebug);
        runDdl(conn, CREATE_TABLETYPE2_TABLE, ddlDebug);
        try {
            Statement stmt = conn.createStatement();
            for (int i = 0; i < POPULATE_TABLETYPE_TABLE.length; i++) {
                stmt.addBatch(POPULATE_TABLETYPE_TABLE[i]);
            }
            stmt.executeBatch();
            stmt = conn.createStatement();
            for (int i = 0; i < POPULATE_TABLETYPE2_TABLE.length; i++) {
                stmt.addBatch(POPULATE_TABLETYPE2_TABLE[i]);
            }
            stmt.executeBatch();
        } catch (SQLException e) {
        // e.printStackTrace();
        }
    }
    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\">tabletype</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=\"TABLETYPE\" " + "/>" + "<table " + "schemaPattern=\"%\" " + "tableNamePattern=\"TABLETYPE2\" " + "/>" + "</dbws-builder>";
    builder = new DBWSBuilder();
    OracleHelper builderHelper = new OracleHelper(builder);
    builder.setBuilderHelper(builderHelper);
    Field workbenchProj_field = BaseDBWSBuilderHelper.class.getDeclaredField("workbenchXMLProject");
    workbenchProj_field.setAccessible(true);
    ObjectPersistenceWorkbenchXMLProject workbenchXMLProject = (ObjectPersistenceWorkbenchXMLProject) workbenchProj_field.get(builderHelper);
    XMLTransformationMapping versionMapping = (XMLTransformationMapping) workbenchXMLProject.getDescriptor(Project.class).getMappings().get(0);
    TransformerBasedFieldTransformation versionTransformer = (TransformerBasedFieldTransformation) versionMapping.getFieldTransformations().get(0);
    Field transformerField = TransformerBasedFieldTransformation.class.getDeclaredField("transformer");
    transformerField.setAccessible(true);
    ConstantTransformer constantTransformer = (ConstantTransformer) transformerField.get(versionTransformer);
    constantTransformer.setValue("Eclipse Persistence Services - " + releaseVersion);
    DBWSTestSuite.setUp(".");
}
Also used : TransformerBasedFieldTransformation(org.eclipse.persistence.internal.descriptors.TransformerBasedFieldTransformation) SQLException(java.sql.SQLException) Statement(java.sql.Statement) XMLTransformationMapping(org.eclipse.persistence.oxm.mappings.XMLTransformationMapping) OracleHelper(org.eclipse.persistence.tools.dbws.oracle.OracleHelper) ConstantTransformer(org.eclipse.persistence.mappings.transformers.ConstantTransformer) SQLException(java.sql.SQLException) WSDLException(javax.wsdl.WSDLException) DBWSBuilder(org.eclipse.persistence.tools.dbws.DBWSBuilder) Field(java.lang.reflect.Field) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) ObjectPersistenceWorkbenchXMLProject(org.eclipse.persistence.internal.sessions.factories.ObjectPersistenceWorkbenchXMLProject) Project(org.eclipse.persistence.sessions.Project) ObjectPersistenceWorkbenchXMLProject(org.eclipse.persistence.internal.sessions.factories.ObjectPersistenceWorkbenchXMLProject) BeforeClass(org.junit.BeforeClass)

Example 4 with XMLTransformationMapping

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

the class ObjectPersistenceRuntimeXMLProject method buildProjectDescriptor.

protected ClassDescriptor buildProjectDescriptor() {
    XMLDescriptor descriptor = new XMLDescriptor();
    descriptor.setJavaClass(Project.class);
    descriptor.setDefaultRootElement(getPrimaryNamespaceXPath() + "object-persistence");
    descriptor.setSchemaReference(new XMLSchemaClassPathReference(SCHEMA_DIR + TOPLINK_10_SCHEMA));
    XMLTransformationMapping versionMapping = new XMLTransformationMapping();
    versionMapping.addFieldTransformer("@version", getConstantTransformerForProjectVersionMapping());
    descriptor.addMapping(versionMapping);
    XMLDirectMapping nameMapping = new XMLDirectMapping();
    nameMapping.setAttributeName("name");
    nameMapping.setSetMethodName("setName");
    nameMapping.setGetMethodName("getName");
    nameMapping.setXPath(getSecondaryNamespaceXPath() + "name/text()");
    descriptor.addMapping(nameMapping);
    XMLCompositeCollectionMapping descriptorsMapping = new XMLCompositeCollectionMapping();
    descriptorsMapping.useCollectionClass(NonSynchronizedVector.class);
    descriptorsMapping.setAttributeName("descriptors");
    descriptorsMapping.setSetMethodName("setOrderedDescriptors");
    descriptorsMapping.setGetMethodName("getOrderedDescriptors");
    descriptorsMapping.setReferenceClass(ClassDescriptor.class);
    descriptorsMapping.setXPath(getSecondaryNamespaceXPath() + "class-mapping-descriptors/" + getSecondaryNamespaceXPath() + "class-mapping-descriptor");
    descriptor.addMapping(descriptorsMapping);
    XMLCompositeObjectMapping loginMapping = new XMLCompositeObjectMapping();
    loginMapping.setSetMethodName("setDatasourceLogin");
    loginMapping.setGetMethodName("getDatasourceLogin");
    loginMapping.setAttributeName("login");
    loginMapping.setReferenceClass(DatasourceLogin.class);
    loginMapping.setXPath(getPrimaryNamespaceXPath() + "login");
    descriptor.addMapping(loginMapping);
    return descriptor;
}
Also used : XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) XMLDirectMapping(org.eclipse.persistence.oxm.mappings.XMLDirectMapping) XMLCompositeCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeCollectionMapping) XMLTransformationMapping(org.eclipse.persistence.oxm.mappings.XMLTransformationMapping) XMLSchemaClassPathReference(org.eclipse.persistence.oxm.schema.XMLSchemaClassPathReference) XMLCompositeObjectMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeObjectMapping)

Example 5 with XMLTransformationMapping

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

the class PLSQLcollectionTestSuite method toProjectXML.

@Test
public void toProjectXML() throws IllegalArgumentException, IllegalAccessException, SecurityException, NoSuchFieldException {
    ObjectPersistenceWorkbenchXMLProject runtimeProject = new ObjectPersistenceWorkbenchXMLProject();
    XMLTransformationMapping versionMapping = (XMLTransformationMapping) runtimeProject.getDescriptor(Project.class).getMappings().get(0);
    TransformerBasedFieldTransformation versionTransformer = (TransformerBasedFieldTransformation) versionMapping.getFieldTransformations().get(0);
    Field transformerField = TransformerBasedFieldTransformation.class.getDeclaredField("transformer");
    transformerField.setAccessible(true);
    ConstantTransformer constantTransformer = (ConstantTransformer) transformerField.get(versionTransformer);
    constantTransformer.setValue(CONSTANT_PROJECT_BUILD_VERSION);
    XMLContext context = new XMLContext(runtimeProject);
    XMLMarshaller marshaller = context.createMarshaller();
    Document testDoc = marshaller.objectToXML(project);
    Document controlDoc = xmlParser.parse(new StringReader(TEST_PROJECT_CONTROL_DOC));
    assertTrue("control document not same as instance document", comparer.isNodeEqual(controlDoc, testDoc));
}
Also used : ObjectPersistenceWorkbenchXMLProject(org.eclipse.persistence.internal.sessions.factories.ObjectPersistenceWorkbenchXMLProject) Project(org.eclipse.persistence.sessions.Project) Field(java.lang.reflect.Field) ObjectPersistenceWorkbenchXMLProject(org.eclipse.persistence.internal.sessions.factories.ObjectPersistenceWorkbenchXMLProject) TransformerBasedFieldTransformation(org.eclipse.persistence.internal.descriptors.TransformerBasedFieldTransformation) XMLContext(org.eclipse.persistence.oxm.XMLContext) XMLMarshaller(org.eclipse.persistence.oxm.XMLMarshaller) StringReader(java.io.StringReader) XMLTransformationMapping(org.eclipse.persistence.oxm.mappings.XMLTransformationMapping) Document(org.w3c.dom.Document) ConstantTransformer(org.eclipse.persistence.mappings.transformers.ConstantTransformer) Test(org.junit.Test)

Aggregations

XMLTransformationMapping (org.eclipse.persistence.oxm.mappings.XMLTransformationMapping)20 XMLDescriptor (org.eclipse.persistence.oxm.XMLDescriptor)9 Field (java.lang.reflect.Field)5 TransformerBasedFieldTransformation (org.eclipse.persistence.internal.descriptors.TransformerBasedFieldTransformation)5 ObjectPersistenceWorkbenchXMLProject (org.eclipse.persistence.internal.sessions.factories.ObjectPersistenceWorkbenchXMLProject)5 ConstantTransformer (org.eclipse.persistence.mappings.transformers.ConstantTransformer)5 XMLField (org.eclipse.persistence.oxm.XMLField)5 XMLDirectMapping (org.eclipse.persistence.oxm.mappings.XMLDirectMapping)5 Project (org.eclipse.persistence.sessions.Project)5 NamespaceResolver (org.eclipse.persistence.oxm.NamespaceResolver)4 XMLCompositeObjectMapping (org.eclipse.persistence.oxm.mappings.XMLCompositeObjectMapping)4 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)3 DescriptorException (org.eclipse.persistence.exceptions.DescriptorException)3 QNameTransformer (org.eclipse.persistence.internal.xr.QNameTransformer)3 StringReader (java.io.StringReader)2 SQLException (java.sql.SQLException)2 DatabaseField (org.eclipse.persistence.internal.helper.DatabaseField)2 AttributeAccessor (org.eclipse.persistence.mappings.AttributeAccessor)2 XMLContext (org.eclipse.persistence.oxm.XMLContext)2 XMLMarshaller (org.eclipse.persistence.oxm.XMLMarshaller)2