001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. The ASF licenses this file to You
004: * under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License. For additional information regarding
015: * copyright in this work, please see the NOTICE file in the top level
016: * directory of this distribution.
017: */
018: /*
019: * Created on Apr 2, 2004
020: */
021: package org.apache.roller.ui.core;
022:
023: /**
024: * TODO: revisit this class once Atom 1.0 support comes to Rome
025: * @author lance.lavandowska
026: */
027: public class ArchiveParser {
028: // protected static final Log mLogger =
029: // LogFactory.getFactory().getInstance(ArchiveParser.class);
030: //
031: // private Roller roller;
032: // private WebsiteData website;
033: // private File archiveFile;
034: //
035: // private Timestamp current;
036: //
037: // private WeblogCategoryData defaultCategory;
038: //
039: // private WeblogCategoryData rootCategory;
040: //
041: // private IndexManager indexMgr;
042: //
043: //
044: // /**
045: // * @param rreq
046: // * @param f
047: // */
048: // public ArchiveParser(Roller roller, WebsiteData website, File f) throws RollerException
049: // {
050: // this.roller = roller;
051: // this.website = website;
052: // archiveFile = f;
053: //
054: // current = new Timestamp( System.currentTimeMillis());
055: // defaultCategory = website.getDefaultCategory();
056: // rootCategory = roller.getWeblogManager().getRootWeblogCategory(website);
057: // indexMgr = roller.getIndexManager();
058: // }
059: //
060: // public String parse() throws RollerException
061: // {
062: // StringBuffer buf = new StringBuffer();
063: //
064: // // parse file and convert to WeblogEntryDatas
065: // Feed atomFeed = getAtomFeed();
066: // if (atomFeed != null)
067: // {
068: // importAtom(buf, atomFeed);
069: // }
070: // else
071: // {
072: // // not an Atom feed, try RSS
073: // ChannelIF channel = getInformaChannel();
074: //
075: // if (channel != null && channel.getItems()!= null)
076: // {
077: // importRSS(buf, channel);
078: // }
079: // }
080: //
081: // return buf.toString();
082: // }
083: //
084: // /**
085: // * @return
086: // * @throws FileNotFoundException
087: // * @throws IOException
088: // */
089: // private Feed getAtomFeed()
090: // {
091: // Feed feed = null;
092: // BufferedInputStream bis = null;
093: // try
094: // {
095: // FileInputStream fis = new FileInputStream(archiveFile);
096: // bis = new BufferedInputStream(fis);
097: // // we need AtomFeedReader for Roller-specific elements
098: // AtomFeedReader reader = new AtomFeedReader(bis);
099: // // current 'version' of Atom4J parses on init, next version won't
100: // if (reader.getFeed() == null)
101: // {
102: // reader.parse();
103: // }
104: // feed = reader.getFeed();
105: // }
106: // catch (FileNotFoundException e)
107: // {
108: // mLogger.debug("You told me to read a non-existant file.", e);
109: // }
110: // catch (IOException e)
111: // {
112: // mLogger.debug("Digester throws IOException for no reason I can tell.", e);
113: // }
114: // finally
115: // {
116: // try
117: // {
118: // if (bis != null) bis.close();
119: // }
120: // catch (IOException e1)
121: // {
122: // mLogger.error("Unable to close stream to " + archiveFile);
123: // }
124: // }
125: // return feed;
126: // }
127: //
128: // /**
129: // * @param channel
130: // * @return
131: // */
132: // private ChannelIF getInformaChannel()
133: // {
134: // ChannelIF channel = null;
135: // BufferedInputStream bis = null;
136: // try
137: // {
138: // FileInputStream fis = new FileInputStream(archiveFile);
139: // bis = new BufferedInputStream(fis);
140: // channel = RSSParser.parse(new ChannelBuilder(), bis);
141: // }
142: // catch (FileNotFoundException e)
143: // {
144: // e.printStackTrace();
145: // }
146: // catch (IOException e)
147: // {
148: // e.printStackTrace();
149: // }
150: // catch (ParseException e)
151: // {
152: // e.printStackTrace();
153: // }
154: // finally
155: // {
156: // try
157: // {
158: // if (bis != null) bis.close();
159: // }
160: // catch (IOException e1)
161: // {
162: // mLogger.error("Unable to close stream to " + archiveFile);
163: // }
164: // }
165: // return channel;
166: // }
167: //
168: // /**
169: // *
170: // */
171: // private void importAtom(StringBuffer buf, Feed atomFeed) throws RollerException
172: // {
173: // AtomEntry atomEntry;
174: // WeblogEntryData entry = null;
175: // HashMap entryMap = new HashMap(); // map of Roller entries
176: // WeblogCategoryData category;
177: // HashMap categoryMap = new HashMap();
178: // categoryMap.put("defaultCategory", defaultCategory);
179: // Collection entries = atomFeed.getEntries();
180: // if (entries != null)
181: // {
182: // Iterator it = entries.iterator();
183: // while (it.hasNext())
184: // {
185: // entry = null; //reset
186: //
187: // // create new Entry from AtomEntry
188: // atomEntry = (AtomEntry)it.next();
189: //
190: // // test to see if this Entry is a Comment (it's
191: // // parent should already exist).
192: // /* Added by Roller's AtomEntry */
193: // if (atomEntry.getAnnotation() != null)
194: // {
195: // createComment(atomEntry, entryMap);
196: // continue;
197: // }
198: //
199: // if (atomEntry.getId() != null)
200: // {
201: // entry = roller.getWeblogManager().retrieveWeblogEntry(atomEntry.getId());
202: // }
203: // if (entry == null)
204: // {
205: // category = null;
206: // /* Atom doesn't currently have a Category definition.
207: // Added by Roller's AtomEntry */
208: // // return WeblogCategoryData for getCategories
209: // if (atomEntry.getCategories() != null)
210: // {
211: // Iterator cIt = atomEntry.getCategories().iterator();
212: // if (cIt.hasNext())
213: // {
214: // String catPath = (String)cIt.next();
215: // category = locateCategory(catPath, categoryMap);
216: // }
217: // }
218: // if (category == null) category = defaultCategory;
219: //
220: // entry = entryFromAtom(buf, atomEntry, entryMap, category);
221: //
222: // indexEntry(entry);
223: // }
224: // else
225: // {
226: // entryMap.put(entry.getId(), entry);
227: // buf.append("An Entry already exists for id: " + atomEntry.getId() + ".<br />");
228: // }
229: // }
230: // }
231: // }
232: //
233: // /**
234: // * Convert an AtomEntry to a WeblogEntryData.
235: // * @param buf
236: // * @param atomEntry
237: // * @param entryMap
238: // * @param category
239: // * @return
240: // * @throws RollerException
241: // */
242: // private WeblogEntryData entryFromAtom(StringBuffer buf, AtomEntry atomEntry, HashMap entryMap, WeblogCategoryData category) throws RollerException
243: // {
244: // System.out.println(atomEntry);
245: // String title = atomEntry.getTitle().getText();
246: // String content = "";
247: // Date issued = new Date(current.getTime());
248: // Date modified = new Date(current.getTime());
249: // String id = atomEntry.getId();
250: // if (atomEntry.getContent() != null)
251: // {
252: // content = atomEntry.getContent().getText();
253: // }
254: // if (atomEntry.getIssued() != null) issued = atomEntry.getIssued();
255: // if (atomEntry.getModified()!= null) modified = atomEntry.getModified();
256: //
257: // WeblogEntryData entry = new WeblogEntryData(
258: // null, category, website,
259: // title, (String)null,
260: // content, (String)null,
261: // new Timestamp(issued.getTime()),
262: // new Timestamp(modified.getTime()),
263: // Boolean.TRUE);
264: // entry.save();
265: // // store entry in local cache for Comments' to lookup
266: // if (id == null) id = entry.getId();
267: // entryMap.put(id, entry);
268: //
269: // buf.append("\"").append(title).append("\" imported.<br />\n");
270: // return entry;
271: // }
272: //
273: // /**
274: // * @param atomEntry
275: // * @param entryMap
276: // */
277: // private void createComment(AtomEntry atomEntry, HashMap entryMap) throws RollerException
278: // {
279: // // first try to get the Entry from local cache
280: // CommentData comment = roller.getWeblogManager().retrieveComment(atomEntry.getId());
281: // if (comment == null)
282: // {
283: // String entryId = atomEntry.getAnnotation();
284: // WeblogEntryData entry = (WeblogEntryData) entryMap.get(entryId);
285: // if (entry == null)
286: // {
287: // // now try getting it from database
288: // entry = roller.getWeblogManager().retrieveWeblogEntry(entryId);
289: // }
290: // if (entry != null)
291: // {
292: // comment = new CommentData(
293: // null,
294: // entry,
295: // atomEntry.getAuthor().getName(),
296: // atomEntry.getAuthor().getEmail(),
297: // atomEntry.getAuthor().getUrl(),
298: // atomEntry.getContent().getText(),
299: // new Timestamp(atomEntry.getIssued().getTime()),
300: // Boolean.FALSE, Boolean.FALSE);
301: // comment.save();
302: // }
303: // else
304: // {
305: // mLogger.warn("Unable to find parent WeblogEntry for id: " + entryId +
306: // ".\n\tComment not created: " + atomEntry.getTitle().getText());
307: // }
308: // }
309: // else
310: // {
311: // mLogger.info("A Comment already exists for id: " + atomEntry.getId());
312: // }
313: // }
314: //
315: // /**
316: // * @param rreq
317: // * @param buf
318: // * @param channel
319: // * @throws RollerException
320: // */
321: // private void importRSS(StringBuffer buf, ChannelIF channel) throws RollerException
322: // {
323: // ItemIF item;
324: // WeblogEntryData entry = null;
325: // WeblogCategoryData category;
326: // HashMap categoryMap = new HashMap();
327: // categoryMap.put("defaultCategory", defaultCategory);
328: // Iterator it = channel.getItems().iterator();
329: // while (it.hasNext())
330: // {
331: // entry = null; //reset
332: // item = (ItemIF)it.next();
333: //
334: // if (item.getGuid() != null && !item.getGuid().isPermaLink())
335: // {
336: // entry = roller.getWeblogManager().retrieveWeblogEntry(item.getGuid().getLocation());
337: // }
338: //
339: // if (entry == null)
340: // {
341: // category = null;
342: // // return WeblogCategoryData for getCategories
343: // if (item.getCategories() != null)
344: // {
345: // Iterator cIt = item.getCategories().iterator();
346: // if (cIt.hasNext())
347: // {
348: // // see if we've already created a category for this String
349: // CategoryIF catIF = (CategoryIF)cIt.next();
350: // category = locateCategory(catIF.getTitle(), categoryMap);
351: // }
352: // }
353: // if (category == null) category = defaultCategory;
354: //
355: // entry = entryFromRSS(buf, item, category);
356: //
357: // indexEntry(entry);
358: // }
359: // else
360: // {
361: // buf.append("An Entry already exists for id: " + entry.getId() + ".<br />");
362: // }
363: // }
364: // }
365: //
366: // /**
367: // * @param entry
368: // */
369: // private void indexEntry(WeblogEntryData entry) throws RollerException
370: // {
371: // // index the new Entry
372: // indexMgr.addEntryIndexOperation(entry);
373: // }
374: //
375: // /**
376: // * Convert an RSS Item to a WeblogEntryData.
377: // * @param buf
378: // * @param item
379: // * @param category
380: // * @return
381: // * @throws RollerException
382: // */
383: // private WeblogEntryData entryFromRSS(StringBuffer buf, ItemIF item, WeblogCategoryData category) throws RollerException
384: // {
385: // WeblogEntryData entry;
386: // // make sure there is an item date
387: // if (item.getDate() == null)
388: // {
389: // item.setDate(new Date(current.getTime()));
390: // }
391: //
392: // entry = new WeblogEntryData(
393: // (String)null, category, website,
394: // item.getTitle(), (String)null,
395: // item.getDescription(), (String)null,
396: // new Timestamp(item.getDate().getTime()),
397: // new Timestamp(item.getDate().getTime()),
398: // Boolean.TRUE);
399: // entry.save();
400: // buf.append("\"").append(item.getTitle()).append("\" imported.<br />\n");
401: // return entry;
402: // }
403: //
404: // /**
405: // * Iterate over Item's Categories, if any, using the first one.
406: // * Try to match against any we've already pulled.
407: // * If none found locally, check against the database.
408: // * If we still don't find a match, create one and store it locally.
409: // * If there are no Item Categories, use defaultCategory
410: // *
411: // * @param mapping
412: // * @param actionForm
413: // * @param request
414: // * @param response
415: // * @return
416: // * @throws IOException
417: // * @throws ServletException
418: // */
419: // private WeblogCategoryData locateCategory(
420: // String catName, HashMap categoryMap)
421: // throws RollerException
422: // {
423: // WeblogCategoryData category = (WeblogCategoryData)categoryMap.get(catName);
424: // if (category == null) // not in local map
425: // {
426: // // look for it in database, by path
427: // category = roller.getWeblogManager()
428: // .getWeblogCategoryByPath(website, category, catName);
429: //
430: // if (category == null) // not in database
431: // {
432: // // create a new one
433: // category = new WeblogCategoryData(null,
434: // website, rootCategory,
435: // catName, catName, null);
436: // category.save();
437: // }
438: // categoryMap.put(catName, category);
439: // }
440: //
441: // return category;
442: // }
443: }
|