I implemented a couple of missing methods in HTMLDocument, which in
effect enables processing of <pre> tags.

2006-10-31  Roman Kennke  <[EMAIL PROTECTED]>

        * javax/swing/text/html/HTMLDocument.java
        (HTMLReader.PreAction.end): Implemented.
        (HTMLReader.PreAction.start): Implemented.
        (HTMLReader.inPreTag): New field.
        (HTMLReader.handleTag): When inside a pre tag, call preContent().
        (HTMLReader.preContent): Implemented.

/Roman

Index: javax/swing/text/html/HTMLDocument.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/text/html/HTMLDocument.java,v
retrieving revision 1.41
diff -u -1 -5 -r1.41 HTMLDocument.java
--- javax/swing/text/html/HTMLDocument.java	3 Sep 2006 20:42:43 -0000	1.41
+++ javax/swing/text/html/HTMLDocument.java	31 Oct 2006 16:36:40 -0000
@@ -547,31 +547,36 @@
     int offset;
     
     /**
      * The tag (inclusve), after that the insertion should start.
      */
     HTML.Tag insertTag;
     
     /**
      * This variable becomes true after the insert tag has been encountered.
      */
     boolean insertTagEncountered;
 
     
     /** A temporary variable that helps with the printing out of debug information **/
     boolean debug = false;
-    
+
+    /**
+     * This is true when we are inside a pre tag.
+     */
+    boolean inPreTag = false;
+
     void print (String line)
     {
       if (debug)
         System.out.println (line);
     }
     
     public class TagAction
     {
       /**
        * This method is called when a start tag is seen for one of the types
        * of tags associated with this Action.  By default this does nothing.
        */
       public void start(HTML.Tag t, MutableAttributeSet a)
       {
         // Nothing to do here.
@@ -713,55 +718,57 @@
        */
       public void start(HTML.Tag t, MutableAttributeSet a)
       {
         blockOpen(t, a);
       }
       
       /**
        * Called when an end tag is seen for one of the types of tags associated
        * with this Action.
        */
       public void end(HTML.Tag t)
       {
         blockClose(t);
       } 
     }
-    
+
+    /**
+     * This action is performed when a &lt;pre&gt; tag is parsed.
+     */
     public class PreAction extends BlockAction
     {
       /**
        * This method is called when a start tag is seen for one of the types
        * of tags associated with this Action.
        */
       public void start(HTML.Tag t, MutableAttributeSet a)
-        throws NotImplementedException
       {
-        // FIXME: Implement.
-        print ("PreAction.start not implemented");
-        super.start(t, a);
+        inPreTag = true;
+        blockOpen(t, a);
+        a.addAttribute(CSS.Attribute.WHITE_SPACE, "pre");
+        blockOpen(HTML.Tag.IMPLIED, a);
       }
       
       /**
        * Called when an end tag is seen for one of the types of tags associated
        * with this Action.
        */
       public void end(HTML.Tag t)
-        throws NotImplementedException
       {
-        // FIXME: Implement.
-        print ("PreAction.end not implemented");
-        super.end(t);
+        blockClose(HTML.Tag.IMPLIED);
+        inPreTag = false;
+        blockClose(t);
       } 
     }
     
     /**
      * Inserts the elements that are represented by ths single tag with 
      * attributes (only). The closing tag, even if present, mut follow
      * immediately after the starting tag without providing any additional
      * information. Hence the [EMAIL PROTECTED] TagAction#end} method need not be
      * overridden and still does nothing.
      */
     public class SpecialAction extends TagAction
     {
       /**
        * The functionality is delegated to [EMAIL PROTECTED] HTMLReader#addSpecialElement}
        */
@@ -1170,31 +1177,37 @@
         insert(offset, elements);
 
       offset += HTMLDocument.this.getLength() - offset;
     }
     
     /**
      * This method is called by the parser to indicate a block of 
      * text was encountered.  Should insert the text appropriately.
      * 
      * @param data the text that was inserted
      * @param pos the position at which the text was inserted
      */
     public void handleText(char[] data, int pos)
     {
       if (shouldInsert() && data != null && data.length > 0)
-        addContent(data, 0, data.length);
+        {
+          if (inPreTag)
+            preContent(data);
+          else
+            addContent(data, 0, data.length);
+            
+        }
     }
     
     /**
      * Checks if the HTML tag should be inserted. The tags before insert tag (if
      * specified) are not inserted. Also, the tags after the end of the html are
      * not inserted.
      * 
      * @return true if the tag should be inserted, false otherwise.
      */
     private boolean shouldInsert()
     {
       return ! endHTMLEncountered
              && (insertTagEncountered || insertTag == null);
     }
     
@@ -1301,38 +1314,54 @@
     /**
      * Adds the given text to the textarea document.  Called only when we are
      * within a textarea.  
      * 
      * @param data the text to add to the textarea
      */
     protected void textAreaContent(char[] data)
       throws NotImplementedException
     {
       // FIXME: Implement.
       print ("HTMLReader.textAreaContent not implemented yet");
     }
     
     /**
      * Adds the given text that was encountered in a <PRE> element.
-     * 
+     * This adds synthesized lines to hold the text runs.
+     *
      * @param data the text
      */
     protected void preContent(char[] data)
-      throws NotImplementedException
     {
-      // FIXME: Implement
-      print ("HTMLReader.preContent not implemented yet");
+      int start = 0;
+      for (int i = 0; i < data.length; i++)
+        {
+          if (data[i] == '\n')
+            {
+              addContent(data, start, i - start + 1);
+              blockClose(HTML.Tag.IMPLIED);
+              MutableAttributeSet atts = new SimpleAttributeSet();
+              atts.addAttribute(CSS.Attribute.WHITE_SPACE, "pre");
+              blockOpen(HTML.Tag.IMPLIED, atts);
+              start = i + 1;
+            }
+        }
+      if (start < data.length)
+        {
+          // Add remaining last line.
+          addContent(data, start, data.length - start);
+        }
     }
     
     /**
      * Instructs the parse buffer to create a block element with the given 
      * attributes.
      * 
      * @param t the tag that requires opening a new block
      * @param attr the attribute set for the new block
      */
     protected void blockOpen(HTML.Tag t, MutableAttributeSet attr)
     {
       printBuffer();
       DefaultStyledDocument.ElementSpec element;
 
       parseStack.push(t);

Reply via email to