KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > roller > presentation > atomapi > AtomCollection


1 /*
2  * Copyright 2005 David M Johnson (For RSS and Atom In Action)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.roller.presentation.atomapi;
17
18 import java.text.ParseException JavaDoc;
19 import java.text.SimpleDateFormat JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.Date JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24
25 import org.jdom.Document;
26 import org.jdom.Element;
27 import org.jdom.Namespace;
28
29 /**
30  * Models an Atom collection.
31  *
32  * @author Dave Johnson
33  */

34 /*
35  * Based on: draft-ietf-atompub-protocol-04.txt
36  *
37  * appCollection = element
38  * app:collection {
39  * attribute next { text } ?,
40  * appMember*
41  * }
42  *
43  * Here is an example Atom collection:
44  *
45  * <?xml version="1.0" encoding='utf-8'?>
46  * <collection xmlns="http://purl.org/atom/app#">
47  * <member HREF="http://example.org/1"
48  * hrefreadonly="http://example.com/1/bar"
49  * title="Sample 1"
50  * updated="2003-12-13T18:30:02Z" />
51  * <member HREF="http://example.org/2"
52  * hrefreadonly="http://example.com/2/bar"
53  * title="Sample 2"
54  * updated="2003-12-13T18:30:02Z" />
55  * <member HREF="http://example.org/3"
56  * hrefreadonly="http://example.com/3/bar"
57  * title="Sample 3"
58  * updated="2003-12-13T18:30:02Z" />
59  * <member HREF="http://example.org/4"
60  * title="Sample 4"
61  * updated="2003-12-13T18:30:02Z" />
62  * </collection>
63  */

64 public class AtomCollection
65 {
66     public static final Namespace ns =
67         Namespace.getNamespace("http://purl.org/atom/app#");
68     private static SimpleDateFormat JavaDoc df =
69         new SimpleDateFormat JavaDoc( "yyyy-MM-dd'T'HH:mm:ssZ" );
70     private String JavaDoc next = null;
71     private List JavaDoc members = new ArrayList JavaDoc();
72
73     public AtomCollection()
74     {
75     }
76
77     /** URI of collection containing member elements updated earlier in time */
78     public String JavaDoc getNext()
79     {
80         return next;
81     }
82
83     public void setNext(String JavaDoc next)
84     {
85         this.next = next;
86     }
87
88     public List JavaDoc getMembers()
89     {
90         return members;
91     }
92
93     public void setMembers(List JavaDoc members)
94     {
95         this.members = members;
96     }
97
98     public void addMember(Member member)
99     {
100         members.add(member);
101     }
102
103     /** Models an Atom collection member */
104     /*
105      * appMember = element app:member { attribute title { text }, attribute href {
106      * text }, attribute hrefreadonly { text } ?, attribute updated { text } }
107      */

108     public static class Member
109     {
110         private String JavaDoc title;
111         private String JavaDoc href;
112         private String JavaDoc hrefreadonly;
113         private Date JavaDoc updated;
114
115         public Member()
116         {
117         }
118
119         /** Human readable title */
120         public String JavaDoc getTitle()
121         {
122             return title;
123         }
124
125         public void setTitle(String JavaDoc title)
126         {
127             this.title = title;
128         }
129
130         /** The URI used to edit the member source */
131         public String JavaDoc getHref()
132         {
133             return href;
134         }
135
136         public void setHref(String JavaDoc href)
137         {
138             this.href = href;
139         }
140
141         /** The URI for readonly access to member source */
142         public String JavaDoc getHrefreadonly()
143         {
144             return hrefreadonly;
145         }
146
147         public void setHrefreadonly(String JavaDoc hrefreadonly)
148         {
149             this.hrefreadonly = hrefreadonly;
150         }
151
152         /** Same as updated value of collection member */
153         public Date JavaDoc getUpdated()
154         {
155             return updated;
156         }
157
158         public void setUpdated(Date JavaDoc updated)
159         {
160             this.updated = updated;
161         }
162     }
163
164     /** Deserialize an Atom Collection XML document into an object */
165     public static AtomCollection documentToCollection(Document document)
166             throws Exception JavaDoc
167     {
168         AtomCollection collection = new AtomCollection();
169         Element root = document.getRootElement();
170         if (root.getAttribute("next") != null)
171         {
172             collection.setNext(root.getAttribute("next").getValue());
173         }
174         List JavaDoc mems = root.getChildren("member", ns);
175         Iterator JavaDoc iter = mems.iterator();
176         while (iter.hasNext())
177         {
178             Element e = (Element) iter.next();
179             collection.addMember(AtomCollection.elementToMember(e));
180         }
181         return collection;
182     }
183
184     /** Serialize an AtomCollection object into an XML document */
185     public static Document collectionToDocument(AtomCollection collection)
186     {
187         Document doc = new Document();
188         Element root = new Element("collection", ns);
189         doc.setRootElement(root);
190         if (collection.getNext() != null)
191         {
192             root.setAttribute("next", collection.getNext());
193         }
194         Iterator JavaDoc iter = collection.getMembers().iterator();
195         while (iter.hasNext())
196         {
197             Member member = (Member) iter.next();
198             root.addContent(AtomCollection.memberToElement(member));
199         }
200         return doc;
201     }
202
203     /** Deserialize an Atom collection member XML element into an object */
204     public static Member elementToMember(Element element) throws Exception JavaDoc
205     {
206         Member member = new Member();
207         member.setTitle(element.getAttribute("title").getValue());
208         member.setHref(element.getAttribute("href").getValue());
209         if (element.getAttribute("href") != null)
210         {
211             member.setHref(element.getAttribute("href").getValue());
212         }
213         member.setUpdated(df.parse(element.getAttribute("updated").getValue()));
214         return member;
215     }
216
217     /** Serialize a collection member into an XML element */
218     public static Element memberToElement(Member member)
219     {
220         Element element = new Element("member", ns);
221         element.setAttribute("title", member.getTitle()); // TODO: escape/strip HTML?
222
element.setAttribute("href", member.getHref());
223         if (member.getHrefreadonly() != null)
224         {
225             element.setAttribute("hrefreadonly", member.getHrefreadonly());
226         }
227         element.setAttribute("updated", df.format(member.getUpdated()));
228         return element;
229     }
230
231     /** Start and end date range */
232     public static class Range { Date JavaDoc start=null; Date JavaDoc end=null; }
233     
234     /** Parse HTTP Range header into a start and end date range */
235     public static Range parseRange(String JavaDoc rangeString) throws ParseException JavaDoc
236     {
237         // Range: updated=<isodate>/<isodate>
238
// Range: updated=<isodate>/
239
// Range: updated=/<isodate>
240

241         Range range = new Range();
242         String JavaDoc[] split = rangeString.split("=");
243         if (split[1].startsWith("/"))
244         {
245             // we have only end date
246
range.end = df.parse(split[1].split("/")[1]);
247         }
248         else if (split[1].endsWith("/"))
249         {
250             // we have only start date
251
range.start = df.parse(split[1].split("/")[0]);
252         }
253         else
254         {
255             // both dates present
256
String JavaDoc[] dates = split[1].split("/");
257             range.start = df.parse(dates[0]);
258             range.end = df.parse(dates[1]);
259         }
260         return range;
261     }
262 }
263
Popular Tags