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