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

The following examples show how to use org.apache.xmlbeans.XmlCursor#currentTokenType() . 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: XML.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 *
 * @return
 */
XmlCursor.TokenType tokenType()
{
    XmlCursor.TokenType result;

    XmlCursor curs = newCursor();

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

    result = curs.currentTokenType();

    curs.dispose();

    return result;
}
 
Example 2
Source File: XML.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 *
 */
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);
            tt = childCurs.currentTokenType();      // Now see where we're pointing after the delete -- next token.
        }
    }
    else
    {
            removeToken(childCurs);
    }

    childCurs.dispose();
}
 
Example 3
Source File: XML.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 *
 * @param curs
 * @return
 */
private static TokenType skipNonElements (XmlCursor curs)
{
    TokenType tt = curs.currentTokenType();
    while (tt.isComment() || tt.isProcinst())
    {
        tt = curs.toNextToken();
    }

    return tt;
}
 
Example 4
Source File: XML.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 *
 * @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;
}
 
Example 5
Source File: XML.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
protected void changeNS (String oldURI, String newURI)
{
    XmlCursor curs = newCursor();
    while (curs.toParent()) {
      /* Goto the top of the document */
    }

    TokenType tt = curs.currentTokenType();
    if (tt.isStartdoc())
    {
        tt = curs.toFirstContentToken();
    }

    if (tt.isStart())
    {
        do
        {
            if (tt.isStart() || tt.isAttr() || tt.isNamespace())
            {
                javax.xml.namespace.QName currQName = curs.getName();
                if (oldURI.equals(currQName.getNamespaceURI()))
                {
                    curs.setName(new javax.xml.namespace.QName(newURI, currQName.getLocalPart()));
                }
            }

            tt = curs.toNextToken();
        } while (!tt.isEnddoc() && !tt.isNone());
    }

    curs.dispose();
}
 
Example 6
Source File: XML.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
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)
        {
            // Drill into where first child would be.
//            curs.toFirstContentToken();
            return true;
        }

        return false;
    }
 
Example 7
Source File: XML.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 *
 * @param cursToCopy
 * @return
 */
private XmlCursor copy (XmlCursor cursToCopy)
{
    XmlObject xo = XmlObject.Factory.newInstance();

    XmlCursor copyCurs = null;

    if (cursToCopy.currentTokenType().isText())
    {
        try
        {
            // Try just as a textnode, to do that we need to wrap the text in a special fragment tag
            // that is not visible from the XmlCursor.
            copyCurs = XmlObject.Factory.parse("<x:fragment xmlns:x=\"http://www.openuri.org/fragment\">" +
                                       cursToCopy.getChars() +
                                       "</x:fragment>").newCursor();
            if (!cursToCopy.toNextSibling())
            {
                if (cursToCopy.currentTokenType().isText())
                {
                    cursToCopy.toNextToken();   // It's not an element it's text so skip it.
                }
            }
        }
        catch (Exception ex)
        {
            throw ScriptRuntime.typeError(ex.getMessage());
        }
    }
    else
    {
        copyCurs = xo.newCursor();
        copyCurs.toFirstContentToken();
        if (cursToCopy.currentTokenType() == XmlCursor.TokenType.STARTDOC)
        {
            cursToCopy.toNextToken();
        }

        cursToCopy.copyXml(copyCurs);
        if (!cursToCopy.toNextSibling())        // If element skip element.
        {
            if (cursToCopy.currentTokenType().isText())
            {
                cursToCopy.toNextToken();       // It's not an element it's text so skip it.
            }
        }

    }

    copyCurs.toStartDoc();
    copyCurs.toFirstContentToken();

    return copyCurs;
}
 
Example 8
Source File: XML.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 *
 * @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();
}
 
Example 9
Source File: XML.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 *
 * @param namespace
 * @return
 */
