Search in sources :

Example 6 with TokenType

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

the class XML method moveSrcToDest.

/**
     *
     * @param srcCurs
     * @param destCurs
     * @param fDontMoveIfSame
     * @return
     */
private boolean moveSrcToDest(XmlCursor srcCurs, XmlCursor destCurs, boolean fDontMoveIfSame) {
    boolean fMovedSomething = true;
    TokenType tt;
    do {
        if (fDontMoveIfSame && srcCurs.isInSameDocument(destCurs) && (srcCurs.comparePosition(destCurs) == 0)) {
            // If the source and destination are pointing at the same place then there's nothing to move.
            fMovedSomething = false;
            break;
        }
        // todo ***TLL*** Use replaceContents (when added) and eliminate children removes (see above todo).
        if (destCurs.currentTokenType().isStartdoc()) {
            destCurs.toNextToken();
        }
        // todo ***TLL*** Can Eric support notion of copy instead of me copying then moving???
        XmlCursor copyCurs = copy(srcCurs);
        copyCurs.moveXml(destCurs);
        copyCurs.dispose();
        tt = srcCurs.currentTokenType();
    } while (!tt.isStart() && !tt.isEnd() && !tt.isEnddoc());
    return fMovedSomething;
}
Also used : TokenType(org.apache.xmlbeans.XmlCursor.TokenType) XmlCursor(org.apache.xmlbeans.XmlCursor)

Example 7 with TokenType

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

the class XML method childIndex.

/**
     *
     * @return
     */
int childIndex() {
    int index = 0;
    XmlCursor curs = newCursor();
    TokenType tt = curs.currentTokenType();
    while (true) {
        if (tt.isText()) {
            index++;
            if (!curs.toPrevSibling()) {
                break;
            }
        } else if (tt.isStart()) {
            tt = curs.toPrevToken();
            if (tt.isEnd()) {
                curs.toNextToken();
                if (!curs.toPrevSibling()) {
                    break;
                }
                index++;
            } else {
                // Hit the parent start tag so get out we're down counting children.
                break;
            }
        } else if (tt.isComment() || tt.isProcinst()) {
            curs.toPrevToken();
        } else {
            break;
        }
        tt = curs.currentTokenType();
    }
    index = curs.currentTokenType().isStartdoc() ? -1 : index;
    curs.dispose();
    return index;
}
Also used : TokenType(org.apache.xmlbeans.XmlCursor.TokenType) XmlCursor(org.apache.xmlbeans.XmlCursor)

Example 8 with TokenType

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

the class XML method nodeKind.

/**
     *
     * @return
     */
Object nodeKind() {
    String result;
    XmlCursor.TokenType tt = tokenType();
    if (tt == XmlCursor.TokenType.ATTR) {
        result = "attribute";
    } else if (tt == XmlCursor.TokenType.TEXT) {
        result = "text";
    } else if (tt == XmlCursor.TokenType.COMMENT) {
        result = "comment";
    } else if (tt == XmlCursor.TokenType.PROCINST) {
        result = "processing-instruction";
    } else if (tt == XmlCursor.TokenType.START) {
        result = "element";
    } else {
        // A non-existant node has the nodeKind() of text
        result = "text";
    }
    return result;
}
Also used : TokenType(org.apache.xmlbeans.XmlCursor.TokenType) XmlCursor(org.apache.xmlbeans.XmlCursor)

Example 9 with TokenType

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

the class XML method insertChild.

/**
     *
     * @param childToMatch
     * @param xmlToInsert
     * @param addToType
     */
private void insertChild(XML childToMatch, Object xmlToInsert, int addToType) {
    XmlCursor curs = newCursor();
    TokenType tt = curs.currentTokenType();
    XmlCursor xmlChildCursor = childToMatch.newCursor();
    if (tt.isStartdoc()) {
        tt = curs.toFirstContentToken();
    }
    if (tt.isContainer()) {
        tt = curs.toNextToken();
        while (!tt.isEnd()) {
            if (tt.isStart()) {
                // See if this child is the same as the one thep passed in
                if (curs.comparePosition(xmlChildCursor) == 0) {
                    // Found it
                    if (addToType == APPEND_CHILD) {
                        // Move the cursor to just past the end of this element
                        curs.toEndToken();
                        curs.toNextToken();
                    }
                    insertChild(curs, xmlToInsert);
                    break;
                }
            }
            // Skip over child elements
            if (tt.isStart()) {
                tt = curs.toEndToken();
            }
            tt = curs.toNextToken();
        }
    }
    xmlChildCursor.dispose();
    curs.dispose();
}
Also used : TokenType(org.apache.xmlbeans.XmlCursor.TokenType) XmlCursor(org.apache.xmlbeans.XmlCursor)

Example 10 with TokenType

use of org.apache.xmlbeans.XmlCursor.TokenType 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)

Aggregations

TokenType (org.apache.xmlbeans.XmlCursor.TokenType)14 XmlCursor (org.apache.xmlbeans.XmlCursor)13