[jdom-interest] Text class in the api

philip.nelson at omniresources.com philip.nelson at omniresources.com
Thu Nov 15 17:04:16 PST 2001


I finally got around to taking a look at the code done by Bradley Huffman
for implementing the Text class.  Looks really good to me. About the only
places we will certainly see some problems are where people are dealing with
the Element list content directly.  The test suite only failed with a couple
of obvious class Cast exceptions and where I was looking for a specific
string in the toString() method. 

So, how do we go about merging it into the source tree?  We probably
shouldn't wait too much longer or we will have yet another merge process to
go through.

Approaches we could take:

Merge it into jdom-wip and release it with FilterList.  Has the advantage of
a separate source tree so we can adequately warn people of the change rather
than suffer the consequences.

Merge it into the jdom source tree.  Makes more sense if jdom-wip will take
awhile to make real and two fairly different source trees have to be
maintained.

Merge it into both.  Well, the second comment leads to this approach anyway.
Seems like we have to pick a time and go with it.


Here is a diff -r file done on the source tree with the files that only had
cvs tag differences in them.  I am sending this not a source release, but
just to show you all what has changed.  I have left out the changes to the
Text class which has been fully changed and implemented with StringBuffer,
and SAXHandler, which I can't easily do a diff with since I made so many
changes to my version for DeclHandler, DTDHandler and the like.  The updated
Text class is included in full.

