List of usage examples for javax.xml.bind Marshaller getProperty
public Object getProperty(String name) throws PropertyException;
From source file:Main.java
public static String convertBean2Xml(Object obj) throws IOException { String result = null;//from ww w .ja v a 2 s . c o m try { JAXBContext context = JAXBContext.newInstance(obj.getClass()); Marshaller marshaller = context.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true); ByteArrayOutputStream baos = new ByteArrayOutputStream(); XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newInstance(); XMLStreamWriter xmlStreamWriter = xmlOutputFactory.createXMLStreamWriter(baos, (String) marshaller.getProperty(Marshaller.JAXB_ENCODING)); xmlStreamWriter.writeStartDocument((String) marshaller.getProperty(Marshaller.JAXB_ENCODING), "1.0"); marshaller.marshal(obj, xmlStreamWriter); xmlStreamWriter.writeEndDocument(); xmlStreamWriter.close(); result = baos.toString("UTF-8"); } catch (JAXBException e) { e.printStackTrace(); return null; } catch (XMLStreamException e) { e.printStackTrace(); return null; } catch (UnsupportedEncodingException e) { e.printStackTrace(); return null; } return result; }
From source file:Main.java
public final String getMessage() throws Exception { Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); jaxbMarshaller.setProperty("jaxb.encoding", "ISO-8859-1"); jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, true); ByteArrayOutputStream baos = new ByteArrayOutputStream(); XMLStreamWriter xmlStreamWriter = xmlOutputFactory.createXMLStreamWriter(baos, (String) jaxbMarshaller.getProperty(Marshaller.JAXB_ENCODING)); xmlStreamWriter.writeStartDocument((String) jaxbMarshaller.getProperty(Marshaller.JAXB_ENCODING), "1.0"); jaxbMarshaller.marshal(this, xmlStreamWriter); xmlStreamWriter.writeEndDocument();/*from w ww .j a va 2 s . c o m*/ xmlStreamWriter.close(); return new String(baos.toByteArray()); }
From source file:org.apache.axis2.datasource.jaxb.JAXBDSContext.java
/** * Marshal the jaxb object/*from w w w .j a v a 2 s .c o m*/ * @param obj * @param writer * @param am AttachmentMarshaller, optional Attachment */ public void marshal(Object obj, XMLStreamWriter writer) throws JAXBException { if (log.isDebugEnabled()) { log.debug("enter marshal"); } boolean installedFilter = false; try { // There may be a preferred classloader that should be used ClassLoader cl = getClassLoader(); // Very easy, use the Context to get the Marshaller. // Use the marshaller to write the object. JAXBContext jbc = getJAXBContext(cl); Marshaller m = JAXBUtils.getJAXBMarshaller(jbc); if (writer instanceof MTOMXMLStreamWriter && ((MTOMXMLStreamWriter) writer).getOutputFormat() != null) { String encoding = ((MTOMXMLStreamWriter) writer).getOutputFormat().getCharSetEncoding(); String marshallerEncoding = (String) m.getProperty(Marshaller.JAXB_ENCODING); // Make sure that the marshaller respects the encoding of the message. // This is accomplished by setting the encoding on the Marshaller's JAXB_ENCODING property. if (encoding == null && marshallerEncoding == null) { if (log.isDebugEnabled()) { log.debug( "The encoding and the marshaller's JAXB_ENCODING are both set to the default (UTF-8)"); } } else { // Must set the encoding to an actual String to set it on the Marshaller if (encoding == null) { encoding = "UTF-8"; } if (!encoding.equalsIgnoreCase(marshallerEncoding)) { if (log.isDebugEnabled()) { log.debug("The Marshaller.JAXB_ENCODING is " + marshallerEncoding); log.debug( "The Marshaller.JAXB_ENCODING is changed to the message encoding " + encoding); } m.setProperty(Marshaller.JAXB_ENCODING, encoding); } else { if (log.isDebugEnabled()) { log.debug("The encoding and the marshaller's JAXB_ENCODING are both set to:" + marshallerEncoding); } } } } AttachmentMarshaller am = createAttachmentMarshaller(writer); if (am != null) { if (DEBUG_ENABLED) { log.debug("Adding JAXBAttachmentMarshaller to Marshaller"); } m.setAttachmentMarshaller(am); } MessageContext mc = getMessageContext(); // If requested install a filter to remove illegal characters installedFilter = installFilter(mc, writer); // Marshal the object if (getProcessType() == null) { marshalByElement(obj, m, writer, true); //!am.isXOPPackage()); } else { marshalByType(obj, m, writer, getProcessType(), isxmlList(), getConstructionType(), true); // Attempt to optimize by writing to OutputStream } JAXBUtils.releaseJAXBMarshaller(jbc, m); if (log.isDebugEnabled()) { log.debug("exit marshal"); } } finally { // Make sure the filter is uninstalled if (installedFilter) { uninstallFilter(writer); } } }
From source file:org.apache.axis2.datasource.jaxb.JAXBDSContext.java
/** * If the writer is backed by an OutputStream, then return the OutputStream * @param writer//w w w .ja v a 2 s . c o m * @param Marshaller * @return OutputStream or null */ private static OutputStream getOutputStream(XMLStreamWriter writer, Marshaller m) throws XMLStreamException { if (log.isDebugEnabled()) { log.debug("XMLStreamWriter is " + writer); } OutputStream os = null; if (writer.getClass() == MTOMXMLStreamWriter.class) { os = ((MTOMXMLStreamWriter) writer).getOutputStream(); if (log.isDebugEnabled()) { log.debug("OutputStream accessible from MTOMXMLStreamWriter is " + os); } } if (writer.getClass() == XMLStreamWriterWithOS.class) { os = ((XMLStreamWriterWithOS) writer).getOutputStream(); if (log.isDebugEnabled()) { log.debug("OutputStream accessible from XMLStreamWriterWithOS is " + os); } } if (os != null) { String marshallerEncoding = null; try { marshallerEncoding = (String) m.getProperty(Marshaller.JAXB_ENCODING); } catch (PropertyException e) { if (DEBUG_ENABLED) { log.debug("Could not query JAXB_ENCODING..Continuing. " + e); } } if (marshallerEncoding != null && !marshallerEncoding.equalsIgnoreCase("UTF-8")) { if (DEBUG_ENABLED) { log.debug("Wrapping output stream to remove BOM"); } os = new BOMOutputStreamFilter(marshallerEncoding, os); } } return os; }
From source file:org.betaconceptframework.astroboa.util.SchemaUtils.java
public static void appendSchemaLocationToMarshaller(Marshaller marshaller, String namespace, String schemaLocationURL, String prefix) throws PropertyException { if (StringUtils.isBlank(namespace) || StringUtils.isBlank(schemaLocationURL)) { return;/* w ww. j a va 2 s . com*/ } String schemaLocation = (String) marshaller.getProperty(Marshaller.JAXB_SCHEMA_LOCATION); if (schemaLocation == null) { schemaLocation = ""; } if (!schemaLocation.contains(namespace)) { marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, schemaLocation + " " + namespace + " " + schemaLocationURL); } }