Source code: org/intabulas/sandler/elements/impl/FeedImpl.java
1 /**
2 * Copyright (c) 2003, Mark Lussier
3 * All rights reserved.
4 *
5 * Portions Copyright (c) 2003 by David A. Czarnecki
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 *
10 * Redistributions of source code must retain the above copyright notice,
11 * this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
15 * Neither the name of the "Mark Lussier" and "Sandler" nor the names of
16 * its contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 * Products derived from this software may not be called "Sandler",
19 * nor may "Sandler" appear in their name, without prior written permission of
20 * Mark Lussier
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
23 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
24 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
26 * EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
31 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
32 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
34 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 */
36 package org.intabulas.sandler.elements.impl;
37
38 import org.intabulas.sandler.elements.*;
39 import org.intabulas.sandler.util.SandlerUtilities;
40 import org.xmlpull.v1.XmlPullParser;
41 import org.xmlpull.v1.XmlPullParserException;
42
43 import java.io.IOException;
44 import java.text.MessageFormat;
45 import java.text.ParseException;
46 import java.util.ArrayList;
47 import java.util.Date;
48 import java.util.List;
49
50 /**
51 * FeedImpl
52 *
53 * @author Mark Lussier
54 * @version $Id: FeedImpl.java,v 1.4 2003/09/10 18:24:25 intabulas Exp $
55 */
56 public class FeedImpl extends AbstractEntryElement implements Feed, AtomElement {
57
58 private String _version = ATOM_VERSION;
59 private String _language = "en";
60 private String _tagline;
61 private String _copyright;
62
63 private Generator _generator;
64
65 private List _entryList;
66
67
68 public FeedImpl() {
69 super();
70 _entryList = new ArrayList(1);
71 }
72
73
74 public void clear() {
75 _entryList.clear();
76 _contributorList.clear();
77 _version = ATOM_VERSION;
78 _language = "en";
79 _title = null;
80 _link = null;
81 _modified = null;
82 _tagline = null;
83 _id = null;
84 _copyright = null;
85 _author = null;
86 _generator = null;
87 }
88
89
90 // ===================================================
91
92 /**
93 *
94 * @param index
95 * @param entry
96 */
97 public void addEntry(int index, Entry entry) {
98 _entryList.add(index, entry);
99 }
100
101 /**
102 *
103 * @param entry
104 */
105 public boolean addEntry(Entry entry) {
106 return _entryList.add(entry);
107 }
108
109 /**
110 *
111 * @return
112 */
113 public int getEntryCount() {
114 return _entryList.size();
115 }
116
117 /**
118 *
119 * @param index
120 */
121 public void removeEntry(int index) {
122 if (index >= 0 && index < _entryList.size()) {
123 _entryList.remove(index);
124 }
125 }
126
127 /**
128 *
129 * @param entry
130 * @return
131 */
132 public boolean removeEntry(Entry entry) {
133 boolean result = false;
134 if (_entryList.contains(entry)) {
135 result = _entryList.remove(entry);
136 }
137
138 return result;
139 }
140
141 /**
142 *
143 * @param index
144 * @return
145 */
146 public Entry getEntry(int index) {
147 Entry result = null;
148 if (index >= 0 && index < _entryList.size()) {
149 result = (Entry) _entryList.get(index);
150 }
151 return result;
152 }
153
154
155
156 // ===================================================
157
158
159 /**
160 *
161 * @param version
162 */
163 public void setVersion(String version) {
164 _version = version;
165 }
166
167 /**
168 *
169 * @return
170 */
171 public String getVersion() {
172 return _version;
173 }
174
175 /**
176 *
177 * @param language
178 */
179 public void setLanguage(String language) {
180 _language = language;
181 }
182
183 /**
184 *
185 * @return
186 */
187 public String getLanguage() {
188 return _language;
189 }
190
191
192 /**
193 *
194 * @return
195 */
196 public String getTagline() {
197 return _tagline;
198 }
199
200 /**
201 *
202 * @param tagline
203 */
204 public void setTagline(String tagline) {
205 _tagline = tagline;
206 }
207
208
209 /**
210 *
211 * @return
212 */
213 public String getCopyright() {
214 return _copyright;
215 }
216
217 /**
218 *
219 * @param copyright
220 */
221 public void setCopyright(String copyright) {
222 _copyright = copyright;
223 }
224
225
226 /**
227 *
228 * @param generator
229 */
230 public void setGenerator(Generator generator) {
231 _generator = generator;
232 }
233
234 /**
235 *
236 * @return
237 */
238 public Generator getGenerator() {
239 return _generator;
240 }
241
242 /**
243 * Returns a string representation of the object.
244 *
245 * @return a String representation of the object.
246 */
247 public String toString() {
248
249 StringBuffer buffer = new StringBuffer(XML_STARTDOC);
250 String feedAttributes = MessageFormat.format(FORMAT_FEEDATTRIBUTES, new Object[]{_version, _language});
251 buffer.append(MessageFormat.format(FORMAT_STARTELEMENT, new Object[]{ELEMENT_FEED, feedAttributes}));
252
253 buffer.append(MessageFormat.format(FORMAT_STRINGELEMENT, new Object[]{ELEMENT_TITLE, "", _title}));
254 buffer.append(MessageFormat.format(FORMAT_STRINGELEMENT, new Object[]{ELEMENT_LINK, "", _link}));
255 buffer.append(MessageFormat.format(FORMAT_STRINGELEMENT, new Object[]{ELEMENT_MODIFIED, "", SandlerUtilities.getISO8601Date(_modified)}));
256
257 if (_summary != null) {
258 buffer.append(MessageFormat.format(FORMAT_STRINGELEMENT, new Object[]{ELEMENT_SUMMARY, "", _summary}));
259 }
260
261
262 if (_tagline != null) {
263 buffer.append(MessageFormat.format(FORMAT_STRINGELEMENT, new Object[]{ELEMENT_TAGLINE, "", _tagline}));
264 }
265
266 if (_id != null) {
267 buffer.append(MessageFormat.format(FORMAT_STRINGELEMENT, new Object[]{ELEMENT_ID, "", _id}));
268 }
269
270 if (_copyright != null) {
271 buffer.append(MessageFormat.format(FORMAT_STRINGELEMENT, new Object[]{ELEMENT_COPYRIGHT, "", _copyright}));
272 }
273
274
275 if (_generator != null) {
276 buffer.append(_generator.toString());
277 }
278
279 if (_author != null) {
280 buffer.append(_author.toString());
281 }
282
283
284 if (_entryList.size() > 0) {
285 for (int x = 0; x < _entryList.size(); x++) {
286 Entry entry = (Entry) _entryList.get(x);
287 buffer.append(entry.toString());
288 }
289
290 }
291
292 if (_contributorList.size() > 0) {
293 for (int x = 0; x < _contributorList.size(); x++) {
294 Contributor contributor = (Contributor) _contributorList.get(x);
295 buffer.append(contributor.toString());
296 }
297
298 }
299
300
301 buffer.append(HTMLTAG_BEGIN).append(ELEMENT_FEED).append(HTMLTAG_CLOSE);
302
303
304 return buffer.toString();
305
306 }
307
308
309 public void loadDocument(XmlPullParser parser) throws XmlPullParserException {
310
311
312 int eventType = parser.getEventType();
313
314 do {
315 if (eventType == XmlPullParser.START_DOCUMENT) {
316 }
317 if (eventType == XmlPullParser.START_TAG) {
318 if (parser.getName().equalsIgnoreCase(ELEMENT_FEED)) {
319 processDocumentAttributes(parser);
320 try {
321 processDocumenteElements(parser);
322 } catch (XmlPullParserException e) {
323 e.printStackTrace();
324 } catch (IOException e) {
325 e.printStackTrace();
326 }
327 }
328 }
329 if (eventType == XmlPullParser.END_TAG) {
330 }
331 try {
332 eventType = parser.next();
333 } catch (XmlPullParserException e) {
334 e.printStackTrace();
335 } catch (IOException e) {
336 e.printStackTrace();
337 }
338 } while (eventType != XmlPullParser.END_DOCUMENT);
339 }
340
341
342 private void processDocumenteElements(XmlPullParser parser) throws XmlPullParserException, IOException {
343
344 String boundingTag = parser.getName();
345 String childTag = null;
346 int eventType = parser.nextTag();
347
348 do {
349 if (eventType == XmlPullParser.START_TAG) {
350 childTag = parser.getName();
351
352 if (parser.getName().equalsIgnoreCase(ELEMENT_TITLE)) {
353 processTitleTag(parser);
354 } else if (parser.getName().equalsIgnoreCase(ELEMENT_LINK)) {
355 processLinkTag(parser);
356 } else if (parser.getName().equalsIgnoreCase(ELEMENT_ID)) {
357 processIdTag(parser);
358 } else if (parser.getName().equalsIgnoreCase(ELEMENT_MODIFIED)) {
359 processModifiedTag(parser);
360 } else if (parser.getName().equalsIgnoreCase(ELEMENT_TAGLINE)) {
361 processTaglineTag(parser);
362 } else if (parser.getName().equalsIgnoreCase(ELEMENT_GENERATOR)) {
363 _generator = new GeneratorImpl();
364 _generator.loadDocument(parser);
365 } else if (parser.getName().equalsIgnoreCase(ELEMENT_ENTRY)) {
366 Entry entry = new EntryImpl();
367 entry.loadDocument(parser);
368 addEntry(entry);
369 } else if (parser.getName().equalsIgnoreCase(ELEMENT_AUTHOR)) {
370 _author = new AuthorImpl();
371 _author.loadDocument(parser);
372 } else if (parser.getName().equalsIgnoreCase(ELEMENT_CONTRIBUTOR)) {
373 ContributorImpl contrib = new ContributorImpl();
374 contrib.loadDocument(parser);
375 _contributorList.add(contrib);
376 }
377
378 }
379 if (eventType == XmlPullParser.END_TAG) {
380 }
381 try {
382 eventType = parser.next();
383 } catch (XmlPullParserException e) {
384 e.printStackTrace();
385 } catch (IOException e) {
386 e.printStackTrace();
387 }
388 } while (!(eventType == XmlPullParser.END_TAG && ELEMENT_FEED.equals(parser.getName())));
389
390
391 }
392
393
394 private void processDocumentAttributes(XmlPullParser parser) {
395 int attributeCount = parser.getAttributeCount();
396 for (int x = 0; x < attributeCount; x++) {
397 if (parser.getAttributeName(x).equals(ATTRIBUTE_VERSION)) {
398 _version = parser.getAttributeValue(x);
399 }
400 if (parser.getAttributeName(x).equals(ATTRIBUTE_LANG_NP)) {
401 _language = parser.getAttributeValue(x);
402 }
403
404 }
405 }
406
407
408 private void processTitleTag(XmlPullParser parser) throws XmlPullParserException, IOException {
409 _title = parser.nextText();
410
411 }
412
413 private void processLinkTag(XmlPullParser parser) throws XmlPullParserException, IOException {
414 _link = parser.nextText();
415 }
416
417 private void processIdTag(XmlPullParser parser) throws XmlPullParserException, IOException {
418 _id = parser.nextText();
419 }
420
421 private void processTaglineTag(XmlPullParser parser) throws XmlPullParserException, IOException {
422 _tagline = parser.nextText();
423 }
424
425
426 private void processModifiedTag(XmlPullParser parser) throws XmlPullParserException, IOException {
427 try {
428 _modified = SandlerUtilities.getDateFromISO8601Date(parser.nextText());
429 } catch (ParseException e) {
430 _modified = new Date();
431 }
432
433 }
434
435
436 }