movabletypeimporter.java

### File Source: movabletypeimporter.java

             /*
        P/P   *  Method: net.sourceforge.pebble.util.importer.MovableTypeImporter__static_init
              */
         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.util.importer;
        33
        34  import net.sourceforge.pebble.Configuration;
        35  import net.sourceforge.pebble.PebbleContext;
        36  import net.sourceforge.pebble.dao.CategoryDAO;
        37  import net.sourceforge.pebble.dao.DAOFactory;
        38  import net.sourceforge.pebble.dao.file.FileDAOFactory;
        39  import net.sourceforge.pebble.domain.Blog;
        40  import net.sourceforge.pebble.domain.BlogEntry;
        41  import net.sourceforge.pebble.domain.BlogService;
        42  import net.sourceforge.pebble.domain.Category;
        43  import net.sourceforge.pebble.domain.Comment;
        44  import net.sourceforge.pebble.domain.State;
        45  import net.sourceforge.pebble.domain.TrackBack;
        46
        47  import java.io.BufferedReader;
        48  import java.io.File;
        49  import java.io.FileInputStream;
        50  import java.io.InputStreamReader;
        51  import java.text.SimpleDateFormat;
        52  import java.util.ArrayList;
        53  import java.util.Date;
        54  import java.util.List;
        55  import java.util.Locale;
        56
        57  /**
        58   * Simple utility to import posts Movable Type into Pebble.
        59   *
        60   * @author    Simon Brown
        61   */
             /*
        P/P   *  Method: void net.sourceforge.pebble.util.importer.MovableTypeImporter()
              */
        62  public class MovableTypeImporter {
        63
        64    /**
        65     * Starts the importer.
        66     */
        67    public static void main(String[] args) throws Exception {
                 /*
        P/P       *  Method: void main(String[])
                  *
                  *  Preconditions:
                  *    args != null
                  *    (soft) init'ed(args[0])
                  *    (soft) init'ed(args[1])
                  *    (soft) init'ed(args[2])
                  *
                  *  Presumptions:
                  *    java.lang.System.out != null
                  *
                  *  Preconditions:
                  *    (soft) init'ed(net.sourceforge.pebble.PebbleContext__static_init.new PebbleContext(PebbleContext__static_init#1).configuration)
                  *
                  *  Postconditions:
                  *    net.sourceforge.pebble.PebbleContext__static_init.new PebbleContext(PebbleContext__static_init#1).configuration == One-of{old net.sourceforge.pebble.PebbleContext__static_init.new PebbleContext(PebbleContext__static_init#1).configuration, &new Configuration(main#2)}
                  *    (soft) init'ed(net.sourceforge.pebble.PebbleContext__static_init.new PebbleContext(PebbleContext__static_init#1).configuration)
                  *    new Configuration(main#2) num objects <= 1
                  *
                  *  Test Vectors:
                  *    args.length: {3}, {0..2, 4..+Inf}
                  *    net.sourceforge.pebble.PebbleContext__static_init.new PebbleContext(PebbleContext__static_init#1).configuration: Inverse{null}, Addr_Set{null}
                  */
        68      if (args.length != 3) {
        69        System.out.println("Usage : net.sourceforge.pebble.util.importer.MovableTypeImporter %1 %2 %3");
        70        System.out.println("   %1 : location of MT export file");
        71        System.out.println("   %2 : location of Pebble blog");
        72        System.out.println("   %3 : time zone (e.g. Europe/London)");
        73
        74        return;
        75      }
        76
        77      File file = new File(args[0]);
        78      if(null == PebbleContext.getInstance().getConfiguration()){
        79        //to prevent NullPointerException upon UpdateNotificationPingsClient.sendUpdateNotificationPing()
        80        Configuration config = new Configuration();
        81        config.setDataDirectory(args[1]);
        82        config.setUrl("http://www.yourdomain.com/blog/");
        83        PebbleContext.getInstance().setConfiguration(config);
        84      }
        85
        86      DAOFactory.setConfiguredFactory(new FileDAOFactory());
        87      Blog blog = new Blog(args[1]);
        88      blog.setProperty(Blog.TIMEZONE_KEY, args[2]);
        89
        90      importBlog(blog, file);
        91    }
        92
        93    /**
        94     * Imports a Movable Type blog from an export file.
        95     *
        96     * @param blog    the SimpleBlo to import to
        97     * @param file    the Movable Type export file
        98     * @throws Exception  if something goes wrong
        99     */
       100    private static void importBlog(Blog blog, File file) throws Exception {
                 /*
        P/P       *  Method: void importBlog(Blog, File)
                  *
                  *  Preconditions:
                  *    file != null
                  *    (soft) blog != null
                  *
                  *  Presumptions:
                  *    java.lang.System.out != null
                  */
       101      System.out.println("Importing " + file.getName());
       102
       103      BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file),"UTF8"));
       104      BlogEntry blogEntry = null;
       105      do {
       106        blogEntry = readBlogEntry(blog, reader);
       107      } while (blogEntry != null);
       108
       109      System.out.println(" " + blog.getNumberOfBlogEntries());
       110    }
       111
       112    private static BlogEntry readBlogEntry(Blog blog, BufferedReader reader) throws Exception {
                 /*
        P/P       *  Method: BlogEntry readBlogEntry(Blog, BufferedReader)
                  *
                  *  Preconditions:
                  *    reader != null
                  *    (soft) blog != null
                  *
                  *  Presumptions:
                  *    java.io.BufferedReader:readLine(...)@120 != null
                  *    java.io.BufferedReader:readLine(...)@122 != null
                  *    java.io.BufferedReader:readLine(...)@124 != null
                  *    java.io.BufferedReader:readLine(...)@126 != null
                  *    java.io.BufferedReader:readLine(...)@128 != null
                  *    ...
                  *
                  *  Postconditions:
                  *    return_value in Addr_Set{null,&new BlogEntry(readBlogEntry#7)}
                  *    new BlogEntry(readBlogEntry#7) num objects <= 1
                  *
                  *  Test Vectors:
                  *    java.io.BufferedReader:readLine(...)@113: Inverse{null}, Addr_Set{null}
                  *    java.lang.String:equals(...)@162: {0}, {1}
                  *    java.lang.String:equals(...)@165: {1}, {0}
                  *    java.lang.String:equals(...)@174: {0}, {1}
                  *    java.lang.String:equals(...)@177: {1}, {0}
                  *    java.lang.String:equals(...)@186: {0}, {1}
                  *    java.lang.String:equals(...)@189: {1}, {0}
                  *    java.lang.String:equals(...)@198: {0}, {1}
                  *    java.lang.String:equals(...)@201: {1}, {0}
                  *    java.lang.String:equals(...)@242: {0}, {1}
                  *    ...
                  */
       113      String line = reader.readLine();
       114      if (line == null) {
       115        return null;
       116      }
       117      SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a", Locale.ENGLISH);
       118      String author = line.substring("AUTHOR: ".length());
       119  //    System.out.println("Author:" + author);
       120      String title = reader.readLine().substring("TITLE: ".length());
       121  //    System.out.println("Title:" + title);
       122      String status = reader.readLine().substring("STATUS: ".length());
       123  //    System.out.println("Status:" + status);
       124      String allowComments = reader.readLine().substring("ALLOW COMMENTS: ".length());
       125  //    System.out.println("Allow comments:" + allowComments);
       126      String convertBreaks = reader.readLine().substring("CONVERT BREAKS: ".length());
       127  //    System.out.println("Convert breaks:" + convertBreaks);
       128      String allowPings = reader.readLine().substring("ALLOW PINGS: ".length());
       129  //    System.out.println("Allow pings:" + allowPings);
       130  //    String primaryCategory = "";
       131      List<String> categories = new ArrayList<String>(1);
       132      line = reader.readLine();
       133      //    System.out.println("Primary category:" + primaryCategory);
       134      if(line.length() != 0){
       135        // the entry is categorized
       136  //      primaryCategory = line.substring("PRIMARY CATEGORY: ".length());
       137        //    System.out.println("Primary category:" + primaryCategory);
       138  //      if (primaryCategory.trim().length() > 0) {
       139          line = reader.readLine();
       140          while(line.length() > 0){
       141            if(line.indexOf("CATEGORY: ") != -1){
       142              String category = line.substring(line.indexOf("CATEGORY: ")+10);
       143              if(category.trim().length() > 0){
       144                categories.add(category);
       145              }
       146  //            categories.add(line.substring(line.indexOf("CATEGORY: ")+10));
       147            }
       148            line = reader.readLine();
       149          }
       150  //      System.out.println("Category:" + category);
       151  //      }else{
       152  //        reader.readLine(); // blank line
       153  //      }
       154      }
       155      Date date = sdf.parse(reader.readLine().substring("DATE: ".length()));
       156  //    System.out.println("Date:" + date);
       157
       158      reader.readLine();
       159      reader.readLine();
       160      StringBuffer body = new StringBuffer();
       161      String bodyLine = reader.readLine();
       162      while (!bodyLine.equals("-----")) {
       163        body.append(bodyLine);
       164        bodyLine = reader.readLine();
       165        if (!bodyLine.equals("-----")) {
       166          body.append("<br />");
       167        }
       168      }
       169  //    System.out.println("Body:" + body);
       170
       171      reader.readLine();
       172      StringBuffer extendedBody = new StringBuffer();
       173      String extendedBodyLine = reader.readLine();
       174      while (!extendedBodyLine.equals("-----")) {
       175        extendedBody.append(extendedBodyLine);
       176        extendedBodyLine = reader.readLine();
       177        if (!extendedBodyLine.equals("-----")) {
       178          extendedBody.append("<br />");
       179        }
       180      }
       181  //    System.out.println("Extended body:" + extendedBody);
       182
       183      reader.readLine();
       184      StringBuffer excerpt = new StringBuffer();
       185      String excerptLine = reader.readLine();
       186      while (!excerptLine.equals("-----")) {
       187        excerpt.append(excerptLine);
       188        excerptLine = reader.readLine();
       189        if (!excerptLine.equals("-----")) {
       190          excerpt.append("<br />");
       191        }
       192      }
       193  //    System.out.println("Excerpt:" + excerpt);
       194
       195      reader.readLine();
       196      StringBuffer keywords = new StringBuffer();
       197      String keywordsLine = reader.readLine();
       198      while (!keywordsLine.equals("-----")) {
       199        keywords.append(keywordsLine);
       200        keywordsLine = reader.readLine();
       201        if (!keywordsLine.equals("-----")) {
       202          keywords.append("<br />");
       203        }
       204      }
       205  //    System.out.println("Keywords:" + keywords);
       206
       207      // create a new Pebble entry, add and store
       208      BlogEntry entry = new BlogEntry(blog);
       209      entry.setTitle(title);
       210      if (extendedBody.length() != 0) {
       211        entry.setBody(body+"<br />"+extendedBody);
       212        entry.setExcerpt(body.toString());
       213      }else{
       214        entry.setBody(body.toString());
       215      }
       216      entry.setDate(date);
       217      if (excerpt.length() != 0) {
       218        entry.setExcerpt(excerpt.toString());
       219      }else if(extendedBody.length() != 0){
       220        entry.setExcerpt(body.toString());
       221      }
       222      entry.setAuthor(author);
       223      entry.setCommentsEnabled(allowComments.equals("1"));
       224      entry.setTrackBacksEnabled(allowPings.equals("1"));
       225
       226      for (String categoryStr : categories) {
       227        if(categoryStr != null && categoryStr.trim().length() > 0) {
       228          Category category = new Category(categoryStr.trim(), categoryStr.trim());
       229          DAOFactory factory = DAOFactory.getConfiguredFactory();
       230          CategoryDAO dao = factory.getCategoryDAO();
       231          dao.addCategory(category, blog);
       232          blog.addCategory(category);
       233          entry.addCategory(category);
       234        }
       235      }
       236      entry.setPublished("Publish".equals(status));
       237
       238      BlogService service = new BlogService();
       239      service.putBlogEntry(entry);
       240
       241      line = reader.readLine();
       242      while (!line.equals("--------")) {
       243        if (line.equals("COMMENT:")) {
       244          String commentAuthor = reader.readLine().substring("AUTHOR: ".length());
       245          String commentEmail = reader.readLine().substring("EMAIL: ".length());
       246          String commentIpAddress = reader.readLine().substring("IP: ".length());
       247          String commentUrl = reader.readLine().substring("URL: ".length());
       248          Date commentDate = sdf.parse(reader.readLine().substring("DATE: ".length()));
       249
       250          StringBuffer commentBody = new StringBuffer();
       251          String commentBodyLine = reader.readLine();
       252          while (!commentBodyLine.equals("-----")) {
       253            commentBody.append(commentBodyLine);
       254            commentBodyLine = reader.readLine();
       255            if (!commentBodyLine.equals("-----")) {
       256              commentBody.append("<br />");
       257            }
       258          }
       259
       260          Comment comment = entry.createComment(null, commentBody.toString(), commentAuthor, commentEmail, commentUrl, commentIpAddress, commentDate, State.APPROVED);
       261          entry.addComment(comment);
       262        } else if (line.equals("PING:")) {
       263          String pingTitle = reader.readLine().substring("TITLE: ".length());
       264          String pingUrl = reader.readLine().substring("URL: ".length());
       265          String pingIpAddress = reader.readLine().substring("IP: ".length());
       266          String pingBlogName = reader.readLine().substring("BLOG NAME: ".length());
       267          Date pingDate = sdf.parse(reader.readLine().substring("DATE: ".length()));
       268
       269          StringBuffer pingBody = new StringBuffer();
       270          String pingBodyLing = reader.readLine();
       271          while (!pingBodyLing.equals("-----")) {
       272            pingBody.append(pingBodyLing);
       273            pingBodyLing = reader.readLine();
       274            if (!pingBodyLing.equals("-----")) {
       275              pingBody.append("<br />");
       276            }
       277          }
       278
       279          TrackBack trackBack = entry.createTrackBack(pingTitle, pingBody.toString(), pingUrl, pingBlogName, pingIpAddress, pingDate, State.APPROVED);
       280          entry.addTrackBack(trackBack);
       281        }
       282        line = reader.readLine();
       283      }
       284
       285      service.putBlogEntry(entry);
       286
       287  //    System.out.println("--------------------------------------------------");
       288      System.out.print(".");
       289
       290      return entry;
       291    }
       292
       293  }

SofCheck Inspector Build Version : 2.22510

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