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

The following examples show how to use org.apache.xmlbeans.XmlCursor#toNextAttribute() . 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: XmlPath.java    From mdw with Apache License 2.0 7 votes vote down vote up
private boolean verify_condition(XmlCursor cursor, Condition condition) {
    boolean more, found=false;
    XmlBookmark bookmark = new XmlBookmark(){};
    cursor.setBookmark(bookmark);
    if (condition.isAttribute) {
        for (more=cursor.toFirstAttribute(); more&&!found; more=cursor.toNextAttribute()) {
            if (cursor.getName().getLocalPart().equals(condition.name)) {
                found = cursor.getTextValue().trim().equals(condition.value);
            }
        }
    } else {
        for (more=cursor.toFirstChild(); more&&!found; more=cursor.toNextSibling()) {
            if (cursor.getName().getLocalPart().equals(condition.name)) {
                found = cursor.getTextValue().trim().equals(condition.value);
            }
        }
    }
    cursor.toBookmark(bookmark);
    return found;
}
 
Example 2
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 3
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 4
Source File: XmlBeanAssert.java    From mdw with Apache License 2.0 4 votes vote down vote up
/**
 * Compares the attributes of the elements at the current position of two XmlCursors.
 * The ordering of the attributes is ignored in the comparison.  Fails the JUnit test
 * case if the attributes or their values are different.
 *
 * @param message to display on test failure (may be null)
 * @param expected
 * @param actual
 */
private static void assertAttributesEqual(String message, XmlCursor expected, XmlCursor actual)
{
  Map<QName,String> map1 = new HashMap<QName,String>();
  Map<QName,String> map2 = new HashMap<QName,String>();

  boolean attr1 = expected.toFirstAttribute();
  boolean attr2 = actual.toFirstAttribute();
  if (attr1 != attr2)
  {
    if (expected.isAttr() || actual.isAttr())
    {
      expected.toParent();
    }
    fail(message, "Differing number of attributes for element: '" + QNameHelper.pretty(expected.getName()) + "'");
  }
  else if (!attr1)
  {
    return;
  }
  else
  {
    map1.put(expected.getName(), expected.getTextValue());
    map2.put(actual.getName(), actual.getTextValue());
  }
  while (true)
  {
    attr1 = expected.toNextAttribute();
    attr2 = actual.toNextAttribute();
    if (attr1 != attr2)
    {
      if (expected.isAttr() || actual.isAttr())
      {
        expected.toParent();
      }
      fail(message, "Differing number of attributes for element: '" + QNameHelper.pretty(expected.getName()) + "'");
    }
    else if (!attr1)
    {
      break;
    }
    else
    {
      map1.put(expected.getName(), expected.getTextValue());
      map2.put(actual.getName(), actual.getTextValue());
    }
  }

  expected.toParent();
  actual.toParent();

  // check that attribute maps match, neglecting order
  Iterator<QName> iter = map1.keySet().iterator();
  while (iter.hasNext())
  {
    QName name = iter.next();
    String value1 = map1.get(name);
    String value2 = map2.get(name);
    if (value2 == null)
    {
      fail(message, "Expected attribute value missing for element: "
          + QNameHelper.pretty(expected.getName()) + "--> '" + name + "'");
    }
    else if (!value2.equals(value1))
    {
      fail(message, "Attribute values for element '" + QNameHelper.pretty(expected.getName())
          + "':  Expected '" + value1 + "', Actual '" + value2 + "'");
    }
  }
}