blogentryhandler.java

### File Source: blogentryhandler.java

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

SofCheck Inspector Build Version : 2.22510

blogentryhandler.java | 2010-Jun-25 19:40:32
blogentryhandler.class | 2010-Jul-19 20:23:40