| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
|
| 21 |
|
|
| 22 |
|
|
| 23 |
|
|
| 24 |
|
|
| 25 |
|
|
| 26 |
|
|
| 27 |
|
|
| 28 |
|
|
| 29 |
|
|
| 30 |
|
|
| 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 |
|
|
| 59 |
|
|
| 60 |
|
@author |
| 61 |
|
|
|
|
|
| 77,8% |
Uncovered Elements: 43 (194) |
Complexity: 31 |
Complexity Density: 0,23 |
|
| 62 |
|
public class MovableTypeImporter { |
| 63 |
|
|
| 64 |
|
|
| 65 |
|
|
| 66 |
|
|
|
|
|
| 45% |
Uncovered Elements: 11 (20) |
Complexity: 3 |
Complexity Density: 0,19 |
|
| 67 |
16
|
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 |
|
|
| 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 |
|
|
| 95 |
|
|
| 96 |
|
@param |
| 97 |
|
@param |
| 98 |
|
@throws |
| 99 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 2 |
Complexity Density: 0,33 |
|
| 100 |
16
|
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 |
|
|
|
|
|
| 80,4% |
Uncovered Elements: 32 (163) |
Complexity: 26 |
Complexity Density: 0,23 |
|
| 112 |
32
|
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 |
|
|
| 120 |
16
|
String title = reader.readLine().substring("TITLE: ".length()); |
| 121 |
|
|
| 122 |
16
|
String status = reader.readLine().substring("STATUS: ".length()); |
| 123 |
|
|
| 124 |
16
|
String allowComments = reader.readLine().substring("ALLOW COMMENTS: ".length()); |
| 125 |
|
|
| 126 |
16
|
String convertBreaks = reader.readLine().substring("CONVERT BREAKS: ".length()); |
| 127 |
|
|
| 128 |
16
|
String allowPings = reader.readLine().substring("ALLOW PINGS: ".length()); |
| 129 |
|
|
| 130 |
|
|
| 131 |
16
|
List<String> categories = new ArrayList<String>(1); |
| 132 |
16
|
line = reader.readLine(); |
| 133 |
|
|
| 134 |
16
|
if(line.length() != 0){ |
| 135 |
|
|
| 136 |
|
|
| 137 |
|
|
| 138 |
|
|
| 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 |
|
|
| 147 |
|
} |
| 148 |
16
|
line = reader.readLine(); |
| 149 |
|
} |
| 150 |
|
|
| 151 |
|
|
| 152 |
|
|
| 153 |
|
|
| 154 |
|
} |
| 155 |
16
|
Date date = sdf.parse(reader.readLine().substring("DATE: ".length())); |
| 156 |
|
|
| 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 |
|
|
| 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 |
|
|
| 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 |
|
|
| 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 |
|
|
| 206 |
|
|
| 207 |
|
|
| 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 |
|
|
| 288 |
16
|
System.out.print("."); |
| 289 |
|
|
| 290 |
16
|
return entry; |
| 291 |
|
} |
| 292 |
|
|
| 293 |
|
} |