Search in sources :

Example 31 with Context

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;
}
Also used : Context(org.apache.sis.internal.jaxb.Context) URI(java.net.URI)

Example 32 with Context

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