Search in sources :

Example 6 with Context

use of org.apache.sis.internal.jaxb.Context in project sis by apache.

the class PooledUnmarshaller method unmarshal.

/**
 * Same as {@link #unmarshal(XMLEventReader, TransformVersion)}, but delegating to the unmarshaller
 * methods returning a JAXB element instead than the one returning the object.
 */
private <T> JAXBElement<T> unmarshal(XMLEventReader input, final TransformVersion version, final Class<T> declaredType) throws XMLStreamException, JAXBException {
    input = new TransformingReader(input, version);
    final Context context = begin();
    final JAXBElement<T> object;
    try {
        object = unmarshaller.unmarshal(input, declaredType);
    } finally {
        context.finish();
    }
    // Despite its name, this method does not close the underlying input stream.
    input.close();
    return object;
}
Also used : Context(org.apache.sis.internal.jaxb.Context)

Example 7 with Context

use of org.apache.sis.internal.jaxb.Context in project sis by apache.

the class PooledUnmarshaller method unmarshal.

/**
 * Delegates the unmarshalling to the wrapped unmarshaller.
 */
@Override
public Object unmarshal(XMLEventReader input) throws JAXBException {
    final TransformVersion version = getTransformVersion();
    if (version != null) {
        input = new TransformingReader(input, version);
    }
    final Context context = begin();
    try {
        return unmarshaller.unmarshal(input);
    } finally {
        context.finish();
    }
}
Also used : Context(org.apache.sis.internal.jaxb.Context)

Example 8 with Context

use of org.apache.sis.internal.jaxb.Context in project sis by apache.

the class PooledMarshaller method marshal.

/**
 * Delegates the marshalling to the wrapped marshaller.
 */
@Override
public void marshal(Object object, final OutputStream output) throws JAXBException {
    // Must be call before getTransformVersion()
    object = toImplementation(object);
    final TransformVersion version = getTransformVersion();
    if (version != null)
        try {
            marshal(object, OutputFactory.createXMLEventWriter(output, getEncoding()), version);
        } catch (XMLStreamException e) {
            throw new JAXBException(e);
        }
    else {
        // Marshalling to the default GML version.
        final Context context = begin();
        try {
            marshaller.marshal(object, output);
        } finally {
            context.finish();
        }
    }
}
Also used : Context(org.apache.sis.internal.jaxb.Context) XMLStreamException(javax.xml.stream.XMLStreamException) JAXBException(javax.xml.bind.JAXBException)

Example 9 with Context

use of org.apache.sis.internal.jaxb.Context in project sis by apache.

the class PT_FreeText method create.

/**
 * Constructs a {@linkplain TextGroup text group} from the given {@link InternationalString}
 * if it contains at least one non-root locale. Otherwise returns {@code null}, meaning that
 * the simpler {@link GO_CharacterString} construct should be used instead.
 *
 * @param  text  an international string which could have several translations embedded for the same text.
 * @return a {@code PT_FreeText} instance if the given text has several translations, or {@code null} otherwise.
 */
public static PT_FreeText create(final InternationalString text) {
    if (text instanceof DefaultInternationalString) {
        final DefaultInternationalString df = (DefaultInternationalString) text;
        final Set<Locale> locales = df.getLocales();
        final TextGroup[] textGroup = new TextGroup[locales.size()];
        int n = 0;
        for (final Locale locale : locales) {
            if (locale != null && !locale.equals(Locale.ROOT)) {
                textGroup[n++] = new TextGroup(locale, text.toString(locale));
            }
        }
        if (n != 0) {
            /*
                 * Invoke toString(Locale) instead than toString() even if the locale is null,
                 * since the desired fallback is typically Locale.ROOT instead than the system
                 * default. It is usually safer to avoid null value, but in this particular case
                 * the implementation (DefaultInternationalString) is known to support null.
                 */
            final Context context = Context.current();
            return new PT_FreeText(df.toString(context != null ? context.getLocale() : null), ArraysExt.resize(textGroup, n));
        }
    }
    return null;
}
Also used : Locale(java.util.Locale) Context(org.apache.sis.internal.jaxb.Context) DefaultInternationalString(org.apache.sis.util.iso.DefaultInternationalString)

