KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > blojsom > plugin > syndication > module > BlojsomParser


1 /**
2  * Copyright (c) 2003-2006, David A. Czarnecki
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * Redistributions of source code must retain the above copyright notice, this list of conditions and the
9  * following disclaimer.
10  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
11  * following disclaimer in the documentation and/or other materials provided with the distribution.
12  * Neither the name of "David A. Czarnecki" and "blojsom" nor the names of its contributors may be used to
13  * endorse or promote products derived from this software without specific prior written permission.
14  * Products derived from this software may not be called "blojsom", nor may "blojsom" appear in their name,
15  * without prior written permission of David A. Czarnecki.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
18  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
19  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
20  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
21  * EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */

31 package org.blojsom.plugin.syndication.module;
32
33 import com.sun.syndication.feed.module.Module;
34 import com.sun.syndication.io.ModuleParser;
35 import com.sun.syndication.io.impl.DateParser;
36 import org.jdom.Element;
37 import org.jdom.Namespace;
38 import org.blojsom.util.BlojsomUtils;
39
40 import java.util.ArrayList JavaDoc;
41 import java.util.List JavaDoc;
42
43 /**
44  * Blojsom parser for ROME
45  *
46  * @author David Czarnecki
47  * @since blojsom 3.0
48  * @version $Id: BlojsomParser.java,v 1.4 2006/08/18 17:24:12 czarneckid Exp $
49  */

