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

The following examples show how to use org.apache.xmlbeans.XmlCursor#toFirstChild() . 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: XmlPath.java    From mdw with Apache License 2.0 7 votes vote down vote up
private boolean verify_condition(XmlCursor cursor, Condition condition) {
    boolean more, found=false;
    XmlBookmark bookmark = new XmlBookmark(){};
    cursor.setBookmark(bookmark);
    if (condition.isAttribute) {
        for (more=cursor.toFirstAttribute(); more&&!found; more=cursor.toNextAttribute()) {
            if (cursor.getName().getLocalPart().equals(condition.name)) {
                found = cursor.getTextValue().trim().equals(condition.value);
            }
        }
    } else {
        for (more=cursor.toFirstChild(); more&&!found; more=cursor.toNextSibling()) {
            if (cursor.getName().getLocalPart().equals(condition.name)) {
                found = cursor.getTextValue().trim().equals(condition.value);
            }
        }
    }
    cursor.toBookmark(bookmark);
    return found;
}
 
Example 2
Source File: XmlHelper.java    From arctic-sea with Apache License 2.0 6 votes vote down vote up
/**
 * Utility method to append the contents of the child docment to the end of
 * the parent XmlObject. This is useful when dealing with elements without
 * generated methods (like elements with xs:any)
 *
 * @param parent
 *            Parent to append contents to
 * @param childDoc
 *            Xml document containing contents to be appended
 */
public static void append(final XmlObject parent, final XmlObject childDoc) {
    final XmlCursor parentCursor = parent.newCursor();
    parentCursor.toEndToken();

    final XmlCursor childCursor = childDoc.newCursor();
    childCursor.toFirstChild();

    childCursor.moveXml(parentCursor);
    parentCursor.dispose();
    childCursor.dispose();
}
 
Example 3
Source File: RawCopier.java    From M2Doc with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Get Xml With Ouput relation Id.
 * 
 * @param inputRelationIdToOutputMap
 *            the relation ID mapping
 * @param inputPartURIToOutputPartURI
 *            the mapping form input part {@link PackagePartName} to output par {@link PackagePartName}
 * @param outputBody
 *            the input {@link IBody}
 * @param inputBody
 *            the ouput {@link IBody}
 * @param xmlObject
 *            the {@link XmlObject} to walk
 * @throws XmlException
 *             XmlException
 * @throws InvalidFormatException
 *             if image copy fails
 * @throws IOException
 *             if a {@link PackagePart} can't be read
 * @throws NoSuchAlgorithmException
 *             if MD5 can't be read
 */
private void updateRelationIds(Map<String, String> inputRelationIdToOutputMap,
        Map<URI, URI> inputPartURIToOutputPartURI, IBody inputBody, IBody outputBody, XmlObject xmlObject)
        throws XmlException, InvalidFormatException, NoSuchAlgorithmException, IOException {
    final XmlObject idAttr = xmlObject.selectAttribute(RELATIONSHIPS_URI, "id");
    if (idAttr != null) {
        updateRelationAttribute(inputRelationIdToOutputMap, inputPartURIToOutputPartURI, inputBody, outputBody,
                idAttr);
    } else {
        final XmlObject embedAttr = xmlObject.selectAttribute(RELATIONSHIPS_URI, "embed");
        if (embedAttr != null) {
            updateRelationAttribute(inputRelationIdToOutputMap, inputPartURIToOutputPartURI, inputBody, outputBody,
                    embedAttr);
        }
    }
    final XmlCursor cursor = xmlObject.newCursor();
    if (cursor.toFirstChild()) {
        updateRelationIds(inputRelationIdToOutputMap, inputPartURIToOutputPartURI, inputBody, outputBody,
                cursor.getObject());
        while (cursor.toNextSibling()) {
            updateRelationIds(inputRelationIdToOutputMap, inputPartURIToOutputPartURI, inputBody, outputBody,
                    cursor.getObject());
        }
    }
    cursor.dispose();
}
 
Example 4
Source File: XML.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 *
 * @return
 */
boolean hasSimpleContent()
{
    boolean simpleContent = false;

    XmlCursor curs = newCursor();

    if (curs.isAttr() || curs.isText()) {
        return true;
    }

    if (curs.isStartdoc())
    {
        curs.toFirstContentToken();
    }

    simpleContent = !(curs.toFirstChild());

    curs.dispose();

    return simpleContent;
}
 
