/** * Marshal the given object into a string. * * @param object the root of content tree to be marshalled. * @return the XML representation of the given object. * @throws JAXBException if an error occurred during the marshalling. */ public static String marshal(final Object object) throws JAXBException { ensureNonNull("object", object); final StringWriter output = new StringWriter(); final MarshallerPool pool = getPool(); final Marshaller marshaller = pool.acquireMarshaller(); marshaller.marshal(object, output); pool.recycle(marshaller); return output.toString(); }
/** * Returns the default (un)marshaller pool used by all methods in this class. * * <div class="note"><b>Implementation note:</b> * Current implementation uses the double-check idiom. This is usually a deprecated practice * (the recommended alterative is to use static class initialization), but in this particular * case the field may be reset to {@code null} if OSGi modules are loaded or unloaded, so static * class initialization would be a little bit too rigid.</div> */ @SuppressWarnings("DoubleCheckedLocking") private static MarshallerPool getPool() throws JAXBException { MarshallerPool pool = POOL; if (pool == null) { synchronized (XML.class) { pool = POOL; // Double-check idiom: see javadoc. if (pool == null) { POOL = pool = new MarshallerPool(null); } } } return pool; }
/** * Unmarshal an object from the given stream. * * @param input the stream from which to read a XML representation. * @return the object unmarshalled from the given input. * @throws JAXBException if an error occurred during the unmarshalling. */ public static Object unmarshal(final InputStream input) throws JAXBException { ensureNonNull("input", input); final MarshallerPool pool = getPool(); final Unmarshaller unmarshaller = pool.acquireUnmarshaller(); final Object object = unmarshaller.unmarshal(input); pool.recycle(unmarshaller); return object; }
@Before public void setUp() throws JAXBException { marshaller = pool.acquireMarshaller(); unmarshaller = pool.acquireUnmarshaller(); }
/** * Tests time instant. The test is executed using an arbitrary locale and timezone. * * @throws JAXBException if an error occurred while marshalling. * @throws DatatypeConfigurationException should never happen. */ @Test public void testTimeInstant() throws JAXBException, DatatypeConfigurationException { createContext(); final Marshaller marshaller = pool.acquireMarshaller(); final Unmarshaller unmarshaller = pool.acquireUnmarshaller(); final TimeInstant instant = new TimeInstant(); instant.timePosition = XmlUtilities.toXML(context, date("1992-01-01 00:00:00")); final String actual = marshal(marshaller, instant); assertXmlEquals( "<gml:TimeInstant xmlns:gml=\"" + Namespaces.GML + "\">\n" + " <gml:timePosition>1992-01-01T01:00:00.000+01:00</gml:timePosition>\n" + "</gml:TimeInstant>\n", actual, "xmlns:*"); final TimeInstant test = (TimeInstant) unmarshal(unmarshaller, actual); assertEquals("1992-01-01 00:00:00", format(XmlUtilities.toDate(context, test.timePosition))); pool.recycle(marshaller); pool.recycle(unmarshaller); }
/** * Returns the XML representation of the given name, wrapped * in a mock {@code <gml:IO_IdentifiedObject>} element. */ private String marshal(final GenericName name) throws JAXBException { if (pool == null) { pool = new MarshallerPool(JAXBContext.newInstance(IdentifiedObjectMock.class), Collections.singletonMap(XML.LENIENT_UNMARSHAL, Boolean.TRUE)); } final Marshaller marshaller = pool.acquireMarshaller(); marshaller.setProperty(XML.METADATA_VERSION, VERSION_2007); final String xml = marshal(marshaller, new IdentifiedObjectMock(null, name)); pool.recycle(marshaller); return xml; }
/** * Declares a unmarshaller as available for reuse. * The caller should not use anymore the given unmarshaller after this method call, * since the unmarshaller may be re-used by another thread at any time after recycle. * * <div class="section">Cautions</div> * <ul> * <li>Do not invoke this method if the unmarshaller threw an exception, since the * unmarshaller may be in an invalid state. In particular, this method should not * be invoked in a {@code finally} block.</li> * <li>Do not invoke this method twice for the same unmarshaller, unless the unmarshaller * has been obtained by a new call to {@link #acquireUnmarshaller()}. * In case of doubt, it is better to not recycle the unmarshaller at all.</li> * </ul> * * Note that this method does not close any input stream. * Closing the unmarshaller stream is caller's or JAXB responsibility. * * @param unmarshaller the unmarshaller to return to the pool. */ public void recycle(final Unmarshaller unmarshaller) { recycle(unmarshallers, unmarshaller); }
@Before public void setUp() throws JAXBException { pool = new MarshallerPool(JAXBContext.newInstance( "org.geotoolkit.xacml.xml.policy:" + "org.geotoolkit.xacml.xml.policy:" + "org.geotoolkit.xacml.xml.context"), null); unmarshaller = pool.acquireUnmarshaller(); }
final MarshallerPool pool = new MarshallerPool(null); final Marshaller marshaller = pool.acquireMarshaller(); marshaller.setProperty(XML.LOCALE, locale); marshaller.setProperty(XML.TIMEZONE, timezone);
/** * Acquires a unmarshaller and set the properties to the given value, if non-null. */ final Unmarshaller acquireUnmarshaller(final Map<String,?> properties) throws JAXBException { final Unmarshaller unmarshaller = acquireUnmarshaller(); if (properties != null) { for (final Map.Entry<String,?> entry : properties.entrySet()) { unmarshaller.setProperty(entry.getKey(), entry.getValue()); } } return unmarshaller; }
m = getMarshallerPool().acquireMarshaller(); m.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
@Override protected Marshaller createMarshaller() throws JAXBException { final Marshaller marshaller = super.createMarshaller(); if (schemaLocation != null) { marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, schemaLocation); } return marshaller; }
/** * Returns a JAXB unmarshaller from the pool. If there is no unmarshaller currently available * in the pool, then this method will {@linkplain #createUnmarshaller() create} a new one. * * <p>This method shall be used as below:</p> * * {@preformat java * Unmarshaller unmarshaller = pool.acquireUnmarshaller(); * Unmarshaller.unmarchall(...); * pool.recycle(unmarshaller); * } * * Note that {@link #recycle(Unmarshaller)} shall not be invoked in case of exception, * since the unmarshaller may be in an invalid state. * * @return a unmarshaller configured for parsing OGC/ISO XML. * @throws JAXBException if an error occurred while creating and configuring the unmarshaller. */ public Unmarshaller acquireUnmarshaller() throws JAXBException { Unmarshaller unmarshaller = unmarshallers.poll(); if (unmarshaller == null) { unmarshaller = new PooledUnmarshaller(createUnmarshaller(), template); } return unmarshaller; }
/** * Tests a time period using GML2 or GML3 syntax. This method is used for the * implementation of {@link #testPeriodGML2()} and {@link #testPeriodGML3()}. * The test is executed using an arbitrary locale and timezone. * * @param expected the expected string. */ private void testPeriod(final TimePeriodBound begin, final TimePeriodBound end, final String expected, final boolean verifyValues) throws JAXBException { final Marshaller marshaller = pool.acquireMarshaller(); final Unmarshaller unmarshaller = pool.acquireUnmarshaller(); final TimePeriod period = new TimePeriod(); period.begin = begin; period.end = end; final String actual = marshal(marshaller, period); assertXmlEquals(expected, actual, "xmlns:*"); final TimePeriod test = (TimePeriod) unmarshal(unmarshaller, actual); if (verifyValues) { assertEquals("1992-01-01 00:00:00", format(XmlUtilities.toDate(context, test.begin.calendar()))); assertEquals("2007-12-31 00:00:00", format(XmlUtilities.toDate(context, test.end .calendar()))); } pool.recycle(marshaller); pool.recycle(unmarshaller); }
final MarshallerPool pool = new MarshallerPool(JAXBContext.newInstance(new Class<?>[0]), null); final Marshaller marshaller = pool.acquireMarshaller(); assertNotNull(marshaller); pool.recycle(marshaller); assertSame(marshaller, pool.acquireMarshaller()); pool.recycle(marshaller);
@Before public void setUp() throws Exception { unmarshaller = pool.acquireUnmarshaller(); marshaller = pool.acquireMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); }
/** * Declares a marshaller as available for reuse. * The caller should not use anymore the given marshaller after this method call, * since the marshaller may be re-used by another thread at any time after recycle. * * <div class="section">Cautions</div> * <ul> * <li>Do not invoke this method if the marshaller threw an exception, since the * marshaller may be in an invalid state. In particular, this method should not * be invoked in a {@code finally} block.</li> * <li>Do not invoke this method twice for the same marshaller, unless the marshaller * has been obtained by a new call to {@link #acquireMarshaller()}. * In case of doubt, it is better to not recycle the marshaller at all.</li> * </ul> * * Note that this method does not close any output stream. * Closing the marshaller stream is caller's or JAXB responsibility. * * @param marshaller the marshaller to return to the pool. */ public void recycle(final Marshaller marshaller) { recycle(marshallers, marshaller); }
/** * Acquires a unmarshaller and set the properties to the given value, if non-null. */ final Unmarshaller acquireUnmarshaller(final Map<String,?> properties) throws JAXBException { final Unmarshaller unmarshaller = acquireUnmarshaller(); if (properties != null) { for (final Map.Entry<String,?> entry : properties.entrySet()) { unmarshaller.setProperty(entry.getKey(), entry.getValue()); } } return unmarshaller; }
@Before public void setUp() throws JAXBException { marshaller = pool.acquireMarshaller(); }
/** * Returns a JAXB marshaller from the pool. If there is no marshaller currently available * in the pool, then this method will {@linkplain #createMarshaller() create} a new one. * * <p>This method shall be used as below:</p> * * {@preformat java * Marshaller marshaller = pool.acquireMarshaller(); * marshaller.marchall(...); * pool.recycle(marshaller); * } * * Note that {@link #recycle(Marshaller)} shall not be invoked in case of exception, * since the marshaller may be in an invalid state. * * @return a marshaller configured for formatting OGC/ISO XML. * @throws JAXBException if an error occurred while creating and configuring a marshaller. */ public Marshaller acquireMarshaller() throws JAXBException { Marshaller marshaller = marshallers.poll(); if (marshaller == null) { marshaller = new PooledMarshaller(createMarshaller(), template); } return marshaller; }