Search in sources :

Example 1 with XmlCursor

use of org.apache.xmlbeans.XmlCursor in project hackpad by dropbox.

the class XML method appendChild.

/**
     *
     * @param xml
     * @return
     */
XML appendChild(Object xml) {
    XmlCursor curs = newCursor();
    if (curs.isStartdoc()) {
        curs.toFirstContentToken();
    }
    // Move the cursor to the end of this element
    if (curs.isStart()) {
        curs.toEndToken();
    }
    insertChild(curs, xml);
    curs.dispose();
    return this;
}
Also used : XmlCursor(org.apache.xmlbeans.XmlCursor)

Example 2 with XmlCursor

use of org.apache.xmlbeans.XmlCursor in project hackpad by dropbox.

the class XML method addNamespace.

//
//
// Methods from section 12.4.4 in the spec
//
//
/**
     * The addNamespace method adds a namespace declaration to the in scope
     * namespaces for this XML object and returns this XML object.
     *
     * @param toAdd
     */
XML addNamespace(Namespace ns) {
    // When a namespace is used it will be added automatically
    // to the inScopeNamespaces set. There is no need to add
    // Namespaces with undefined prefixes.
    String nsPrefix = ns.prefix();
    if (nsPrefix == null)
        return this;
    XmlCursor cursor = newCursor();
    try {
        if (!cursor.isContainer())
            return this;
        javax.xml.namespace.QName qname = cursor.getName();
        // with QNames in no namespace.
        if (qname.getNamespaceURI().equals("") && nsPrefix.equals(""))
            return this;
        // Get all declared namespaces that are in scope
        Map prefixToURI = NamespaceHelper.getAllNamespaces(lib, cursor);
        String uri = (String) prefixToURI.get(nsPrefix);
        if (uri != null) {
            // Check if the Namespace is not already in scope
            if (uri.equals(ns.uri()))
                return this;
            cursor.push();
            // Let's see if we have to delete a namespace declaration
            while (cursor.toNextToken().isAnyAttr()) {
                if (cursor.isNamespace()) {
                    qname = cursor.getName();
                    String prefix = qname.getLocalPart();
                    if (prefix.equals(nsPrefix)) {
                        // Delete the current Namespace declaration
                        cursor.removeXml();
                        break;
                    }
                }
            }
            cursor.pop();
        }
        cursor.toNextToken();
        cursor.insertNamespace(nsPrefix, ns.uri());
    } finally {
        cursor.dispose();
    }
    return this;
}
Also used : XmlCursor(org.apache.xmlbeans.XmlCursor)

Example 3 with XmlCursor

use of org.apache.xmlbeans.XmlCursor in project hackpad by dropbox.

the class XML method doPut.

/**
     *
     * @param currXMLNode
     * @param xmlValue
     * @return
     */
private boolean doPut(XMLName name, XML currXMLNode, XMLObjectImpl xmlValue) {
    boolean result = false;
    XmlCursor curs = currXMLNode.newCursor();
    try {
        // Replace the node with this new xml value.
        XML xml;
        int toAssignLen = xmlValue.length();
        for (int i = 0; i < toAssignLen; i++) {
            if (xmlValue instanceof XMLList) {
                xml = ((XMLList) xmlValue).item(i);
            } else {
                xml = (XML) xmlValue;
            }
            // If it's an attribute or text node, make text node.
            XmlCursor.TokenType tt = xml.tokenType();
            if (tt == XmlCursor.TokenType.ATTR || tt == XmlCursor.TokenType.TEXT) {
                xml = makeXmlFromString(lib, name, xml.toString());
            }
            if (i == 0) {
                // 1st assignment is replaceChild all others are appendChild
                replace(curs, xml);
            } else {
                insertChild(curs, xml);
            }
        }
        // We're done we've blown away the node because the rvalue was XML...
        result = true;
    } catch (Exception ex) {
        ex.printStackTrace();
        throw ScriptRuntime.typeError(ex.getMessage());
    } finally {
        curs.dispose();
    }
    return result;
}
Also used : TokenType(org.apache.xmlbeans.XmlCursor.TokenType) XmlException(org.apache.xmlbeans.XmlException) XmlCursor(org.apache.xmlbeans.XmlCursor)