Example 5
Source File: XmlPath.java    From mdw with Apache License 2.0 5 votes vote down vote up
private String evaluate_recursive_descent(XmlCursor cursor, PathSegment path) {
    String value;
    XmlBookmark bookmark = new XmlBookmark(){};
    cursor.setBookmark(bookmark);
    value = evaluate_segment(cursor, path);
    if (value!=null) return value;
    boolean more = cursor.toFirstChild();
    while (value==null && more) {
        value = evaluate_recursive_descent(cursor, path);
        more = cursor.toNextSibling();
    }
    cursor.toBookmark(bookmark);
    return value;
}
 
Example 6
Source File: XML.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 *
 * @param cursor
 * @param opts
 * @return
 */
private static String dumpNode(XmlCursor cursor, XmlOptions opts)
{
    if (cursor.isText())
        return cursor.getChars();

    if (cursor.isFinish())
        return "";

    cursor.push();
    boolean wanRawText = cursor.isStartdoc() && !cursor.toFirstChild();
    cursor.pop();

    return wanRawText ? cursor.getTextValue() : cursor.xmlText( opts );
}
 
Example 7
Source File: DhlXTeeService.java    From j-road with Apache License 2.0 5 votes vote down vote up
protected Map<Class<? extends XmlObject>, List<? extends XmlObject>> parse(XmlObject xmlObject) {
    if (log.isTraceEnabled()) {
        log.trace("starting to parse xmlObject:\n" + xmlObject + "\n\n");
    }
    XmlCursor cursor = xmlObject.newCursor();
    cursor.toFirstChild();// move before the root element
    cursor.toFirstChild();// move to the first child of the root
    Map<Class<? extends XmlObject>, List<? extends XmlObject>> subElementsList = new HashMap<Class<? extends XmlObject>, List<? extends XmlObject>>();
    try {
        do {
            XmlObject object = XmlObject.Factory.parse(cursor.getDomNode());
            @SuppressWarnings("unchecked")
            // to be able to add object that is in fact a subclass of XmlObject, but referenced using XmlObject
            List<XmlObject> list = (List<XmlObject>) subElementsList.get(object.getClass());
            if (list == null) {
                list = new ArrayList<XmlObject>();
            }
            list.add(object);
            subElementsList.put(object.getClass(), list);
            log.debug("adding node: '" + cursor.getDomNode().getLocalName() + "' (" + cursor.getDomNode().getNamespaceURI()
                    + "),\nclass: " + object.getClass() + ", cursor: \n" + object);
        } while (cursor.toNextSibling());
    } catch (XmlException e) {
        throw new RuntimeException("Failed to parse xmlObject:\n" + xmlObject, e);
    }
    return subElementsList;
}
 
Example 8
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 9
Source File: SosDecoderv100.java    From arctic-sea with Apache License 2.0 4 votes vote down vote up
@Override
public OwsServiceCommunicationObject decode(XmlObject xmlObject) throws DecodingException {
    OwsServiceCommunicationObject request = null;
    LOGGER.debug("REQUESTTYPE:" + xmlObject.getClass());

    /*
     * Add O&M 1.0.0 namespace to GetObservation document. XmlBeans removes
     * the namespace from the document because there are no om:... elements
     * in the document. But the validation fails if the <resultModel>
     * element is set with e.g. om:Measurement.
     */
    if (xmlObject instanceof GetObservationDocument) {
        XmlCursor cursor = xmlObject.newCursor();
        cursor.toFirstChild();
        cursor.insertNamespace(OmConstants.NS_OM_PREFIX, OmConstants.NS_OM);
        cursor.dispose();
    }
    // validate document
    XmlHelper.validateDocument(xmlObject);

    if (xmlObject instanceof GetCapabilitiesDocument) {
        // getCapabilities request
        GetCapabilitiesDocument getCapsDoc = (GetCapabilitiesDocument) xmlObject;
        request = parseGetCapabilities(getCapsDoc);
    } else if (xmlObject instanceof DescribeSensorDocument) {
        // DescribeSensor request (still SOS 1.0 NS_URI
        DescribeSensorDocument descSensorDoc = (DescribeSensorDocument) xmlObject;
        request = parseDescribeSensor(descSensorDoc);
    } else if (xmlObject instanceof GetObservationDocument) {
        // getObservation request
        GetObservationDocument getObsDoc = (GetObservationDocument) xmlObject;
        request = parseGetObservation(getObsDoc);
    } else if (xmlObject instanceof GetFeatureOfInterestDocument) {
        // getFeatureOfInterest request
        GetFeatureOfInterestDocument getFoiDoc = (GetFeatureOfInterestDocument) xmlObject;
        request = parseGetFeatureOfInterest(getFoiDoc);
    } else if (xmlObject instanceof GetObservationByIdDocument) {
        // getObservationById request
        GetObservationByIdDocument getObsByIdDoc = (GetObservationByIdDocument) xmlObject;
        request = parseGetObservationById(getObsByIdDoc);
    } else {
        throw new UnsupportedDecoderXmlInputException(this, xmlObject);
    }
    return request;
}
 
