Uploaded image for project: 'XalanJ2'
  1. XalanJ2
  2. XALANJ-2473

DTMNodeProxy.getTextContent() does not return text content of child nodes

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 2.7.1
    • Component/s: DTM
    • Security Level: No security risk; visible to anyone (Ordinary problems in Xalan projects. Anybody can view the issue.)
    • Labels:
      None
    • Environment:
      Java 1.5

      Description

      If a java extension functions takes an org.w3c.dom.Node as an argument then it gets an org.apache.xml.dtm.ref.DTMNodeProxy object.
      The "getTextContent" method of this object returns null if the node is an element node, although according to the java 1.5 documentation, the "getTextContent" method should return the "concatenation of the textContent attribute value of every child node".

      This is the implementation:
      public String getTextContent() throws DOMException {
      return getNodeValue(); // overriden in some subclasses
      }

      Of course, "getNodeValue" returns null if the node is an element node, but I think this is not the way "getTextContent" should be implemented.

      The following code can be used to reproduce this error:
      XSLT:
      <xsl:value-of select="src:myFunction(foo)"/>
      XML:
      <foo>Some Text</foo>
      Java:
      public static String myFunction(org.w3c.dom.Node n) {
      String s = n.getTextContent();
      // s should be "Some Text", but is actually null.
      }

      1. XALANJ_2473_Test1.java
        3 kB
        Martin von Gagern
      2. XALANJ-2473_2.patch
        0.5 kB
        Martin von Gagern
      3. XALANJ-2473-getTextContent.patch
        2 kB
        Martin von Gagern

        Activity

        Hide
        gagern Martin von Gagern added a comment -

        Patch to solve the issue, together with a simple test case. Works for me.

        Show
        gagern Martin von Gagern added a comment - Patch to solve the issue, together with a simple test case. Works for me.
        Hide
        gagern Martin von Gagern added a comment -

        Cleaned up test case, especially imports and comments. Had a bit too much copy&paste from my example for XALANJ-2493.

        Show
        gagern Martin von Gagern added a comment - Cleaned up test case, especially imports and comments. Had a bit too much copy&paste from my example for XALANJ-2493 .
        Hide
        zongaro@ca.ibm.com Henry Zongaro added a comment -

        Hi, Martin. Your patch for this bug report looks correct. However, I would recommend trying to delegate the implementation of getTextContent to the underlying DTM implementation instead of doing a recursive walk that ultimately gets the string value of each text or CData section node and concatenates them all together. If the DTMNodeProxy wraps a node from a SAX2DTM instance, in particular, the implementation can take advantage of the FastStringBuffer that stores the character content of the SAX2DTM implementation. (Take a look at SAX2DTM.getStringValue(int) to understand how that might be much faster.)

        Show
        zongaro@ca.ibm.com Henry Zongaro added a comment - Hi, Martin. Your patch for this bug report looks correct. However, I would recommend trying to delegate the implementation of getTextContent to the underlying DTM implementation instead of doing a recursive walk that ultimately gets the string value of each text or CData section node and concatenates them all together. If the DTMNodeProxy wraps a node from a SAX2DTM instance, in particular, the implementation can take advantage of the FastStringBuffer that stores the character content of the SAX2DTM implementation. (Take a look at SAX2DTM.getStringValue(int) to understand how that might be much faster.)
        Hide
        gagern Martin von Gagern added a comment -

        DTM.getStringValue sure makes things a lot easier. Thanks for pointing this out, Henry!

        Show
        gagern Martin von Gagern added a comment - DTM.getStringValue sure makes things a lot easier. Thanks for pointing this out, Henry!
        Hide
        zongaro@ca.ibm.com Henry Zongaro added a comment -

        Committed Martin's second patch[1] to the repository.

        [1] https://issues.apache.org/jira/secure/attachment/12426194/XALANJ-2473_2.patch

        Show
        zongaro@ca.ibm.com Henry Zongaro added a comment - Committed Martin's second patch [1] to the repository. [1] https://issues.apache.org/jira/secure/attachment/12426194/XALANJ-2473_2.patch

          People

          • Assignee:
            Unassigned
            Reporter:
            chschroe Christian Schröder
            Reviewer:
            Henry Zongaro
          • Votes:
            1 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development