50 public class BlojsomParser implements ModuleParser {
51
52     private static final Namespace BLOJSOM_NS = Namespace.getNamespace("blojsom", Blojsom.BLOJSOM_URI);
53
54     public String JavaDoc getNamespaceUri() {
55         return Blojsom.BLOJSOM_URI;
56     }
57
58     public Module parse(Element element) {
59         boolean foundSomething = false;
60         BlojsomImplementation blojsomModule = new BlojsomImplementation();
61
62         Element e = element.getChild("author", BLOJSOM_NS);
63         if (e != null) {
64             foundSomething = true;
65             blojsomModule.setAuthor(e.getText());
66         }
67
68         e = element.getChild("technoratiTags", BLOJSOM_NS);
69         if (e != null) {
70             foundSomething = true;
71             blojsomModule.setTechnoratiTags(e.getText());
72         }
73
74         e = element.getChild("postSlug", BLOJSOM_NS);
75         if (e != null) {
76             foundSomething = true;
77             blojsomModule.setPostSlug(BlojsomUtils.urlDecode(e.getText()));
78         }
79
80         e = element.getChild("allowsComments", BLOJSOM_NS);
81         if (e != null) {
82             foundSomething = true;
83             blojsomModule.setAllowsComments(Boolean.valueOf(e.getText()).booleanValue());
84         }
85
86         e = element.getChild("allowsTrackbacks", BLOJSOM_NS);
87         if (e != null) {
88             foundSomething = true;
89             blojsomModule.setAllowsTrackbacks(Boolean.valueOf(e.getText()).booleanValue());
90         }
91
92         e = element.getChild("allowsPingbacks", BLOJSOM_NS);
93         if (e != null) {
94             foundSomething = true;
95             blojsomModule.setAllowsPingbacks(Boolean.valueOf(e.getText()).booleanValue());
96         }
97
98         e = element.getChild("comments", BLOJSOM_NS);
99         if (e != null) {
100             foundSomething = true;
101             blojsomModule.setComments(parseComments(e));
102         }
103
104         e = element.getChild("trackbacks", BLOJSOM_NS);
105         if (e != null) {
106             foundSomething = true;
107             blojsomModule.setTrackbacks(parseTrackbacks(e));
108         }
109
110         e = element.getChild("pingbacks", BLOJSOM_NS);
111         if (e != null) {
112             foundSomething = true;
113             blojsomModule.setPingbacks(parsePingbacks(e));
114         }
115
116         e = element.getChild("metadataItems", BLOJSOM_NS);
117         if (e != null) {
118             foundSomething = true;
119             blojsomModule.setMetadata(parseMetadata(e));
120         }
121
122         return (foundSomething) ? blojsomModule : null;
123     }
124
125     private List JavaDoc parseComments(Element e) {
126         List JavaDoc comments = new ArrayList JavaDoc();
127         List JavaDoc commentElements = e.getChildren("comment", BLOJSOM_NS);
128
129         for (int i = 0; i < commentElements.size(); i++) {
130             Element element = (Element) commentElements.get(i);
131             if (element != null) {
132                 SimpleComment comment = new SimpleComment();
133
134                 Element author = element.getChild("commentAuthor", BLOJSOM_NS);
135                 if (author != null) {
136                     comment.setAuthor(author.getText());
137                 }
138
139                 Element authorEmail = element.getChild("commentAuthorEmail", BLOJSOM_NS);
140                 if (authorEmail != null) {
141                     comment.setAuthorEmail(authorEmail.getText());
142                 }
143
144                 Element authorURL = element.getChild("commentAuthorURL", BLOJSOM_NS);
145                 if (authorURL != null) {
146                     comment.setAuthorURL(authorURL.getText());
147                 }
148
149                 Element commentText = element.getChild("commentText", BLOJSOM_NS);
150                 if (commentText != null) {
151                     comment.setComment(commentText.getText());
152                 }
153
154                 Element commentDate = element.getChild("commentDate", BLOJSOM_NS);
155                 if (commentDate != null) {
156                     comment.setCommentDate(DateParser.parseRFC822(commentDate.getText()));
157                 }
158
159                 Element status = element.getChild("commentStatus", BLOJSOM_NS);
160                 if (status != null) {
161                     comment.setStatus(status.getText());
162                 }
163
164                 Element ip = element.getChild("commentIP", BLOJSOM_NS);
165                 if (ip != null) {
166                     comment.setIp(ip.getText());
167                 }
168
169                 Element metadata = element.getChild("commentMetadata", BLOJSOM_NS);
170                 if (metadata != null) {
171                     comment.setMetadata(parseMetadata(metadata));
172                 }
173
174                 comments.add(comment);
175             }
176         }
177
178         return comments;
179     }
180
181     private List JavaDoc parseTrackbacks(Element e) {
182         List JavaDoc trackbacks = new ArrayList JavaDoc();
183         List JavaDoc trackbackElements = e.getChildren("trackback", BLOJSOM_NS);
184
185         for (int i = 0; i < trackbackElements.size(); i++) {
186             Element element = (Element) trackbackElements.get(i);
187             if (element != null) {
188                 SimpleTrackback trackback = new SimpleTrackback();
189
190                 Element trackbackTitle = element.getChild("trackbackTitle", BLOJSOM_NS);
191                 if (trackbackTitle != null) {
192                     trackback.setTitle(trackbackTitle.getText());
193                 }
194
195                 Element trackbackExcerpt = element.getChild("trackbackExcerpt", BLOJSOM_NS);
196                 if (trackbackExcerpt != null) {
197                     trackback.setExcerpt(trackbackExcerpt.getText());
198                 }
199
200                 Element trackbackUrl = element.getChild("trackbackUrl", BLOJSOM_NS);
201                 if (trackbackUrl != null) {
202                     trackback.setUrl(trackbackUrl.getText());
203                 }
204
205                 Element trackbackBlogName = element.getChild("trackbackBlogName", BLOJSOM_NS);
206                 if (trackbackBlogName != null) {
207                     trackback.setBlogName(trackbackBlogName.getName());
208                 }
209
210                 Element trackbackIp = element.getChild("trackbackIP", BLOJSOM_NS);
211                 if (trackbackIp != null) {
212                     trackback.setIp(trackbackIp.getText());
213                 }
214
215                 Element trackbackDate = element.getChild("trackbackDate", BLOJSOM_NS);
216                 if (trackbackDate != null) {
217                     trackback.setTrackbackDate(DateParser.parseRFC822(trackbackDate.getText()));
218                 }
219
220                 Element trackbackStatus = element.getChild("trackbackStatus", BLOJSOM_NS);
221                 if (trackbackStatus != null) {
222                     trackback.setStatus(trackbackStatus.getText());
223                 }
224
225                 Element metadata = element.getChild("trackbackMetadata", BLOJSOM_NS);
226                 if (metadata != null) {
227                     trackback.setMetadata(parseMetadata(metadata));
228                 }
229
230                 trackbacks.add(trackback);
231             }
232         }
233
234         return trackbacks;
235     }
236
237     private List JavaDoc parsePingbacks(Element e) {
238         List JavaDoc pingbacks = new ArrayList JavaDoc();
239         List JavaDoc pingbackElements = e.getChildren("pingback", BLOJSOM_NS);
240
241         for (int i = 0; i < pingbackElements.size(); i++) {
242             Element element = (Element) pingbackElements.get(i);
243             if (element != null) {
244                 SimplePingback pingback = new SimplePingback();
245
246                 Element pingbackTitle = element.getChild("pingbackTitle", BLOJSOM_NS);
247                 if (pingbackTitle != null) {
248                     pingback.setTitle(pingbackTitle.getText());
249                 }
250
251                 Element pingbackExcerpt = element.getChild("pingbackExcerpt", BLOJSOM_NS);
252                 if (pingbackExcerpt != null) {
253                     pingback.setExcerpt(pingbackExcerpt.getText());
254                 }
255
256                 Element pingbackUrl = element.getChild("pingbackUrl", BLOJSOM_NS);
257                 if (pingbackUrl != null) {
258                     pingback.setUrl(pingbackUrl.getText());
259                 }
260
261                 Element pingbackBlogName = element.getChild("pingbackBlogName", BLOJSOM_NS);
262                 if (pingbackBlogName != null) {
263                     pingback.setBlogName(pingbackBlogName.getName());
264                 }
265
266                 Element pingbackIp = element.getChild("pingbackIP", BLOJSOM_NS);
267                 if (pingbackIp != null) {
268                     pingback.setIp(pingbackIp.getText());
269                 }
270
271                 Element pingbackDate = element.getChild("pingbackDate", BLOJSOM_NS);
272                 if (pingbackDate != null) {
273                     pingback.setPingbackDate(DateParser.parseRFC822(pingbackDate.getText()));
274                 }
275
276                 Element pingbackStatus = element.getChild("pingbackStatus", BLOJSOM_NS);
277                 if (pingbackStatus != null) {
278                     pingback.setStatus(pingbackStatus.getText());
279                 }
280
281                 Element pingbackSourceURI = element.getChild("pingbackSourceURI", BLOJSOM_NS);
282                 if (pingbackSourceURI != null) {
283                     pingback.setSourceURI(pingbackSourceURI.getText());
284                 }
285
286                 Element pingbackTargetURI = element.getChild("pingbackTargetURI", BLOJSOM_NS);
287                 if (pingbackTargetURI != null) {
288                     pingback.setTargetURI(pingbackTargetURI.getText());
289                 }
290
291                 Element metadata = element.getChild("pingbackMetadata", BLOJSOM_NS);
292                 if (metadata != null) {
293                     pingback.setMetadata(parseMetadata(metadata));
294                 }
295
296                 pingbacks.add(pingback);
297             }
298         }
299
300         return pingbacks;
301     }
302
303     private List JavaDoc parseMetadata(Element e) {
304         List JavaDoc metadata = new ArrayList JavaDoc();
305         List JavaDoc metadataElements = e.getChildren("metadata", BLOJSOM_NS);
306
307         for (int i = 0; i < metadataElements.size(); i++) {
308             Element element = (Element) metadataElements.get(i);
309             Metadata metadataItem = new Metadata();
310
311             Element metadataKey = element.getChild("key", BLOJSOM_NS);
312             if (metadataKey != null) {
313                 metadataItem.setKey(metadataKey.getText());
314             }
315
316             Element metadataValue = element.getChild("value", BLOJSOM_NS);
317             if (metadataValue != null) {
318                 metadataItem.setValue(metadataValue.getText());
319             }
320
321             metadata.add(metadataItem);
322         }
323         
324         return metadata;
325     }
326 }
327
328
Popular Tags