[jdom-interest] PATCH: SAXHandler, Text, and CDATA

Bradley S. Huffman hip at a.cs.okstate.edu
Wed Jan 9 19:39:20 PST 2002


I got phil's patch from the archives, looked at it, and modified the
current versions of SAXHandler, Text, and CDATA to reflect the concepts.

Brad

==================== Cut Here ====================
Common subdirectories: jdom/src and jdom-new/src
Common subdirectories: jdom/src/java and jdom-new/src/java
Common subdirectories: jdom/src/java/org and jdom-new/src/java/org
Common subdirectories: jdom/src/java/org/jdom and jdom-new/src/java/org/jdom
diff -r -c jdom/src/java/org/jdom/CDATA.java jdom-new/src/java/org/jdom/CDATA.java
*** jdom/src/java/org/jdom/CDATA.java	Wed Jan  9 21:18:57 2002
--- jdom-new/src/java/org/jdom/CDATA.java	Wed Jan  9 21:22:20 2002
***************
*** 77,89 ****
      private static final String CVS_ID = 
        "@(#) $RCSfile: CDATA.java,v $ $Revision: 1.17 $ $Date: 2002/01/08 09:17:10 $ $Name:  $";
  
      /** The actual character content */
-     // XXX See http://www.servlets.com/archive/servlet/ReadMsg?msgId=8776
-     // from Alex Rosen for a reason why String might be better
      // XXX See http://www.servlets.com/archive/servlet/ReadMsg?msgId=8612
      // from elharo for a description of why Java characters may not suffice
      // long term
!     protected StringBuffer value;
  
      /** This <code>CDATA</code> node's parent. */
      protected Element parent;
--- 77,89 ----
      private static final String CVS_ID = 
        "@(#) $RCSfile: CDATA.java,v $ $Revision: 1.17 $ $Date: 2002/01/08 09:17:10 $ $Name:  $";
  
+     private static final String EMPTY_STRING = "";
+ 
      /** The actual character content */
      // XXX See http://www.servlets.com/archive/servlet/ReadMsg?msgId=8612
      // from elharo for a description of why Java characters may not suffice
      // long term
!     protected String value;
  
      /** This <code>CDATA</code> node's parent. */
      protected Element parent;
***************
*** 112,118 ****
       * @return <code>String</code> - character content of this node.
       */
      public String getText() {
!         return value.toString();
      }
  
      /**
--- 112,118 ----
       * @return <code>String</code> - character content of this node.
       */
      public String getText() {
!         return value;
      }
  
      /**
***************
*** 146,155 ****
       * @param str value for node's content.
       */
      public CDATA setText(String str) {
!         value = new StringBuffer();          /* Clear old cdata */
!         if (str == null)
              return this;
!         append(str);        /* so we get the Verifier check */
          return this;
      }
  
--- 146,162 ----
       * @param str value for node's content.
       */
      public CDATA setText(String str) {
!         String reason;
! 
!         if (str == null) {
!             value = EMPTY_STRING;
              return this;
!         }
! 
!         if ((reason = Verifier.checkCDATASection(str)) != null) {
!             throw new IllegalDataException(str, "CDATA section", reason);
!         }
!         value = str;
          return this;
      }
  
***************
*** 169,175 ****
              throw new IllegalDataException(str, "CDATA section", reason);
          }
  
!         value.append(str);
      }
  
      /**
--- 176,184 ----
              throw new IllegalDataException(str, "CDATA section", reason);
          }
  
!         if (value == EMPTY_STRING)
!              value = str;
!         else value += str;
      }
  
      /**
***************
*** 182,188 ****
          if (cdata == null) {
              return;
          }
!         value.append(cdata.getText());
      }
  
      /**
--- 191,197 ----
          if (cdata == null) {
              return;
          }
!         value += cdata.getText();
      }
  
      /**
***************
*** 286,292 ****
          }
  
          cdata.parent = null;
!         cdata.value = new StringBuffer(value.toString());
  
          return cdata;
      }
--- 295,301 ----
          }
  
          cdata.parent = null;
!         cdata.value = value;
  
          return cdata;
      }
diff -r -c jdom/src/java/org/jdom/Text.java jdom-new/src/java/org/jdom/Text.java
*** jdom/src/java/org/jdom/Text.java	Wed Jan  9 21:19:45 2002
--- jdom-new/src/java/org/jdom/Text.java	Wed Jan  9 21:22:57 2002
***************
*** 75,87 ****
      private static final String CVS_ID = 
        "@(#) $RCSfile: Text.java,v $ $Revision: 1.9 $ $Date: 2002/01/08 09:17:10 $ $Name:  $";
  
      /** The actual character content */