private XMLList allChildNodes(String namespace)
{
    XMLList result = new XMLList(lib);
    XmlCursor curs = newCursor();
    TokenType tt = curs.currentTokenType();
    javax.xml.namespace.QName targetProperty = new javax.xml.namespace.QName(namespace, "*");

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

    if (tt.isContainer())
    {
        tt = curs.toFirstContentToken();

        while (!tt.isEnd())
        {
            if (!tt.isStart())
            {
                // Not an element
                result.addToList(findAnnotation(curs));

                // Reset target property to null in this case
                targetProperty = null;
            }
            else
            {
                // Match namespace as well if specified
                if (namespace == null ||
                    namespace.length() == 0 ||
                    namespace.equals("*") ||
                    curs.getName().getNamespaceURI().equals(namespace))
                {
                    // Add it to the list
                    result.addToList(findAnnotation(curs));

                    // Set target property if target name is "*",
                    // Otherwise if target property does not match current, then
                    // 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();

    // Set the targets for this XMLList.
    result.setTargets(this, targetProperty);

    return result;
}
 
Example 10
Source File: XML.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 *
 * @return
 */
private XMLList matchDescendantAttributes(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();

            // Only try to match names for attributes
            if (tt.isAttr())
            {
                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;
}
 
Example 11
Source File: XML.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 *
 * @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;
}
 
Example 12
Source File: XML.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 *
 * @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 target property if target name is "*",
                        // Otherwise if target property does not match current, then
                        // 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;

}
 
Example 13
Source File: XML.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 *
 * @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;
}
 
Example 14
Source File: XML.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 *
 */
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();
}
 
Example 15
Source File: LogicalEquality.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public static boolean nodesEqual(XmlCursor xmlOne, XmlCursor xmlTwo)
{
    boolean result = false;

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

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

    if (xmlOne.currentTokenType() == xmlTwo.currentTokenType())
    {
        if (xmlOne.isEnddoc())
        {
            // Both empty
            result = true;
        }
        else if (xmlOne.isAttr())
        {
            result = attributesEqual(xmlOne, xmlTwo);
        }
        else if (xmlOne.isText())
        {
            result = textNodesEqual(xmlOne, xmlTwo);
        }
        else if (xmlOne.isComment())
        {
            result = commentsEqual(xmlOne, xmlTwo);
        }
        else if (xmlOne.isProcinst())
        {
            result = processingInstructionsEqual(xmlOne, xmlTwo);
        }
        else if (xmlOne.isStart())
        {
            // Compare root elements
            result = elementsEqual(xmlOne, xmlTwo);
        }
    }

    return result;
}
 
Example 16
Source File: LogicalEquality.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
private static boolean elementsEqual(XmlCursor xmlOne, XmlCursor xmlTwo)
{
    boolean result = true;

    if (!qnamesEqual(xmlOne.getName(), xmlTwo.getName()))
    {
        result = false;
    }
    else
    {
        // These filter out empty text nodes.
        nextToken(xmlOne);
        nextToken(xmlTwo);

        do
        {
            if (xmlOne.currentTokenType() != xmlTwo.currentTokenType())
            {
                // Not same token
                result = false;
                break;
            }
            else if (xmlOne.isEnd())
            {
                // Done with this element, step over end
                break;
            }
            else if (xmlOne.isEnddoc())
            {
                // Shouldn't get here
                break;
            }
            else if (xmlOne.isAttr())
            {
                // This one will move us to the first non-attr token.
                result = attributeListsEqual(xmlOne, xmlTwo);
            }
            else
            {
                if (xmlOne.isText())
                {
                    result = textNodesEqual(xmlOne, xmlTwo);
                }
                else if (xmlOne.isComment())
                {
                    result = commentsEqual(xmlOne, xmlTwo);
                }
                else if (xmlOne.isProcinst())
                {
                    result = processingInstructionsEqual(xmlOne, xmlTwo);
                }
                else if (xmlOne.isStart())
                {
                    result = elementsEqual(xmlOne, xmlTwo);
                }
                else
                {
                    //XML.log("Unknown token type" + xmlOne.currentTokenType());
                }

                // These filter out empty text nodes.
                nextToken(xmlOne);
                nextToken(xmlTwo);
            }
        }
        while(result);
    }

    return result;
}