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

The following examples show how to use org.apache.xmlbeans.XmlCursor#setAttributeText() . 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: XmlHelper.java    From arctic-sea with Apache License 2.0 6 votes vote down vote up
public static void fixNamespaceForXsiType(final XmlObject object, final QName value) {
    final XmlCursor cursor = object.newCursor();
    while (cursor.hasNextToken()) {
        if (cursor.toNextToken().isStart()) {
            final String xsiType = cursor.getAttributeText(W3CConstants.QN_XSI_TYPE);
            if (xsiType != null) {

                final String[] toks = xsiType.split(":");
                String localName;
                if (toks.length > 1) {
                    localName = toks[1];
                } else {
                    localName = toks[0];
                }
                if (localName.equals(value.getLocalPart())) {
                    cursor.setAttributeText(W3CConstants.QN_XSI_TYPE,
                            Joiner.on(":").join(XmlHelper.getPrefixForNamespace(object, value.getNamespaceURI()),
                                    value.getLocalPart()));
                }
            }
        }
    }
    cursor.dispose();
}
 
Example 2
Source File: XmlHelper.java    From arctic-sea with Apache License 2.0 6 votes vote down vote up
public static void fixNamespaceForXsiType(XmlObject content, Map<?, ?> namespaces) {
    final XmlCursor cursor = content.newCursor();
    while (cursor.hasNextToken()) {
        if (cursor.toNextToken().isStart()) {
            final String xsiType = cursor.getAttributeText(W3CConstants.QN_XSI_TYPE);
            if (xsiType != null) {
                final String[] toks = xsiType.split(":");
                if (toks.length > 1) {
                    String prefix = toks[0];
                    String localName = toks[1];
                    if (namespaces.containsKey(prefix)) {
                        cursor.setAttributeText(
                                W3CConstants.QN_XSI_TYPE,
                                Joiner.on(":").join(
                                        XmlHelper.getPrefixForNamespace(content, (String) namespaces.get(prefix)),
                                        localName));
                    }
                }

            }
        }
    }
    cursor.dispose();

}
 
Example 3
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 4
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 5
Source File: Soap12Decoder.java    From arctic-sea with Apache License 2.0 5 votes vote down vote up
private void fixNamespaceForXsiType(XmlObject content, Map<?, ?> namespaces) {
    final XmlCursor cursor = content.newCursor();
    while (cursor.hasNextToken()) {
        if (cursor.toNextToken().isStart()) {
            final String xsiType = cursor.getAttributeText(W3CConstants.QN_XSI_TYPE);
            if (xsiType != null) {
                final String[] toks = xsiType.split(":");
                if (toks.length > 1) {
                    String prefix = toks[0];
                    String localName = toks[1];
                    String namespace = (String) namespaces.get(prefix);
                    if (Strings.isNullOrEmpty(namespace)) {
                        namespace = schemaRepository.getNamespaceFor(prefix);
                    }
                    if (!Strings.isNullOrEmpty(namespace)) {
                        cursor.setAttributeText(W3CConstants.QN_XSI_TYPE,
                                Joiner.on(":").join(
                                        XmlHelper.getPrefixForNamespace(content, (String) namespaces.get(prefix)),
                                        localName));
                    }
                }

            }
        }
    }
    cursor.dispose();

}
 
Example 6
Source File: XML.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 *
 * @param attrName
 * @param value
 */
void setAttribute(XMLName xmlName, Object value)
{
    if (xmlName.uri() == null &&
        xmlName.localName().equals("*"))
    {
        throw ScriptRuntime.typeError("@* assignment not supported.");
    }

    XmlCursor curs = newCursor();

    String strValue = ScriptRuntime.toString(value);
    if (curs.currentTokenType().isStartdoc())
    {
        curs.toFirstContentToken();
    }

    javax.xml.namespace.QName qName;

    try
    {
        qName = new javax.xml.namespace.QName(xmlName.uri(), xmlName.localName());
    }
    catch(Exception e)
    {
        throw ScriptRuntime.typeError(e.getMessage());
    }

    if (!curs.setAttributeText(qName, strValue))
    {
        if (curs.currentTokenType().isStart())
        {
            // Can only add attributes inside of a start.
            curs.toNextToken();
        }
        curs.insertAttributeWithValue(qName, strValue);
    }

    curs.dispose();
}
 
Example 7
Source File: SoapUtil.java    From SensorWebClient with GNU General Public License v2.0 5 votes vote down vote up
public static EnvelopeDocument wrapToSoapEnvelope(XmlObject bodyContent, String action) {
		EnvelopeDocument envelopeDoc = EnvelopeDocument.Factory.newInstance();
		
		Envelope envelope = envelopeDoc.addNewEnvelope();
		Header header = envelope.addNewHeader();
		
        XmlCursor cur = header.newCursor();

        cur.toFirstContentToken();
        cur.insertElementWithText(new QName(ns_addressing,"To","wsa"),"http://www.ogc.org/SOS");
        cur.insertElementWithText(new QName(ns_addressing,"Action","wsa"), action);
        cur.insertElementWithText(new QName(ns_addressing,"MessageID","wsa"),
                UUID.randomUUID().toString());
//        cur.beginElement(new QName(ns_addressing,"From","wsa"));
//        cur.insertElementWithText(new QName(ns_addressing,"Address","wsa"),
//        "http://www.w3.org/2005/08/addressing/role/anonymous");
        cur.dispose();
        
		Body body = envelope.addNewBody();
		body.set(bodyContent);
		
		XmlCursor cursor = envelopeDoc.newCursor();
		if (cursor.toFirstChild())
		{
		  cursor.setAttributeText(new QName("http://www.w3.org/2001/XMLSchema-instance","schemaLocation"), "http://www.w3.org/2003/05/soap-envelope http://www.w3.org/2003/05/soap-envelope/soap-envelope.xsd http://www.opengis.net/sos/2.0 http://schemas.opengis.net/sos/2.0/sos.xsd");
		}
		
		return envelopeDoc;
	}
 
Example 8
Source File: N52XmlHelper.java    From arctic-sea with Apache License 2.0 3 votes vote down vote up
/**
 * Sets the schema location to a XmlObject
 *
 * @param document
 *            XML document
 * @param schemaLocations
 *            schema location
 */
public static void setSchemaLocationToDocument(XmlObject document, String schemaLocations) {
    XmlCursor cursor = document.newCursor();
    if (cursor.toFirstChild()) {
        cursor.setAttributeText(getSchemaLocationQName(), schemaLocations);
    }
    cursor.dispose();
}