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

The following examples show how to use org.apache.xmlbeans.XmlCursor#getName() . 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: N52XmlHelper.java    From arctic-sea with Apache License 2.0 6 votes vote down vote up
public static Set<String> getNamespaces(XmlObject xmlObject) {
    Set<String> namespaces = Sets.newHashSet();
    XmlCursor newCursor = xmlObject.newCursor();
    while (newCursor.hasNextToken()) {
        TokenType evt = newCursor.toNextToken();
        if (evt == TokenType.START) {
            QName qn = newCursor.getName();
            if (qn != null) {
                namespaces.add(qn.getNamespaceURI());
            }
        } else if (evt == TokenType.NAMESPACE) {
            namespaces.add(newCursor.getName().getNamespaceURI());
        }
    }
    return namespaces;
}
 
Example 2
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 3
Source File: XML.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 *
 * @param name
 */
void setLocalName(String localName)
{
    XmlCursor cursor = newCursor();

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

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


        javax.xml.namespace.QName qname = cursor.getName();
        cursor.setName(new javax.xml.namespace.QName(
            qname.getNamespaceURI(), localName, qname.getPrefix()));
    }
    finally
    {
        cursor.dispose();
    }
}
 
Example 4
Source File: NamespaceHelper.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public static void getNamespaces(XmlCursor cursor, Map prefixToURI)
{
    cursor.push();
    while(cursor.toNextToken().isAnyAttr())
    {
        if(cursor.isNamespace())
        {
            javax.xml.namespace.QName name = cursor.getName();
            String prefix = name.getLocalPart();
            String uri = name.getNamespaceURI();

            prefixToURI.put(prefix, uri);
        }
    }
    cursor.pop();
}
 
Example 5
Source File: NamespaceHelper.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public static void removeNamespace(XmlCursor cursor, String prefix)
{
    cursor.push();
    while(cursor.toNextToken().isAnyAttr())
    {
        if(cursor.isNamespace())
        {
            javax.xml.namespace.QName name = cursor.getName();
            if(name.getLocalPart().equals(prefix))
            {
                cursor.removeXml();
                break;
            }
        }
    }
    cursor.pop();
}
 
Example 6
Source File: XmlBeanAssert.java    From mdw with Apache License 2.0 5 votes vote down vote up
/**
 * Uses cursors to compare two XML documents, ignoring whitespace and
 * ordering of attributes.  Fails the JUnit test if they're different.
 *
 * @param message to display on test failure (may be null)
 * @param expected
 * @param actual
 */
public static void assertEquals(String message, XmlCursor expected, XmlCursor actual)
{
  for (int child = 0; true; child++)
  {
    boolean child1 = expected.toChild(child);
    boolean child2 = actual.toChild(child);
    if (child1 != child2)
    {
      fail(message, "Different XML structure near " + QNameHelper.pretty(expected.getName()));
    }
    else if (expected.getName() != null && !expected.getName().equals(actual.getName()))
    {
      fail(message, "Expected element: '" + expected.getName()
        + "' differs from actual element: '" + actual.getName() + "'");
    }
    else if (child == 0 && !child1)
    {
      if (!(expected.getTextValue().equals(actual.getTextValue())))
      {
        fail(message, "Expected value for element " + QNameHelper.pretty(expected.getName()) + " -> '"
          + expected.getTextValue() + "' differs from actual value '" + actual.getTextValue() + "'");
      }
      break;
    }
    else if (child1)
    {
      assertEquals(message, expected, actual);
      expected.toParent();
      actual.toParent();
    }
    else
    {
      break;
    }
  }

  assertAttributesEqual(message, expected, actual);
}
 
Example 7
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 8
Source File: NamespaceHelper.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Updates the internal state of this NamespaceHelper to reflect the
 * existance of the XML token pointed to by the cursor.
 */
private void processName(XmlCursor cursor, ObjArray declarations)
{
    javax.xml.namespace.QName qname = cursor.getName();
    String uri = qname.getNamespaceURI();
    Set prefixes = (Set)uriToPrefix.get(uri);
    if(prefixes == null || prefixes.size() == 0)
    {
        undeclared.add(uri);
        if(declarations != null)
            declarations.add(new Namespace(lib, uri));
    }
}
 
Example 9
Source File: NamespaceHelper.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Updates the internal state of this NamespaceHelper with the
 * namespace information of the element pointed to by the cursor.
 */
private void update(XmlCursor cursor, ObjArray declarations)
{
    // Process the Namespace declarations
    cursor.push();
    while(cursor.toNextToken().isAnyAttr())
    {
        if(cursor.isNamespace())
        {
            javax.xml.namespace.QName name = cursor.getName();
            String prefix = name.getLocalPart();
            String uri = name.getNamespaceURI();

            declareNamespace(prefix, uri, declarations);
        }
    }
    cursor.pop();

    // Process the element
    processName(cursor, declarations);

    // Process the attributes
    cursor.push();
    boolean hasNext = cursor.toFirstAttribute();
    while(hasNext)
    {
        processName(cursor, declarations);
        hasNext = cursor.toNextAttribute();
    }
    cursor.pop();
}
 
Example 10
Source File: XML.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
XScriptAnnotation (XmlCursor curs)
{
    _name = curs.getName();
}
 
Example 11
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 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: 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;
}