Search in sources :

Example 11 with Context

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

the class TM_Primitive method setTimePeriod.

/**
 * Sets the value from the {@link TimePeriod}.
 * This method is called at unmarshalling-time by JAXB.
 *
 * @param  period  the wrapper to set.
 */
public final void setTimePeriod(final TimePeriod period) {
    // Cleaned first in case of failure.
    metadata = null;
    if (period != null) {
        final Context context = Context.current();
        final Date begin = toDate(context, period.begin);
        final Date end = toDate(context, period.end);
        if (begin != null || end != null) {
            if (begin != null && end != null && end.before(begin)) {
                /*
                     * Be tolerant - we can treat such case as an empty range, which is a similar
                     * approach to what JDK does for Rectangle width and height. We will log with
                     * TemporalPrimitive as the source class, since it is the closest we can get
                     * to a public API.
                     */
                Context.warningOccured(context, TemporalPrimitive.class, "setTimePeriod", Errors.class, Errors.Keys.IllegalRange_2, begin, end);
            } else
                try {
                    metadata = TemporalUtilities.createPeriod(begin, end);
                    period.copyIdTo(metadata);
                } catch (UnsupportedOperationException e) {
                    warningOccured("setTimePeriod", e);
                }
        }
    }
}
Also used : Context(org.apache.sis.internal.jaxb.Context) Date(java.util.Date)

Example 12 with Context

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

the class MD_CharacterSetCode method marshal.

/**
 * Substitutes the code list by the adapter to be marshalled into an XML file
 * or stream. JAXB calls automatically this method at marshalling time.
 *
 * @param  value  the code list value.
 * @return the adapter for the given code list.
 */
@Override
public final MD_CharacterSetCode marshal(final Charset value) {
    final Context context = Context.current();
    final ValueConverter converter = Context.converter(context);
    final String code = converter.toCharsetCode(context, value);
    if (code != null) {
        final Locale locale = context.getLocale();
        final MD_CharacterSetCode c = new MD_CharacterSetCode();
        c.identifier = new CodeListUID(context, "MD_CharacterSetCode", code, (locale != null) ? converter.toLanguageCode(context, locale) : null, (locale != null) ? value.displayName(locale) : value.displayName());
        return c;
    }
    return null;
}
Also used : Context(org.apache.sis.internal.jaxb.Context) Locale(java.util.Locale) CodeListUID(org.apache.sis.internal.jaxb.cat.CodeListUID) ValueConverter(org.apache.sis.xml.ValueConverter)

Example 13 with Context

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

the class DateAdapter method marshal.

/**
 * Converts the date to the object to be marshalled in a XML file or stream.
 * JAXB calls automatically this method at marshalling time.
 *
 * @param  value  the {@code java.util} date value, or {@code null}.
 * @return the XML date, or {@code null}.
 */
@Override
public XMLGregorianCalendar marshal(final Date value) {
    if (value != null) {
        final Context context = Context.current();
        try {
            final XMLGregorianCalendar gc = XmlUtilities.toXML(context, value);
            // Type is xs:date without time.
            XmlUtilities.trimTime(gc, true);
            return gc;
        } catch (DatatypeConfigurationException e) {
            Context.warningOccured(context, XmlAdapter.class, "marshal", e, true);
        }
    }
    return null;
}
Also used : Context(org.apache.sis.internal.jaxb.Context) XMLGregorianCalendar(javax.xml.datatype.XMLGregorianCalendar) DatatypeConfigurationException(javax.xml.datatype.DatatypeConfigurationException) XmlAdapter(javax.xml.bind.annotation.adapters.XmlAdapter)

Example 14 with Context

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

the class MetadataUtilities method logOrFormat.

/**
 * Formats an error message and logs it if we are (un)marshalling a document, or return the message otherwise.
 * In the later case, it is caller's responsibility to use the message for throwing an exception.
 *
 * @param  classe     the caller class, used only in case of warning message to log.
 * @param  property   the property name. Method name will be inferred by the usual Java bean convention.
 * @param  key        a {@code Errors.Keys} value.
 * @param  arguments  the argument to use for formatting the error message.
 * @return {@code null} if the message has been logged, or the message to put in an exception otherwise.
 */
private static String logOrFormat(final Class<?> classe, final String property, final short key, final Object... arguments) {
    final Context context = Context.current();
    if (context == null) {
        return Errors.format(key, arguments);
    } else {
        final StringBuilder buffer = new StringBuilder(property.length() + 3).append("set").append(property);
        buffer.setCharAt(3, Character.toUpperCase(buffer.charAt(3)));
        Context.warningOccured(context, classe, buffer.toString(), Errors.class, key, arguments);
        return null;
    }
}
Also used : Context(org.apache.sis.internal.jaxb.Context)

Example 15 with Context

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

the class MetadataUtilities method setObjectID.

/**
 * Invoked by {@code setID(String)} method implementations for assigning an identifier to an object
 * at unmarshalling time.
 *
 * @param object  the object for which to assign an identifier.
 * @param id      the {@code gco:id} or {@code gml:id} value.
 *
 * @since 0.7
 */
public static void setObjectID(final IdentifiedObject object, String id) {
    id = CharSequences.trimWhitespaces(id);
    if (id != null && !id.isEmpty()) {
        object.getIdentifierMap().putSpecialized(IdentifierSpace.ID, id);
        final Context context = Context.current();
        if (!Context.setObjectForID(context, object, id)) {
            Context.warningOccured(context, object.getClass(), "setID", Errors.class, Errors.Keys.DuplicatedIdentifier_1, id);
        }
    }
}
Also used : Context(org.apache.sis.internal.jaxb.Context)

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