Java Code Examples for org.apache.xmlbeans.XmlCursor#prefixForNamespace()

The following examples show how to use org.apache.xmlbeans.XmlCursor#prefixForNamespace() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractOmV20XmlStreamWriter.java    From arctic-sea with Apache License 2.0 5 votes vote down vote up
/**
 * write om:result to stream
 *
 * @throws XMLStreamException
 *             If an error occurs when writing to stream
 * @throws EncodingException
 *             If an error occurs when creating elements to be written
 */
protected void writeResult() throws XMLStreamException, EncodingException {
    OmObservation observation = getElement();
    if (observation.getValue() instanceof AbstractObservationValue<?>) {
        ((AbstractObservationValue<?>) observation.getValue()).setValuesForResultEncoding(observation);
    }
    XmlObject createResult = (XmlObject) getEncoder(getEncodeNamespace().orElse(OmConstants.NS_OM_2),
                                                    observation.getValue()).encode(observation.getValue());
    if (createResult != null) {
        if (createResult.xmlText().contains(XML_FRAGMENT)) {
            XmlObject set =
                    OMObservationType.Factory.newInstance(getXmlOptions()).addNewResult().set(createResult);
            writeXmlObject(set, OmConstants.QN_OM_20_RESULT);
        } else {
            if (checkResult(createResult)) {
                QName name = createResult.schemaType().getName();
                String prefix = name.getPrefix();
                if (Strings.isNullOrEmpty(prefix)) {
                    XmlCursor newCursor = createResult.newCursor();
                    prefix = newCursor.prefixForNamespace(name.getNamespaceURI());
                    newCursor.setAttributeText(W3CConstants.QN_XSI_TYPE, prefix + ":" + name.getLocalPart());
                    newCursor.dispose();
                }
                writeXmlObject(createResult, OmConstants.QN_OM_20_RESULT);
            } else {
                start(OmConstants.QN_OM_20_RESULT);
                writeXmlObject(createResult, OmConstants.QN_OM_20_RESULT);
                end(OmConstants.QN_OM_20_RESULT);
            }
        }
    } else {
        empty(OmConstants.QN_OM_20_RESULT);
    }
}
 
Example 2
Source File: AbstractSwesXmlStreamWriter.java    From arctic-sea with Apache License 2.0 5 votes vote down vote up
protected void writeExtension(SweAbstractDataComponent sweAbstractDataComponent)
        throws EncodingException, XMLStreamException {
    EncodingContext ctx = EncodingContext.of(XmlBeansEncodingFlags.PROPERTY_TYPE, "true");
    XmlObject extension = encodeSwe(ctx, sweAbstractDataComponent);
    if (extension.xmlText().contains(XML_FRAGMENT)) {
        XmlObject set =
                ExtensibleResponseType.Factory.newInstance(getXmlOptions())
                        .addNewExtension().set(extension);
        writeXmlObject(set, SwesStreamingConstants.QN_EXTENSION);
    } else {
        if (checkExtension(extension)) {
            QName name = extension.schemaType().getName();
            String prefix = name.getPrefix();
            if (Strings.isNullOrEmpty(prefix)) {
                XmlCursor newCursor = extension.newCursor();
                prefix = newCursor.prefixForNamespace(name.getNamespaceURI());
                newCursor.setAttributeText(W3CConstants.QN_XSI_TYPE,
                        prefix + ":" + name.getLocalPart());
                newCursor.dispose();
            }
            writeXmlObject(extension, SwesStreamingConstants.QN_EXTENSION);
        } else {
            start(SwesStreamingConstants.QN_EXTENSION);
            writeXmlObject(extension, SwesStreamingConstants.QN_EXTENSION);
            end(SwesStreamingConstants.QN_EXTENSION);
        }
    }
}
 
Example 3
Source File: SosDecoderv20.java    From arctic-sea with Apache License 2.0 4 votes vote down vote up
private OwsServiceRequest parseInsertObservation(final InsertObservationDocument insertObservationDoc)
        throws DecodingException {
    // set namespace for default XML type (e.g. xs:string, xs:integer,
    // xs:boolean, ...)
    // Fix for problem with XmlBeans: namespace is not set in child elements
    // when defined in root of request (SOAP)
    final XmlCursor cursor = insertObservationDoc.newCursor();
    if (cursor.toFirstChild() && cursor.namespaceForPrefix(W3CConstants.NS_XS_PREFIX) == null) {
        cursor.prefixForNamespace(W3CConstants.NS_XS);
    }
    cursor.dispose();
    final InsertObservationRequest insertObservationRequest = new InsertObservationRequest();
    final InsertObservationType insertObservationType = insertObservationDoc.getInsertObservation();
    insertObservationRequest.setService(insertObservationType.getService());
    insertObservationRequest.setVersion(insertObservationType.getVersion());
    if (insertObservationDoc.getInsertObservation().getOfferingArray() != null) {
        insertObservationRequest.setOfferings(Arrays.asList(insertObservationType.getOfferingArray()));
    }
    insertObservationRequest.setExtensions(parseExtensibleRequest(insertObservationType));

    if (insertObservationType.getObservationArray() != null) {
        final int length = insertObservationType.getObservationArray().length;
        final Map<String, Time> phenomenonTimes = new HashMap<>(length);
        final Map<String, TimeInstant> resultTimes = new HashMap<>(length);
        final Map<String, AbstractFeature> features = new HashMap<>(length);

        CompositeException exceptions = new CompositeException();
        for (final Observation observation : insertObservationType.getObservationArray()) {
            final Object decodedObject = decodeXmlElement(observation.getOMObservation());
            if (decodedObject instanceof OmObservation) {
                final OmObservation sosObservation = (OmObservation) decodedObject;
                checkAndAddPhenomenonTime(sosObservation.getPhenomenonTime(), phenomenonTimes);
                checkAndAddResultTime(sosObservation.getResultTime(), resultTimes);
                checkAndAddFeatures(sosObservation.getObservationConstellation().getFeatureOfInterest(), features);
                insertObservationRequest.addObservation(sosObservation);
            } else {
                exceptions.add(new DecodingException(Sos2Constants.InsertObservationParams.observation,
                        "The requested observation type (%s) is not supported by this server!",
                        observation.getOMObservation().getDomNode().getNodeName()));
            }
        }
        checkReferencedElements(insertObservationRequest.getObservations(), phenomenonTimes, resultTimes,
                features);
        try {
            exceptions.throwIfNotEmpty();
        } catch (CompositeException ex) {
            throw new DecodingException(ex, Sos2Constants.InsertObservationParams.observation);
        }
    } else {
        // TODO MissingMandatoryParameterException?
        throw new DecodingException(Sos2Constants.InsertObservationParams.observation,
                "The request does not contain an observation");
    }
    return insertObservationRequest;

}
 
Example 4
Source File: XmlHelper.java    From arctic-sea with Apache License 2.0 4 votes vote down vote up
public static String getPrefixForNamespace(XmlObject element, String namespace) {
    final XmlCursor cursor = element.newCursor();
    final String prefix = cursor.prefixForNamespace(namespace);
    cursor.dispose();
    return prefix;
}