KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > webservices > atomprotocol > AtomService


1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. The ASF licenses this file to You
4 * under the Apache License, Version 2.0 (the "License"); you may not
5 * 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. For additional information regarding
15 * copyright in this work, please see the NOTICE file in the top level
16 * directory of this distribution.
17 */

18 package org.apache.roller.webservices.atomprotocol;
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.LinkedHashSet JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Set JavaDoc;
25
26 import org.jdom.Document;
27 import org.jdom.Element;
28 import org.jdom.Namespace;
29
30 /**
31  * This class models an Atom Ublishing Protocol Service Document.
32  * <p />
33  * Based on: draft-ietf-atompub-protocol-10.txt
34  * <p />
35  * Designed to be Roller independent.
36  */
/*
37
38     namespace app = "http://purl.org/atom/app#"
39     start = appService
40                  
41     appService =
42        element app:service {
43           appCommonAttributes,
44           ( appWorkspace+
45             & extensionElement* )
46        }
47        
48     For example:
49      
50     <?xml version="1.0" encoding='utf-8'?>
51     <service xmlns="http://purl.org/atom/app#"
52              xmlns:atom="http://www.w3.org/2005/Atom">
53       <workspace>
54         <atom:title>Main Site</atom:title>
55         <collection
56             href="http://example.org/reilly/main" >
57           <atom:title>My Blog Entries</atom:title>
58           <categories
59              href="http://example.com/cats/forMain.cats" />
60         </collection>
61         <collection
62             href="http://example.org/reilly/pic" >
63           <atom:title>Pictures</atom:title>
64           <accept>image/*</accept>
65         </collection>
66       </workspace>
67       <workspace>
68         <atom:title>Side Bar Blog</atom:title>
69         <collection
70             href="http://example.org/reilly/list" >
71           <atom:title>Remaindered Links</atom:title>
72           <accept>entry</accept>
73           <categories fixed="yes">
74             <atom:category
75               scheme="http://example.org/extra-cats/"
76               term="joke" />
77             <atom:category
78               scheme="http://example.org/extra-cats/"
79               term="serious" />
80           </categories>
81         </collection>
82       </workspace>
83     </service>
84  */

