Clover coverage report -
Coverage timestamp: Sun Apr 18 2004 21:32:30 EDT
file stats: LOC: 241   Methods: 8
NCLOC: 117   Classes: 2
 
 Source file Conditionals Statements Methods TOTAL
SHTMLEditorKit.java 50% 70.7% 75% 65.2%
coverage coverage
 1   
 /*
 2   
  * SimplyHTML, a word processor based on Java, HTML and CSS
 3   
  * Copyright (C) 2002 Ulrich Hilger
 4   
  *
 5   
  * This program is free software; you can redistribute it and/or
 6   
  * modify it under the terms of the GNU General Public License
 7   
  * as published by the Free Software Foundation; either version 2
 8   
  * of the License, or (at your option) any later version.
 9   
  *
 10   
  * This program is distributed in the hope that it will be useful,
 11   
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12   
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13   
  * GNU General Public License for more details.
 14   
  *
 15   
  * You should have received a copy of the GNU General Public License
 16   
  * along with this program; if not, write to the Free Software
 17   
  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 18   
  */
 19   
 
 20   
 import java.io.*;
 21   
 import javax.swing.text.*;
 22   
 import javax.swing.text.html.*;
 23   
 import java.util.prefs.*;
 24   
 
 25   
 /**
 26   
  * Extensions to <code>HTMLEditorKit</code> for application SimplyHTML.
 27   
  *
 28   
  * <p>In stage 1 this only re-implements how style sheets are handled by
 29   
  * default.</p>
 30   
  *
 31   
  * <p>Stage 3 adds functionality for usage of the custom HTML document
 32   
  * and HTML reader used with SimplyHTML from this stage on.</p>
 33   
  *
 34   
  * <p>With stage 9 some additional views have been added to
 35   
  * the view factory as a workaround for bug id 4765271
 36   
  * (see http://developer.java.sun.com/developer/bugParade/bugs/4765271.html).</p>
 37   
  *
 38   
  * <p>OK, I give up: With release 2 of stage 9 above views are used no longer and
 39   
  * bug fixing is not done anymore. The HTML support is almost taken as is in the hope
 40   
  * that Sun will enhance it some day. To do compensation inside a single application
 41   
  * is not possible with a reasonable effort.</p>
 42   
  *
 43   
  * @author Ulrich Hilger
 44   
  * @author Light Development
 45   
  * @author <a href="http://www.lightdev.com">http://www.lightdev.com</a>
 46   
  * @author <a href="mailto:info@lightdev.com">info@lightdev.com</a>
 47   
  * @author published under the terms and conditions of the
 48   
  *      GNU General Public License,
 49   
  *      for details see file gpl.txt in the distribution
 50   
  *      package of this software
 51   
  *
 52   
  * @version stage 11, April 27, 2003
 53   
  */
 54   
 
 55   
 public class SHTMLEditorKit extends HTMLEditorKit {
 56   
 
 57  159
   public SHTMLEditorKit() {
 58  159
     super();
 59  159
     try {
 60  159
       Preferences prefs = Preferences.userNodeForPackage(getClass().forName("PrefsDialog"));
 61  159
       this.renderMode = prefs.get(PrefsDialog.PREFSID_WRITE_MODE, PrefsDialog.PREFS_WRITE_MODE_HTML32);
 62   
     }
 63   
     catch(Exception e) {}
 64   
   }
 65   
 
 66  0
   public SHTMLEditorKit(String renderMode) {
 67  0
     this();
 68  0
     this.renderMode = renderMode;
 69   
   }
 70   
 
 71   
   /* --------------- SHTMLDocument implementation start ------------ */
 72   
 
 73   
   /**
 74   
    * Create an uninitialized text storage model
 75   
    * that is appropriate for this type of editor.
 76   
    *
 77   
    * @return the model
 78   
    */
 79  238
   public Document createDefaultDocument() {
 80  238
     StyleSheet styles = getStyleSheet();
 81  238
     StyleSheet ss = new StyleSheet();
 82  238
     try {
 83  238
       ss.importStyleSheet(getClass().forName("javax.swing.text.html.HTMLEditorKit").getResource(DEFAULT_CSS));
 84   
     }
 85   
     catch(Exception e) {}
 86  238
     SHTMLDocument doc = new SHTMLDocument(ss);
 87  238
     doc.setParser(getParser());
 88  238
     doc.setAsynchronousLoadPriority(4);
 89  238
     doc.setTokenThreshold(100);
 90  238
     return doc;
 91   
   }
 92   
 
 93   
   /**
 94   
    * Inserts content from the given stream. If <code>doc</code> is
 95   
    * an instance of HTMLDocument, this will read
 96   
    * HTML 3.2 text. Inserting HTML into a non-empty document must be inside
 97   
    * the body Element, if you do not insert into the body an exception will
 98   
    * be thrown. When inserting into a non-empty document all tags outside
 99   
    * of the body (head, title) will be dropped.
 100   
    *
 101   
    * @param in  the stream to read from
 102   
    * @param doc the destination for the insertion
 103   
    * @param pos the location in the document to place the
 104   
    *   content
 105   
    * @exception IOException on any I/O error
 106   
    * @exception BadLocationException if pos represents an invalid
 107   
    *   location within the document
 108   
    * @exception RuntimeException (will eventually be a BadLocationException)
 109   
    *            if pos is invalid
 110   
    */
 111  31
   public void read(Reader in, Document doc, int pos) throws IOException, BadLocationException {
 112  31
     if (doc instanceof SHTMLDocument) {
 113  31
       SHTMLDocument hdoc = (SHTMLDocument) doc;
 114  31
       Parser p = getParser();
 115  31
       if (p == null) {
 116  0
         throw new IOException("Can't load parser");
 117   
       }
 118  31
       if (pos > doc.getLength()) {
 119  0
         throw new BadLocationException("Invalid location", pos);
 120   
       }
 121  31
       ParserCallback receiver = hdoc.getReader(pos);
 122  31
       Boolean ignoreCharset = (Boolean)doc.getProperty("IgnoreCharsetDirective");
 123  31
       p.parse(in, receiver, (ignoreCharset == null) ? false : ignoreCharset.booleanValue());
 124  31
       receiver.flush();
 125   
     }
 126   
     else {
 127  0
       super.read(in, doc, pos);
 128   
     }
 129   
   }
 130   
 
 131   
   /**
 132   
    * Write content from a document to the given stream
 133   
    * in a format appropriate for this kind of content handler.
 134   
    *
 135   
    * @param out  the stream to write to
 136   
    * @param doc  the source for the write
 137   
    * @param pos  the location in the document to fetch the
 138   
    *   content
 139   
    * @param len  the amount to write out
 140   
    * @exception IOException on any I/O error
 141   
    * @exception BadLocationException if pos represents an invalid
 142   
    *   location within the document
 143   
    */
 144  24
   public void write(Writer out, Document doc, int pos, int len)
 145   
       throws IOException, BadLocationException {
 146   
 
 147  24
     if (doc instanceof SHTMLDocument) {
 148  24
       try {
 149  24
         Preferences prefs = Preferences.userNodeForPackage(getClass().forName(
 150   
             "PrefsDialog"));
 151  24
         String writeMode = prefs.get(PrefsDialog.PREFSID_WRITE_MODE,
 152   
                                      PrefsDialog.PREFS_WRITE_MODE_HTML32);
 153  24
         if (writeMode.equalsIgnoreCase(PrefsDialog.PREFS_WRITE_MODE_HTML4)) {
 154  0
           SHTMLWriter w = new SHTMLWriter(out, (SHTMLDocument) doc, pos, len);
 155  0
           w.write();
 156   
         }
 157   
         else {
 158  24
           FixedHTMLWriter w = new FixedHTMLWriter(out, (SHTMLDocument) doc, pos,
 159   
                                                   len);
 160  24
           w.write();
 161   
         }
 162   
       }
 163   
       catch(Exception e) {}
 164   
     }
 165  0
     else if (doc instanceof StyledDocument) {
 166  0
       MinimalHTMLWriter w = new MinimalHTMLWriter(out, (StyledDocument)doc, pos, len);
 167  0
       w.write();
 168   
     }
 169   
     else {
 170  0
       super.write(out, doc, pos, len);
 171   
     }
 172   
   }
 173   
 
 174   
   /* --------------- SHTMLDocument implementaion end --------------- */
 175   
 
 176   
   /* --------------- ViewFactory implementation start -------------- */
 177   
 
 178   
   /** Shared factory for creating HTML Views. */
 179   
   private static final ViewFactory defaultFactory = new SHTMLFactory();
 180   
 
 181   
   /**
 182   
    * Fetch a factory that is suitable for producing
 183   
    * views of any models that are produced by this
 184   
    * kit.
 185   
    *
 186   
    * @return the factory
 187   
    */
 188  2388
   public ViewFactory getViewFactory() {
 189  2389
     return defaultFactory;
 190   
   }
 191   
 
 192   
   public static class SHTMLFactory extends HTMLEditorKit.HTMLFactory
 193   
       implements ViewFactory
 194   
   {
 195  1845
     public View create(Element elem) {
 196  1845
       View view = null;
 197  1845
       Object o = elem.getAttributes().getAttribute(StyleConstants.NameAttribute);
 198  1845
       if (o instanceof HTML.Tag) {
 199  1845
         HTML.Tag kind = (HTML.Tag) o;
 200   
         //System.out.println("SHTMLEditorKit.SHTMLFactory o is HTML.Tag kind=" + kind.toString());
 201  1845
         if (kind == HTML.Tag.TABLE) {
 202  2
           if(renderMode.equalsIgnoreCase(PrefsDialog.PREFS_WRITE_MODE_HTML32)) {
 203  2
             view = super.create(elem);
 204   
           }
 205  0
           else if(renderMode.equalsIgnoreCase(PrefsDialog.PREFS_WRITE_MODE_HTML4)) {
 206  0
             view = new SHTMLTableView(elem);
 207   
           }
 208   
         }
 209  1843
         else if (kind == HTML.Tag.COMMENT) {
 210  0
           view = new InvisibleView(elem);
 211   
         }
 212  1843
         else if (kind instanceof HTML.UnknownTag) {
 213  0
           view = new InvisibleView(elem);
 214   
         }
 215   
         else {
 216  1843
           view = super.create(elem);
 217   
         }
 218   
       }
 219   
       else {
 220  0
         view = new LabelView(elem);
 221   
       }
 222  1845
       return view;
 223   
     }
 224   
   }
 225   
 
 226   
   /**
 227   
    * set the render mode
 228   
    *
 229   
    * <p>This influences how the ViewFactory creates view to render content</p>
 230   
    *
 231   
    * @param mode the mode, one of PrefsDialog.PREFS_WRITE_MODE_HTML32 or _HTML4
 232   
    */
 233  0
   public void setRenderMode(String mode) {
 234  0
     this.renderMode = mode;
 235   
   }
 236   
 
 237   
   private static String renderMode = PrefsDialog.PREFS_WRITE_MODE_HTML32;
 238   
 
 239   
   /* --------------- ViewFactory implementation end -------------- */
 240   
 
 241   
 }