You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xerces.apache.org by le...@locus.apache.org on 2000/11/22 03:22:03 UTC
cvs commit: xml-xerces/java/src/org/apache/xerces/dom ElementImpl.java AttrImpl.java
lehors 00/11/21 18:22:02
Modified: java/src/org/apache/xerces/dom ElementImpl.java
AttrImpl.java
Log:
applied patch from Lynn Monson:
As I was poking around Xerces, I came across a pair of bugs in the
implementation of Node.normalize(). The problems are:
1) Normalizing an element does not normalize the attributes of the element.
2) Normalize does not remove isolated, empty text nodes.
Revision Changes Path
1.29 +31 -11 xml-xerces/java/src/org/apache/xerces/dom/ElementImpl.java
Index: ElementImpl.java
===================================================================
RCS file: /home/cvs/xml-xerces/java/src/org/apache/xerces/dom/ElementImpl.java,v
retrieving revision 1.28
retrieving revision 1.29
diff -u -r1.28 -r1.29
--- ElementImpl.java 2000/11/13 17:30:24 1.28
+++ ElementImpl.java 2000/11/22 02:22:01 1.29
@@ -287,25 +287,45 @@
* normal Text or with other CDATASections.
*/
public void normalize() {
-
Node kid, next;
for (kid = getFirstChild(); kid != null; kid = next) {
next = kid.getNextSibling();
- // If kid and next are both Text nodes (but _not_ CDATASection,
- // which is a subclass of Text), they can be merged.
- if (next != null
- && kid.getNodeType() == Node.TEXT_NODE
- && next.getNodeType() == Node.TEXT_NODE)
- {
- ((Text)kid).appendData(next.getNodeValue());
- removeChild(next);
- next = kid; // Don't advance; there might be another.
- }
+ // If kid is a text node, we need to check for one of two
+ // conditions:
+ // 1) There is an adjacent text node
+ // 2) There is no adjacent text node, but kid is
+ // an empty text node.
+ if ( kid.getNodeType() == Node.TEXT_NODE )
+ {
+ // If an adjacent text node, merge it with kid
+ if ( next!=null && next.getNodeType() == Node.TEXT_NODE )
+ {
+ ((Text)kid).appendData(next.getNodeValue());
+ removeChild( next );
+ next = kid; // Don't advance; there might be another.
+ }
+ else
+ {
+ // If kid is empty, remove it
+ if ( kid.getNodeValue().length()==0 )
+ removeChild( kid );
+ }
+ }
// Otherwise it might be an Element, which is handled recursively
else if (kid.getNodeType() == Node.ELEMENT_NODE) {
((Element)kid).normalize();
+ }
+ }
+
+ // We must also normalize all of the attributes
+ if ( attributes!=null )
+ {
+ for( int i=0; i<attributes.getLength(); ++i )
+ {
+ Node attr = attributes.item(i);
+ attr.normalize();
}
}
1.23 +24 -14 xml-xerces/java/src/org/apache/xerces/dom/AttrImpl.java
Index: AttrImpl.java
===================================================================
RCS file: /home/cvs/xml-xerces/java/src/org/apache/xerces/dom/AttrImpl.java,v
retrieving revision 1.22
retrieving revision 1.23
diff -u -r1.22 -r1.23
--- AttrImpl.java 2000/10/02 21:58:09 1.22
+++ AttrImpl.java 2000/11/22 02:22:02 1.23
@@ -353,21 +353,31 @@
public void normalize() {
- Node kid, next;
- for (kid = firstChild; kid != null; kid = next) {
- next = kid.getNextSibling();
+ Node kid, next;
+ for (kid = firstChild; kid != null; kid = next) {
+ next = kid.getNextSibling();
- // If kid and next are both Text nodes (but _not_ CDATASection,
- // which is a subclass of Text), they can be merged.
- if (next != null
- && kid.getNodeType() == Node.TEXT_NODE
- && next.getNodeType() == Node.TEXT_NODE)
- {
- ((Text)kid).appendData(next.getNodeValue());
- removeChild(next);
- next = kid; // Don't advance; there might be another.
- }
-
+ // If kid is a text node, we need to check for one of two
+ // conditions:
+ // 1) There is an adjacent text node
+ // 2) There is no adjacent text node, but kid is
+ // an empty text node.
+ if ( kid.getNodeType() == Node.TEXT_NODE )
+ {
+ // If an adjacent text node, merge it with kid
+ if ( next!=null && next.getNodeType() == Node.TEXT_NODE )
+ {
+ ((Text)kid).appendData(next.getNodeValue());
+ removeChild( next );
+ next = kid; // Don't advance; there might be another.
+ }
+ else
+ {
+ // If kid is empty, remove it
+ if ( kid.getNodeValue().length()==0 )
+ removeChild( kid );
+ }
+ }
}
} // normalize()