Source code: org/intabulas/sandler/elements/impl/EntryImpl.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.AtomElement;
39 import org.intabulas.sandler.elements.Content;
40 import org.intabulas.sandler.elements.Contributor;
41 import org.intabulas.sandler.elements.Entry;
42 import org.intabulas.sandler.util.SandlerUtilities;
43 import org.xmlpull.v1.XmlPullParser;
44 import org.xmlpull.v1.XmlPullParserException;
45
46 import java.io.IOException;
47 import java.text.MessageFormat;
48 import java.text.ParseException;
49 import java.util.ArrayList;
50 import java.util.Date;
51 import java.util.List;
52
53 /**
54 * EntryImpl
55 *
56 * @author Mark Lussier
57 * @version $Id: EntryImpl.java,v 1.3 2003/09/10 18:24:25 intabulas Exp $
58 */
59 public class EntryImpl extends AbstractEntryElement implements Entry, AtomElement {
60
61 private Date _created;
62 private Date _issued;
63 private String _summary;
64 private List _contentList;
65
66
67 public EntryImpl() {
68 super();
69 _contentList = new ArrayList(1);
70 }
71
72
73 public String getSummary() {
74 return _summary;
75 }
76
77 public void setSummary(String summary) {
78 _summary = summary;
79 }
80
81 /**
82 *
83 * @return
84 */
85 public Date getCreated() {
86 return _created;
87 }
88
89 /**
90 *
91 * @param date
92 */
93 public void setIssued(Date date) {
94 _issued = date;
95 }
96
97 /**
98 *
99 * @param date
100 */
101 public void setCreated(Date date) {
102 _created = date;
103 }
104
105 /**
106 *
107 * @return
108 */
109 public Date getIssued() {
110 return _issued;
111 }
112
113
114 /**
115 *
116 * @param content
117 */
118 public boolean addContent(Content content) {
119 return _contentList.add(content);
120 }
121
122 /**
123 *
124 * @return
125 */
126 public int getContentCount() {
127 return _contentList.size();
128 }
129
130 /**
131 *
132 * @param index
133 * @return
134 */
135 public Content getContent(int index) {
136 Content result = null;
137 if (index >= 0 && index < _contentList.size()) {
138 result = (Content) _contentList.get(index);
139 }
140 return result;
141 }
142
143 /**
144 *
145 * @param content
146 * @return
147 */
148 public boolean removeContent(Content content) {
149 return _contentList.remove(content);
150 }
151
152 /**
153 *
154 * @param index
155 */
156 public void removeContent(int index) {
157 if (index >= 0 && index < _contentList.size()) {
158 _contentList.remove(index);
159 }
160 }
161
162 /**
163 *
164 * @param index
165 * @param content
166 */
167 public void addContent(int index, Content content) {
168 _contentList.add(index, content);
169 }
170
171 /**
172 * Returns a string representation of the object.
173 *
174 * @return a String representation of the object.
175 */
176 public String toString() {
177
178 //@todo Add XMLNS: Entry when marshalled alone
179
180 StringBuffer buffer = new StringBuffer();
181 buffer.append(MessageFormat.format(FORMAT_STARTELEMENT, new Object[]{ELEMENT_ENTRY, " " + ATTRIBUTE_XMLNS + '=' + ATOM_NAMESPACE}));
182
183 buffer.append(MessageFormat.format(FORMAT_STRINGELEMENT, new Object[]{ELEMENT_TITLE, "", _title}));
184 buffer.append(MessageFormat.format(FORMAT_STRINGELEMENT, new Object[]{ELEMENT_ID, "", _id}));
185
186 if (_summary != null) {
187 buffer.append(MessageFormat.format(FORMAT_STRINGELEMENT, new Object[]{ELEMENT_SUMMARY, "", _summary}));
188 }
189
190 if (_link != null) {
191 buffer.append(MessageFormat.format(FORMAT_STRINGELEMENT, new Object[]{ELEMENT_LINK, "", _link}));
192 }
193
194
195 if (_modified != null) {
196 buffer.append(MessageFormat.format(FORMAT_STRINGELEMENT, new Object[]{ELEMENT_MODIFIED, "", SandlerUtilities.getISO8601Date(_modified)}));
197 }
198 if (_issued != null) {
199 buffer.append(MessageFormat.format(FORMAT_STRINGELEMENT, new Object[]{ELEMENT_ISSUED, "", SandlerUtilities.getISO8601Date(_issued)}));
200 }
201
202 if (_created != null) {
203 buffer.append(MessageFormat.format(FORMAT_STRINGELEMENT, new Object[]{ELEMENT_CREATED, "", SandlerUtilities.getISO8601Date(_created)}));
204 }
205 if (_summary != null) {
206 buffer.append(MessageFormat.format(FORMAT_STRINGELEMENT, new Object[]{ELEMENT_CREATED, "", _summary}));
207 }
208
209 if (_author != null) {
210 buffer.append(_author.toString());
211 }
212
213
214 if (_contributorList.size() > 0) {
215 for (int x = 0; x < _contributorList.size(); x++) {
216 Contributor contributor = (Contributor) _contributorList.get(x);
217 buffer.append(contributor.toString());
218 }
219
220 }
221
222 if (_contentList.size() > 0) {
223 for (int x = 0; x < _contentList.size(); x++) {
224 Content content = (Content) _contentList.get(x);
225 buffer.append(content.toString());
226 }
227
228 }
229 buffer.append(HTMLTAG_BEGIN).append(ELEMENT_ENTRY).append(HTMLTAG_CLOSE);
230
231
232 return buffer.toString();
233
234 }
235
236 public void loadDocument(XmlPullParser parser) throws XmlPullParserException {
237 int eventType = parser.getEventType();
238 do {
239 if (eventType == XmlPullParser.START_TAG) {
240
241 if (parser.getName().equalsIgnoreCase(ELEMENT_TITLE)) {
242 try {
243 _title = parser.nextText();
244 } catch (IOException e) {
245 e.printStackTrace();
246 }
247 } else if (parser.getName().equalsIgnoreCase(ELEMENT_LINK)) {
248 try {
249 _link = parser.nextText();
250 } catch (IOException e) {
251 e.printStackTrace();
252 }
253 } else if (parser.getName().equalsIgnoreCase(ELEMENT_ID)) {
254 try {
255 _id = parser.nextText();
256 } catch (IOException e) {
257 e.printStackTrace();
258 }
259 } else if (parser.getName().equalsIgnoreCase(ELEMENT_SUMMARY)) {
260 try {
261 _summary = parser.nextText();
262 } catch (IOException e) {
263 e.printStackTrace();
264 }
265
266 } else if (parser.getName().equalsIgnoreCase(ELEMENT_MODIFIED)) {
267 processModifiedTag(parser);
268 } else if (parser.getName().equalsIgnoreCase(ELEMENT_ISSUED)) {
269 processIssuedTag(parser);
270 } else if (parser.getName().equalsIgnoreCase(ELEMENT_CREATED)) {
271 processCreatedTag(parser);
272
273 } else if (parser.getName().equalsIgnoreCase(ELEMENT_AUTHOR)) {
274 _author = new AuthorImpl();
275 _author.loadDocument(parser);
276 } else if (parser.getName().equalsIgnoreCase(ELEMENT_CONTENT)) {
277 Content content = new ContentImpl();
278 content.loadDocument(parser);
279 _contentList.add(content);
280
281 } else if (parser.getName().equalsIgnoreCase(ELEMENT_CONTRIBUTOR)) {
282 ContributorImpl contrib = new ContributorImpl();
283 contrib.loadDocument(parser);
284 _contributorList.add(contrib);
285 }
286
287 } else if (eventType == XmlPullParser.END_TAG) {
288 }
289 try {
290 eventType = parser.next();
291 } catch (XmlPullParserException e) {
292 e.printStackTrace();
293 } catch (IOException e) {
294 e.printStackTrace();
295 }
296
297 } while (!(eventType == XmlPullParser.END_TAG && ELEMENT_ENTRY.equals(parser.getName())));
298
299
300 }
301
302 private void processModifiedTag(XmlPullParser parser) throws XmlPullParserException {
303 try {
304 _modified = SandlerUtilities.getDateFromISO8601Date(parser.nextText());
305 } catch (ParseException e) {
306 _modified = new Date();
307 } catch (IOException e) {
308 _modified = null;
309 }
310
311 }
312
313 private void processCreatedTag(XmlPullParser parser) throws XmlPullParserException {
314 try {
315 _created = SandlerUtilities.getDateFromISO8601Date(parser.nextText());
316 } catch (ParseException e) {
317 _created = new Date();
318 } catch (IOException e) {
319 _created = null;
320 }
321
322 }
323
324 private void processIssuedTag(XmlPullParser parser) throws XmlPullParserException {
325 try {
326 _issued = SandlerUtilities.getDateFromISO8601Date(parser.nextText());
327 } catch (ParseException e) {
328 _issued = new Date();
329 } catch (IOException e) {
330 _issued = null;
331 }
332
333 }
334
335
336 }