Coverage Report - net.sourceforge.pebble.dao.file.BlogEntryHandler
 
Classes in this File Line Coverage Branch Coverage Complexity
BlogEntryHandler
93%
205/220
71%
110/154
10.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  
 
 33  
 package net.sourceforge.pebble.dao.file;
 34  
 
 35  
 import net.sourceforge.pebble.domain.*;
 36  
 import org.apache.commons.logging.Log;
 37  
 import org.apache.commons.logging.LogFactory;
 38  
 import org.xml.sax.Attributes;
 39  
 import org.xml.sax.SAXException;
 40  
 import org.xml.sax.SAXParseException;
 41  
 import org.xml.sax.helpers.DefaultHandler;
 42  
 
 43  
 import java.text.ParseException;
 44  
 import java.text.SimpleDateFormat;
 45  
 import java.util.Date;
 46  
 import java.util.Locale;
 47  
 
 48  
 public class BlogEntryHandler extends DefaultHandler {
 49  
 
 50  
   /** the log used by this class */
 51  4
   private static Log log = LogFactory.getLog(BlogEntryHandler.class);
 52  
 
 53  
   private static final int NOT_DEFINED = -1;
 54  
   private static final int TITLE = 0;
 55  
   private static final int EXCERPT = 1;
 56  
   private static final int BODY = 2;
 57  
   private static final int DATE = 3;
 58  
   private static final int AUTHOR = 4;
 59  
   private static final int ORIGINAL_PERMALINK = 5;
 60  
   private static final int STATIC_NAME = 6;
 61  
   private static final int CATEGORY = 7;
 62  
   private static final int COMMENTS_ENABLED = 8;
 63  
   private static final int TRACKBACKS_ENABLED = 9;
 64  
   private static final int EMAIL = 10;
 65  
   private static final int WEBSITE = 11;
 66  
   private static final int BLOG_NAME = 12;
 67  
   private static final int URL = 13;
 68  
   private static final int PARENT = 14;
 69  
   private static final int IP_ADDRESS = 15;
 70  
   private static final int SIZE = 16;
 71  
   private static final int TYPE = 17;
 72  
   private static final int STATE = 18;
 73  
   private static final int TAGS = 19;
 74  
   private static final int SUBTITLE = 20;
 75  
   private static final int TIME_ZONE = 21;
 76  
   private static final int AUTHENTICATED = 22;
 77  
   private static final int AVATAR = 23;
 78  
 
 79  
   private static final int IN_BLOG_ENTRY = 100;
 80  
   private static final int IN_COMMENT = 101;
 81  
   private static final int IN_TRACKBACK = 102;
 82  
   private static final int IN_ATTACHMENT = 103;
 83  
 
 84  
   private BlogEntry blogEntry;
 85  108
   private int groupStatus = IN_BLOG_ENTRY;
 86  108
   private int elementStatus = NOT_DEFINED;
 87  
   private SimpleDateFormat dateTimeFormats[];
 88  
 
 89  
   private StringBuffer elementContent;
 90  
 
 91  
   private String attachmentUrl;
 92  
   private String attachmentSize;
 93  
   private String attachmentType;
 94  
 
 95  
   private String commentTitle;
 96  
   private String commentBody;
 97  
   private String commentAuthor;
 98  
   private String commentWebsite;
 99  
   private String commentAvatar;
 100  
   private String commentIpAddress;
 101  
   private String commentEmail;
 102  
   private Date commentDate;
 103  108
   private long commentParent = -1;
 104  108
   private State commentState = State.APPROVED;
 105  108
   private boolean commentAuthenticated = false;
 106  
 
 107  
   private String trackBackTitle;
 108  
   private String trackBackExcerpt;
 109  
   private String trackBackBlogName;
 110  
   private String trackBackUrl;
 111  
   private String trackBackIpAddress;
 112  
   private Date trackBackDate;
 113  108
   private State trackBackState = State.APPROVED;
 114  
 
 115  108
   public BlogEntryHandler(BlogEntry blogEntry) {
 116  108
     this.blogEntry = blogEntry;
 117  
 
 118  
     // create all date/time formats, for backwards compatibility
 119  
     SimpleDateFormat format;
 120  108
     dateTimeFormats = new SimpleDateFormat[6];
 121  
 
 122  108
     format = new SimpleDateFormat(FileBlogEntryDAO.NEW_PERSISTENT_DATETIME_FORMAT, Locale.ENGLISH);
 123  108
     format.setTimeZone(blogEntry.getBlog().getTimeZone());
 124  108
     dateTimeFormats[0] = format;
 125  
 
 126  108
     format = new SimpleDateFormat(FileBlogEntryDAO.NEW_PERSISTENT_DATETIME_FORMAT, blogEntry.getBlog().getLocale());
 127  108
     format.setTimeZone(blogEntry.getBlog().getTimeZone());
 128  108
     dateTimeFormats[1] = format;
 129  
 
 130  108
     format = new SimpleDateFormat(FileBlogEntryDAO.NEW_PERSISTENT_DATETIME_FORMAT);
 131  108
     format.setTimeZone(blogEntry.getBlog().getTimeZone());
 132  108
     dateTimeFormats[2] = format;
 133  
 
 134  108
     format = new SimpleDateFormat(FileBlogEntryDAO.OLD_PERSISTENT_DATETIME_FORMAT, Locale.ENGLISH);
 135  108
     format.setTimeZone(blogEntry.getBlog().getTimeZone());
 136  108
     dateTimeFormats[3] = format;
 137  
 
 138  108
     format = new SimpleDateFormat(FileBlogEntryDAO.OLD_PERSISTENT_DATETIME_FORMAT, blogEntry.getBlog().getLocale());
 139  108
     format.setTimeZone(blogEntry.getBlog().getTimeZone());
 140  108
     dateTimeFormats[4] = format;
 141  
 
 142  108
     format = new SimpleDateFormat(FileBlogEntryDAO.OLD_PERSISTENT_DATETIME_FORMAT);
 143  108
     format.setTimeZone(blogEntry.getBlog().getTimeZone());
 144  108
     dateTimeFormats[5] = format;
 145  108
   }
 146  
 
 147  
   public void startElement(String uri, String name, String qName, Attributes attributes) throws SAXException {
 148  
     //log.info("startElement : " + name);
 149  1920
     elementContent = new StringBuffer();
 150  1920
     if (name.equals("title")) {
 151  144
       elementStatus = TITLE;
 152  1776
     } else if (name.equals("subtitle")) {
 153  108
       elementStatus = SUBTITLE;
 154  1668
     } else if (name.equals("excerpt")) {
 155  116
       elementStatus = EXCERPT;
 156  1552
     } else if (name.equals("body")) {
 157  140
       elementStatus = BODY;
 158  1412
     } else if (name.equals("date")) {
 159  148
       elementStatus = DATE;
 160  1264
     } else if (name.equals("timeZone")) {
 161  108
       elementStatus = TIME_ZONE;
 162  1156
     } else if (name.equals("author")) {
 163  140
       elementStatus = AUTHOR;
 164  1016
     } else if (name.equals("originalPermalink")) {
 165  0
       elementStatus = ORIGINAL_PERMALINK;
 166  1016
     } else if (name.equals("staticName")) {
 167  104
       elementStatus = STATIC_NAME;
 168  912
     } else if (name.equals("category")) {
 169  112
       elementStatus = CATEGORY;
 170  800
     } else if (name.equals("tags")) {
 171  108
       elementStatus = TAGS;
 172  692
     } else if (name.equals("commentsEnabled")) {
 173  108
       elementStatus = COMMENTS_ENABLED;
 174  584
     } else if (name.equals("trackBacksEnabled")) {
 175  108
       elementStatus = TRACKBACKS_ENABLED;
 176  476
     } else if (name.equals("email")) {
 177  32
       elementStatus = EMAIL;
 178  444
     } else if (name.equals("website")) {
 179  32
       elementStatus = WEBSITE;
 180  412
     } else if (name.equals("avatar")) {
 181  24
       elementStatus = AVATAR;
 182  388
     } else if (name.equals("ipAddress")) {
 183  40
       elementStatus = IP_ADDRESS;
 184  348
     } else if (name.equals("authenticated")) {
 185  28
       elementStatus = AUTHENTICATED;
 186  320
     } else if (name.equals("blogName")) {
 187  8
       elementStatus = BLOG_NAME;
 188  312
     } else if (name.equals("url")) {
 189  12
       elementStatus = URL;
 190  300
     } else if (name.equals("parent")) {
 191  0
       elementStatus = PARENT;
 192  300
     } else if (name.equals("state")) {
 193  140
       elementStatus = STATE;
 194  160
     } else if (name.equals("size")) {
 195  4
       elementStatus = SIZE;
 196  156
     } else if (name.equals("type")) {
 197  4
       elementStatus = TYPE;
 198  152
     } else if (name.equals("attachment")) {
 199  4
       groupStatus = IN_ATTACHMENT;
 200  4
       elementStatus = NOT_DEFINED;
 201  148
     } else if (name.equals("comment")) {
 202  32
       groupStatus = IN_COMMENT;
 203  32
       elementStatus = NOT_DEFINED;
 204  116
     } else if (name.equals("trackback")) {
 205  8
       groupStatus = IN_TRACKBACK;
 206  8
       elementStatus = NOT_DEFINED;
 207  
     } else {
 208  108
       elementStatus = NOT_DEFINED;
 209  
     }
 210  1920
   }
 211  
 
 212  
   public void endElement(String uri, String name, String qName) throws SAXException {
 213  
     //log.info("endElement : " + name);
 214  1920
     if (groupStatus == IN_BLOG_ENTRY) {
 215  1512
       switch (elementStatus) {
 216  
         case TITLE :
 217  108
           blogEntry.setTitle(elementContent.toString());
 218  108
           break;
 219  
         case SUBTITLE :
 220  108
           blogEntry.setSubtitle(elementContent.toString());
 221  108
           break;
 222  
         case EXCERPT :
 223  108
           blogEntry.setExcerpt(elementContent.toString());
 224  108
           break;
 225  
         case BODY :
 226  108
           blogEntry.setBody(elementContent.toString());
 227  108
           break;
 228  
         case DATE :
 229  108
           blogEntry.setDate(getDate(elementContent.toString()));
 230  108
           break;
 231  
         case TIME_ZONE :
 232  108
           blogEntry.setTimeZoneId(elementContent.toString());
 233  108
           break;
 234  
         case STATE :
 235  108
           if (elementContent.toString().equals(State.UNPUBLISHED.getName())) {
 236  8
             blogEntry.setPublished(false);
 237  
           } else {
 238  100
             blogEntry.setPublished(true);
 239  
           }
 240  100
           break;
 241  
         case AUTHOR :
 242  108
           blogEntry.setAuthor(elementContent.toString());
 243  108
           break;
 244  
         case ORIGINAL_PERMALINK :
 245  0
           blogEntry.setOriginalPermalink(elementContent.toString());
 246  0
           break;
 247  
         case CATEGORY :
 248  112
           blogEntry.addCategory(blogEntry.getBlog().getCategory(elementContent.toString()));
 249  112
           break;
 250  
         case TAGS :
 251  108
           blogEntry.setTags(elementContent.toString());
 252  108
           break;
 253  
         case COMMENTS_ENABLED :
 254  108
           blogEntry.setCommentsEnabled(Boolean.valueOf(elementContent.toString()).booleanValue());
 255  108
           break;
 256  
         case TRACKBACKS_ENABLED :
 257  108
           blogEntry.setTrackBacksEnabled(Boolean.valueOf(elementContent.toString()).booleanValue());
 258  1512
           break;
 259  
       }
 260  408
     } else if (groupStatus == IN_ATTACHMENT && name.equals("attachment")) {
 261  4
       Attachment attachment = new Attachment();
 262  4
       attachment.setUrl(attachmentUrl);
 263  4
       attachment.setSize(Long.parseLong(attachmentSize));
 264  4
       attachment.setType(attachmentType);
 265  4
       blogEntry.setAttachment(attachment);
 266  4
       groupStatus = IN_BLOG_ENTRY;
 267  
 
 268  4
       attachmentUrl = null;
 269  4
       attachmentSize = null;
 270  4
       attachmentType = null;
 271  4
     } else if (groupStatus == IN_ATTACHMENT) {
 272  12
       switch (elementStatus) {
 273  
         case URL :
 274  4
           attachmentUrl = elementContent.toString();
 275  4
           break;
 276  
         case SIZE :
 277  4
           attachmentSize = elementContent.toString();
 278  4
           break;
 279  
         case TYPE :
 280  4
           attachmentType = elementContent.toString();
 281  12
           break;
 282  
       }
 283  392
     } else if (groupStatus == IN_COMMENT && name.equals("comment")) {
 284  32
       Comment comment = blogEntry.createComment(commentTitle, commentBody, commentAuthor, commentEmail, commentWebsite, commentAvatar, commentIpAddress, commentDate, commentState);
 285  32
       if (commentParent != -1) {
 286  0
         comment.setParent(blogEntry.getComment(commentParent));
 287  
       }
 288  32
       comment.setAuthenticated(commentAuthenticated);
 289  32
       blogEntry.addComment(comment);
 290  32
       groupStatus = IN_BLOG_ENTRY;
 291  
 
 292  
       // and blank all the comment variables
 293  32
       commentTitle = null;
 294  32
       commentBody = null;
 295  32
       commentAuthor = null;
 296  32
       commentWebsite = null;
 297  32
       commentAvatar = null;
 298  32
       commentIpAddress = null;
 299  32
       commentEmail = null;
 300  32
       commentDate = null;
 301  32
       commentParent = -1;
 302  32
       commentState = State.APPROVED;
 303  32
       commentAuthenticated = false;
 304  32
     } else if (groupStatus == IN_COMMENT) {
 305  300
       switch (elementStatus) {
 306  
         case TITLE :
 307  28
           commentTitle = elementContent.toString();
 308  28
           break;
 309  
         case BODY :
 310  32
           commentBody = elementContent.toString();
 311  32
           break;
 312  
         case DATE :
 313  32
           commentDate = getDate(elementContent.toString());
 314  32
           break;
 315  
         case AUTHOR :
 316  32
           commentAuthor = elementContent.toString();
 317  32
           break;
 318  
         case EMAIL :
 319  32
           commentEmail = elementContent.toString();
 320  32
           break;
 321  
         case WEBSITE :
 322  32
           commentWebsite = elementContent.toString();
 323  32
           break;
 324  
         case AVATAR :
 325  24
           commentAvatar = elementContent.toString();
 326  24
           break;
 327  
         case IP_ADDRESS :
 328  32
           commentIpAddress = elementContent.toString();
 329  32
           break;
 330  
         case PARENT :
 331  0
           commentParent = Long.parseLong(elementContent.toString());
 332  0
           break;
 333  
         case STATE :
 334  28
           commentState = State.getState(elementContent.toString());
 335  28
           break;
 336  
         case AUTHENTICATED :
 337  28
           commentAuthenticated = Boolean.parseBoolean(elementContent.toString());
 338  300
           break;
 339  
       }
 340  60
     } else if (groupStatus == IN_TRACKBACK && name.equals("trackback")) {
 341  8
       TrackBack trackBack = blogEntry.createTrackBack(trackBackTitle, trackBackExcerpt, trackBackUrl, trackBackBlogName, trackBackIpAddress, trackBackDate, trackBackState);
 342  8
       blogEntry.addTrackBack(trackBack);
 343  8
       groupStatus = IN_BLOG_ENTRY;
 344  
 
 345  
       // and blank all the TrackBack variables
 346  8
       trackBackTitle = null;
 347  8
       trackBackExcerpt = null;
 348  8
       trackBackBlogName = null;
 349  8
       trackBackUrl = null;
 350  8
       trackBackIpAddress = null;
 351  8
       trackBackDate = null;
 352  8
       trackBackState = State.APPROVED;
 353  8
     } else if (groupStatus == IN_TRACKBACK) {
 354  52
       switch (elementStatus) {
 355  
         case TITLE :
 356  8
           trackBackTitle = elementContent.toString();
 357  8
           break;
 358  
         case EXCERPT :
 359  8
           trackBackExcerpt = elementContent.toString();
 360  8
           break;
 361  
         case DATE :
 362  8
           trackBackDate = getDate(elementContent.toString());
 363  8
           break;
 364  
         case BLOG_NAME :
 365  8
           trackBackBlogName = elementContent.toString();
 366  8
           break;
 367  
         case URL :
 368  8
           trackBackUrl = elementContent.toString();
 369  8
           break;
 370  
         case IP_ADDRESS :
 371  8
           trackBackIpAddress = elementContent.toString();
 372  8
           break;
 373  
         case STATE :
 374  4
           trackBackState = State.getState(elementContent.toString());
 375  
           break;
 376  
       }
 377  
     }
 378  
 
 379  1920
     elementStatus = NOT_DEFINED;
 380  1920
   }
 381  
 
 382  
   public void characters(char ch[], int start, int length) throws SAXException {
 383  3328
     elementContent.append(new String(ch, start, length));
 384  
     //log.info("characters : " + s);
 385  3328
   }
 386  
 
 387  
   public void warning(SAXParseException e) throws SAXException {
 388  0
     log.warn("Exception encountered", e);
 389  0
   }
 390  
 
 391  
   public void error(SAXParseException e) throws SAXException {
 392  0
     log.error("Exception encountered", e);
 393  0
   }
 394  
 
 395  
   public void fatalError(SAXParseException e) throws SAXException {
 396  0
     log.fatal("Exception encountered", e);
 397  0
   }
 398  
 
 399  
   private Date getDate(String s) {
 400  208
     for (int i = 0; i < dateTimeFormats.length; i++) {
 401  
       try {
 402  208
         return dateTimeFormats[i].parse(s);
 403  60
       } catch (ParseException pe) {
 404  
       }
 405  
     }
 406  
 
 407  0
     log.error("Could not parse date of " + s);
 408  0
     return null;
 409  
   }
 410  
 
 411  
 }