Search in sources :

Example 11 with MarshallerPool

use of org.apache.sis.xml.MarshallerPool in project sis by apache.

the class CodeListMarshallingTest method testISO_URL.

/**
 * Tests marshaling using the ISO URL.
 *
 * @throws JAXBException if an error occurred while marshaling 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", // Intentionally omit trailing '/'.
    "http://standards.iso.org/ittf/PubliclyAvailableStandards/ISO_19139_Schemas"));
    final String actual = marshal(marshaller, rp);
    pool.recycle(marshaller);
    assertXmlEquals(expected, actual, "xmlns:*");
}
Also used : Marshaller(javax.xml.bind.Marshaller) MarshallerPool(org.apache.sis.xml.MarshallerPool) ResponsibleParty(org.opengis.metadata.citation.ResponsibleParty) Test(org.junit.Test)

Example 12 with MarshallerPool

use of org.apache.sis.xml.MarshallerPool in project sis by apache.

the class DefaultBrowseGraphicTest method testStringSubstitution.

/**
 * Tests XML marshalling of filename substituted by {@code <gco:CharacterString>}
 * inside {@code <mcc:MD_BrowseGraphic>}.
 *
 * @throws JAXBException if an error occurred while (un)marshalling the {@code BrowseGraphic}.
 */
@Test
@DependsOnMethod("testFileNameAndType")
public void testStringSubstitution() throws JAXBException {
    final DefaultBrowseGraphic browse = new DefaultBrowseGraphic(URI.create("file:/catalog/image.png"));
    browse.setFileType("image/tiff");
    final MarshallerPool pool = getMarshallerPool();
    final Marshaller marshaller = pool.acquireMarshaller();
    marshaller.setProperty(XML.STRING_SUBSTITUTES, new String[] { "filename", "mimetype" });
    final String xml = marshal(marshaller, browse);
    pool.recycle(marshaller);
    assertXmlEquals("<mcc:MD_BrowseGraphic xmlns:mcc=\"" + Namespaces.MCC + '"' + " xmlns:gco=\"" + Namespaces.GCO + "\">\n" + "  <mcc:fileName>\n" + "    <gco:CharacterString>file:/catalog/image.png</gco:CharacterString>\n" + "  </mcc:fileName>\n" + "  <mcc:fileType>\n" + "    <gco:CharacterString>image/tiff</gco:CharacterString>\n" + "  </mcc:fileType>\n" + "</mcc:MD_BrowseGraphic>", xml, "xmlns:*");
    /*
         * Unmarshal the element back to a Java object and compare to the original.
         */
    assertEquals(browse, unmarshal(BrowseGraphic.class, xml));
}
Also used : Marshaller(javax.xml.bind.Marshaller) MarshallerPool(org.apache.sis.xml.MarshallerPool) BrowseGraphic(org.opengis.metadata.identification.BrowseGraphic) Test(org.junit.Test) DependsOnMethod(org.apache.sis.test.DependsOnMethod)

Example 13 with MarshallerPool

use of org.apache.sis.xml.MarshallerPool in project sis by apache.

the class XMLTestCase method marshal.

/**
 * 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;
}
Also used : Marshaller(javax.xml.bind.Marshaller) MarshallerPool(org.apache.sis.xml.MarshallerPool)

Example 14 with MarshallerPool

use of org.apache.sis.xml.MarshallerPool in project sis by apache.

the class TimePeriodTest method createMarshallerPool.

/**
 * Creates the XML (un)marshaller pool to be shared by all test methods.
 * The (un)marshallers locale and timezone will be set to fixed values.
 *
 * @throws JAXBException if an error occurred while creating the pool.
 *
 * @see #disposeMarshallerPool()
 */
@BeforeClass
public static void createMarshallerPool() throws JAXBException {
    final Map<String, Object> properties = new HashMap<>(4);
    assertNull(properties.put(XML.LOCALE, Locale.FRANCE));
    assertNull(properties.put(XML.TIMEZONE, "CET"));
    pool = new MarshallerPool(JAXBContext.newInstance(TimeInstant.class, TimePeriod.class), properties);
}
Also used : HashMap(java.util.HashMap) MarshallerPool(org.apache.sis.xml.MarshallerPool) BeforeClass(org.junit.BeforeClass)

Example 15 with MarshallerPool

use of org.apache.sis.xml.MarshallerPool in project sis by apache.

the class LanguageCodeTest method createMarshallerPool.

/**
 * Creates the XML (un)marshaller pool to be shared by all test methods.
 * The (un)marshallers locale and timezone will be set to fixed values.
 *
 * <p>This test uses its own pool instead of {@link XMLTestCase#getMarshallerPool()} because it
 * uses {@link MetadataMock} instead of {@link org.apache.sis.metadata.iso.DefaultMetadata}.</p>
 *
 * @throws JAXBException if an error occurred while creating the pool.
 *
 * @see #disposeMarshallerPool()
 */
@BeforeClass
public static void createMarshallerPool() throws JAXBException {
    final Map<String, Object> properties = new HashMap<>(4);
    assertNull(properties.put(XML.LOCALE, Locale.UK));
    assertNull(properties.put(XML.TIMEZONE, UTC));
    pool = new MarshallerPool(JAXBContext.newInstance(MetadataMock.class), properties);
}
Also used : HashMap(java.util.HashMap) MarshallerPool(org.apache.sis.xml.MarshallerPool) BeforeClass(org.junit.BeforeClass)

Aggregations

MarshallerPool (org.apache.sis.xml.MarshallerPool)16 Marshaller (javax.xml.bind.Marshaller)9 Test (org.junit.Test)7 Unmarshaller (javax.xml.bind.Unmarshaller)5 HashMap (java.util.HashMap)3 DependsOnMethod (org.apache.sis.test.DependsOnMethod)3 BeforeClass (org.junit.BeforeClass)2 StringWriter (java.io.StringWriter)1 XMLComparator (org.apache.sis.test.XMLComparator)1 IdentifiedObjectMock (org.apache.sis.test.mock.IdentifiedObjectMock)1 Version (org.apache.sis.util.Version)1 CitationDate (org.opengis.metadata.citation.CitationDate)1 ResponsibleParty (org.opengis.metadata.citation.ResponsibleParty)1 BrowseGraphic (org.opengis.metadata.identification.BrowseGraphic)1