Example 10
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 11
Source File: RawCopier.java    From M2Doc with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Copies the fragment of the input {@link XWPFParagraph} into the output {@link XWPFParagraph} starting at the given start
 * {@link XmlObject}.
 * 
 * @param inputRelationIdToOutputMap
 *            the relation ID mapping
 * @param inputPartURIToOutputPartURI
 *            the mapping form input part {@link PackagePartName} to output par {@link PackagePartName}
 * @param outputParagraph
 *            the output {@link XWPFParagraph}
 * @param inputParagraph
 *            the input {@link XWPFParagraph}
 * @param startXmlObject
 *            the {@link XmlObject} to start at
 * @param endXmlObject
 *            the {@link XmlObject} to end at
 * @throws XmlException
 *             if xml manipulation fails
 * @throws InvalidFormatException
 *             if image copy fails
 * @throws IOException
 *             if a {@link PackagePart} can't be read
 * @throws NoSuchAlgorithmException
 *             if MD5 can't be found
 */
private void copyParagraphFragment(Map<String, String> inputRelationIdToOutputMap,
        Map<URI, URI> inputPartURIToOutputPartURI, XWPFParagraph outputParagraph, XWPFParagraph inputParagraph,
        XmlObject startXmlObject, XmlObject endXmlObject)
        throws XmlException, InvalidFormatException, NoSuchAlgorithmException, IOException {
    if (inputParagraph.getCTP().isSetPPr()) {
        outputParagraph.getCTP().setPPr((CTPPr) inputParagraph.getCTP().getPPr().copy());
    }

    final XmlCursor inputCursor;
    if (startXmlObject != null) {
        inputCursor = startXmlObject.newCursor();
    } else {
        inputCursor = inputParagraph.getCTP().newCursor();
        inputCursor.toFirstChild();
    }
    try {
        if (startXmlObject == null || inputCursor.getObject().equals(startXmlObject)) {
            final XmlCursor outputCursor = outputParagraph.getCTP().newCursor();
            try {
                outputCursor.toLastChild();
                outputCursor.toEndToken();
                outputCursor.toNextToken();
                do {
                    if (endXmlObject != null && inputCursor.getObject().equals(endXmlObject)) {
                        break;
                    }
                    inputCursor.copyXml(outputCursor);
                    final XmlCursor tmpCursor = outputCursor.newCursor();
                    tmpCursor.toPrevSibling();
                    tmpCursor.getObject();
                    updateRelationIds(inputRelationIdToOutputMap, inputPartURIToOutputPartURI,
                            inputParagraph.getBody(), outputParagraph.getBody(), tmpCursor.getObject());
                    // TODO update bookmark manager
                    tmpCursor.dispose();
                } while (inputCursor.toNextSibling());
            } finally {
                outputCursor.dispose();
            }
        }
    } finally {
        inputCursor.dispose();
    }
}
 
Example 12
Source File: XmlPath.java    From mdw with Apache License 2.0 4 votes vote down vote up
public static String getRootNodeName(XmlObject xmlbean) {
    XmlCursor cursor = xmlbean.newCursor();
    cursor.toFirstChild();
    return cursor.getName().getLocalPart();
}
 
Example 13
Source File: XmlPath.java    From mdw with Apache License 2.0 4 votes vote down vote up
public static String getRootNodeValue(XmlObject xmlbean) {
    XmlCursor cursor = xmlbean.newCursor();
    cursor.toFirstChild();
    return cursor.getTextValue();
}
 
Example 14
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();
}