Example usage for javax.xml.bind Marshaller getAdapter

List of usage examples for javax.xml.bind Marshaller getAdapter

Introduction

In this page you can find the example usage for javax.xml.bind Marshaller getAdapter.

Prototype

public <A extends XmlAdapter> A getAdapter(Class<A> type);

Source Link

Document

Gets the adapter associated with the specified type.

Usage

From source file:org.betaconceptframework.astroboa.model.jaxb.CmsEntitySerialization.java

private String marshalEntity(CmsRepositoryEntity cmsRepositoryEntity,
        ResourceRepresentationType<?> resourceRepresentationType, boolean exportBinaryContent,
        boolean prettyPrint, String... propertyPathsToBeMarshalled) {

    if (cmsRepositoryEntity != null) {

        if (cmsRepositoryEntity instanceof ContentObject || cmsRepositoryEntity instanceof Topic
                || cmsRepositoryEntity instanceof Space || cmsRepositoryEntity instanceof RepositoryUser
                || cmsRepositoryEntity instanceof Taxonomy) {

            StringWriter writer = new StringWriter();

            Marshaller marshaller = null;
            try {
                marshaller = createMarshaller(resourceRepresentationType, prettyPrint);

                if (cmsRepositoryEntity instanceof ContentObject) {

                    if (!ArrayUtils.isEmpty(propertyPathsToBeMarshalled)) {
                        marshaller.setProperty(AstroboaMarshaller.CMS_PROPERTIES_TO_BE_MARSHALLED,
                                Arrays.asList(propertyPathsToBeMarshalled));
                    }/*  w ww.  ja  v a 2 s. c  o m*/

                    //ContentObject needs special marshaling as JAXB does not have
                    //enough information in order to initialize appropriate adapter for
                    //ContentObject
                    ContentObjectAdapter adapter = new ContentObjectAdapter();
                    adapter.setMarshaller(marshaller, exportBinaryContent,
                            ArrayUtils.isEmpty(propertyPathsToBeMarshalled));

                    marshaller.setAdapter(adapter);

                    ContentObjectType contentObjectType = marshaller.getAdapter(ContentObjectAdapter.class)
                            .marshal((ContentObject) cmsRepositoryEntity);

                    JAXBElement<ContentObjectType> contentObjectTypeJaxb = new JAXBElement<ContentObjectType>(
                            ((ContentObject) cmsRepositoryEntity).getTypeDefinition().getQualifiedName(),
                            ContentObjectType.class, null, contentObjectType);

                    marshaller.marshal(contentObjectTypeJaxb, writer);
                } else {

                    //Provide schema location 
                    marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, " "
                            + BetaConceptNamespaceConstants.ASTROBOA_MODEL_DEFINITION_URI + " "
                            + SchemaUtils.buildSchemaLocationForAstroboaModelSchemaAccordingToActiveClient());

                    marshaller.marshal(cmsRepositoryEntity, writer);
                }

            } catch (Exception e) {
                throw new CmsException(e);
            } finally {

                marshaller = null;
            }

            return writer.toString();
        } else {
            throw new CmsException("Creating XML for entity type " + cmsRepositoryEntity.getClass().getName()
                    + " is not supported");
        }
    }

    throw new CmsException("No entity is provided. Unable to create xml");
}

From source file:org.betaconceptframework.astroboa.model.jaxb.visitor.ContentObjectMarshalVisitor.java

