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);
}
}
}
}
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;
}
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;
}
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;
}
}
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);
}
}
}
Aggregations