Search in sources :

Example 1 with Workaround

use of org.apache.sis.util.Workaround in project sis by apache.

the class GO_CharacterString method getCodeList.

/**
 * Returns the code list wrapped in a JAXB element, or {@code null} if the {@link #text} is not a wrapper for
 * a code list. Only one of {@link #getValue()} and {@code getCodeList()} should return a non-null value.
 *
 * <div class="note"><b>Note:</b>
 * we have to rely on a somewhat complicated mechanism because the code lists implementations in GeoAPI
 * do not have JAXB annotations. If those annotations are added in a future GeoAPI implementation, then
 * we could replace this mechanism by a simple property annotated with {@code XmlElementRef}.</div>
 *
 * @since 0.7
 */
@XmlAnyElement
@Workaround(library = "GeoAPI", version = "3.0")
private Object getCodeList() {
    if (type != ENUM) {
        return null;
    }
    final CodeList<?> code = Types.forCodeTitle(text);
    final String name = Types.getListName(code);
    /*
         * The namespace has have various value like CIT, SRV, MDQ, MRI, MSR, LAN, etc.
         * The real namespace is declared in the @XmlElement annotation of the getElement
         * method in the JAXB adapter. We could use reflection, but we do not in order to
         * avoid potential class loading issue and also because not all CodeList are in the
         * same package.
         */
    String namespace = Namespaces.guessForType(name);
    if (namespace == null) {
        namespace = XMLConstants.NULL_NS_URI;
    }
    return new JAXBElement<>(new QName(namespace, name), CodeListUID.class, new CodeListUID(Context.current(), code));
}
Also used : CodeListUID(org.apache.sis.internal.jaxb.cat.CodeListUID) QName(javax.xml.namespace.QName) JAXBElement(javax.xml.bind.JAXBElement) XmlAnyElement(javax.xml.bind.annotation.XmlAnyElement) Workaround(org.apache.sis.util.Workaround)

Example 2 with Workaround

use of org.apache.sis.util.Workaround in project sis by apache.

the class XML method unmarshal.

/**
 * Unmarshal an object from the given stream, DOM or other sources.
 * Together with the {@linkplain #unmarshal(Source, Map) Unmarshal Global Root Element} variant,
 * this is the most flexible unmarshalling method provided in this {@code XML} class.
 * The source is specified by the {@code input} argument implementation, for example
 * {@link javax.xml.transform.stream.StreamSource} for reading from a file or input stream.
 * The optional {@code properties} map can contain any key documented in this {@code XML} class,
 * together with the keys documented in the <cite>supported properties</cite> section of the the
 * {@link Unmarshaller} class.
 *
 * @param  <T>           compile-time value of the {@code declaredType} argument.
 * @param  input         the file from which to read a XML representation.
 * @param  declaredType  the JAXB mapped class of the object to unmarshal.
 * @param  properties    an optional map of properties to give to the unmarshaller, or {@code null} if none.
 * @return the object unmarshalled from the given input, wrapped in a JAXB element.
 * @throws JAXBException if a property has an illegal value, or if an error occurred during the unmarshalling.
 *
 * @since 0.8
 */
public static <T> JAXBElement<T> unmarshal(final Source input, final Class<T> declaredType, final Map<String, ?> properties) throws JAXBException {
    ensureNonNull("input", input);
    ensureNonNull("declaredType", declaredType);
    final MarshallerPool pool = getPool();
    final Unmarshaller unmarshaller = pool.acquireUnmarshaller(properties);
    final JAXBElement<T> element;
    if (input instanceof StAXSource) {
        // Same workaround than the one documented in above method.
        @Workaround(library = "JDK", version = "1.8") final XMLStreamReader reader = ((StAXSource) input).getXMLStreamReader();
        if (reader != null) {
            element = unmarshaller.unmarshal(reader, declaredType);
        } else {
            element = unmarshaller.unmarshal(((StAXSource) input).getXMLEventReader(), declaredType);
        }
    } else {
        element = unmarshaller.unmarshal(input, declaredType);
    }
    pool.recycle(unmarshaller);
    return element;
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) StAXSource(javax.xml.transform.stax.StAXSource) Unmarshaller(javax.xml.bind.Unmarshaller) Workaround(org.apache.sis.util.Workaround)

Example 3 with Workaround

use of org.apache.sis.util.Workaround in project sis by apache.

the class NoOp method parameters.

/**
 * Work around for RFE #4093999 in Sun's bug database
 * ("Relax constraint on placement of this()/super() call in constructors").
 */
@Workaround(library = "JDK", version = "1.7")
private static Parameters parameters(final double semiMajor, final double semiMinor) {
    final ParameterValueGroup group = new ParameterBuilder().addName("No-operation").createGroupForMapProjection().createValue();
    group.parameter(Constants.SEMI_MAJOR).setValue(semiMajor);
    group.parameter(Constants.SEMI_MINOR).setValue(semiMinor);
    return (Parameters) group;
}
Also used : Parameters(org.apache.sis.parameter.Parameters) ParameterValueGroup(org.opengis.parameter.ParameterValueGroup) ParameterBuilder(org.apache.sis.parameter.ParameterBuilder) Workaround(org.apache.sis.util.Workaround)

Example 4 with Workaround

