| 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 | |
|
| 61 | |
|
| 62 | 0 | public class MovableTypeImporter { |
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 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 | |
|
| 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 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 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 | |
|
| 120 | 32 | String title = reader.readLine().substring("TITLE: ".length()); |
| 121 | |
|
| 122 | 32 | String status = reader.readLine().substring("STATUS: ".length()); |
| 123 | |
|
| 124 | 32 | String allowComments = reader.readLine().substring("ALLOW COMMENTS: ".length()); |
| 125 | |
|
| 126 | 32 | String convertBreaks = reader.readLine().substring("CONVERT BREAKS: ".length()); |
| 127 | |
|
| 128 | 32 | String allowPings = reader.readLine().substring("ALLOW PINGS: ".length()); |
| 129 | |
|
| 130 | |
|
| 131 | 32 | List<String> categories = new ArrayList<String>(1); |
| 132 | 32 | line = reader.readLine(); |
| 133 | |
|
| 134 | 32 | if(line.length() != 0){ |
| 135 | |
|
| 136 | |
|
| 137 | |
|
| 138 | |
|
| 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 | |
|
| 147 | |
} |
| 148 | 32 | line = reader.readLine(); |
| 149 | |
} |
| 150 | |
|
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
} |
| 155 | 32 | Date date = sdf.parse(reader.readLine().substring("DATE: ".length())); |
| 156 | |
|
| 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 | |
|
| 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 | |
|
| 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 | |
|
| 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 | |
|
| 206 | |
|
| 207 | |
|
| 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 | |
|
| 288 | 32 | System.out.print("."); |
| 289 | |
|
| 290 | 32 | return entry; |
| 291 | |
} |
| 292 | |
|
| 293 | |
} |