85 public class AtomService {
86     public static final Namespace ns =
87             Namespace.getNamespace("app","http://purl.org/atom/app#");
88     public static final Namespace atomns =
89             Namespace.getNamespace("atom","http://www.w3.org/2005/atom");
90
91     private List JavaDoc workspaces = new ArrayList JavaDoc();
92     
93     public AtomService() {
94     }
95     
96     public void addWorkspace(AtomService.Workspace workspace) {
97         workspaces.add(workspace);
98     }
99     
100     public List JavaDoc getWorkspaces() {
101         return workspaces;
102     }
103     
104     public void setWorkspaces(List JavaDoc workspaces) {
105         this.workspaces = workspaces;
106     }
107     
108     /**
109      * This class models an Atom workspace.
110      * @author Dave Johnson
111      */
/*
112     appWorkspace =
113        element app:workspace {
114           appCommonAttributes,
115           ( appCollection*
116             & extensionElement* )
117        }
118     atomTitle = element atom:title { atomTextConstruct }
119      */

120     public static class Workspace {
121         private String JavaDoc title = null;
122         private String JavaDoc titleType = null; // may be TEXT, HTML, XHTML
123
private Set JavaDoc collections = new LinkedHashSet JavaDoc();
124         
125         public Workspace() {
126         }
127         
128         /** Iterate over collections in workspace */
129         public Iterator JavaDoc getCollections() {
130             return collections.iterator();
131         }
132         
133         /** Add new collection to workspace */
134         public void addCollection(AtomService.Collection col) {
135             collections.add(col);
136         }
137         
138         /** Workspace must have a human readable title */
139         public String JavaDoc getTitle() {
140             return title;
141         }
142         
143         public void setTitle(String JavaDoc title) {
144             this.title = title;
145         }
146
147         public String JavaDoc getTitleType() {
148             return titleType;
149         }
150
151         public void setTitleType(String JavaDoc titleType) {
152             this.titleType = titleType;
153         }
154     }
155     
156     /**
157      * This class models an Atom workspace collection.
158      */
/*
159     appCollection =
160        element app:collection {
161           appCommonAttributes,
162           attribute href { atomURI },
163           ( appAccept?
164             & appCategories*
165             & extensionElement* )
166        }
167      */

168     public static class Collection {
169         private String JavaDoc title = null;
170         private String JavaDoc titleType =null; // may be TEXT, HTML, XHTML
171
private String JavaDoc accept = "entry";
172         private String JavaDoc listTemplate = null;
173         private String JavaDoc href = null;
174         private Set JavaDoc categories = new LinkedHashSet JavaDoc(); // of Categories objects
175

176         public Collection() {
177         }
178         
179         /**
180          * Comma separated list of media-ranges accepted by collection
181          * or "entry" to indicate Atom entries only. Leaving null will
182          * default to entries only.
183          */

184         public String JavaDoc getAccept() {
185             return accept;
186         }
187         
188         public void setAccept(String JavaDoc accept) {
189             this.accept = accept;
190         }
191         
192         /** The URI of the collection */
193         public String JavaDoc getHref() {
194             return href;
195         }
196         
197         public void setHref(String JavaDoc href) {
198             this.href = href;
199         }
200         
201         /** Must have human readable title */
202         public String JavaDoc getTitle() {
203             return title;
204         }
205         
206         public void setTitle(String JavaDoc title) {
207             this.title = title;
208         }
209
210         public String JavaDoc getTitleType() {
211             return titleType;
212         }
213
214         public void setTitleType(String JavaDoc titleType) {
215             this.titleType = titleType;
216         }
217
218         /** Workspace can have multiple Categories objects */
219         public void addCategories(Categories cats) {
220             categories.add(cats);
221         }
222         
223         /** Iterate over Categories objects in Collection */
224         public Iterator JavaDoc getCategories() {
225             return categories.iterator();
226         }
227     }
228     
229     /**
230      * Categories object contains Category objects
231      */
/*
232      appInlineCategories =
233         element app:categories {
234             attribute fixed { "yes" | "no" }?,
235             attribute scheme { atomURI }?,
236             (atomCategory*)
237         }
238      */

239     public static class Categories {
240         private Set JavaDoc categories = new LinkedHashSet JavaDoc(); // of Category objects
241
private String JavaDoc scheme = null;
242         private boolean fixed = false;
243         
244         /** Add category list of those specified*/
245         public void addCategory(Category cat) {
246             categories.add(cat);
247         }
248         
249         /** Iterate over Category objects */
250         public Iterator JavaDoc getCategories() {
251             return categories.iterator();
252         }
253
254         /** True if clients MUST use one of the categories specified */
255         public boolean isFixed() {
256             return fixed;
257         }
258
259         /** True if clients MUST use one of the categories specified */
260         public void setFixed(boolean fixed) {
261             this.fixed = fixed;
262         }
263
264         /** Category URI scheme to use for Categories without a scheme */
265         public String JavaDoc getScheme() {
266             return scheme;
267         }
268
269         /** Category URI scheme to use for Categories without a scheme */
270         public void setScheme(String JavaDoc scheme) {
271             this.scheme = scheme;
272         }
273     }
274     
275       
276     /**
277      * Represents an <atom:category> object
278      */
/*
279         atomCategory =
280         element atom:category {
281            atomCommonAttributes,
282            attribute term { text },
283            attribute scheme { atomURI }?,
284            attribute label { text }?,
285            undefinedContent
286         }
287      */

288     public static class Category {
289         private String JavaDoc term;
290         private String JavaDoc scheme;
291         private String JavaDoc label;
292
293         public String JavaDoc getTerm() {
294             return term;
295         }
296
297         public void setTerm(String JavaDoc term) {
298             this.term = term;
299         }
300
301         public String JavaDoc getScheme() {
302             return scheme;
303         }
304
305         public void setScheme(String JavaDoc scheme) {
306             this.scheme = scheme;
307         }
308
309         public String JavaDoc getLabel() {
310             return label;
311         }
312
313         public void setLabel(String JavaDoc label) {
314             this.label = label;
315         }
316     }
317     
318     /** Deserialize an Atom service XML document into an object */
319     public static AtomService documentToService(Document document) {
320         AtomService service = new AtomService();
321         Element root = document.getRootElement();
322         List JavaDoc spaces = root.getChildren("workspace", ns);
323         Iterator JavaDoc iter = spaces.iterator();
324         while (iter.hasNext()) {
325             Element e = (Element) iter.next();
326             service.addWorkspace(AtomService.elementToWorkspace(e));
327         }
328         return service;
329     }
330     
331     /** Serialize an AtomService object into an XML document */
332     public static Document serviceToDocument(AtomService service) {
333         Document doc = new Document();
334         Element root = new Element("service", ns);
335         doc.setRootElement(root);
336         Iterator JavaDoc iter = service.getWorkspaces().iterator();
337         while (iter.hasNext()) {
338             AtomService.Workspace space = (AtomService.Workspace) iter.next();
339             root.addContent(AtomService.workspaceToElement(space));
340         }
341         return doc;
342     }
343     
344     /** Deserialize a Atom workspace XML element into an object */
345     public static AtomService.Workspace elementToWorkspace(Element element) {
346         AtomService.Workspace space = new AtomService.Workspace();
347         
348         Element titleElem = element.getChild("title", atomns);
349         space.setTitle(titleElem.getText());
350         if (titleElem.getAttribute("type", atomns) != null) {
351             space.setTitleType(titleElem.getAttribute("type", atomns).getValue());
352         }
353         
354         List JavaDoc collections = element.getChildren("collection", ns);
355         Iterator JavaDoc iter = collections.iterator();
356         while (iter.hasNext()) {
357             Element e = (Element) iter.next();
358             space.addCollection(AtomService.elementToCollection(e));
359         }
360         return space;
361     }
362     
363     /** Serialize an AtomService.Workspace object into an XML element */
364     public static Element workspaceToElement(Workspace space) {
365         Element element = new Element("workspace", ns);
366         
367         Element title = new Element("title", atomns);
368         title.setText(space.getTitle());
369         element.addContent(title);
370         if (space.getTitleType() != null && !space.getTitleType().equals("TEXT")) {
371             element.setAttribute("type", space.getTitleType(), atomns);
372         }
373         
374         Iterator JavaDoc iter = space.getCollections();
375         while (iter.hasNext()) {
376             AtomService.Collection col = (AtomService.Collection) iter.next();
377             element.addContent(collectionToElement(col));
378         }
379         return element;
380     }
381     
382     /** Deserialize an Atom service collection XML element into an object */
383     public static AtomService.Collection elementToCollection(Element element) {
384         AtomService.Collection collection = new AtomService.Collection();
385         collection.setHref(element.getAttribute("href").getValue());
386         
387         Element titleElem = element.getChild("title", atomns);
388         if (titleElem != null) {
389             collection.setTitle(titleElem.getText());
390             if (titleElem.getAttribute("type", atomns) != null) {
391                 collection.setTitleType(titleElem.getAttribute("type", atomns).getValue());
392             }
393         }
394                 
395         Element memberType = element.getChild("accept", ns);
396         if (memberType != null) {
397             collection.setAccept(memberType.getText());
398         }
399         
400         // Loop to parse <app:categories> element to Categories objects
401
List JavaDoc catsElems = element.getChildren("categories", ns);
402         for (Iterator JavaDoc catsIter = catsElems.iterator(); catsIter.hasNext();) {
403             Element catsElem = (Element) catsIter.next();
404             Categories cats = new Categories();
405             if ("yes".equals(catsElem.getAttribute("fixed", ns))) {
406                 cats.setFixed(true);
407             }
408             // Loop to parse <atom:category> elemenents to Category objects
409
List JavaDoc catElems = catsElem.getChildren("category", atomns);
410             for (Iterator JavaDoc catIter = catElems.iterator(); catIter.hasNext();) {
411                 Element catElem = (Element) catIter.next();
412                 Category cat = new Category();
413                 cat.setTerm(catElem.getAttributeValue("term", atomns));
414                 cat.setLabel(catElem.getAttributeValue("label", atomns));
415                 cat.setScheme(catElem.getAttributeValue("scheme", atomns));
416                 cats.addCategory(cat);
417             }
418             collection.addCategories(cats);
419         }
420         return collection;
421     }
422     
423     /** Serialize an AtomService.Collection object into an XML element */
424     public static Element collectionToElement(AtomService.Collection collection) {
425         Element element = new Element("collection", ns);
426         element.setAttribute("href", collection.getHref());
427                        
428         Element title = new Element("title", atomns);
429         title.setText(collection.getTitle());
430         element.addContent(title);
431         if (collection.getTitleType() != null && !collection.getTitleType().equals("TEXT")) {
432             element.setAttribute("type", collection.getTitleType(), atomns);
433         }
434                     
435         // Loop to create <app:categories> elements
436
for (Iterator JavaDoc it = collection.getCategories(); it.hasNext();) {
437             Categories cats = (Categories)it.next();
438             Element catsElem = new Element("categories", ns);
439             catsElem.setAttribute("fixed", cats.isFixed() ? "yes" : "no", ns);
440             if (cats.getScheme() != null) {
441                 catsElem.setAttribute("scheme", cats.getScheme(), ns);
442             }
443             // Loop to create <atom:category> elements
444
for (Iterator JavaDoc catIter = cats.getCategories(); catIter.hasNext();) {
445                 Category cat = (Category) catIter.next();
446                 Element catElem = new Element("category", atomns);
447                 catElem.setAttribute("term", cat.getTerm(), atomns);
448                 if (cat.getScheme() != null) { // optional
449
catElem.setAttribute("scheme", cat.getScheme(), atomns);
450                 }
451                 if (cat.getLabel() != null) { // optional
452
catElem.setAttribute("label", cat.getLabel(), atomns);
453                 }
454                 catsElem.addContent(catElem);
455             }
456             element.addContent(catsElem);
457         }
458         
459         Element memberType = new Element("accept", ns);
460         memberType.setText(collection.getAccept());
461         element.addContent(memberType);
462         
463         return element;
464     }
465 }
466
467
Popular Tags