diff -r ./java/org/jdom/Element.java
\temp\jdom-test\brad\jdom\src/java/org/jdom/Element.java
565,566c565,567
<      *   all String nodes returned by getContent().  The call does not
<      *   recurse into child elements.  If no textual value exists for the 
---
>      *   all <code>Text</code> and <code>CDATA</code> nodes returned by
>      *   getContent().  The call does not recurse into child elements.
>      *   If no textual value exists for the 
579c580
<         // If we hold only a String or CDATA, return it directly
---
>         // If we hold only a Text or CDATA, return it directly
582,583c583,58
<             if (obj instanceof String) {
<                 return (String)obj;
---
>             if (obj instanceof Text) {
>                 return ((Text) obj).getText();
585c586
<                 return ((CDATA)obj).getText();
---
>                 return ((CDATA) obj).getText();
598,599c599,600
<             if (obj instanceof String) {
<                 textContent.append((String)obj);
---
>             if (obj instanceof Text) {
>                 textContent.append(((Text) obj).getText());
602c603
<                 textContent.append(((CDATA)obj).getText());
---
>                 textContent.append(((CDATA) obj).getText());
641,666c642
<         return normalizeString(getText());
<     }
< 
<     // Support method for fast string normalization
<     // Per XML 1.0 Production 3 whitespace includes: #x20, #x9, #xD, #xA
<     private static String normalizeString(String value) {
<         char[] c = value.toCharArray();
<         char[] n = new char[c.length];
<         boolean white = true;
<         int pos = 0;
<         for (int i = 0; i < c.length; i++) {
<             if (" \t\n\r".indexOf(c[i]) != -1) {
<                 if (!white) {
<                     n[pos++] = ' ';
<                     white = true;
<                 }
<             }
<             else {
<                 n[pos++] = c[i];
<                 white = false;
<             }
<         }
<         if (white && pos > 0) {
<             pos--;
<         }
<         return new String(n, 0, pos);
---
>         return Text.normalizeString(getText());
810c786
<             content.add(text);
---
>             content.add( new Text( text));
856c832
<      * may contain objects of type <code>String</code>,
<code>Element</code>,
---
>      * may contain objects of type <code>Text</code>, 
> <code>Element</code>,
865c841
<      *         element: may contain <code>String</code>,
---
>      *         element: may contain <code>Text</code>,
884,885c860,862
<      * contain only objects of type <code>String</code>,
<code>Element</code>,
<      * <code>Comment</code>, <code>ProcessingInstruction</code>, 
---
>      * contain only objects of type <code>String</code>,
<code>Text</code>,
>      * <code>Element</code>, <code>Comment</code>,
>      * <code>ProcessingInstruction</code>,
916c893,896
<                     addContent((String)obj);
---
>                     addContent( new Text( (String) obj));
>                 }
>                 else if (obj instanceof Text) {
>                     addContent((Text)obj);
933,934c913,914
<                       "String, Element, Comment, CDATA, EntityRef, and " +

<                       "ProcessingInstruction: " +
---
>                       "String, Text, Element, Comment, CDATA, EntityRef, "
+
>                       "and ProcessingInstruction: " +
955a936,938
>                     else if (obj instanceof Text) {
>                         ((Text)obj).setParent(null);
>                     }
958a942,944
>                     else if (obj instanceof CDATA) {
>                         ((CDATA)obj).setParent(null);
>                     }
981a968,970
>             else if (obj instanceof Text) {
>                 ((Text)obj).setParent(null);
>             }
984a974,976
>             else if (obj instanceof CDATA) {
>                 ((CDATA)obj).setParent(null);
>             }
1186c1178,1207
<      * @param text <code>String</code> to add
---
>      * @param str <code>String</code> to add
>      * @return this element modified
>      */
>     public Element addContent(String str) {
>         if (content == null) {
>             content = new ArrayList(INITIAL_ARRAY_SIZE);
>         }
> 
>         int size = content.size();
>         if (size > 0) {
>             Object ob = content.get(size - 1);
>             if (ob instanceof Text) {
>                 Text txt = (Text) ob;
>                 txt.append( str);
>                 return this;
>             }
>         }
>         Text txt = new Text( str);
>         txt.setParent(this);
>         content.add( txt);
>         return this;
>     }
> 
>     /**
>      * <p>
>      * This adds text content to this element.  It does not replace the
>      * existing content as does <code>setText()</code>.
>      * </p>
>      *
>      * @param text <code>Text</code> to add
1189c1210
<     public Element addContent(String text) {
---
>     public Element addContent(Text text) {
1197,1199c1218,1221
<             if (ob instanceof String) {
<                 text = (String)ob + text;
<                 content.remove(size - 1);
---
>             if (ob instanceof Text) {
>                 Text txt = (Text) ob;
>                 txt.append( text);
>                 return this;
1202c1224,1226
<         content.add(text);
---
> 
>         content.add( text);
>         text.setParent(this);
1325a1350
>         cdata.setParent(this);
1590c1615
<         this.attributes = null;
---
>         this.attributes = (List) null;
1597,1598c1622,1629
<                 setAttribute((Attribute) iter.next());
<                 itemsAdded++;
---
>                 Object obj = iter.next();
>                 if (obj instanceof Attribute) {
>                     setAttribute( (Attribute) obj);
>                     itemsAdded++;
>                 }
>                 else {
>                     throw new IllegalAddException( "Non-Attribute in
list");
>                 }
1942d1972
<         // No need to clone CDATA or String since they're immutable
1946,1948c1976
<                 if (obj instanceof String) {
<                     element.content.add(obj);
<                 } else if (obj instanceof Element) {
---
>                 if (obj instanceof Element) {
1951a1980,1983
>                 } else if (obj instanceof Text) {
>                     Text t = (Text)((Text)obj).clone();
>                     t.setParent(element);
>                     element.content.add(t);
1957c1989,1991
<                     element.content.add(obj);
---
>                     CDATA d = (CDATA)((CDATA)obj).clone();
>                     d.setParent(element);
>                     element.content.add(d);
2082a2117,2139
>             cdata.setParent(null);
>             return true;
>         } else {
>             return false;
>         }
>     }
> 
>     /**
>      * <p>
>      * This removes the specified <code>Text</code>.
>      * If the specified <code>Text</code> is not a child of
>      * this <code>Element</code>, this method does nothing.
>      * </p>
>      *
>      * @param text <code>Text</code> to delete
>      * @return whether deletion occurred
>      */
>     public boolean removeContent(Text text) {
>         if (content == null) {
>             return false;
>         }
>         if (content.remove(text)) {
>             text.setParent(null);

diff -r ./java/org/jdom/input/DefaultJDOMFactory.java
\temp\jdom-test\brad\jdom\src/java/org/jdom/input/DefaultJDOMFactory.java
88,89c88,93
<     public CDATA cdata(String text) {
<         return new CDATA(text);
---
>     public CDATA cdata(String data) {
>         return new CDATA(data);
>     }
> 
>     public Text text(String str) {
>         return new Text(str);

diff -r ./java/org/jdom/input/DOMBuilder.java
\temp\jdom-test\brad\jdom\src/java/org/jdom/input/DOMBuilder.java
475,476c475,476
<                 String text = node.getNodeValue();
<                 current.addContent(text);
---
>                 String data = node.getNodeValue();
>                 current.addContent(factory.text( data));

diff -r ./java/org/jdom/input/JDOMFactory.java
\temp\jdom-test\brad\jdom\src/java/org/jdom/input/JDOMFactory.java
113c113
<      * @param text <code>String</code> content of CDATA.
---
>      * @param data <code>String</code> content of CDATA.
115c115,127
<     public CDATA cdata(String text);
---
>     public CDATA cdata(String data);
> 
>     // **** constructing Text ****
> 
>     /**
>      * <p>
>      * This creates new <code>Text</code> with the supplied
>      *   string.
>      * </p>
>      *
>      * @param str <code>String</code> content of Text.
>      */
>     public Text text(String str);
diff -r ./java/org/jdom/output/DOMOutputter.java
\temp\jdom-test\brad\jdom\src/java/org/jdom/output/DOMOutputter.java
352,354c352,355
<                 else if (node instanceof String) {
<                     String str = (String) node;
<                     org.w3c.dom.Text domText = domDoc.createTextNode(str);
---
>                 else if (node instanceof Text) {
>                     Text text = (Text) node;
>                     org.w3c.dom.Text domText =
>                         domDoc.createTextNode(text.getText());
diff -r ./java/org/jdom/output/SAXOutputter.java
\temp\jdom-test\brad\jdom\src/java/org/jdom/output/SAXOutputter.java
917c917
<             eltContent.get(0) instanceof String;
---
>             eltContent.get(0) instanceof Text;
930c930
<                 else if (content instanceof String) {
---
>                 else if (content instanceof Text) {
932c932
<                     characters((String) content);
---
>                     characters(((Text) content).getText());

diff -r ./java/org/jdom/output/XMLOutputter.java
\temp\jdom-test\brad\jdom\src/java/org/jdom/output/XMLOutputter.java
285a286,287
>      * <p>Sets whether new lines should be printed.</p>
>      *
699d700
<      * <p>
723c724
<         printString(text.getValue(), out);
---
>         printString(text.getText(), out);
728d728
<      * <p>
874a875,889
>      * Return a string representing a Text node. Warning: a String is
>      * Unicode, which may not match the outputter's specified
>      * encoding.
>      *
>      * @param text <code>Text</code> to format.
>      **/
>     public String outputString(Text text) {
>         StringWriter out = new StringWriter();
>         try {
>             output(text, out);  // output() flushes
>         } catch (IOException e) { }
>         return out.toString();
>     }
> 
>     /**
1014a1030,1042
>      * This will write the text node to the specified writer.
>      * </p>
>      *
>      * @param text <code>Text</code> to write.
>      * @param out <code>Writer</code> to write to.
>      */
>     protected void printText(Text text, Writer out)
>                                   throws IOException {
>         printString(text.getText(), out);
>     }
>       
>     /**
>      * <p>
1114c1142
<             if (!(o instanceof String) && !(o instanceof CDATA)) {
---
>             if (!(o instanceof Text) && !(o instanceof CDATA)) {
1188,1189c1216,1217
<                 if (content instanceof String) {
<                     String scontent = (String) content;
---
>                 if (content instanceof Text) {
>                     String scontent = ((Text) content).getText();
1225,1226c1253,1254
<                 } else if (content instanceof String) {
<                     String scontent = (String) content;
---
>                 } else if (content instanceof Text) {
>                     String scontent = ((Text) content).getText();
1631c1659
<             if (!(o instanceof String) && !(o instanceof CDATA)) {
---
>             if (!(o instanceof Text) && !(o instanceof CDATA)) {
1638a1667,1669
>      * <p>Return a string listing of the settings for this
>      *    XMLOutputter instance.</p>
>      *


-------------- next part --------------
A non-text attachment was scrubbed...
Name: Text.java
Type: application/octet-stream
Size: 10870 bytes
Desc: not available
Url : http://jdom.org/pipermail/jdom-interest/attachments/20011115/ad4ba428/Text.obj


More information about the jdom-interest mailing list