protected <T> void marshallValueForSimpleProperty(SimpleCmsPropertyDefinition<T> simplePropertyDefinition,
        CmsPropertyInfo simpleCmsProperty, Object value) {

    if (value != null) {

        switch (simplePropertyDefinition.getValueType()) {
        case String:
        case Boolean:
        case Double:
        case Long:
        case Date:

            final SimpleCmsPropertyType simpleCmsPropertyType = new SimpleCmsPropertyType();

            simpleCmsPropertyType.setExportAsAnAttribute(
                    ((SimpleCmsPropertyDefinitionImpl) simplePropertyDefinition).isRepresentsAnXmlAttribute());

            if (marshalOutputTypeIsJSON() && simplePropertyDefinition.isMultiple()) {
                simpleCmsPropertyType.setExportAsAnArray(true);
            }//from  w  w w  . ja v  a2s.  c  o  m

            CmsPropertyTypeJAXBElement<SimpleCmsPropertyType> simpleCmsPropertyTypeJaxbElement = new CmsPropertyTypeJAXBElement(
                    new QName(simplePropertyDefinition.getQualifiedName().getLocalPart()),
                    SimpleCmsPropertyType.class, null, simpleCmsPropertyType);

            if (value instanceof String) {
                simpleCmsPropertyTypeJaxbElement.getValue().setContent((String) value);
            } else if (value instanceof Boolean) {
                simpleCmsPropertyTypeJaxbElement.getValue().setContent(((Boolean) value).toString());
            } else if (value instanceof Double) {
                simpleCmsPropertyTypeJaxbElement.getValue().setContent(((Double) value).toString());
            } else if (value instanceof Long) {
                simpleCmsPropertyTypeJaxbElement.getValue().setContent(((Long) value).toString());
            } else if (value instanceof Calendar) {
                Calendar calendar = (Calendar) value;

                try {
                    if (((CalendarPropertyDefinition) simplePropertyDefinition).isDateTime()) {
                        GregorianCalendar gregCalendar = new GregorianCalendar(calendar.getTimeZone());
                        gregCalendar.setTimeInMillis(calendar.getTimeInMillis());

                        simpleCmsPropertyTypeJaxbElement.getValue()
                                .setContent(df.newXMLGregorianCalendar(gregCalendar).toXMLFormat());
                    } else {
                        simpleCmsPropertyTypeJaxbElement.getValue()
                                .setContent(df.newXMLGregorianCalendarDate(calendar.get(Calendar.YEAR),
                                        calendar.get(Calendar.MONTH) + 1, // Calendar.MONTH is zero based, XSD Date datatype's month field starts
                                        //   with JANUARY as 1.
                                        calendar.get(Calendar.DAY_OF_MONTH), DatatypeConstants.FIELD_UNDEFINED)
                                        .toXMLFormat());
                    }
                } catch (Exception e) {
                    throw new CmsException("Property " + simpleCmsProperty.getFullPath() + " Calendar value "
                            + DateUtils.format(calendar), e);
                }
            } else {
                throw new CmsException("Property " + simpleCmsProperty.getFullPath() + " has value type "
                        + simplePropertyDefinition.getValueType() + " but contains value of type "
                        + value.getClass().getName());
            }

            addJaxbElementToCurrentParentComplexCmsPropertyType(simpleCmsPropertyTypeJaxbElement);

            break;
        case TopicReference:

            try {
                TopicType topicType = marshalTopicReference(value);

                if (marshalOutputTypeIsJSON() && simplePropertyDefinition.isMultiple()) {
                    topicType.setExportAsAnArray(true);
                }

                CmsPropertyTypeJAXBElement<TopicType> topicTypeJaxbElement = new CmsPropertyTypeJAXBElement(
                        new QName(simplePropertyDefinition.getQualifiedName().getLocalPart()), TopicType.class,
                        null, topicType);

                addJaxbElementToCurrentParentComplexCmsPropertyType(topicTypeJaxbElement);

            } catch (Exception e) {
                throw new CmsException("Unable to marshal topic " + ((Topic) value).getName(), e);
            }

            break;
        case Binary:
            try {
                BinaryChannelType binaryChannelType = getBinaryChannelAdapter().marshal((BinaryChannel) value);

                if (marshalOutputTypeIsJSON() && simplePropertyDefinition.isMultiple()) {
                    binaryChannelType.setExportAsAnArray(true);
                }

                CmsPropertyTypeJAXBElement<BinaryChannelType> binaryChannelTypeJaxbElement = new CmsPropertyTypeJAXBElement(
                        new QName(simplePropertyDefinition.getQualifiedName().getLocalPart()),
                        BinaryChannelType.class, null, binaryChannelType);

                addJaxbElementToCurrentParentComplexCmsPropertyType(binaryChannelTypeJaxbElement);

            } catch (Exception e) {
                throw new CmsException("Unable to marshal binary channel " + ((BinaryChannel) value).getName(),
                        e);
            }

            break;
        case ObjectReference:
            try {

                logger.debug("\t Property is a reference to another object");

                Marshaller objectReferenceMarshaller = CmsEntitySerialization.Context
                        .createMarshaller(marshalOutputTypeIsJSON() ? ResourceRepresentationType.JSON
                                : ResourceRepresentationType.XML, prettyPrintIsEnabled());

                //For now only porifle.title is provided. 
                objectReferenceMarshaller.setProperty(AstroboaMarshaller.CMS_PROPERTIES_TO_BE_MARSHALLED,
                        Arrays.asList("profile.title"));

                ContentObjectAdapter adapter = new ContentObjectAdapter();
                adapter.setMarshaller(objectReferenceMarshaller, marshallBinaryContent, false);
                objectReferenceMarshaller.setAdapter(adapter);

                ContentObjectType contentObjectType = objectReferenceMarshaller
                        .getAdapter(ContentObjectAdapter.class).marshal((ContentObject) value);

                if (marshalOutputTypeIsJSON() && simplePropertyDefinition.isMultiple()) {
                    contentObjectType.setExportAsAnArray(true);
                }

                CmsPropertyTypeJAXBElement<ContentObjectType> contentObjectReferenceTypeJaxbElement = new CmsPropertyTypeJAXBElement(
                        new QName(simplePropertyDefinition.getQualifiedName().getLocalPart()),
                        ContentObjectType.class, null, contentObjectType);

                addJaxbElementToCurrentParentComplexCmsPropertyType(contentObjectReferenceTypeJaxbElement);

            } catch (Exception e) {
                throw new CmsException("Unable to marshal contentObject " + ((ContentObject) value).getId(), e);
            }

            break;
        default:
            break;
        }

    }
}