Example 4 with XmlCursor

use of org.apache.xmlbeans.XmlCursor in project hackpad by dropbox.

the class XML method matchChildren.

/**
     *
     * @return
     */
private XMLList matchChildren(XmlCursor.TokenType tokenType, XMLName name) {
    XMLList result = new XMLList(lib);
    XmlCursor curs = newCursor();
    TokenType tt = curs.currentTokenType();
    javax.xml.namespace.QName qname = new javax.xml.namespace.QName(name.uri(), name.localName());
    javax.xml.namespace.QName targetProperty = qname;
    if (tt.isStartdoc()) {
        tt = curs.toFirstContentToken();
    }
    if (tt.isContainer()) {
        tt = curs.toFirstContentToken();
        while (!tt.isEnd()) {
            if (tt == tokenType) {
                // Only try to match names for elements or processing instructions.
                if (!tt.isStart() && !tt.isProcinst()) {
                    // Not an element or no name specified.
                    result.addToList(findAnnotation(curs));
                    // Reset target property to null in this case
                    targetProperty = null;
                } else {
                    // Match names as well
                    if (qnameMatches(name, curs.getName())) {
                        // Add it to the list
                        result.addToList(findAnnotation(curs));
                        // set to null
                        if (targetProperty != null) {
                            if (targetProperty.getLocalPart().equals("*")) {
                                targetProperty = curs.getName();
                            } else if (!targetProperty.getLocalPart().equals(curs.getName().getLocalPart())) {
                                // Not a match, unset target property
                                targetProperty = null;
                            }
                        }
                    }
                }
            }
            // Skip over child elements
            if (tt.isStart()) {
                tt = curs.toEndToken();
            }
            tt = curs.toNextToken();
        }
    }
    curs.dispose();
    if (tokenType == XmlCursor.TokenType.START) {
        // Set the targets for this XMLList.
        result.setTargets(this, targetProperty);
    }
    return result;
}
Also used : TokenType(org.apache.xmlbeans.XmlCursor.TokenType) XmlCursor(org.apache.xmlbeans.XmlCursor)

Example 5 with XmlCursor

use of org.apache.xmlbeans.XmlCursor in project hackpad by dropbox.

the class XML method createFromXmlObject.

static XML createFromXmlObject(XMLLibImpl lib, XmlObject xo) {
    XScriptAnnotation anno;
    XmlCursor curs = xo.newCursor();
    if (curs.currentTokenType().isStartdoc()) {
        curs.toFirstContentToken();
    }
    try {
        anno = new XScriptAnnotation(curs);
        curs.setBookmark(anno);
    } finally {
        curs.dispose();
    }
    return new XML(lib, anno);
}
Also used : XmlCursor(org.apache.xmlbeans.XmlCursor)

Aggregations

XmlCursor (org.apache.xmlbeans.XmlCursor)160 XmlObject (org.apache.xmlbeans.XmlObject)68 QName (javax.xml.namespace.QName)21 XmlException (org.apache.xmlbeans.XmlException)16 TokenType (org.apache.xmlbeans.XmlCursor.TokenType)14 CTTbl (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl)14 CTP (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP)10 ArrayList (java.util.ArrayList)9 POSIXApplicationType (org.ggf.schemas.jsdl.x2005.x11.jsdlPosix.POSIXApplicationType)8 HPCProfileApplicationType (org.ggf.schemas.jsdl.x2006.x07.jsdlHpcpa.HPCProfileApplicationType)8 SPMDApplicationType (org.ogf.schemas.jsdl.x2007.x02.jsdlSpmd.SPMDApplicationType)8 IOException (java.io.IOException)5 POIXMLException (org.apache.poi.POIXMLException)5 InputStream (java.io.InputStream)4 DrawPaint (org.apache.poi.sl.draw.DrawPaint)3 ArrayType (org.dmg.pmml.ArrayType)3 ApplicationType (org.ggf.schemas.jsdl.x2005.x11.jsdl.ApplicationType)3 LineString (org.locationtech.jts.geom.LineString)3 CTRow (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRow)3 CTSdtBlock (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSdtBlock)3