Clover Coverage Report - Pebble 2.5-SNAPSHOT
Coverage timestamp: Sat Jun 12 2010 09:39:29 EST
../../../../../img/srcFileCovDistChart8.png 29% of files have more coverage
137   293   31   45,67
54   194   0,23   3
3     10,33  
1    
This report was generated with an evaluation server license. Purchase Clover or configure your license.
 
  MovableTypeImporter       Line # 62 137 0% 31 43 77,8% 0.77835053
 
  (16)
 
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    */
 
62    public class MovableTypeImporter {
63   
64    /**
65    * Starts the importer.
66    */
 
67  16 toggle public static void main(String[] args) throws Exception {
68  16 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  16 File file = new File(args[0]);
78  16 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  16 DAOFactory.setConfiguredFactory(new FileDAOFactory());
87  16 Blog blog = new Blog(args[1]);
88  16 blog.setProperty(Blog.TIMEZONE_KEY, args[2]);
89   
90  16 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  16 toggle private static void importBlog(Blog blog, File file) throws Exception {
101  16 System.out.println("Importing " + file.getName());
102   
103  16 BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file),"UTF8"));
104  16 BlogEntry blogEntry = null;
105  16 do {
106  32 blogEntry = readBlogEntry(blog, reader);
107  32 } while (blogEntry != null);
108   
109  16 System.out.println(" " + blog.getNumberOfBlogEntries());
110    }
111   
 
