Search in sources :

Example 96 with XmlCursor

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

the class XML method matchDescendantChildren.

/**
     *
     * @return
     */
private XMLList matchDescendantChildren(XMLName xmlName) {
    XMLList result = new XMLList(lib);
    XmlCursor curs = newCursor();
    TokenType tt = curs.currentTokenType();
    // Set the targets for this XMLList.
    result.setTargets(this, null);
    if (tt.isStartdoc()) {
        tt = curs.toFirstContentToken();
    }
    if (tt.isContainer()) {
        int nestLevel = 1;
        while (nestLevel > 0) {
            tt = curs.toNextToken();
            if (!tt.isAttr() && !tt.isEnd() && !tt.isEnddoc()) {
                // Only try to match names for elements or processing instructions.
                if (!tt.isStart() && !tt.isProcinst()) {
                    // Not an element or procinst, only add if qname is all
                    if (xmlName.localName().equals("*")) {
                        result.addToList(findAnnotation(curs));
                    }
                } else {
                    if (qnameMatches(xmlName, curs.getName())) {
                        result.addToList(findAnnotation(curs));
                    }
                }
            }
            if (tt.isStart()) {
                nestLevel++;
            } else if (tt.isEnd()) {
                nestLevel--;
            } else if (tt.isEnddoc()) {
                // Shouldn't get here, but just in case.
                break;
            }
        }
    }
    curs.dispose();
    return result;
}
Also used : TokenType(org.apache.xmlbeans.XmlCursor.TokenType) XmlCursor(org.apache.xmlbeans.XmlCursor)

Example 97 with XmlCursor

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

the class XML method setName.

/**
     *
     * @param name
     */
void setName(QName qname) {
    XmlCursor cursor = newCursor();
    try {
        if (cursor.isStartdoc())
            cursor.toFirstContentToken();
        if (cursor.isText() || cursor.isComment())
            return;
        if (cursor.isProcinst()) {
            String localName = qname.localName();
            cursor.setName(new javax.xml.namespace.QName(localName));
        } else {
            String prefix = qname.prefix();
            if (prefix == null) {
                prefix = "";
            }
            cursor.setName(new javax.xml.namespace.QName(qname.uri(), qname.localName(), prefix));
        }
    } finally {
        cursor.dispose();
    }
}
Also used : XmlCursor(org.apache.xmlbeans.XmlCursor)

Example 98 with XmlCursor

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

the class XML method removeNamespace.

/**
     *
     * @param namespace
     */
XML removeNamespace(Namespace ns) {
    XmlCursor cursor = newCursor();
    try {
        if (cursor.isStartdoc())
            cursor.toFirstContentToken();
        if (!cursor.isStart())
            return this;
        String nsPrefix = ns.prefix();
        String nsURI = ns.uri();
        Map prefixToURI = new HashMap();
        int depth = 1;
        while (!(cursor.isEnd() && depth == 0)) {
            if (cursor.isStart()) {
                // Get the namespaces declared in this element.
                // The ones with undefined prefixes are not candidates
                // for removal because they are used.
                prefixToURI.clear();
                NamespaceHelper.getNamespaces(cursor, prefixToURI);
                ObjArray inScopeNSBag = new ObjArray();
                Iterator i = prefixToURI.entrySet().iterator();
                while (i.hasNext()) {
                    Map.Entry entry = (Map.Entry) i.next();
                    ns = new Namespace(lib, (String) entry.getKey(), (String) entry.getValue());
                    inScopeNSBag.add(ns);
                }
                // Add the URI we are looking for to avoid matching
                // non-existing Namespaces.
                ns = new Namespace(lib, nsURI);
                inScopeNSBag.add(ns);
                Object[] inScopeNS = inScopeNSBag.toArray();
                // Check the element name
                Namespace n = NamespaceHelper.getNamespace(lib, cursor, inScopeNS);
                if (nsURI.equals(n.uri()) && (nsPrefix == null || nsPrefix.equals(n.prefix()))) {
                    // This namespace is used
                    return this;
                }
                // Check the attributes
                cursor.push();
                boolean hasNext = cursor.toFirstAttribute();
                while (hasNext) {
                    n = NamespaceHelper.getNamespace(lib, cursor, inScopeNS);
                    if (nsURI.equals(n.uri()) && (nsPrefix == null || nsPrefix.equals(n.prefix()))) {
                        // This namespace is used
                        return this;
                    }
                    hasNext = cursor.toNextAttribute();
                }
                cursor.pop();
                if (nsPrefix == null) {
                    // Remove all namespaces declarations that match nsURI
                    i = prefixToURI.entrySet().iterator();
                    while (i.hasNext()) {
                        Map.Entry entry = (Map.Entry) i.next();
                        if (entry.getValue().equals(nsURI))
                            NamespaceHelper.removeNamespace(cursor, (String) entry.getKey());
                    }
                } else if (nsURI.equals(prefixToURI.get(nsPrefix))) {
                    // Remove the namespace declaration that matches nsPrefix
                    NamespaceHelper.removeNamespace(cursor, String.valueOf(nsPrefix));
                }
            }
            switch(cursor.toNextToken().intValue()) {
                case XmlCursor.TokenType.INT_START:
                    depth++;
                    break;
                case XmlCursor.TokenType.INT_END:
                    depth--;
                    break;
            }
        }
    } finally {
        cursor.dispose();
    }
    return this;
}
Also used : XmlCursor(org.apache.xmlbeans.XmlCursor) XmlObject(org.apache.xmlbeans.XmlObject)

Example 99 with XmlCursor

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

the class XML method remove.

/**
     *
     */
void remove() {
    XmlCursor childCurs = newCursor();
    if (childCurs.currentTokenType().isStartdoc()) {
        // Remove on the document removes all children.
        TokenType tt = childCurs.toFirstContentToken();
        while (!tt.isEnd() && !tt.isEnddoc()) {
            removeToken(childCurs);
            // Now see where we're pointing after the delete -- next token.
            tt = childCurs.currentTokenType();
        }
    } else {
        removeToken(childCurs);
    }
    childCurs.dispose();
}
Also used : TokenType(org.apache.xmlbeans.XmlCursor.TokenType) XmlCursor(org.apache.xmlbeans.XmlCursor)

Example 100 with XmlCursor

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

the class XML method matchAttributes.

/**
     *
     * @param name
     * @return
     */
private XMLList matchAttributes(XMLName xmlName) {
    XMLList result = new XMLList(lib);
    XmlCursor curs = newCursor();
    if (curs.currentTokenType().isStartdoc()) {
        curs.toFirstContentToken();
    }
    if (curs.isStart()) {
        if (curs.toFirstAttribute()) {
            do {
                if (qnameMatches(xmlName, curs.getName())) {
                    result.addToList(createAttributeObject(curs));
                }
            } while (curs.toNextAttribute());
        }
    }
    curs.dispose();
    return result;
}
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