use of org.apache.sis.util.Workaround in project sis by apache.

the class XML method marshal.

/**
 * Marshal the given object to a stream, DOM or other destinations.
 * This is the most flexible marshalling method provided in this {@code XML} class.
 * The destination is specified by the {@code output} argument implementation, for example
 * {@link javax.xml.transform.stream.StreamResult} for writing to a file or output stream.
 * The optional {@code properties} map can contain any key documented in this {@code XML} class,
 * together with the keys documented in the <cite>supported properties</cite> section of the the
 * {@link Marshaller} class.
 *
 * @param  object      the root of content tree to be marshalled.
 * @param  output      the file to be written.
 * @param  properties  an optional map of properties to give to the marshaller, or {@code null} if none.
 * @throws JAXBException if a property has an illegal value, or if an error occurred during the marshalling.
 *
 * @since 0.4
 */
public static void marshal(final Object object, final Result output, final Map<String, ?> properties) throws JAXBException {
    ensureNonNull("object", object);
    ensureNonNull("output", output);
    final MarshallerPool pool = getPool();
    final Marshaller marshaller = pool.acquireMarshaller();
    if (properties != null) {
        for (final Map.Entry<String, ?> entry : properties.entrySet()) {
            marshaller.setProperty(entry.getKey(), entry.getValue());
        }
    }
    /*
         * STAX results are not handled by JAXB as of JDK 8. We have to handle those cases ourselves.
         * This workaround should be removed if a future JDK version handles those cases.
         */
    if (output instanceof StAXResult) {
        @Workaround(library = "JDK", version = "1.8") final XMLStreamWriter writer = ((StAXResult) output).getXMLStreamWriter();
        if (writer != null) {
            marshaller.marshal(object, writer);
        } else {
            marshaller.marshal(object, ((StAXResult) output).getXMLEventWriter());
        }
    } else {
        marshaller.marshal(object, output);
    }
    pool.recycle(marshaller);
}
Also used : StAXResult(javax.xml.transform.stax.StAXResult) Marshaller(javax.xml.bind.Marshaller) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) Map(java.util.Map) Workaround(org.apache.sis.util.Workaround)

Example 5 with Workaround

use of org.apache.sis.util.Workaround in project sis by apache.

the class XML method unmarshal.

/**
 * Unmarshal an object from the given stream, DOM or other sources.
 * Together with the {@linkplain #unmarshal(Source, Class, Map) Unmarshal by Declared Type} variant,
 * this is the most flexible unmarshalling method provided in this {@code XML} class.
 * The source is specified by the {@code input} argument implementation, for example
 * {@link javax.xml.transform.stream.StreamSource} for reading from a file or input stream.
 * The optional {@code properties} map can contain any key documented in this {@code XML} class,
 * together with the keys documented in the <cite>supported properties</cite> section of the the
 * {@link Unmarshaller} class.
 *
 * @param  input       the file from which to read a XML representation.
 * @param  properties  an optional map of properties to give to the unmarshaller, or {@code null} if none.
 * @return the object unmarshalled from the given input.
 * @throws JAXBException if a property has an illegal value, or if an error occurred during the unmarshalling.
 *
 * @since 0.4
 */
public static Object unmarshal(final Source input, final Map<String, ?> properties) throws JAXBException {
    ensureNonNull("input", input);
    final MarshallerPool pool = getPool();
    final Unmarshaller unmarshaller = pool.acquireUnmarshaller(properties);
    final Object object;
    /*
         * STAX sources are not handled by javax.xml.bind.helpers.AbstractUnmarshallerImpl implementation as of JDK 8.
         * We have to handle those cases ourselves. This workaround should be removed if a future JDK version handles
         * those cases.
         */
    if (input instanceof StAXSource) {
        @Workaround(library = "JDK", version = "1.8") final XMLStreamReader reader = ((StAXSource) input).getXMLStreamReader();
        if (reader != null) {
            object = unmarshaller.unmarshal(reader);
        } else {
            object = unmarshaller.unmarshal(((StAXSource) input).getXMLEventReader());
        }
    } else {
        object = unmarshaller.unmarshal(input);
    }
    pool.recycle(unmarshaller);
    return object;
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) StAXSource(javax.xml.transform.stax.StAXSource) Unmarshaller(javax.xml.bind.Unmarshaller) Workaround(org.apache.sis.util.Workaround)

Aggregations

Workaround (org.apache.sis.util.Workaround)5 Unmarshaller (javax.xml.bind.Unmarshaller)2 XMLStreamReader (javax.xml.stream.XMLStreamReader)2 StAXSource (javax.xml.transform.stax.StAXSource)2 Map (java.util.Map)1 JAXBElement (javax.xml.bind.JAXBElement)1 Marshaller (javax.xml.bind.Marshaller)1 XmlAnyElement (javax.xml.bind.annotation.XmlAnyElement)1 QName (javax.xml.namespace.QName)1 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)1 StAXResult (javax.xml.transform.stax.StAXResult)1 CodeListUID (org.apache.sis.internal.jaxb.cat.CodeListUID)1 ParameterBuilder (org.apache.sis.parameter.ParameterBuilder)1 Parameters (org.apache.sis.parameter.Parameters)1 ParameterValueGroup (org.opengis.parameter.ParameterValueGroup)1