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

The following examples show how to use org.apache.xmlbeans.XmlCursor#isProcinst() . 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
 */
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 2
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 3
Source File: LogicalEquality.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 *
 * @param xmlOne
 * @param xmlTwo
 * @return
 */
private static boolean processingInstructionsEqual(XmlCursor xmlOne, XmlCursor xmlTwo)
{
    boolean result = false;

    if (xmlOne.isProcinst() && xmlTwo.isProcinst())
    {
        if (qnamesEqual(xmlOne.getName(), xmlTwo.getName()))
        {
            if (xmlOne.getTextValue().equals(xmlTwo.getTextValue()))
            {
                result = true;
            }
        }
    }

    return result;
}
 
Example 4
Source File: XML.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 *
 * @param name
 */
void setName(QName qname)
{
    XmlCursor cursor = newCursor();

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

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

        if(cursor.isProcinst())
        {
            String localName = qname.localName();
            cursor.setName(new javax.xml.namespace.QName(localName));
        }
        else
        {
            String prefix = qname.prefix();
            if (prefix == null) { prefix = ""; }
            cursor.setName(new javax.xml.namespace.QName(
                qname.uri(), qname.localName(), prefix));
        }
    }
    finally
    {
        cursor.dispose();
    }
}
 
Example 5
Source File: XML.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 *
 * @return
 */
String toXMLString(int indent)
{
    // XXX indent is ignored

    String result;

    XmlCursor curs = newCursor();

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

    try
    {
        if (curs.isText())
        {
            result = curs.getChars();
        }
        else if (curs.isAttr())
        {
            result = curs.getTextValue();
        }
        else if (curs.isComment() || curs.isProcinst())
        {
            result = XML.dumpNode(curs, getOptions());

            // todo: XBeans-dependent hack here
            // If it's a comment or PI, take off the xml-frament stuff
            String start = "<xml-fragment>";
            String end = "</xml-fragment>";

            if (result.startsWith(start))
            {
                result = result.substring(start.length());
            }

            if (result.endsWith(end))
            {
                result = result.substring(0, result.length() - end.length());
            }
        }
        else
        {
            result = XML.dumpNode(curs, getOptions());
        }
    }
    finally
    {
        curs.dispose();
    }

    return result;
}
 
Example 6
Source File: NamespaceHelper.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
static Namespace getNamespace(XMLLibImpl lib, XmlCursor cursor,
                              Object[] inScopeNamespaces)
{
    String uri;
    String prefix;

    if (cursor.isProcinst()) {
        uri = "";
        prefix = "";
    } else {
        javax.xml.namespace.QName qname = cursor.getName();
        uri = qname.getNamespaceURI();
        prefix = qname.getPrefix();
    }

    if (inScopeNamespaces == null)
        return new Namespace(lib, prefix, uri);

    Namespace result = null;
    for (int i = 0; i != inScopeNamespaces.length; ++i) {
        Namespace ns = (Namespace)inScopeNamespaces[i];
        if(ns == null) continue;

        String nsURI = ns.uri();
        if(nsURI.equals(uri))
        {
            if(prefix.equals(ns.prefix()))
            {
                result = ns;
                break;
            }

            if(result == null ||
               (result.prefix() == null &&
                ns.prefix() != null))
                result = ns;
        }
    }

    if(result == null)
        result = new Namespace(lib, prefix, uri);

    return result;
}
 
Example 7
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 8
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;
}