/** * 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(); }
/** * 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(); }
@Before public void setUp() throws JAXBException { marshaller = pool.acquireMarshaller(); }
/** * Marshal the given object into a stream. * * @param object the root of content tree to be marshalled. * @param output the stream where to write. * @throws JAXBException if an error occurred during the marshalling. */ public static void marshal(final Object object, final OutputStream output) throws JAXBException { ensureNonNull("object", object); ensureNonNull("output", output); final MarshallerPool pool = getPool(); final Marshaller marshaller = pool.acquireMarshaller(); marshaller.marshal(object, output); pool.recycle(marshaller); }
/** * Marshal the given object into a stream. * * @param object the root of content tree to be marshalled. * @param output the stream where to write. * @throws JAXBException if an error occurred during the marshalling. */ public static void marshal(final Object object, final OutputStream output) throws JAXBException { ensureNonNull("object", object); ensureNonNull("output", output); final MarshallerPool pool = getPool(); final Marshaller marshaller = pool.acquireMarshaller(); marshaller.marshal(object, output); pool.recycle(marshaller); }
/** * Marshal the given object into a file. * * @param object the root of content tree to be marshalled. * @param output the file to be written. * @throws JAXBException if an error occurred during the marshalling. */ public static void marshal(final Object object, final File output) throws JAXBException { ensureNonNull("object", object); ensureNonNull("output", output); final MarshallerPool pool = getPool(); final Marshaller marshaller = pool.acquireMarshaller(); marshaller.marshal(object, output); pool.recycle(marshaller); }
/** * Marshal the given object into a file. * * @param object the root of content tree to be marshalled. * @param output the file to be written. * @throws JAXBException if an error occurred during the marshalling. */ public static void marshal(final Object object, final File output) throws JAXBException { ensureNonNull("object", object); ensureNonNull("output", output); final MarshallerPool pool = getPool(); final Marshaller marshaller = pool.acquireMarshaller(); marshaller.marshal(object, output); pool.recycle(marshaller); }
@Before public void setUp() throws Exception { unmarshaller = pool.acquireUnmarshaller(); marshaller = pool.acquireMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); }
/** * Marshal the given object into a path. * * @param object the root of content tree to be marshalled. * @param output the file to be written. * @throws JAXBException if an error occurred during the marshalling. */ public static void marshal(final Object object, final Path output) throws JAXBException { ensureNonNull("object", object); ensureNonNull("output", output); try (OutputStream out = Files.newOutputStream(output, StandardOpenOption.CREATE, StandardOpenOption.WRITE)) { final MarshallerPool pool = getPool(); final Marshaller marshaller = pool.acquireMarshaller(); marshaller.marshal(object, out); pool.recycle(marshaller); } catch (IOException e) { throw new JAXBException(Errors.format(Errors.Keys.CanNotOpen_1, output), e); } }
/** * Marshal the given object into a path. * * @param object the root of content tree to be marshalled. * @param output the file to be written. * @throws JAXBException if an error occurred during the marshalling. */ public static void marshal(final Object object, final Path output) throws JAXBException { ensureNonNull("object", object); ensureNonNull("output", output); try (OutputStream out = Files.newOutputStream(output, StandardOpenOption.CREATE, StandardOpenOption.WRITE)) { final MarshallerPool pool = getPool(); final Marshaller marshaller = pool.acquireMarshaller(); marshaller.marshal(object, out); pool.recycle(marshaller); } catch (IOException e) { throw new JAXBException(Errors.format(Errors.Keys.CanNotOpen_1, output), e); } }
@Before public void setUp() throws JAXBException { marshaller = pool.acquireMarshaller(); unmarshaller = pool.acquireUnmarshaller(); }
@Before public void setUp() throws Exception { pool = JTSWrapperMarshallerPool.getInstance(); factory = new ObjectFactory(); un = pool.acquireUnmarshaller(); m = pool.acquireMarshaller(); File xsdDirectory = getDirectoryFromResource("org.geotoolkit.gml.311.base"); SchemaFactory sf = SchemaFactory.newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = sf.newSchema(new File(xsdDirectory, "gml.xsd")); un.setSchema(schema); //m.setSchema(schema); }
/** * 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; }
/** * Marshals the given object using the {@linkplain #getMarshallerPool() test marshaller pool}. * The XML schema identified by the given version is used. * * @param object the object to marshal. * @param metadataVersion whether to marshal legacy 19139:2007 or newer ISO 19115-3 document. Can be {@code null}. * @return the marshalled object. * @throws JAXBException if an error occurred while marshalling the object. * * @since 1.0 */ protected final String marshal(final Object object, final Version metadataVersion) throws JAXBException { final MarshallerPool pool = getMarshallerPool(); final Marshaller marshaller = pool.acquireMarshaller(); marshaller.setProperty(XML.METADATA_VERSION, metadataVersion); final String xml = marshal(marshaller, object); pool.recycle(marshaller); return xml; }
/** * Marshals the given object using the {@linkplain #getMarshallerPool() test marshaller pool}. * The default XML schema is used (usually the most recent one). * * @param object the object to marshal. * @return the marshalled object. * @throws JAXBException if an error occurred while marshalling the object. * * @see #unmarshal(Class, String) */ protected final String marshal(final Object object) throws JAXBException { final MarshallerPool pool = getMarshallerPool(); final Marshaller marshaller = pool.acquireMarshaller(); final String xml = marshal(marshaller, object); pool.recycle(marshaller); return xml; }
/** * Tests marshalling using the ISO URL. * * @throws JAXBException if an error occurred while marshalling the XML. */ @Test public void testISO_URL() throws JAXBException { final String expected = getResponsiblePartyXML("http://standards.iso.org/ittf/PubliclyAvailableStandards/ISO_19139_Schemas/"); final ResponsibleParty rp = unmarshal(ResponsibleParty.class, expected); assertEquals(Role.PRINCIPAL_INVESTIGATOR, rp.getRole()); final MarshallerPool pool = getMarshallerPool(); final Marshaller marshaller = pool.acquireMarshaller(); marshaller.setProperty(XML.METADATA_VERSION, VERSION_2007); marshaller.setProperty(XML.SCHEMAS, Collections.singletonMap("gmd", "http://standards.iso.org/ittf/PubliclyAvailableStandards/ISO_19139_Schemas")); // Intentionally omit trailing '/'. final String actual = marshal(marshaller, rp); pool.recycle(marshaller); assertXmlEquals(expected, actual, "xmlns:*"); }
/** * Tests marshalling of {@code <gco:CharacterString>}, which require explicit marshaller configuration. * The result shall be as documented in {@link #testUnmarshalCharacterString()}. * * @throws JAXBException if an error occurs while marshalling the language. * * @see #testMarshalLanguageCode() */ @Test public void testMarshalCharacterString() throws JAXBException { final MetadataMock metadata = new MetadataMock(Locale.JAPANESE); final Marshaller marshaller = pool.acquireMarshaller(); marshaller.setProperty(XML.METADATA_VERSION, VERSION_2007); marshaller.setProperty(XML.STRING_SUBSTITUTES, new String[] {"dummy","language","foo"}); assertArrayEquals(new String[] {"language"}, (String[]) marshaller.getProperty(XML.STRING_SUBSTITUTES)); assertXmlEquals(getMetadataXML(CHARACTER_STRING), marshal(marshaller, metadata), "xmlns:*"); pool.recycle(marshaller); }
/** * Tests marshalling in the GML 3.1 namespace. * * @throws JAXBException if an error occurred during marshalling. */ @Test @DependsOnMethod("testMarshall") public void testMarshallGML31() throws JAXBException { final DefaultPrimeMeridian pm = new DefaultPrimeMeridian(PrimeMeridianMock.GREENWICH); final MarshallerPool pool = getMarshallerPool(); final Marshaller marshaller = pool.acquireMarshaller(); marshaller.setProperty(XML.GML_VERSION, LegacyNamespaces.VERSION_3_0); final String xml = marshal(marshaller, pm); pool.recycle(marshaller); assertXmlEquals(getGreenwichXml(LegacyNamespaces.GML), xml, "xmlns:*", "xsi:schemaLocation"); }
/** * Tests marshalling of {@code <gmd:LanguageCode>}. * The result shall be as documented in {@link #testMarshalLanguageCode()}. * * @throws JAXBException if an error occurs while marshalling the language. * * @see #testMarshalCharacterString() */ @Test public void testMarshalLanguageCode() throws JAXBException { final MetadataMock metadata = new MetadataMock(Locale.JAPANESE); final Marshaller marshaller = pool.acquireMarshaller(); assertNull(marshaller.getProperty(XML.STRING_SUBSTITUTES)); marshaller.setProperty(XML.METADATA_VERSION, VERSION_2007); assertXmlEquals(getMetadataXML(LANGUAGE_CODE), marshal(marshaller, metadata), "xmlns:*"); pool.recycle(marshaller); }
/** * 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); }