Example usage for javax.xml.bind Marshaller setListener

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

Introduction

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

Prototype

public void setListener(Listener listener);

Source Link

Document

Register marshal event callback Listener with this Marshaller .

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    JAXBContext jc = JAXBContext.newInstance(Root.class);

    Root rootA = new Root();
    rootA.setName("A");

    Root rootB = new Root();
    rootB.setName("B");
    rootA.setChild(rootB);/* ww  w  .j a v a2  s.c o  m*/

    Root rootC = new Root();
    rootC.setName("C");
    rootB.setChild(rootC);

    Root rootD = new Root();
    rootD.setName("D");
    rootC.setChild(rootD);

    Root rootE = new Root();
    rootE.setName("E");
    rootD.setChild(rootE);

    Marshaller marshaller = jc.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

    DepthListener depthListener = new DepthListener(3);
    marshaller.setListener(depthListener);
    marshaller.setAdapter(new RootAdapter(depthListener));
    marshaller.marshal(rootA, System.out);
}

From source file:Main.java

private static <T> Marshaller createMarshaller(Class<T> clazz, Marshaller.Listener listener)
        throws JAXBException {
    JAXBContext jc = JAXBContext.newInstance(clazz);
    Marshaller marshaller = jc.createMarshaller();
    if (listener != null) {
        marshaller.setListener(listener);
    }/* w  w w.j a  va  2  s .com*/
    marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
    return marshaller;
}

From source file:com.bitplan.jaxb.JaxbFactory.java

/**
 * get a marshaller for the given <T> instance
 * //www . j  av a  2 s.  c om
 * @param instance
 *          - the instance to get a marshaller for
 * @return a marshaller for <T>
 * @throws JAXBException
 */
public Marshaller getMarshaller(T instance) throws JAXBException {
    JAXBContext lcontext = getJAXBContext();
    Marshaller marshaller = lcontext.createMarshaller();
    if (this.marshalListener != null) {
        marshaller.setListener(marshalListener);
    }
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    return marshaller;
}

From source file:org.apache.cxf.jaxbplus.io.DataWriterImpl.java

public Marshaller createMarshaller(Object elValue, MessagePartInfo part) {
    Class<?> cls = null;/*  w  w w  .jav  a 2 s  .  co  m*/
    if (part != null) {
        cls = part.getTypeClass();
    }

    if (cls == null) {
        cls = null != elValue ? elValue.getClass() : null;
    }

    if (cls != null && cls.isArray() && elValue instanceof Collection) {
        Collection<?> col = (Collection<?>) elValue;
        elValue = col.toArray((Object[]) Array.newInstance(cls.getComponentType(), col.size()));
    }
    Marshaller marshaller;
    try {

        marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
        marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.FALSE);
        marshaller.setListener(databinding.getMarshallerListener());
        if (databinding.getValidationEventHandler() != null) {
            marshaller.setEventHandler(databinding.getValidationEventHandler());
        }

        final Map<String, String> nspref = databinding.getDeclaredNamespaceMappings();
        if (nspref != null) {
            JAXBUtils.setNamespaceWrapper(nspref, marshaller);
        }
        if (databinding.getMarshallerProperties() != null) {
            for (Map.Entry<String, Object> propEntry : databinding.getMarshallerProperties().entrySet()) {
                try {
                    marshaller.setProperty(propEntry.getKey(), propEntry.getValue());
                } catch (PropertyException pe) {
                    LOG.log(Level.INFO, "PropertyException setting Marshaller properties", pe);
                }
            }
        }

        marshaller.setSchema(schema);
        AttachmentMarshaller atmarsh = getAttachmentMarshaller();
        marshaller.setAttachmentMarshaller(atmarsh);

        if (schema != null && atmarsh instanceof JAXBAttachmentMarshaller) {
            //we need a special even handler for XOP attachments 
            marshaller.setEventHandler(new MtomValidationHandler(marshaller.getEventHandler(),
                    (JAXBAttachmentMarshaller) atmarsh));
        }
    } catch (JAXBException ex) {
        if (ex instanceof javax.xml.bind.MarshalException) {
            javax.xml.bind.MarshalException marshalEx = (javax.xml.bind.MarshalException) ex;
            Message faultMessage = new Message("MARSHAL_ERROR", LOG,
                    marshalEx.getLinkedException().getMessage());
            throw new Fault(faultMessage, ex);
        } else {
            throw new Fault(new Message("MARSHAL_ERROR", LOG, ex.getMessage()), ex);
        }
    }
    return marshaller;
}

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

