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

The following examples show how to use org.apache.xmlbeans.XmlCursor#isAttr() . 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
 */
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: 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 attributesEqual(XmlCursor xmlOne, XmlCursor xmlTwo)
{
    boolean result = false;

    if (xmlOne.isAttr() && xmlTwo.isAttr())
    {
        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
/**
 * Special constructor for making an attribute
 *
 */
private static XML createAttributeXML(XMLLibImpl lib, XmlCursor cursor)
{
    if (!cursor.isAttr())
        throw new IllegalArgumentException();

    XScriptAnnotation anno = new XScriptAnnotation(cursor);
    cursor.setBookmark(anno);

    return new XML(lib, anno);
}
 
Example 5
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 6
Source File: LogicalEquality.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 *
 * @param xml
 * @return
 */
private static TreeMap loadAttributeMap(XmlCursor xml)
{
    TreeMap result = new TreeMap();

    while (xml.isAttr())
    {
        result.put(xml.getTextValue(), xml.getName());
        nextToken(xml);
    }

    return result;
}
 
Example 7
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 + "'");
    }
  }
}
 
Example 8
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 9
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 10
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;
}