Coverage Report - net.sourceforge.pebble.event.blogentry.TidyListener
 
Classes in this File Line Coverage Branch Coverage Complexity
TidyListener
0%
0/34
0%
0/10
2.5
 
 1  
 /*
 2  
  * Copyright (c) 2003-2011, 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;
 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  0
 public class TidyListener extends BlogEntryListenerSupport {
 54  
 
 55  
   /** the log used by this class */
 56  0
   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  
    */
 63  
   public void blogEntryAdded(BlogEntryEvent event) {
 64  0
     tidy(event.getBlogEntry());
 65  0
   }
 66  
 
 67  
   /**
 68  
    * Called when a blog entry has been changed.
 69  
    *
 70  
    * @param event a BlogEntryEvent instance
 71  
    */
 72  
   public void blogEntryChanged(BlogEntryEvent event) {
 73  0
     List propertyChangeEvents = event.getPropertyChangeEvents();
 74  0
     Iterator it = propertyChangeEvents.iterator();
 75  0
     while (it.hasNext()) {
 76  0
       PropertyChangeEvent pce = (PropertyChangeEvent)it.next();
 77  0
       String property = pce.getPropertyName();
 78  0
       if (property.equals(BlogEntry.EXCERPT_PROPERTY) ||
 79  
           property.equals(BlogEntry.BODY_PROPERTY)) {
 80  0
         tidy(event.getBlogEntry());
 81  
       }
 82  0
     }
 83  0
   }
 84  
 
 85  
   private void tidy(BlogEntry blogEntry) {
 86  0
     blogEntry.setExcerpt(tidy(blogEntry.getExcerpt()));
 87  0
     blogEntry.setBody(tidy(blogEntry.getBody()));
 88  0
   }
 89  
 
 90  
   private String tidy(String s) {
 91  0
     if (s != null && s.length() > 0) {
 92  0
       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  0
       ByteArrayInputStream in = new ByteArrayInputStream(s.getBytes());
 95  0
       ByteArrayOutputStream out = new ByteArrayOutputStream();
 96  0
       Tidy tidy = new Tidy();
 97  0
       tidy.setXHTML(true);
 98  0
       tidy.setDocType("\"-//W3C//DTD XHTML 1.0 Transitional//EN\"");
 99  0
       tidy.setQuiet(true);
 100  0
       tidy.setShowWarnings(false);
 101  0
       tidy.setIndentContent(false);
 102  0
       tidy.setSmartIndent(false);
 103  0
       tidy.setIndentAttributes(false);
 104  0
       tidy.setWraplen(0);
 105  0
       Document doc = tidy.parseDOM(in, null);
 106  0
       tidy.pprint(doc, out);
 107  
 
 108  0
       String tidied = new String(out.toByteArray());
 109  0
       return tidied.substring(tidied.indexOf("<body>")+"<body>".length(), tidied.indexOf("</body>")).trim();
 110  
     } else {
 111  0
       return "";
 112  
     }
 113  
   }
 114  
 
 115  
 }