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

The following examples show how to use org.apache.xmlbeans.XmlCursor#toPrevToken() . 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 5 votes vote down vote up
/**
 *
 * @param destCurs
 * @param newValue
 */
private void replace(XmlCursor destCurs, XML newValue)
{
    if (destCurs.isStartdoc())
    {
        // Can't overwrite a whole document (user really wants to overwrite the contents of).
        destCurs.toFirstContentToken();
    }

    // Orphan the token -- don't delete it outright on the XmlCursor.
    removeToken(destCurs);

    XmlCursor srcCurs = newValue.newCursor();
    if (srcCurs.currentTokenType().isStartdoc())
    {
        // Cann't append a whole document (user really wants to append the contents of).
        srcCurs.toFirstContentToken();
    }

    moveSrcToDest(srcCurs, destCurs, false);

    // Re-link a new annotation to this cursor -- we just deleted the previous annotation on entrance to replace.
    if (!destCurs.toPrevSibling())
    {
        destCurs.toPrevToken();
    }
    destCurs.setBookmark(new XScriptAnnotation(destCurs));

    // todo would be nice if destCurs.toNextSibling went to where the next token if the cursor was pointing at the last token in the stream.
    destCurs.toEndToken();
    destCurs.toNextToken();

    srcCurs.dispose();
}
 
Example 2
Source File: XML.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 *
 * @param curs
 * @param xmlToInsert
 */
private void insertChild(XmlCursor curs, Object xmlToInsert)
{
    if (xmlToInsert == null || xmlToInsert instanceof Undefined)
    {
        // Do nothing
    }
    else if (xmlToInsert instanceof XmlCursor)
    {
        moveSrcToDest((XmlCursor)xmlToInsert, curs, true);
    }
    else if (xmlToInsert instanceof XML)
    {
        XML xmlValue = (XML) xmlToInsert;

        // If it's an attribute, then change to text node
        if (xmlValue.tokenType() == XmlCursor.TokenType.ATTR)
        {
            insertChild(curs, xmlValue.toString());
        }
        else
        {
            XmlCursor cursToInsert = ((XML) xmlToInsert).newCursor();

            moveSrcToDest(cursToInsert, curs, true);

            cursToInsert.dispose();
        }
    }
    else if (xmlToInsert instanceof XMLList)
    {
        XMLList list = (XMLList) xmlToInsert;

        for (int i = 0; i < list.length(); i++)
        {
            insertChild(curs, list.item(i));
        }
    }
    else
    {
        // Convert to string and make XML out of it
        String  xmlStr = ScriptRuntime.toString(xmlToInsert);
        XmlObject xo = XmlObject.Factory.newInstance();         // Create an empty document.

        XmlCursor sourceCurs = xo.newCursor();
        sourceCurs.toNextToken();

        // To hold the text.
        sourceCurs.insertChars(xmlStr);

        sourceCurs.toPrevToken();

        // Call us again with the cursor.
        moveSrcToDest(sourceCurs, curs, true);
    }
}
 
Example 3
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 4
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();
}