Search in sources :

Example 1 with TokenType

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

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

the class XML method normalize.

/**
     *
     */
void normalize() {
    XmlCursor curs = newCursor();
    TokenType tt = curs.currentTokenType();
    // Walk through the tokens removing empty text nodes and merging adjacent text nodes.
    if (tt.isStartdoc()) {
        tt = curs.toFirstContentToken();
    }
    if (tt.isContainer()) {
        int nestLevel = 1;
        String previousText = null;
        while (nestLevel > 0) {
            tt = curs.toNextToken();
            if (tt == XmlCursor.TokenType.TEXT) {
                String currentText = curs.getChars().trim();
                if (currentText.trim().length() == 0) {
                    // Empty text node, remove.
                    removeToken(curs);
                    curs.toPrevToken();
                } else if (previousText == null) {
                    // No previous text node, reset to trimmed version
                    previousText = currentText;
                } else {
                    // It appears that this case never happens with XBeans.
                    // Previous text node exists, concatenate
                    String newText = previousText + currentText;
                    curs.toPrevToken();
                    removeToken(curs);
                    removeToken(curs);
                    curs.insertChars(newText);
                }
            } else {
                previousText = null;
            }
            if (tt.isStart()) {
                nestLevel++;
            } else if (tt.isEnd()) {
                nestLevel--;
            } else if (tt.isEnddoc()) {
                // Shouldn't get here, but just in case.
                break;
            }
        }
    }
    curs.dispose();
}
Also used : TokenType(org.apache.xmlbeans.XmlCursor.TokenType) XmlCursor(org.apache.xmlbeans.XmlCursor)

Example 3 with TokenType

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

the class XML method equivalentXml.

//
// Other public Functions from XMLObject
//
/**
     *
     * @param target
     * @return
     */
boolean equivalentXml(Object target) {
    boolean result = false;
    if (target instanceof XML) {
        XML otherXml = (XML) target;
        // Compare with toString() if either side is text node or attribute
        // otherwise compare as XML
        XmlCursor.TokenType thisTT = tokenType();
        XmlCursor.TokenType otherTT = otherXml.tokenType();
        if (thisTT == XmlCursor.TokenType.ATTR || otherTT == XmlCursor.TokenType.ATTR || thisTT == XmlCursor.TokenType.TEXT || otherTT == XmlCursor.TokenType.TEXT) {
            result = toString().equals(otherXml.toString());
        } else {
            XmlCursor cursOne = newCursor();
            XmlCursor cursTwo = otherXml.newCursor();
            result = LogicalEquality.nodesEqual(cursOne, cursTwo);
            cursOne.dispose();
            cursTwo.dispose();
        // Old way of comparing by string.
        //                boolean orgPrettyPrinting = prototype.prettyPrinting;
        //                prototype.prettyPrinting = true;
        //                result = toXMLString(0).equals(otherXml.toXMLString(0));
        //                prototype.prettyPrinting = orgPrettyPrinting;
        }
    } else if (target instanceof XMLList) {
        XMLList otherList = (XMLList) target;
        if (otherList.length() == 1) {
            result = equivalentXml(otherList.getXmlFromAnnotation(0));
        }
    } else if (hasSimpleContent()) {
        String otherStr = ScriptRuntime.toString(target);
        result = toString().equals(otherStr);
    }
    return result;
}
Also used : TokenType(org.apache.xmlbeans.XmlCursor.TokenType) XmlCursor(org.apache.xmlbeans.XmlCursor)

Example 4 with TokenType

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

the class XML method moveToChild.

/*
     * fUseStartDoc used by child(int index) the index is at startDoc is the element at the top-level
     *              otherwise we always want to drill in.
     */
private boolean moveToChild(XmlCursor curs, long index, boolean fFirstChild, boolean fUseStartDoc) {
    if (index < 0)
        throw new IllegalArgumentException();
    long idxChild = 0;
    if (!fUseStartDoc && curs.currentTokenType().isStartdoc()) {
        // We always move to the children of the top node.
        // todo:  This assumes that we want have multiple top-level nodes.  Which we should be able tohave.
        curs.toFirstContentToken();
    }
    TokenType tt = curs.toFirstContentToken();
    if (!tt.isNone() && !tt.isEnd()) {
        while (true) {
            if (index == idxChild) {
                return true;
            }
            tt = curs.currentTokenType();
            if (tt.isText()) {
                curs.toNextToken();
            } else if (tt.isStart()) {
                // Need to do this we want to be pointing at the text if that after the end token.
                curs.toEndToken();
                curs.toNextToken();
            } else if (tt.isComment() || tt.isProcinst()) {
                continue;
            } else {
                break;
            }
            idxChild++;
        }
    } else if (fFirstChild && index == 0) {
        //            curs.toFirstContentToken();
        return true;
    }
    return false;
}
Also used : TokenType(org.apache.xmlbeans.XmlCursor.TokenType)

Example 5 with TokenType

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

the class XML method tokenType.

/**
     *
     * @return
     */
XmlCursor.TokenType tokenType() {
    XmlCursor.TokenType result;
    XmlCursor curs = newCursor();
    if (curs.isStartdoc()) {
        curs.toFirstContentToken();
    }
    result = curs.currentTokenType();
    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