-     // XXX See http://www.servlets.com/archive/servlet/ReadMsg?msgId=8776
-     // from Alex Rosen for a reason why String might be better
      // XXX See http://www.servlets.com/archive/servlet/ReadMsg?msgId=8612
      // from elharo for a description of why Java characters may not suffice
      // long term
!     protected StringBuffer value;
  
      /** This <code>Text</code> node's parent. */
      protected Element parent;
--- 75,87 ----
      private static final String CVS_ID = 
        "@(#) $RCSfile: Text.java,v $ $Revision: 1.9 $ $Date: 2002/01/08 09:17:10 $ $Name:  $";
  
+     private static final String EMPTY_STRING = "";
+ 
      /** The actual character content */
      // XXX See http://www.servlets.com/archive/servlet/ReadMsg?msgId=8612
      // from elharo for a description of why Java characters may not suffice
      // long term
!     protected String value;
  
      /** This <code>Text</code> node's parent. */
      protected Element parent;
***************
*** 110,116 ****
       * @return <code>String</code> - character content of this node.
       */
      public String getText() {
!         return value.toString();
      }
  
      /**
--- 110,116 ----
       * @return <code>String</code> - character content of this node.
       */
      public String getText() {
!         return value;
      }
  
      /**
***************
*** 153,159 ****
       */
      public static String normalizeString(String str) {
          if (str == null)
!             return null; /* XXX or should it return "" */
  
          char[] c = str.toCharArray();
          char[] n = new char[c.length];
--- 153,159 ----
       */
      public static String normalizeString(String str) {
          if (str == null)
!             return EMPTY_STRING;
  
          char[] c = str.toCharArray();
          char[] n = new char[c.length];
***************
*** 183,192 ****
       * @param str value for node's content.
       */
      public Text setText(String str) {
!         value = new StringBuffer();          /* Clear old text */
!         if (str == null)
              return this;
!         append(str);        /* so we get the Verifier check */
          return this;
      }
  
--- 183,199 ----
       * @param str value for node's content.
       */
      public Text setText(String str) {
!         String reason;
! 
!         if (str == null) {
!             value = EMPTY_STRING;
              return this;
!         }
! 
!         if ((reason = Verifier.checkCharacterData(str)) != null) {
!             throw new IllegalDataException(str, "character content", reason);
!         }
!         value = str;
          return this;
      }
  
***************
*** 206,212 ****
              throw new IllegalDataException(str, "character content", reason);
          }
  
!         value.append(str);
      }
  
      /**
--- 213,221 ----
              throw new IllegalDataException(str, "character content", reason);
          }
  
!         if (str == EMPTY_STRING)
!              value = str;
!         else value += str;
      }
  
      /**
***************
*** 219,225 ****
          if (text == null) {
              return;
          }
!         value.append(text.getText());
      }
  
      /**
--- 228,234 ----
          if (text == null) {
              return;
          }
!         value += text.getText();
      }
  
      /**
***************
*** 323,329 ****
          }
  
          text.parent = null;
!         text.value = new StringBuffer(value.toString());
  
          return text;
      }
--- 332,338 ----
          }
  
          text.parent = null;
!         text.value = value;
  
          return text;
      }
Common subdirectories: jdom/src/java/org/jdom/input and jdom-new/src/java/org/jdom/input
diff -r -c jdom/src/java/org/jdom/input/SAXHandler.java jdom-new/src/java/org/jdom/input/SAXHandler.java
*** jdom/src/java/org/jdom/input/SAXHandler.java	Wed Jan  9 21:19:22 2002
--- jdom-new/src/java/org/jdom/input/SAXHandler.java	Wed Jan  9 21:25:05 2002
***************
*** 76,81 ****
--- 76,82 ----
   * @author Jason Hunter
   * @author Philip Nelson
   * @author Bradley S. Huffman
+  * @author phil at triloggroup.com
   * @version $Revision: 1.29 $, $Date: 2002/01/08 09:17:10 $
   */
  public class SAXHandler extends DefaultHandler implements LexicalHandler,
***************
*** 130,135 ****
--- 131,139 ----
      /** Temporary holder for the internal subset */
      private StringBuffer buffer = new StringBuffer();
  