public Marshaller createMarshaller(ResourceRepresentationType<?> resourceRepresentationType,
        boolean prettyPrint) throws JAXBException {

    boolean jsonResourceRepresentationType = resourceRepresentationType != null
            && ResourceRepresentationType.JSON.equals(resourceRepresentationType);

    Marshaller marshaller = jaxbContext.createMarshaller();
    marshaller.setListener(new AstroboaMarshalListener(jsonResourceRepresentationType));

    if (jsonResourceRepresentationType) {
        LocalizationAdapter localizationAdapter = new LocalizationAdapter();
        localizationAdapter.useJsonVersion();
        marshaller.setAdapter(localizationAdapter);
    }//w w  w  . j  a va  2s . c om

    marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");

    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, prettyPrint);

    return new AstroboaMarshaller(marshaller, resourceRepresentationType);
}

From source file:org.linguafranca.pwdb.kdbx.jaxb.JaxbSerializableDatabase.java

@Override
public void save(OutputStream outputStream) throws IOException {
    final List<String> toEncrypt = new ArrayList<>();
    if (keePassFile.getMeta().getMemoryProtection().getProtectTitle()) {
        toEncrypt.add(org.linguafranca.pwdb.Entry.STANDARD_PROPERTY_NAME_TITLE);
    }/*from w  w w  . j a  v a  2s.co  m*/
    if (keePassFile.getMeta().getMemoryProtection().getProtectURL()) {
        toEncrypt.add(org.linguafranca.pwdb.Entry.STANDARD_PROPERTY_NAME_URL);
    }
    if (keePassFile.getMeta().getMemoryProtection().getProtectUserName()) {
        toEncrypt.add(org.linguafranca.pwdb.Entry.STANDARD_PROPERTY_NAME_USER_NAME);
    }
    if (keePassFile.getMeta().getMemoryProtection().getProtectPassword()) {
        toEncrypt.add(org.linguafranca.pwdb.Entry.STANDARD_PROPERTY_NAME_PASSWORD);
    }
    if (keePassFile.getMeta().getMemoryProtection().getProtectNotes()) {
        toEncrypt.add(org.linguafranca.pwdb.Entry.STANDARD_PROPERTY_NAME_NOTES);
    }
    try {
        JAXBContext jc = JAXBContext.newInstance(KeePassFile.class);
        Marshaller u = jc.createMarshaller();
        u.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        u.setListener(new Marshaller.Listener() {
            @Override
            public void beforeMarshal(Object source) {
                try {
                    if (source instanceof StringField) {
                        StringField field = (StringField) source;
                        if (toEncrypt.contains(field.getKey())) {
                            byte[] encrypted = encryption.encrypt(field.getValue().getValue().getBytes());
                            String b64 = new String(Base64.encodeBase64(encrypted), "UTF-8");
                            field.getValue().setValue(b64);
                            field.getValue().setProtected(true);
                        }
                    }
                } catch (UnsupportedEncodingException e) {
                    throw new IllegalStateException();
                }
            }
        });
        u.marshal(keePassFile, outputStream);
    } catch (JAXBException e) {
        throw new IllegalStateException(e);
    }
}

From source file:org.openlegacy.utils.XmlSerializationUtil.java

public static <T> void serialize(Class<T> rootClass, T persistedObject, OutputStream out) throws JAXBException {
    JAXBContext context = JAXBContext.newInstance(rootClass);
    Marshaller marshaller = context.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    marshaller.setListener(new Listener() {

        @Override//from   w  ww .  j a va  2s  .  c om
        public void beforeMarshal(Object source) {
            resetDefaultValues(source);
            super.beforeMarshal(source);
        }

    });
    marshaller.marshal(persistedObject, out);
}

From source file:org.springframework.oxm.jaxb.Jaxb2Marshaller.java

/**
 * Template method that can be overridden by concrete JAXB marshallers for custom initialization behavior.
 * Gets called after creation of JAXB {@code Marshaller}, and after the respective properties have been set.
 * <p>The default implementation sets the {@link #setMarshallerProperties(Map) defined properties}, the {@link
 * #setValidationEventHandler(ValidationEventHandler) validation event handler}, the {@link #setSchemas(Resource[])
 * schemas}, {@link #setMarshallerListener(javax.xml.bind.Marshaller.Listener) listener}, and
 * {@link #setAdapters(XmlAdapter[]) adapters}.
 *//*from w ww.  ja v a2  s . c o m*/
protected void initJaxbMarshaller(Marshaller marshaller) throws JAXBException {
    if (this.marshallerProperties != null) {
        for (String name : this.marshallerProperties.keySet()) {
            marshaller.setProperty(name, this.marshallerProperties.get(name));
        }
    }
    if (this.marshallerListener != null) {
        marshaller.setListener(this.marshallerListener);
    }
    if (this.validationEventHandler != null) {
        marshaller.setEventHandler(this.validationEventHandler);
    }
    if (this.adapters != null) {
        for (XmlAdapter<?, ?> adapter : this.adapters) {
            marshaller.setAdapter(adapter);
        }
    }
    if (this.schema != null) {
        marshaller.setSchema(this.schema);
    }
}