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

The following examples show how to use org.apache.xmlbeans.XmlCursor#toFirstContentToken() . 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
 */
boolean hasSimpleContent()
{
    boolean simpleContent = false;

    XmlCursor curs = newCursor();

    if (curs.isAttr() || curs.isText()) {
        return true;
    }

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

    simpleContent = !(curs.toFirstChild());

    curs.dispose();

    return simpleContent;
}
 
Example 2
Source File: XML.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 *
 * @return
 */
Object copy()
{
    XmlCursor srcCurs = newCursor();

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

    XML xml = createEmptyXML(lib);

    XmlCursor destCurs = xml.newCursor();
    destCurs.toFirstContentToken();

    srcCurs.copyXml(destCurs);

    destCurs.dispose();
    srcCurs.dispose();

    return xml;
}
 
Example 3
Source File: XML.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 *
 * @param xml
 * @return
 */
XML appendChild(Object xml)
{
    XmlCursor curs = newCursor();

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

    // Move the cursor to the end of this element
    if (curs.isStart())
    {
        curs.toEndToken();
    }

    insertChild(curs, xml);

    curs.dispose();

    return this;
}
 
Example 4
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 5
Source File: XML.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 *
 * @return
 */
String localName()
{
    XmlCursor cursor = newCursor();
    if (cursor.isStartdoc())
        cursor.toFirstContentToken();

    String name = null;

    if(cursor.isStart() ||
       cursor.isAttr() ||
       cursor.isProcinst())
    {
        javax.xml.namespace.QName qname = cursor.getName();
        name = qname.getLocalPart();
    }
    cursor.dispose();

    return name;
}
 
Example 6
Source File: XML.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 *
 * @param ns
 */
void setNamespace(Namespace ns)
{
    XmlCursor cursor = newCursor();

    try
    {
        if(cursor.isStartdoc())
            cursor.toFirstContentToken();

        if(cursor.isText() ||
           cursor.isComment() ||
           cursor.isProcinst()) return;

        String prefix = ns.prefix();
        if (prefix == null) {
            prefix = "";
        }
        cursor.setName(new javax.xml.namespace.QName(
            ns.uri(), localName(), prefix));
    }
    finally
    {
        cursor.dispose();
    }
}
 
Example 7
Source File: XML.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 *
 * @param xml
 * @return
 */
XML prependChild (Object xml)
{
    XmlCursor curs = newCursor();

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

    // Move the cursor to the first content token
    curs.toFirstContentToken();

    insertChild(curs, xml);

    curs.dispose();

    return this;
}
 
Example 8
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 9
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 10
Source File: SweCommonDecoderV20.java    From arctic-sea with Apache License 2.0 5 votes vote down vote up
private List<List<String>> parseValues(final SweCount elementCount, final SweAbstractDataComponent elementType,
        final SweAbstractEncoding encoding, final EncodedValuesPropertyType encodedValuesPropertyType)
        throws DecodingException {
    if (checkParameterTypes(elementType, encoding)) {
        // Get swe values String via cursor as String
        String values;
        // TODO replace XmlCursor
        /*
         * if (encodedValuesPropertyType.schemaType() == XmlString.type) {
         * XmlString xbString
         */
        // @see SosDecoderv20#parseResultValues
        XmlCursor xbCursor = encodedValuesPropertyType.newCursor();
        xbCursor.toFirstContentToken();
        if (xbCursor.isText()) {
            values = xbCursor.getTextValue().trim();
            xbCursor.dispose();
            if (values != null && !values.isEmpty()) {
                SweTextEncoding textEncoding = (SweTextEncoding) encoding;

                String[] blocks = values.split(textEncoding.getBlockSeparator());
                List<List<String>> resultValues = new ArrayList<>(blocks.length);
                for (String block : blocks) {
                    String[] tokens = block.split(textEncoding.getTokenSeparator());
                    List<String> tokenList = Arrays.asList(tokens);
                    resultValues.add(tokenList);
                }
                return resultValues;
            }
        }
    }
    return null;
}
 
Example 11
Source File: XML.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 *
 * @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;
}
 
Example 12
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 13
Source File: XML.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 *
 * @param curs
 */
protected void removeToken (XmlCursor curs)
{
    XmlObject xo = XmlObject.Factory.newInstance();

    // Don't delete anything move to another document so it gets orphaned nicely.
    XmlCursor tmpCurs = xo.newCursor();
    tmpCurs.toFirstContentToken();


    curs.moveXml(tmpCurs);

    tmpCurs.dispose();
}
 
Example 14
Source File: XML.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 *
 * @param prefix
 * @return
 */
Object namespace(String prefix)
{
    XmlCursor cursor = newCursor();
    if (cursor.isStartdoc())
    {
        cursor.toFirstContentToken();
    }

    Object result = null;

    if (prefix == null)
    {
        if(cursor.isStart() ||
           cursor.isAttr())
        {
            Object[] inScopeNS = NamespaceHelper.inScopeNamespaces(lib, cursor);
            // XXX Is it reaaly necessary to create the second cursor?
            XmlCursor cursor2 = newCursor();
            if (cursor2.isStartdoc())
                cursor2.toFirstContentToken();

            result = NamespaceHelper.getNamespace(lib, cursor2, inScopeNS);

            cursor2.dispose();
        }
    }
    else
    {
        Map prefixToURI = NamespaceHelper.getAllNamespaces(lib, cursor);
        String uri = (String)prefixToURI.get(prefix);
        result = (uri == null) ? Undefined.instance : new Namespace(lib, prefix, uri);
    }

    cursor.dispose();

    return result;
}
 
Example 15
Source File: XML.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private static XML createXML (XMLLibImpl lib, XmlCursor curs)
{
    if (curs.currentTokenType().isStartdoc())
    {
        curs.toFirstContentToken();
    }

    XScriptAnnotation anno = findAnnotation(curs);

    return new XML(lib, anno);
}
 
Example 16
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 17
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 18
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 19
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 20
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;
    }