+     /** Temporary holder for Text and CDATA */
+     private StringBuffer textBuffer = new StringBuffer();
+ 
      /** The external entities defined in this document */
      private Map externalEntities;
  
***************
*** 454,459 ****
--- 458,465 ----
      public void processingInstruction(String target, String data)
          throws SAXException {
  
+         flushCharacters();
+ 
          if (suppress) return;
  
          if (atRoot) {
***************
*** 533,538 ****
--- 539,546 ----
      public void startElement(String namespaceURI, String localName,
                               String qName, Attributes atts)
                               throws SAXException {
+         flushCharacters();
+ 
          if (suppress) return;
  
          Element element = null;
***************
*** 656,671 ****
       * @param ch <code>char[]</code> character array with character data
       * @param start <code>int</code> index in array where data starts.
       * @param length <code>int</code> length of data.
-      * @throws SAXException when things go wrong
       */
!     public void characters(char[] ch, int start, int length)
!         throws SAXException {
  
!         if (suppress) return;
!         if (length == 0) return;
  
!         String data = new String(ch, start, length);
  
  /**
   * This is commented out because of some problems with
   * the inline DTDs that Xerces seems to have.
--- 664,692 ----
       * @param ch <code>char[]</code> character array with character data
       * @param start <code>int</code> index in array where data starts.
       * @param length <code>int</code> length of data.
       */
!     public void characters(char[] ch, int start, int length) {
!         if (suppress || (length == 0))
!             return;
  
!         textBuffer.append( ch, start, length);
!     }
  
!     /**
!      * <p>
!      * This will flush any characters from SAX character calls we've
!      * been buffering.
!      * </p>
!      *
!      * @throws SAXException when things go wrong
!      */
!     protected void flushCharacters() throws SAXException {
!         if (textBuffer.length() == 0)
!             return;
  
+         String data = textBuffer.toString();
+         textBuffer.setLength( 0);
+ 
  /**
   * This is commented out because of some problems with
   * the inline DTDs that Xerces seems to have.
***************
*** 703,711 ****
          if (ignoringWhite) return;
          if (length == 0) return;
  
!         String data = new String(ch, start, length);
!         getCurrentElement().addContent(factory.text(data));
! 
      }
  
      /**
--- 724,730 ----
          if (ignoringWhite) return;
          if (length == 0) return;
  
!         textBuffer.append( ch, start, length);
      }
  
      /**
***************
*** 725,730 ****
--- 744,751 ----
      public void endElement(String namespaceURI, String localName,
                             String qName) throws SAXException {
  
+         flushCharacters();
+ 
          if (suppress) return;
  
          try {
***************
*** 761,766 ****
--- 782,789 ----
      public void startDTD(String name, String publicId, String systemId)
          throws SAXException {
  
+         flushCharacters();
+ 
          document.setDocType(
              factory.docType(name, publicId, systemId));
          inDTD = true;
***************
*** 773,785 ****
       * </p>
       */
      public void endDTD() throws SAXException {
          document.getDocType().setInternalSubset(buffer.toString());
          inDTD = false;
          inInternalSubset = false;
      }
  
!     public void startEntity(String name)
!         throws SAXException {
  
          entityDepth++;
  
--- 796,810 ----
       * </p>
       */
      public void endDTD() throws SAXException {
+         flushCharacters();
+ 
          document.getDocType().setInternalSubset(buffer.toString());
          inDTD = false;
          inInternalSubset = false;
      }
  
!     public void startEntity(String name) throws SAXException {
!         flushCharacters();
  
          entityDepth++;
  
***************
*** 828,833 ****
--- 853,860 ----
      }
  
      public void endEntity(String name) throws SAXException {
+         flushCharacters();
+ 
          entityDepth--;
          if (entityDepth == 0) {
              // No way are we suppressing if not in an entity,
***************
*** 844,849 ****
--- 871,878 ----
       * </p>
       */
      public void startCDATA() throws SAXException {
+         flushCharacters();
+ 
          if (suppress) return;
  
          inCDATA = true;
***************
*** 855,860 ****
--- 884,891 ----
       * </p>
       */
      public void endCDATA() throws SAXException {
+         flushCharacters();
+ 
          if (suppress) return;
  
          inCDATA = false;
***************
*** 875,880 ****
--- 906,913 ----
      public void comment(char[] ch, int start, int length)
          throws SAXException {
  
+         flushCharacters();
+ 
          if (suppress) return;
  
          String commentText = new String(ch, start, length);



More information about the jdom-interest mailing list