Example 10 with Context

use of org.apache.sis.internal.jaxb.Context in project sis by apache.

the class CharSequenceAdapter method wrap.

/**
 * Converts a {@linkplain CharSequence character sequence} to the object to be marshalled
 * in a XML file or stream.
 *
 * @param  value  the character representation of the object being marshalled.
 * @return the wrapper for the given character sequence, or {@code null}.
 */
static GO_CharacterString wrap(CharSequence value) {
    if (value instanceof String) {
        // Slightly more efficient variant of this method.
        return wrap(Context.current(), value, (String) value);
    }
    /*
         * <mdb:someElement xsi:type="lan:PT_FreeText_PropertyType">
         *   <gco:CharacterString>...</gco:CharacterString>
         *   <lan:PT_FreeText>
         *     ... see PT_FreeText ...
         *   </lan:PT_FreeText>
         * </mdb:someElement>
         */
    if (value instanceof InternationalString) {
        final PT_FreeText ft = PT_FreeText.create((InternationalString) value);
        if (ft != null) {
            return ft;
        }
    }
    /*
         * Invoking (indirectly) CharSequence.subSequence(…) may change the kind of object.
         * We know that Anchor is safe, and that most InternationalString implementations
         * lost the localized strings. This is why we trim the white spaces only here.
         */
    value = CharSequences.trimWhitespaces(value);
    if (value == null || value.length() == 0) {
        return null;
    }
    /*
         * Substitute <gco:CharacterString> by <gcx:Anchor> if a linkage is found.
         */
    if (!(value instanceof Anchor)) {
        final String key = CharSequences.trimWhitespaces(value.toString());
        if (key != null && !key.isEmpty()) {
            final Context context = Context.current();
            final XLink linkage = Context.resolver(context).anchor(context, value, key);
            if (linkage != null) {
                if (linkage instanceof Anchor) {
                    value = (Anchor) linkage;
                } else {
                    value = new Anchor(linkage, key);
                }
            }
        }
    }
    /*
         * At this stage, the value (typically a String or InternationalString) may
         * have been replaced by an Anchor. The output will be one of the following:
         *
         * ┌──────────────────────────────────────────────────┬────────────────────────────────┐
         * │ <mdb:someElement>                                │ <mdb:someElement>              │
         * │   <gco:CharacterString>...</gco:CharacterString> │   <gcx:Anchor>...</gcx:Anchor> │
         * │ </mdb:someElement>                               │ </mdb:someElement>             │
         * └──────────────────────────────────────────────────┴────────────────────────────────┘
         */
    return new GO_CharacterString(value);
}
Also used : Context(org.apache.sis.internal.jaxb.Context) Anchor(org.apache.sis.internal.jaxb.gcx.Anchor) InternationalString(org.opengis.util.InternationalString) InternationalString(org.opengis.util.InternationalString) PT_FreeText(org.apache.sis.internal.jaxb.lan.PT_FreeText) XLink(org.apache.sis.xml.XLink)

Aggregations

Context (org.apache.sis.internal.jaxb.Context)32 JAXBException (javax.xml.bind.JAXBException)7 XMLStreamException (javax.xml.stream.XMLStreamException)6 Locale (java.util.Locale)2 DependsOnMethod (org.apache.sis.test.DependsOnMethod)2 DefaultInternationalString (org.apache.sis.util.iso.DefaultInternationalString)2 Test (org.junit.Test)2 BufferedOutputStream (java.io.BufferedOutputStream)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 URI (java.net.URI)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 XmlAdapter (javax.xml.bind.annotation.adapters.XmlAdapter)1 DatatypeConfigurationException (javax.xml.datatype.DatatypeConfigurationException)1 XMLGregorianCalendar (javax.xml.datatype.XMLGregorianCalendar)1 CodeListUID (org.apache.sis.internal.jaxb.cat.CodeListUID)1 GO_CharacterString (org.apache.sis.internal.jaxb.gco.GO_CharacterString)1 Anchor (org.apache.sis.internal.jaxb.gcx.Anchor)1