1 /*2 * Created on Apr 2, 20043 */4 package org.roller.presentation;5 6 7 /**8 * TODO: revisit this class once Atom 1.0 support comes to Rome9 * @author lance.lavandowska10 */11 public class ArchiveParser12 {13 // protected static final Log mLogger = 14 // LogFactory.getFactory().getInstance(ArchiveParser.class);15 // 16 // private Roller roller;17 // private WebsiteData website;18 // private File archiveFile;19 //20 // private Timestamp current;21 //22 // private WeblogCategoryData defaultCategory;23 //24 // private WeblogCategoryData rootCategory;25 //26 // private IndexManager indexMgr;27 // 28 //29 // /**30 // * @param rreq31 // * @param f32 // */33 // public ArchiveParser(Roller roller, WebsiteData website, File f) throws RollerException34 // {35 // this.roller = roller;36 // this.website = website;37 // archiveFile = f;38 // 39 // current = new Timestamp( System.currentTimeMillis());40 // defaultCategory = website.getDefaultCategory();41 // rootCategory = roller.getWeblogManager().getRootWeblogCategory(website);42 // indexMgr = roller.getIndexManager();43 // }44 //45 // public String parse() throws RollerException46 // { 47 // StringBuffer buf = new StringBuffer();48 // 49 // // parse file and convert to WeblogEntryDatas50 // Feed atomFeed = getAtomFeed();51 // if (atomFeed != null)52 // { 53 // importAtom(buf, atomFeed);54 // }55 // else56 // { 57 // // not an Atom feed, try RSS58 // ChannelIF channel = getInformaChannel();59 // 60 // if (channel != null && channel.getItems()!= null)61 // {62 // importRSS(buf, channel);63 // }64 // }65 // 66 // return buf.toString();67 // }68 //69 // /**70 // * @return71 // * @throws FileNotFoundException72 // * @throws IOException73 // */74 // private Feed getAtomFeed()75 // {76 // Feed feed = null;77 // BufferedInputStream bis = null;78 // try79 // {80 // FileInputStream fis = new FileInputStream(archiveFile);81 // bis = new BufferedInputStream(fis);82 // // we need AtomFeedReader for Roller-specific elements83 // AtomFeedReader reader = new AtomFeedReader(bis);84 // // current 'version' of Atom4J parses on init, next version won't85 // if (reader.getFeed() == null) 86 // {87 // reader.parse(); 88 // }89 // feed = reader.getFeed();90 // }91 // catch (FileNotFoundException e)92 // {93 // mLogger.debug("You told me to read a non-existant file.", e);94 // }95 // catch (IOException e)96 // {97 // mLogger.debug("Digester throws IOException for no reason I can tell.", e);98 // }99 // finally100 // {101 // try102 // {103 // if (bis != null) bis.close();104 // }105 // catch (IOException e1)106 // {107 // mLogger.error("Unable to close stream to " + archiveFile);108 // }109 // }110 // return feed;111 // }112 //113 // /**114 // * @param channel115 // * @return116 // */117 // private ChannelIF getInformaChannel()118 // {119 // ChannelIF channel = null;120 // BufferedInputStream bis = null;121 // try122 // {123 // FileInputStream fis = new FileInputStream(archiveFile);124 // bis = new BufferedInputStream(fis);125 // channel = RSSParser.parse(new ChannelBuilder(), bis);126 // }127 // catch (FileNotFoundException e)128 // {129 // e.printStackTrace();130 // }131 // catch (IOException e)132 // {133 // e.printStackTrace();134 // }135 // catch (ParseException e)136 // {137 // e.printStackTrace();138 // }139 // finally140 // {141 // try142 // {143 // if (bis != null) bis.close();144 // }145 // catch (IOException e1)146 // {147 // mLogger.error("Unable to close stream to " + archiveFile);148 // }149 // }150 // return channel;151 // }152 //153 // /**154 // * 155 // */156 // private void importAtom(StringBuffer buf, Feed atomFeed) throws RollerException157 // {158 // AtomEntry atomEntry;159 // WeblogEntryData entry = null;160 // HashMap entryMap = new HashMap(); // map of Roller entries161 // WeblogCategoryData category;162 // HashMap categoryMap = new HashMap();163 // categoryMap.put("defaultCategory", defaultCategory);164 // Collection entries = atomFeed.getEntries();165 // if (entries != null)166 // {167 // Iterator it = entries.iterator();168 // while (it.hasNext())169 // {170 // entry = null; //reset171 //172 // // create new Entry from AtomEntry173 // atomEntry = (AtomEntry)it.next();174 //175 // // test to see if this Entry is a Comment (it's176 // // parent should already exist).177 // /* Added by Roller's AtomEntry */178 // if (atomEntry.getAnnotation() != null)179 // {180 // createComment(atomEntry, entryMap);181 // continue;182 // }183 //184 // if (atomEntry.getId() != null)185 // {186 // entry = roller.getWeblogManager().retrieveWeblogEntry(atomEntry.getId());187 // }188 // if (entry == null)189 // {190 // category = null;191 // /* Atom doesn't currently have a Category definition.192 // Added by Roller's AtomEntry */193 // // return WeblogCategoryData for getCategories194 // if (atomEntry.getCategories() != null)195 // {196 // Iterator cIt = atomEntry.getCategories().iterator();197 // if (cIt.hasNext())198 // {199 // String catPath = (String)cIt.next();200 // category = locateCategory(catPath, categoryMap);201 // }202 // }203 // if (category == null) category = defaultCategory;204 //205 // entry = entryFromAtom(buf, atomEntry, entryMap, category);206 //207 // indexEntry(entry);208 // }209 // else210 // {211 // entryMap.put(entry.getId(), entry);212 // buf.append("An Entry already exists for id: " + atomEntry.getId() + ".<br />");213 // }214 // }215 // }216 // }217 //218 // /**219 // * Convert an AtomEntry to a WeblogEntryData.220 // * @param buf221 // * @param atomEntry222 // * @param entryMap223 // * @param category224 // * @return225 // * @throws RollerException226 // */227 // private WeblogEntryData entryFromAtom(StringBuffer buf, AtomEntry atomEntry, HashMap entryMap, WeblogCategoryData category) throws RollerException228 // { 229 // System.out.println(atomEntry);230 // String title = atomEntry.getTitle().getText();231 // String content = "";232 // Date issued = new Date(current.getTime());233 // Date modified = new Date(current.getTime()); 234 // String id = atomEntry.getId();235 // if (atomEntry.getContent() != null) 236 // {237 // content = atomEntry.getContent().getText();238 // }239 // if (atomEntry.getIssued() != null) issued = atomEntry.getIssued();240 // if (atomEntry.getModified()!= null) modified = atomEntry.getModified();241 // 242 // WeblogEntryData entry = new WeblogEntryData(243 // null, category, website, 244 // title, (String)null, 245 // content, (String)null, 246 // new Timestamp(issued.getTime()),247 // new Timestamp(modified.getTime()), 248 // Boolean.TRUE);249 // entry.save();250 // // store entry in local cache for Comments' to lookup251 // if (id == null) id = entry.getId();252 // entryMap.put(id, entry);253 // 254 // buf.append("\"").append(title).append("\" imported.<br />\n");255 // return entry;256 // }257 //258 // /**259 // * @param atomEntry260 // * @param entryMap261 // */262 // private void createComment(AtomEntry atomEntry, HashMap entryMap) throws RollerException263 // {264 // // first try to get the Entry from local cache265 // CommentData comment = roller.getWeblogManager().retrieveComment(atomEntry.getId());266 // if (comment == null)267 // { 268 // String entryId = atomEntry.getAnnotation();269 // WeblogEntryData entry = (WeblogEntryData) entryMap.get(entryId);270 // if (entry == null)271 // {272 // // now try getting it from database273 // entry = roller.getWeblogManager().retrieveWeblogEntry(entryId);274 // }275 // if (entry != null)276 // { 277 // comment = new CommentData(278 // null, 279 // entry, 280 // atomEntry.getAuthor().getName(), 281 // atomEntry.getAuthor().getEmail(), 282 // atomEntry.getAuthor().getUrl(), 283 // atomEntry.getContent().getText(), 284 // new Timestamp(atomEntry.getIssued().getTime()), 285 // Boolean.FALSE, Boolean.FALSE);286 // comment.save();287 // }288 // else289 // {290 // mLogger.warn("Unable to find parent WeblogEntry for id: " + entryId +291 // ".\n\tComment not created: " + atomEntry.getTitle().getText());292 // }293 // }294 // else295 // {296 // mLogger.info("A Comment already exists for id: " + atomEntry.getId());297 // }298 // }299 //300 // /**301 // * @param rreq302 // * @param buf303 // * @param channel304 // * @throws RollerException305 // */306 // private void importRSS(StringBuffer buf, ChannelIF channel) throws RollerException307 // { 308 // ItemIF item;309 // WeblogEntryData entry = null;310 // WeblogCategoryData category;311 // HashMap categoryMap = new HashMap();312 // categoryMap.put("defaultCategory", defaultCategory);313 // Iterator it = channel.getItems().iterator();314 // while (it.hasNext())315 // {316 // entry = null; //reset317 // item = (ItemIF)it.next();318 // 319 // if (item.getGuid() != null && !item.getGuid().isPermaLink())320 // {321 // entry = roller.getWeblogManager().retrieveWeblogEntry(item.getGuid().getLocation());322 // }323 // 324 // if (entry == null)325 // { 326 // category = null;327 // // return WeblogCategoryData for getCategories328 // if (item.getCategories() != null)329 // {330 // Iterator cIt = item.getCategories().iterator();331 // if (cIt.hasNext()) 332 // {333 // // see if we've already created a category for this String334 // CategoryIF catIF = (CategoryIF)cIt.next();335 // category = locateCategory(catIF.getTitle(), categoryMap);336 // }337 // }338 // if (category == null) category = defaultCategory;339 // 340 // entry = entryFromRSS(buf, item, category);341 // 342 // indexEntry(entry);343 // }344 // else345 // {346 // buf.append("An Entry already exists for id: " + entry.getId() + ".<br />");347 // }348 // }349 // }350 //351 // /**352 // * @param entry353 // */354 // private void indexEntry(WeblogEntryData entry) throws RollerException355 // {356 // // index the new Entry357 // indexMgr.addEntryIndexOperation(entry);358 // }359 //360 // /**361 // * Convert an RSS Item to a WeblogEntryData.362 // * @param buf363 // * @param item364 // * @param category365 // * @return366 // * @throws RollerException367 // */368 // private WeblogEntryData entryFromRSS(StringBuffer buf, ItemIF item, WeblogCategoryData category) throws RollerException369 // {370 // WeblogEntryData entry;371 // // make sure there is an item date372 // if (item.getDate() == null)373 // {374 // item.setDate(new Date(current.getTime()));375 // }376 // 377 // entry = new WeblogEntryData(378 // (String)null, category, website, 379 // item.getTitle(), (String)null, 380 // item.getDescription(), (String)null, 381 // new Timestamp(item.getDate().getTime()),382 // new Timestamp(item.getDate().getTime()), 383 // Boolean.TRUE);384 // entry.save();385 // buf.append("\"").append(item.getTitle()).append("\" imported.<br />\n");386 // return entry;387 // }388 //389 // /**390 // * Iterate over Item's Categories, if any, using the first one. 391 // * Try to match against any we've already pulled. 392 // * If none found locally, check against the database. 393 // * If we still don't find a match, create one and store it locally.394 // * If there are no Item Categories, use defaultCategory395 // * 396 // * @param mapping397 // * @param actionForm398 // * @param request399 // * @param response400 // * @return401 // * @throws IOException402 // * @throws ServletException403 // */404 // private WeblogCategoryData locateCategory(405 // String catName, HashMap categoryMap) 406 // throws RollerException407 // {408 // WeblogCategoryData category = (WeblogCategoryData)categoryMap.get(catName);409 // if (category == null) // not in local map410 // {411 // // look for it in database, by path412 // category = roller.getWeblogManager()413 // .getWeblogCategoryByPath(website, category, catName);414 // 415 // if (category == null) // not in database416 // { 417 // // create a new one418 // category = new WeblogCategoryData(null, 419 // website, rootCategory, 420 // catName, catName, null);421 // category.save();422 // }423 // categoryMap.put(catName, category);424 // }425 // 426 // return category;427 // }428 }429