use of org.apache.sis.internal.jaxb.Context in project sis by apache.
the class ReferenceResolver method resolve.
/**
* Returns an object of the given type for the given {@code xlink} attribute, or {@code null} if none.
* The default implementation performs the following lookups:
*
* <ul>
* <li>If the {@link XLink#getHRef() xlink:href} attribute is a {@linkplain URI#getFragment() URI fragment}
* of the form {@code "#foo"} and if an object of class {@code type} with the {@code gml:id="foo"} attribute
* has previously been seen in the same XML document, then that object is returned.</li>
* <li>Otherwise returns {@code null}.</li>
* </ul>
*
* @param <T> the compile-time type of the {@code type} argument.
* @param context context (GML version, locale, <i>etc.</i>) of the (un)marshalling process.
* @param type the type of object to be unmarshalled, often as a GeoAPI interface.
* @param link the {@code xlink} attributes.
* @return an object of the given type for the given {@code xlink} attribute, or {@code null} if none.
*/
public <T> T resolve(final MarshalContext context, final Class<T> type, final XLink link) {
ensureNonNull("type", type);
ensureNonNull("xlink", link);
final URI href = link.getHRef();
if (href != null && href.toString().startsWith("#")) {
final String id = href.getFragment();
final Context c = (context instanceof Context) ? (Context) context : Context.current();
final Object object = Context.getObjectForID(c, id);
if (type.isInstance(object)) {
return type.cast(object);
} else {
final short key;
final Object args;
if (object == null) {
key = Errors.Keys.NotABackwardReference_1;
args = id;
} else {
key = Errors.Keys.UnexpectedTypeForReference_3;
args = new Object[] { id, type, object.getClass() };
}
Context.warningOccured(c, ReferenceResolver.class, "resolve", Errors.class, key, args);
}
}
return null;
}
use of org.apache.sis.internal.jaxb.Context in project sis by apache.
the class PooledMarshaller method marshal.
/**
* Marshals to the given output with on-the-fly substitution of namespaces.
* This method is invoked when the user asked to marshal to a different GML or metadata version than the
* one supported natively by SIS, i.e. when {@link #getTransformVersion()} returns a non-null value.
*
* @param object the object to marshal.
* @param output the writer created by SIS (<b>not</b> the writer given by the user).
* @param version identifies the namespace substitutions to perform.
*/
private void marshal(Object object, XMLEventWriter output, final TransformVersion version) throws XMLStreamException, JAXBException {
output = new TransformingWriter(output, version);
final Context context = begin();
try {
marshaller.marshal(object, output);
} finally {
context.finish();
}
// Despite its name, this method does not close the underlying output stream.
output.close();
}
Aggregations