112  32 toggle private static BlogEntry readBlogEntry(Blog blog, BufferedReader reader) throws Exception {
113  32 String line = reader.readLine();
114  32 if (line == null) {
115  16 return null;
116    }
117  16 SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a", Locale.ENGLISH);
118  16 String author = line.substring("AUTHOR: ".length());
119    // System.out.println("Author:" + author);
120  16 String title = reader.readLine().substring("TITLE: ".length());
121    // System.out.println("Title:" + title);
122  16 String status = reader.readLine().substring("STATUS: ".length());
123    // System.out.println("Status:" + status);
124  16 String allowComments = reader.readLine().substring("ALLOW COMMENTS: ".length());
125    // System.out.println("Allow comments:" + allowComments);
126  16 String convertBreaks = reader.readLine().substring("CONVERT BREAKS: ".length());
127    // System.out.println("Convert breaks:" + convertBreaks);
128  16 String allowPings = reader.readLine().substring("ALLOW PINGS: ".length());
129    // System.out.println("Allow pings:" + allowPings);
130    // String primaryCategory = "";
131  16 List<String> categories = new ArrayList<String>(1);
132  16 line = reader.readLine();
133    // System.out.println("Primary category:" + primaryCategory);
134  16 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  14 line = reader.readLine();
140  30 while(line.length() > 0){
141  16 if(line.indexOf("CATEGORY: ") != -1){
142  16 String category = line.substring(line.indexOf("CATEGORY: ")+10);
143  16 if(category.trim().length() > 0){
144  16 categories.add(category);
145    }
146    // categories.add(line.substring(line.indexOf("CATEGORY: ")+10));
147    }
148  16 line = reader.readLine();
149    }
150    // System.out.println("Category:" + category);
151    // }else{
152    // reader.readLine(); // blank line
153    // }
154    }
155  16 Date date = sdf.parse(reader.readLine().substring("DATE: ".length()));
156    // System.out.println("Date:" + date);
157   
158  16 reader.readLine();
159  16 reader.readLine();
160  16 StringBuffer body = new StringBuffer();
161  16 String bodyLine = reader.readLine();
162  32 while (!bodyLine.equals("-----")) {
163  16 body.append(bodyLine);
164  16 bodyLine = reader.readLine();
165  16 if (!bodyLine.equals("-----")) {
166  0 body.append("<br />");
167    }
168    }
169    // System.out.println("Body:" + body);
170   
171  16 reader.readLine();
172  16 StringBuffer extendedBody = new StringBuffer();
173  16 String extendedBodyLine = reader.readLine();
174  32 while (!extendedBodyLine.equals("-----")) {
175  16 extendedBody.append(extendedBodyLine);
176  16 extendedBodyLine = reader.readLine();
177  16 if (!extendedBodyLine.equals("-----")) {
178  0 extendedBody.append("<br />");
179    }
180    }
181    // System.out.println("Extended body:" + extendedBody);
182   
183  16 reader.readLine();
184  16 StringBuffer excerpt = new StringBuffer();
185  16 String excerptLine = reader.readLine();
186  32 while (!excerptLine.equals("-----")) {
187  16 excerpt.append(excerptLine);
188  16 excerptLine = reader.readLine();
189  16 if (!excerptLine.equals("-----")) {
190  0 excerpt.append("<br />");
191    }
192    }
193    // System.out.println("Excerpt:" + excerpt);
194   
195  16 reader.readLine();
196  16 StringBuffer keywords = new StringBuffer();
197  16 String keywordsLine = reader.readLine();
198  32 while (!keywordsLine.equals("-----")) {
199  16 keywords.append(keywordsLine);
200  16 keywordsLine = reader.readLine();
201  16 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  16 BlogEntry entry = new BlogEntry(blog);
209  16 entry.setTitle(title);
210  16 if (extendedBody.length() != 0) {
211  14 entry.setBody(body+"<br />"+extendedBody);
212  14 entry.setExcerpt(body.toString());
213    }else{
214  2 entry.setBody(body.toString());
215    }
216  16 entry.setDate(date);
217  16 if (excerpt.length() != 0) {
218  12 entry.setExcerpt(excerpt.toString());
219  4 }else if(extendedBody.length() != 0){
220  2 entry.setExcerpt(body.toString());
221    }
222  16 entry.setAuthor(author);
223  16 entry.setCommentsEnabled(allowComments.equals("1"));
224  16 entry.setTrackBacksEnabled(allowPings.equals("1"));
225   
226  16 for (String categoryStr : categories) {
227  16 if(categoryStr != null && categoryStr.trim().length() > 0) {
228  16 Category category = new Category(categoryStr.trim(), categoryStr.trim());
229  16 DAOFactory factory = DAOFactory.getConfiguredFactory();
230  16 CategoryDAO dao = factory.getCategoryDAO();
231  16 dao.addCategory(category, blog);
232  16 blog.addCategory(category);
233  16 entry.addCategory(category);
234    }
235    }
236  16 entry.setPublished("Publish".equals(status));
237   
238  16 BlogService service = new BlogService();
239  16 service.putBlogEntry(entry);
240   
241  16 line = reader.readLine();
242  52 while (!line.equals("--------")) {
243  36 if (line.equals("COMMENT:")) {
244  4 String commentAuthor = reader.readLine().substring("AUTHOR: ".length());
245  4 String commentEmail = reader.readLine().substring("EMAIL: ".length());
246  4 String commentIpAddress = reader.readLine().substring("IP: ".length());
247  4 String commentUrl = reader.readLine().substring("URL: ".length());
248  4 Date commentDate = sdf.parse(reader.readLine().substring("DATE: ".length()));
249   
250  4 StringBuffer commentBody = new StringBuffer();
251  4 String commentBodyLine = reader.readLine();
252  8 while (!commentBodyLine.equals("-----")) {
253  4 commentBody.append(commentBodyLine);
254  4 commentBodyLine = reader.readLine();
255  4 if (!commentBodyLine.equals("-----")) {
256  0 commentBody.append("<br />");
257    }
258    }
259   
260  4 Comment comment = entry.createComment(null, commentBody.toString(), commentAuthor, commentEmail, commentUrl, commentIpAddress, commentDate, State.APPROVED);
261  4 entry.addComment(comment);
262  32 } 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  36 line = reader.readLine();
283    }
284   
285  16 service.putBlogEntry(entry);
286   
287    // System.out.println("--------------------------------------------------");
288  16 System.out.print(".");
289   
290  16 return entry;
291    }
292   
293    }