001: /**
002: * Copyright (c) 2003-2007, David A. Czarnecki
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions are met:
007: *
008: * Redistributions of source code must retain the above copyright notice, this list of conditions and the
009: * following disclaimer.
010: * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
011: * following disclaimer in the documentation and/or other materials provided with the distribution.
012: * Neither the name of "David A. Czarnecki" and "blojsom" nor the names of its contributors may be used to
013: * endorse or promote products derived from this software without specific prior written permission.
014: * Products derived from this software may not be called "blojsom", nor may "blojsom" appear in their name,
015: * without prior written permission of David A. Czarnecki.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
018: * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
019: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
020: * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
021: * EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
022: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
023: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
025: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
026: * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
027: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
028: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
029: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
030: */package org.blojsom.plugin.syndication.module;
031:
032: import com.sun.syndication.feed.module.Module;
033: import com.sun.syndication.io.ModuleParser;
034: import com.sun.syndication.io.impl.DateParser;
035: import org.jdom.Element;
036: import org.jdom.Namespace;
037: import org.blojsom.util.BlojsomUtils;
038:
039: import java.util.ArrayList;
040: import java.util.List;
041:
042: /**
043: * Blojsom parser for ROME
044: *
045: * @author David Czarnecki
046: * @since blojsom 3.0
047: * @version $Id: BlojsomParser.java,v 1.5 2007/01/17 02:35:06 czarneckid Exp $
048: */
049: public class BlojsomParser implements ModuleParser {
050:
051: private static final Namespace BLOJSOM_NS = Namespace.getNamespace(
052: "blojsom", Blojsom.BLOJSOM_URI);
053:
054: public String getNamespaceUri() {
055: return Blojsom.BLOJSOM_URI;
056: }
057:
058: public Module parse(Element element) {
059: boolean foundSomething = false;
060: BlojsomImplementation blojsomModule = new BlojsomImplementation();
061:
062: Element e = element.getChild("author", BLOJSOM_NS);
063: if (e != null) {
064: foundSomething = true;
065: blojsomModule.setAuthor(e.getText());
066: }
067:
068: e = element.getChild("technoratiTags", BLOJSOM_NS);
069: if (e != null) {
070: foundSomething = true;
071: blojsomModule.setTechnoratiTags(e.getText());
072: }
073:
074: e = element.getChild("postSlug", BLOJSOM_NS);
075: if (e != null) {
076: foundSomething = true;
077: blojsomModule.setPostSlug(BlojsomUtils.urlDecode(e
078: .getText()));
079: }
080:
081: e = element.getChild("allowsComments", BLOJSOM_NS);
082: if (e != null) {
083: foundSomething = true;
084: blojsomModule.setAllowsComments(Boolean
085: .valueOf(e.getText()).booleanValue());
086: }
087:
088: e = element.getChild("allowsTrackbacks", BLOJSOM_NS);
089: if (e != null) {
090: foundSomething = true;
091: blojsomModule.setAllowsTrackbacks(Boolean.valueOf(
092: e.getText()).booleanValue());
093: }
094:
095: e = element.getChild("allowsPingbacks", BLOJSOM_NS);
096: if (e != null) {
097: foundSomething = true;
098: blojsomModule.setAllowsPingbacks(Boolean.valueOf(
099: e.getText()).booleanValue());
100: }
101:
102: e = element.getChild("comments", BLOJSOM_NS);
103: if (e != null) {
104: foundSomething = true;
105: blojsomModule.setComments(parseComments(e));
106: }
107:
108: e = element.getChild("trackbacks", BLOJSOM_NS);
109: if (e != null) {
110: foundSomething = true;
111: blojsomModule.setTrackbacks(parseTrackbacks(e));
112: }
113:
114: e = element.getChild("pingbacks", BLOJSOM_NS);
115: if (e != null) {
116: foundSomething = true;
117: blojsomModule.setPingbacks(parsePingbacks(e));
118: }
119:
120: e = element.getChild("metadataItems", BLOJSOM_NS);
121: if (e != null) {
122: foundSomething = true;
123: blojsomModule.setMetadata(parseMetadata(e));
124: }
125:
126: return (foundSomething) ? blojsomModule : null;
127: }
128:
129: private List parseComments(Element e) {
130: List comments = new ArrayList();
131: List commentElements = e.getChildren("comment", BLOJSOM_NS);
132:
133: for (int i = 0; i < commentElements.size(); i++) {
134: Element element = (Element) commentElements.get(i);
135: if (element != null) {
136: SimpleComment comment = new SimpleComment();
137:
138: Element author = element.getChild("commentAuthor",
139: BLOJSOM_NS);
140: if (author != null) {
141: comment.setAuthor(author.getText());
142: }
143:
144: Element authorEmail = element.getChild(
145: "commentAuthorEmail", BLOJSOM_NS);
146: if (authorEmail != null) {
147: comment.setAuthorEmail(authorEmail.getText());
148: }
149:
150: Element authorURL = element.getChild(
151: "commentAuthorURL", BLOJSOM_NS);
152: if (authorURL != null) {
153: comment.setAuthorURL(authorURL.getText());
154: }
155:
156: Element commentText = element.getChild("commentText",
157: BLOJSOM_NS);
158: if (commentText != null) {
159: comment.setComment(commentText.getText());
160: }
161:
162: Element commentDate = element.getChild("commentDate",
163: BLOJSOM_NS);
164: if (commentDate != null) {
165: comment.setCommentDate(DateParser
166: .parseRFC822(commentDate.getText()));
167: }
168:
169: Element status = element.getChild("commentStatus",
170: BLOJSOM_NS);
171: if (status != null) {
172: comment.setStatus(status.getText());
173: }
174:
175: Element ip = element.getChild("commentIP", BLOJSOM_NS);
176: if (ip != null) {
177: comment.setIp(ip.getText());
178: }
179:
180: Element metadata = element.getChild("commentMetadata",
181: BLOJSOM_NS);
182: if (metadata != null) {
183: comment.setMetadata(parseMetadata(metadata));
184: }
185:
186: comments.add(comment);
187: }
188: }
189:
190: return comments;
191: }
192:
193: private List parseTrackbacks(Element e) {
194: List trackbacks = new ArrayList();
195: List trackbackElements = e.getChildren("trackback", BLOJSOM_NS);
196:
197: for (int i = 0; i < trackbackElements.size(); i++) {
198: Element element = (Element) trackbackElements.get(i);
199: if (element != null) {
200: SimpleTrackback trackback = new SimpleTrackback();
201:
202: Element trackbackTitle = element.getChild(
203: "trackbackTitle", BLOJSOM_NS);
204: if (trackbackTitle != null) {
205: trackback.setTitle(trackbackTitle.getText());
206: }
207:
208: Element trackbackExcerpt = element.getChild(
209: "trackbackExcerpt", BLOJSOM_NS);
210: if (trackbackExcerpt != null) {
211: trackback.setExcerpt(trackbackExcerpt.getText());
212: }
213:
214: Element trackbackUrl = element.getChild("trackbackUrl",
215: BLOJSOM_NS);
216: if (trackbackUrl != null) {
217: trackback.setUrl(trackbackUrl.getText());
218: }
219:
220: Element trackbackBlogName = element.getChild(
221: "trackbackBlogName", BLOJSOM_NS);
222: if (trackbackBlogName != null) {
223: trackback.setBlogName(trackbackBlogName.getName());
224: }
225:
226: Element trackbackIp = element.getChild("trackbackIP",
227: BLOJSOM_NS);
228: if (trackbackIp != null) {
229: trackback.setIp(trackbackIp.getText());
230: }
231:
232: Element trackbackDate = element.getChild(
233: "trackbackDate", BLOJSOM_NS);
234: if (trackbackDate != null) {
235: trackback.setTrackbackDate(DateParser
236: .parseRFC822(trackbackDate.getText()));
237: }
238:
239: Element trackbackStatus = element.getChild(
240: "trackbackStatus", BLOJSOM_NS);
241: if (trackbackStatus != null) {
242: trackback.setStatus(trackbackStatus.getText());
243: }
244:
245: Element metadata = element.getChild(
246: "trackbackMetadata", BLOJSOM_NS);
247: if (metadata != null) {
248: trackback.setMetadata(parseMetadata(metadata));
249: }
250:
251: trackbacks.add(trackback);
252: }
253: }
254:
255: return trackbacks;
256: }
257:
258: private List parsePingbacks(Element e) {
259: List pingbacks = new ArrayList();
260: List pingbackElements = e.getChildren("pingback", BLOJSOM_NS);
261:
262: for (int i = 0; i < pingbackElements.size(); i++) {
263: Element element = (Element) pingbackElements.get(i);
264: if (element != null) {
265: SimplePingback pingback = new SimplePingback();
266:
267: Element pingbackTitle = element.getChild(
268: "pingbackTitle", BLOJSOM_NS);
269: if (pingbackTitle != null) {
270: pingback.setTitle(pingbackTitle.getText());
271: }
272:
273: Element pingbackExcerpt = element.getChild(
274: "pingbackExcerpt", BLOJSOM_NS);
275: if (pingbackExcerpt != null) {
276: pingback.setExcerpt(pingbackExcerpt.getText());
277: }
278:
279: Element pingbackUrl = element.getChild("pingbackUrl",
280: BLOJSOM_NS);
281: if (pingbackUrl != null) {
282: pingback.setUrl(pingbackUrl.getText());
283: }
284:
285: Element pingbackBlogName = element.getChild(
286: "pingbackBlogName", BLOJSOM_NS);
287: if (pingbackBlogName != null) {
288: pingback.setBlogName(pingbackBlogName.getName());
289: }
290:
291: Element pingbackIp = element.getChild("pingbackIP",
292: BLOJSOM_NS);
293: if (pingbackIp != null) {
294: pingback.setIp(pingbackIp.getText());
295: }
296:
297: Element pingbackDate = element.getChild("pingbackDate",
298: BLOJSOM_NS);
299: if (pingbackDate != null) {
300: pingback.setPingbackDate(DateParser
301: .parseRFC822(pingbackDate.getText()));
302: }
303:
304: Element pingbackStatus = element.getChild(
305: "pingbackStatus", BLOJSOM_NS);
306: if (pingbackStatus != null) {
307: pingback.setStatus(pingbackStatus.getText());
308: }
309:
310: Element pingbackSourceURI = element.getChild(
311: "pingbackSourceURI", BLOJSOM_NS);
312: if (pingbackSourceURI != null) {
313: pingback.setSourceURI(pingbackSourceURI.getText());
314: }
315:
316: Element pingbackTargetURI = element.getChild(
317: "pingbackTargetURI", BLOJSOM_NS);
318: if (pingbackTargetURI != null) {
319: pingback.setTargetURI(pingbackTargetURI.getText());
320: }
321:
322: Element metadata = element.getChild("pingbackMetadata",
323: BLOJSOM_NS);
324: if (metadata != null) {
325: pingback.setMetadata(parseMetadata(metadata));
326: }
327:
328: pingbacks.add(pingback);
329: }
330: }
331:
332: return pingbacks;
333: }
334:
335: private List parseMetadata(Element e) {
336: List metadata = new ArrayList();
337: List metadataElements = e.getChildren("metadata", BLOJSOM_NS);
338:
339: for (int i = 0; i < metadataElements.size(); i++) {
340: Element element = (Element) metadataElements.get(i);
341: Metadata metadataItem = new Metadata();
342:
343: Element metadataKey = element.getChild("key", BLOJSOM_NS);
344: if (metadataKey != null) {
345: metadataItem.setKey(metadataKey.getText());
346: }
347:
348: Element metadataValue = element.getChild("value",
349: BLOJSOM_NS);
350: if (metadataValue != null) {
351: metadataItem.setValue(metadataValue.getText());
352: }
353:
354: metadata.add(metadataItem);
355: }
356:
357: return metadata;
358: }
359: }
|