Java Code Examples for org.apache.xmlbeans.XmlCursor#TokenType

The following examples show how to use org.apache.xmlbeans.XmlCursor#TokenType . 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 5 votes vote down vote up
/**
 *
 * @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;
}
 
Example 3
Source File: XML.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 *
 * @param currXMLNode
 * @param xmlValue
 * @return
 */
private boolean doPut(XMLName name, XML currXMLNode, XMLObjectImpl xmlValue)
{
    boolean result = false;
    XmlCursor curs = currXMLNode.newCursor();

    try
    {
        // Replace the node with this new xml value.
        XML xml;

        int toAssignLen = xmlValue.length();

        for (int i = 0; i < toAssignLen; i++)
        {
            if (xmlValue instanceof XMLList)
            {
                xml = ((XMLList) xmlValue).item(i);
            }
            else
            {
                xml = (XML) xmlValue;
            }

            // If it's an attribute or text node, make text node.
            XmlCursor.TokenType tt = xml.tokenType();
            if (tt == XmlCursor.TokenType.ATTR || tt == XmlCursor.TokenType.TEXT)
            {
                xml = makeXmlFromString(lib, name, xml.toString());
            }

            if (i == 0)
            {
                // 1st assignment is replaceChild all others are appendChild
                replace(curs, xml);
            }
            else
            {
                insertChild(curs, xml);
            }
        }

        // We're done we've blown away the node because the rvalue was XML...
        result = true;
    }
    catch (Exception ex)
    {
        ex.printStackTrace();
        throw ScriptRuntime.typeError(ex.getMessage());
    }
    finally
    {
        curs.dispose();
    }

    return result;
}
 
Example 4
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 5
Source File: XML.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
     *
     * @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;
    }
 
Example 6
Source File: XML.java    From astor with GNU General Public License v2.0 2 votes vote down vote up
/**
 *
 * @param tokenType
 * @return
 */
private XMLList matchChildren(XmlCursor.TokenType tokenType)
{
    return matchChildren(tokenType, XMLName.formStar());
}