KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.ArrayList JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.List JavaDoc;
21
22 import org.jdom.Document;
23 import org.jdom.Element;
24 import org.jdom.Namespace;
25 import org.jdom.filter.Filter;
26
27 /**
28  * This class models an Atom workspace.
29  *
30  * @author Dave Johnson
31  */

32 /*
33  * Based on: draft-ietf-atompub-protocol-04.txt
34  *
35  * appService =
36  * element app:service {
37  * (appWorkspace* & anyElement* )
38  * }
39  *
40  * Here is an example Atom workspace:
41  *
42  * <?xml version="1.0" encoding='utf-8'?>
43  * <service
44  * xmlns="http://purl.org/atom/app#">
45  * <workspace title="Main Site" >
46  * <collection
47  * contents="entries" title="My Blog Entries"
48  * HREF="http://example.org/reilly/feed" />
49  * <collection contents="generic"
50  * title="Documents" HREF="http://example.org/reilly/pic" />
51  * </workspace>
52  * <workspace title="Side Bar Blog">
53  * <collection contents="entries"
54  * title="Entries" HREF="http://example.org/reilly/feed" />
55  * <collection
56  * contents="http://example.net/booklist" title="Books"
57  * HREF="http://example.org/reilly/books" />
58  * </workspace>
59  * </service>
60  */

61 public class AtomService
62 {
63     public static final Namespace ns =
64         Namespace.getNamespace("http://purl.org/atom/app#");
65     private List JavaDoc workspaces = new ArrayList JavaDoc();
66
67     public AtomService()
68     {
69     }
70
71     public void addWorkspace(AtomService.Workspace workspace)
72     {
73         workspaces.add(workspace);
74     }
75
76     public List JavaDoc getWorkspaces()
77     {
78         return workspaces;
79     }
80
81     public void setWorkspaces(List JavaDoc workspaces)
82     {
83         this.workspaces = workspaces;
84     }
85
86     /**
87      * This class models an Atom workspace.
88      *
89      * @author Dave Johnson
90      */

91     /*
92      * appWorkspace = element app:workspace { attribute title { text }, (
93      * appCollection* & anyElement* ) }
94      */

95     public static class Workspace
96     {
97         private String JavaDoc title = null;
98         private List JavaDoc collections = new ArrayList JavaDoc();
99
100         public Workspace()
101         {
102         }
103
104         public List JavaDoc getCollections()
105         {
106             return collections;
107         }
108
109         public void setCollections(List JavaDoc collections)
110         {
111             this.collections = collections;
112         }
113
114         public void addCollection(AtomService.Collection col)
115         {
116             collections.add(col);
117         }
118
119         /** Workspace must have a 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
131     /**
132      * This class models an Atom workspace collection.
133      *
134      * @author Dave Johnson
135      */

136     /*
137      * appCollection = element app:collection { attribute title { text },
138      * attribute contents { text }, attribute href { text }, anyElement* }
139      */

140     public static class Collection
141     {
142         private String JavaDoc title;
143         private String JavaDoc contents = "generic";
144         private String JavaDoc href;
145
146         public Collection()
147         {
148         }
149
150         /**
151          * Contents attribute conveys the nature of a collection's member
152          * resources. May be "entry" or "generic" and defaults to "generic"
153          */

154         public String JavaDoc getContents()
155         {
156             return contents;
157         }
158
159         public void setContents(String JavaDoc contents)
160         {
161             this.contents = contents;
162         }
163
164                 /** The URI of the collection */
165         public String JavaDoc getHref()
166         {
167             return href;
168         }
169
170         public void setHref(String JavaDoc href)
171         {
172             this.href = href;
173         }
174
175                 /** Must have human readable title */
176         public String JavaDoc getTitle()
177         {
178             return title;
179         }
180
181         public void setTitle(String JavaDoc title)
182         {
183             this.title = title;
184         }
185     }
186
187     /** Deserialize an Atom service XML document into an object */
188     public static AtomService documentToService(Document document)
189     {
190         AtomService service = new AtomService();
191         Element root = document.getRootElement();
192         List JavaDoc spaces = root.getChildren("workspace", ns);
193         Iterator JavaDoc iter = spaces.iterator();
194         while (iter.hasNext())
195         {
196             Element e = (Element) iter.next();
197             service.addWorkspace(AtomService.elementToWorkspace(e));
198         }
199         return service;
200     }
201
202     /** Serialize an AtomService object into an XML document */
203     public static Document serviceToDocument(AtomService service)
204     {
205         Document doc = new Document();
206         Element root = new Element("service", ns);
207         doc.setRootElement(root);
208         Iterator JavaDoc iter = service.getWorkspaces().iterator();
209         while (iter.hasNext())
210         {
211             AtomService.Workspace space = (AtomService.Workspace) iter.next();
212             root.addContent(AtomService.workspaceToElement(space));
213         }
214         return doc;
215     }
216
217     /** Deserialize a Atom workspace XML element into an object */
218     public static AtomService.Workspace elementToWorkspace(Element element)
219     {
220         AtomService.Workspace space = new AtomService.Workspace();
221         space.setTitle(element.getAttribute("title").getValue());
222         List JavaDoc collections = element.getChildren("collection", ns);
223         Iterator JavaDoc iter = collections.iterator();
224         while (iter.hasNext())
225         {
226             Element e = (Element) iter.next();
227             space.addCollection(AtomService.elementToCollection(e));
228         }
229         return space;
230     }
231
232     /** Serialize an AtomService.Workspace object into an XML element */
233     public static Element workspaceToElement(Workspace space)
234     {
235         Namespace ns = Namespace.getNamespace("http://purl.org/atom/app#");
236         Element element = new Element("workspace", ns);
237         element.setAttribute("title", space.getTitle());
238         Iterator JavaDoc iter = space.getCollections().iterator();
239         while (iter.hasNext())
240         {
241             AtomService.Collection col = (AtomService.Collection) iter.next();
242             element.addContent(collectionToElement(col));
243         }
244         return element;
245     }
246
247     /** Deserialize an Atom service collection XML element into an object */
248     public static AtomService.Collection elementToCollection(Element element)
249     {
250         AtomService.Collection collection = new AtomService.Collection();
251         collection.setTitle(element.getAttribute("title").getValue());
252         collection.setHref(element.getAttribute("href").getValue());
253         if (element.getAttribute("href") != null)
254         {
255             collection.setContents(element.getAttribute("contents").getValue());
256         }
257         return collection;
258     }
259
260     /** Serialize an AtomService.Collection object into an XML element */
261     public static Element collectionToElement(AtomService.Collection collection)
262     {
263         Namespace ns = Namespace.getNamespace("http://purl.org/atom/app#");
264         Element element = new Element("collection", ns);
265         element.setAttribute("title", collection.getTitle());
266         element.setAttribute("href", collection.getHref());
267         if (collection.getContents() != null)
268         {
269             element.setAttribute("contents", collection.getContents());
270         }
271         return element;
272     }
273 }
Popular Tags