File Source: tidylistener.java

         /* 
    P/P   *  Method: net.sourceforge.pebble.event.blogentry.TidyListener__static_init
          * 
          *  Postconditions:
          *    init'ed(log)
          */
     1  /*
     2   * Copyright (c) 2003-2006, Simon Brown
     3   * All rights reserved.
     4   *
     5   * Redistribution and use in source and binary forms, with or without
     6   * modification, are permitted provided that the following conditions are met:
     7   *
     8   *   - Redistributions of source code must retain the above copyright
     9   *     notice, this list of conditions and the following disclaimer.
    10   *
    11   *   - Redistributions in binary form must reproduce the above copyright
    12   *     notice, this list of conditions and the following disclaimer in
    13   *     the documentation and/or other materials provided with the
    14   *     distribution.
    15   *
    16   *   - Neither the name of Pebble nor the names of its contributors may
    17   *     be used to endorse or promote products derived from this software
    18   *     without specific prior written permission.
    19   *
    20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
    24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    30   * POSSIBILITY OF SUCH DAMAGE.
    31   */
    32  package net.sourceforge.pebble.event.blogentry;
    33  
    34  import net.sourceforge.pebble.domain.BlogEntry;
    35  import net.sourceforge.pebble.api.event.blogentry.BlogEntryEvent;
    36  import org.apache.commons.logging.Log;
    37  import org.apache.commons.logging.LogFactory;
    38  import org.w3c.dom.Document;
         /* 
    P/P   *  Method: void net.sourceforge.pebble.event.blogentry.TidyListener()
          */
    39  import org.w3c.tidy.Tidy;
    40  
    41  import java.beans.PropertyChangeEvent;
    42  import java.util.Iterator;
    43  import java.util.List;
    44  import java.io.ByteArrayInputStream;
    45  import java.io.ByteArrayOutputStream;
    46  
    47  /**
    48   * Runs W3C Tidy over the excerpt and body of blog entries so that
    49   * they are valid XHTML.
    50   *
    51   * @author Simon Brown
    52   */
    53  public class TidyListener extends BlogEntryListenerSupport {
    54  
    55    /** the log used by this class */
    56    private static final Log log = LogFactory.getLog(TidyListener.class);
    57  
    58    /**
    59     * Called when a blog entry has been added.
    60     *
    61     * @param event   a BlogEntryEvent instance
    62     */
           /* 
    P/P     *  Method: void blogEntryAdded(BlogEntryEvent)
            * 
            *  Preconditions:
            *    event != null
            * 
            *  Presumptions:
            *    getBlogEntry(...).propertyChangeSupport@64 != null
            *    net.sourceforge.pebble.api.event.blogentry.BlogEntryEvent:getSource(...)@91 != null
            */
    63    public void blogEntryAdded(BlogEntryEvent event) {
    64      tidy(event.getBlogEntry());
    65    }
    66  
    67    /**
    68     * Called when a blog entry has been changed.
    69     *
    70     * @param event a BlogEntryEvent instance
    71     */
           /* 
    P/P     *  Method: void blogEntryChanged(BlogEntryEvent)
            * 
            *  Preconditions:
            *    event != null
            *    event.propertyChangeEvents != null
            * 
            *  Presumptions:
            *    getBlogEntry(...).propertyChangeSupport@80 != null
            *    java.beans.PropertyChangeEvent:getPropertyName(...)@77 != null
            *    java.util.Iterator:next(...)@76 != null
            *    net.sourceforge.pebble.api.event.blogentry.BlogEntryEvent:getSource(...)@91 != null
            * 
            *  Test Vectors:
            *    java.lang.String:equals(...)@78: {1}, {0}
            *    java.lang.String:equals(...)@79: {0}, {1}
            *    java.util.Iterator:hasNext(...)@75: {1}, {0}
            */
    72    public void blogEntryChanged(BlogEntryEvent event) {
    73      List propertyChangeEvents = event.getPropertyChangeEvents();
    74      Iterator it = propertyChangeEvents.iterator();
    75      while (it.hasNext()) {
    76        PropertyChangeEvent pce = (PropertyChangeEvent)it.next();
    77        String property = pce.getPropertyName();
    78        if (property.equals(BlogEntry.EXCERPT_PROPERTY) ||
    79            property.equals(BlogEntry.BODY_PROPERTY)) {
    80          tidy(event.getBlogEntry());
    81        }
    82      }
    83    }
    84  
           /* 
    P/P     *  Method: void tidy(BlogEntry)
            * 
            *  Preconditions:
            *    blogEntry != null
            *    blogEntry.propertyChangeSupport != null
            *    init'ed(blogEntry.body)
            *    init'ed(blogEntry.excerpt)
            * 
            *  Postconditions:
            *    blogEntry.body != null
            *    init'ed(blogEntry.excerpt)
            */
    85    private void tidy(BlogEntry blogEntry) {
    86      blogEntry.setExcerpt(tidy(blogEntry.getExcerpt()));
    87      blogEntry.setBody(tidy(blogEntry.getBody()));
    88    }
    89  
    90    private String tidy(String s) {
    91      if (s != null && s.length() > 0) {
    92        s = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n" +
    93            "<html xmlns=\"http://www.w3.org/1999/xhtml\"><title></title><body>" + s + "</body></html>";
    94        ByteArrayInputStream in = new ByteArrayInputStream(s.getBytes());
    95        ByteArrayOutputStream out = new ByteArrayOutputStream();
               /* 
    P/P         *  Method: String tidy(String)
                * 
                *  Presumptions:
                *    java.lang.String:indexOf(...)@109 + java.lang.String:length(...)@109 in -231..232-1
                * 
                *  Postconditions:
                *    return_value != null
                * 
                *  Test Vectors:
                *    s: Addr_Set{null}, Inverse{null}
                *    java.lang.String:length(...)@91: {0}, {1..232-1}
                */
    96        Tidy tidy = new Tidy();
    97        tidy.setXHTML(true);
    98        tidy.setDocType("\"-//W3C//DTD XHTML 1.0 Transitional//EN\"");
    99        tidy.setQuiet(true);
   100        tidy.setShowWarnings(false);
   101        tidy.setIndentContent(false);
   102        tidy.setSmartIndent(false);
   103        tidy.setIndentAttributes(false);
   104        tidy.setWraplen(0);
   105        Document doc = tidy.parseDOM(in, null);
   106        tidy.pprint(doc, out);
   107  
   108        String tidied = new String(out.toByteArray());
   109        return tidied.substring(tidied.indexOf("<body>")+"<body>".length(), tidied.indexOf("</body>")).trim();
   110      } else {
   111        return "";
   112      }
   113    }
   114  
   115  }








SofCheck Inspector Build Version : 2.22510
tidylistener.java 2010-Jun-25 19:40:32
tidylistener.class 2010-Jul-19 20:23:38