1 /*2 * Licensed to the Apache Software Foundation (ASF) under one or more3 * contributor license agreements. The ASF licenses this file to You4 * under the Apache License, Version 2.0 (the "License"); you may not5 * use this file except in compliance with the License.6 * You may obtain a copy of the License at7 *8 * http://www.apache.org/licenses/LICENSE-2.09 *10 * Unless required by applicable law or agreed to in writing, software11 * distributed under the License is distributed on an "AS IS" BASIS,12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13 * See the License for the specific language governing permissions and14 * limitations under the License. For additional information regarding15 * copyright in this work, please see the NOTICE file in the top level16 * directory of this distribution.17 */18 /*19 * Created on Apr 2, 200420 */21 package org.apache.roller.ui.core;22 23 24 /**25 * TODO: revisit this class once Atom 1.0 support comes to Rome26 * @author lance.lavandowska27 */28 public class ArchiveParser29 {30 // protected static final Log mLogger = 31 // LogFactory.getFactory().getInstance(ArchiveParser.class);32 // 33 // private Roller roller;34 // private WebsiteData website;35 // private File archiveFile;36 //37 // private Timestamp current;38 //39 // private WeblogCategoryData defaultCategory;40 //41 // private WeblogCategoryData rootCategory;42 //43 // private IndexManager indexMgr;44 // 45 //46 // /**47 // * @param rreq48 // * @param f49 // */50 // public ArchiveParser(Roller roller, WebsiteData website, File f) throws RollerException51 // {52 // this.roller = roller;53 // this.website = website;54 // archiveFile = f;55 // 56 // current = new Timestamp( System.currentTimeMillis());57 // defaultCategory = website.getDefaultCategory();58 // rootCategory = roller.getWeblogManager().getRootWeblogCategory(website);59 // indexMgr = roller.getIndexManager();60 // }61 //62 // public String parse() throws RollerException63 // { 64 // StringBuffer buf = new StringBuffer();65 // 66 // // parse file and convert to WeblogEntryDatas67 // Feed atomFeed = getAtomFeed();68 // if (atomFeed != null)69 // { 70 // importAtom(buf, atomFeed);71 // }72 // else73 // { 74 // // not an Atom feed, try RSS75 // ChannelIF channel = getInformaChannel();76 // 77 // if (channel != null && channel.getItems()!= null)78 // {79 // importRSS(buf, channel);80 // }81 // }82 // 83 // return buf.toString();84 // }85 //86 // /**87 // * @return88 // * @throws FileNotFoundException89 // * @throws IOException90 // */91 // private Feed getAtomFeed()92 // {93 // Feed feed = null;94 // BufferedInputStream bis = null;95 // try96 // {97 // FileInputStream fis = new FileInputStream(archiveFile);98 // bis = new BufferedInputStream(fis);99 // // we need AtomFeedReader for Roller-specific elements100 // AtomFeedReader reader = new AtomFeedReader(bis);101 // // current 'version' of Atom4J parses on init, next version won't102 // if (reader.getFeed() == null) 103 // {104 // reader.parse(); 105 // }106 // feed = reader.getFeed();107 // }108 // catch (FileNotFoundException e)109 // {110 // mLogger.debug("You told me to read a non-existant file.", e);111 // }112 // catch (IOException e)113 // {114 // mLogger.debug("Digester throws IOException for no reason I can tell.", e);115 // }116 // finally117 // {118 // try119 // {120 // if (bis != null) bis.close();121 // }122 // catch (IOException e1)123 // {124 // mLogger.error("Unable to close stream to " + archiveFile);125 // }126 // }127 // return feed;128 // }129 //130 // /**131 // * @param channel132 // * @return133 // */134 // private ChannelIF getInformaChannel()135 // {136 // ChannelIF channel = null;137 // BufferedInputStream bis = null;138 // try139 // {140 // FileInputStream fis = new FileInputStream(archiveFile);141 // bis = new BufferedInputStream(fis);142 // channel = RSSParser.parse(new ChannelBuilder(), bis);143 // }144 // catch (FileNotFoundException e)145 // {146 // e.printStackTrace();147 // }148 // catch (IOException e)149 // {150 // e.printStackTrace();151 // }152 // catch (ParseException e)153 // {154 // e.printStackTrace();155 // }156 // finally157 // {158 // try159 // {160 // if (bis != null) bis.close();161 // }162 // catch (IOException e1)163 // {164 // mLogger.error("Unable to close stream to " + archiveFile);165 // }166 // }167 // return channel;168 // }169 //170 // /**171 // * 172 // */173 // private void importAtom(StringBuffer buf, Feed atomFeed) throws RollerException174 // {175 // AtomEntry atomEntry;176 // WeblogEntryData entry = null;177 // HashMap entryMap = new HashMap(); // map of Roller entries178 // WeblogCategoryData category;179 // HashMap categoryMap = new HashMap();180 // categoryMap.put("defaultCategory", defaultCategory);181 // Collection entries = atomFeed.getEntries();182 // if (entries != null)183 // {184 // Iterator it = entries.iterator();185 // while (it.hasNext())186 // {187 // entry = null; //reset188 //189 // // create new Entry from AtomEntry190 // atomEntry = (AtomEntry)it.next();191 //192 // // test to see if this Entry is a Comment (it's193 // // parent should already exist).194 // /* Added by Roller's AtomEntry */195 // if (atomEntry.getAnnotation() != null)196 // {197 // createComment(atomEntry, entryMap);198 // continue;199 // }200 //201 // if (atomEntry.getId() != null)202 // {203 // entry = roller.getWeblogManager().retrieveWeblogEntry(atomEntry.getId());204 // }205 // if (entry == null)206 // {207 // category = null;208 // /* Atom doesn't currently have a Category definition.209 // Added by Roller's AtomEntry */210 // // return WeblogCategoryData for getCategories211 // if (atomEntry.getCategories() != null)212 // {213 // Iterator cIt = atomEntry.getCategories().iterator();214 // if (cIt.hasNext())215 // {216 // String catPath = (String)cIt.next();217 // category = locateCategory(catPath, categoryMap);218 // }219 // }220 // if (category == null) category = defaultCategory;221 //222 // entry = entryFromAtom(buf, atomEntry, entryMap, category);223 //224 // indexEntry(entry);225 // }226 // else227 // {228 // entryMap.put(entry.getId(), entry);229 // buf.append("An Entry already exists for id: " + atomEntry.getId() + ".<br />");230 // }231 // }232 // }233 // }234 //235 // /**236 // * Convert an AtomEntry to a WeblogEntryData.237 // * @param buf238 // * @param atomEntry239 // * @param entryMap240 // * @param category241 // * @return242 // * @throws RollerException243 // */244 // private WeblogEntryData entryFromAtom(StringBuffer buf, AtomEntry atomEntry, HashMap entryMap, WeblogCategoryData category) throws RollerException245 // { 246 // System.out.println(atomEntry);247 // String title = atomEntry.getTitle().getText();248 // String content = "";249 // Date issued = new Date(current.getTime());250 // Date modified = new Date(current.getTime()); 251 // String id = atomEntry.getId();252 // if (atomEntry.getContent() != null) 253 // {254 // content = atomEntry.getContent().getText();255 // }256 // if (atomEntry.getIssued() != null) issued = atomEntry.getIssued();257 // if (atomEntry.getModified()!= null) modified = atomEntry.getModified();258 // 259 // WeblogEntryData entry = new WeblogEntryData(260 // null, category, website, 261 // title, (String)null, 262 // content, (String)null, 263 // new Timestamp(issued.getTime()),264 // new Timestamp(modified.getTime()), 265 // Boolean.TRUE);266 // entry.save();267 // // store entry in local cache for Comments' to lookup268 // if (id == null) id = entry.getId();269 // entryMap.put(id, entry);270 // 271 // buf.append("\"").append(title).append("\" imported.<br />\n");272 // return entry;273 // }274 //275 // /**276 // * @param atomEntry277 // * @param entryMap278 // */279 // private void createComment(AtomEntry atomEntry, HashMap entryMap) throws RollerException280 // {281 // // first try to get the Entry from local cache282 // CommentData comment = roller.getWeblogManager().retrieveComment(atomEntry.getId());283 // if (comment == null)284 // { 285 // String entryId = atomEntry.getAnnotation();286 // WeblogEntryData entry = (WeblogEntryData) entryMap.get(entryId);287 // if (entry == null)288 // {289 // // now try getting it from database290 // entry = roller.getWeblogManager().retrieveWeblogEntry(entryId);291 // }292 // if (entry != null)293 // { 294 // comment = new CommentData(295 // null, 296 // entry, 297 // atomEntry.getAuthor().getName(), 298 // atomEntry.getAuthor().getEmail(), 299 // atomEntry.getAuthor().getUrl(), 300 // atomEntry.getContent().getText(), 301 // new Timestamp(atomEntry.getIssued().getTime()), 302 // Boolean.FALSE, Boolean.FALSE);303 // comment.save();304 // }305 // else306 // {307 // mLogger.warn("Unable to find parent WeblogEntry for id: " + entryId +308 // ".\n\tComment not created: " + atomEntry.getTitle().getText());309 // }310 // }311 // else312 // {313 // mLogger.info("A Comment already exists for id: " + atomEntry.getId());314 // }315 // }316 //317 // /**318 // * @param rreq319 // * @param buf320 // * @param channel321 // * @throws RollerException322 // */323 // private void importRSS(StringBuffer buf, ChannelIF channel) throws RollerException324 // { 325 // ItemIF item;326 // WeblogEntryData entry = null;327 // WeblogCategoryData category;328 // HashMap categoryMap = new HashMap();329 // categoryMap.put("defaultCategory", defaultCategory);330 // Iterator it = channel.getItems().iterator();331 // while (it.hasNext())332 // {333 // entry = null; //reset334 // item = (ItemIF)it.next();335 // 336 // if (item.getGuid() != null && !item.getGuid().isPermaLink())337 // {338 // entry = roller.getWeblogManager().retrieveWeblogEntry(item.getGuid().getLocation());339 // }340 // 341 // if (entry == null)342 // { 343 // category = null;344 // // return WeblogCategoryData for getCategories345 // if (item.getCategories() != null)346 // {347 // Iterator cIt = item.getCategories().iterator();348 // if (cIt.hasNext()) 349 // {350 // // see if we've already created a category for this String351 // CategoryIF catIF = (CategoryIF)cIt.next();352 // category = locateCategory(catIF.getTitle(), categoryMap);353 // }354 // }355 // if (category == null) category = defaultCategory;356 // 357 // entry = entryFromRSS(buf, item, category);358 // 359 // indexEntry(entry);360 // }361 // else362 // {363 // buf.append("An Entry already exists for id: " + entry.getId() + ".<br />");364 // }365 // }366 // }367 //368 // /**369 // * @param entry370 // */371 // private void indexEntry(WeblogEntryData entry) throws RollerException372 // {373 // // index the new Entry374 // indexMgr.addEntryIndexOperation(entry);375 // }376 //377 // /**378 // * Convert an RSS Item to a WeblogEntryData.379 // * @param buf380 // * @param item381 // * @param category382 // * @return383 // * @throws RollerException384 // */385 // private WeblogEntryData entryFromRSS(StringBuffer buf, ItemIF item, WeblogCategoryData category) throws RollerException386 // {387 // WeblogEntryData entry;388 // // make sure there is an item date389 // if (item.getDate() == null)390 // {391 // item.setDate(new Date(current.getTime()));392 // }393 // 394 // entry = new WeblogEntryData(395 // (String)null, category, website, 396 // item.getTitle(), (String)null, 397 // item.getDescription(), (String)null, 398 // new Timestamp(item.getDate().getTime()),399 // new Timestamp(item.getDate().getTime()), 400 // Boolean.TRUE);401 // entry.save();402 // buf.append("\"").append(item.getTitle()).append("\" imported.<br />\n");403 // return entry;404 // }405 //406 // /**407 // * Iterate over Item's Categories, if any, using the first one. 408 // * Try to match against any we've already pulled. 409 // * If none found locally, check against the database. 410 // * If we still don't find a match, create one and store it locally.411 // * If there are no Item Categories, use defaultCategory412 // * 413 // * @param mapping414 // * @param actionForm415 // * @param request416 // * @param response417 // * @return418 // * @throws IOException419 // * @throws ServletException420 // */421 // private WeblogCategoryData locateCategory(422 // String catName, HashMap categoryMap) 423 // throws RollerException424 // {425 // WeblogCategoryData category = (WeblogCategoryData)categoryMap.get(catName);426 // if (category == null) // not in local map427 // {428 // // look for it in database, by path429 // category = roller.getWeblogManager()430 // .getWeblogCategoryByPath(website, category, catName);431 // 432 // if (category == null) // not in database433 // { 434 // // create a new one435 // category = new WeblogCategoryData(null, 436 // website, rootCategory, 437 // catName, catName, null);438 // category.save();439 // }440 // categoryMap.put(catName, category);441 // }442 // 443 // return category;444 // }445 }446