File Source: AtomService.java
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.weblogger.webservices.atomprotocol;
19
20
21 import java.util.ArrayList;
22 import java.util.Iterator;
23 import java.util.List;
24
25 import org.jdom.Document;
26 import org.jdom.Element;
27 import org.jdom.Namespace;
28
29 /**
30 * This class models an Atom Publishing Protocol AtomService Document.
31 * Based on: draft-ietf-atompub-protocol-15.txt.
32 * Designed to be Roller independent.
33 */
34 public class AtomService {
35
36 private List workspaces = new ArrayList();
37
38 /** Namespace for Atom Syndication Format */
/*
P/P * Method: org.apache.roller.weblogger.webservices.atomprotocol.AtomService__static_init
*
* Postconditions:
* init'ed(ATOM_FORMAT)
* init'ed(ATOM_PROTOCOL)
*/
39 public static Namespace ATOM_FORMAT =
40 Namespace.getNamespace("atom","http://www.w3.org/2005/Atom");
41
42 /** Namespace for Atom Publishing Protocol */
43 public static Namespace ATOM_PROTOCOL =
44 Namespace.getNamespace("app","http://www.w3.org/2007/app");
45
/*
P/P * Method: void org.apache.roller.weblogger.webservices.atomprotocol.AtomService()
*
* Postconditions:
* this.workspaces == &new ArrayList(AtomService#1)
* new ArrayList(AtomService#1) num objects == 1
*/
46 public AtomService() {
47 }
48
49 public void addWorkspace(Workspace workspace) {
/*
P/P * Method: void addWorkspace(Workspace)
*
* Preconditions:
* this.workspaces != null
*/
50 workspaces.add(workspace);
51 }
52
53 public List getWorkspaces() {
/*
P/P * Method: List getWorkspaces()
*
* Preconditions:
* init'ed(this.workspaces)
*
* Postconditions:
* return_value == this.workspaces
* init'ed(return_value)
*/
54 return workspaces;
55 }
56
57 public void setWorkspaces(List workspaces) {
/*
P/P * Method: void setWorkspaces(List)
*
* Postconditions:
* this.workspaces == workspaces
* init'ed(this.workspaces)
*/
58 this.workspaces = workspaces;
59 }
60
61 public Workspace findWorkspace(String title) {
/*
P/P * Method: Workspace findWorkspace(String)
*
* Preconditions:
* this.workspaces != null
* (soft) title != null
*
* Presumptions:
* java.util.Iterator:next(...)@63 != null
*
* Postconditions:
* (soft) init'ed(return_value)
*
* Test Vectors:
* java.lang.String:equals(...)@64: {0}, {1}
* java.util.Iterator:hasNext(...)@62: {0}, {1}
*/
62 for (Iterator it = workspaces.iterator(); it.hasNext();) {
63 Workspace ws = (Workspace) it.next();
64 if (title.equals(ws.getTitle())) {
65 return ws;
66 }
67 }
68 return null;
69 }
70
71 /** Deserialize an Atom service XML document into an object */
72 public static AtomService documentToService(Document document) {
/*
P/P * Method: AtomService documentToService(Document)
*
* Preconditions:
* init'ed(ATOM_PROTOCOL)
* document != null
* (soft) init'ed(ATOM_FORMAT)
*
* Presumptions:
* java.util.Iterator:next(...)@78 != null
* org.jdom.Document:getRootElement(...)@74 != null
* org.jdom.Element:getChildren(...)@75 != null
*
* Postconditions:
* return_value == &new AtomService(documentToService#1)
* new ArrayList(AtomService#1) num objects == 1
* new AtomService(documentToService#1) num objects == 1
* return_value.workspaces == &new ArrayList(AtomService#1)
*
* Test Vectors:
* java.util.Iterator:hasNext(...)@77: {0}, {1}
*/
73 AtomService service = new AtomService();
74 Element root = document.getRootElement();
75 List spaces = root.getChildren("workspace", ATOM_PROTOCOL);
76 Iterator iter = spaces.iterator();
77 while (iter.hasNext()) {
78 Element e = (Element) iter.next();
79 service.addWorkspace(Workspace.elementToWorkspace(e));
80 }
81 return service;
82 }
83
84 /**
85 * Serialize an AtomService object into an XML document
86 */
87 public static Document serviceToDocument(AtomService service) {
/*
P/P * Method: Document serviceToDocument(AtomService)
*
* Preconditions:
* init'ed(ATOM_PROTOCOL)
* service != null
* service.workspaces != null
* (soft) init'ed(ATOM_FORMAT)
*
* Presumptions:
* java.util.Iterator:next(...)@93 != null
* space.collections@93 != null
*
* Postconditions:
* return_value == &new Document(serviceToDocument#1)
* new Document(serviceToDocument#1) num objects == 1
*
* Test Vectors:
* java.util.Iterator:hasNext(...)@92: {0}, {1}
*/
88 Document doc = new Document();
89 Element root = new Element("service", ATOM_PROTOCOL);
90 doc.setRootElement(root);
91 Iterator iter = service.getWorkspaces().iterator();
92 while (iter.hasNext()) {
93 Workspace space = (Workspace) iter.next();
94 root.addContent(Workspace.workspaceToElement(space));
95 }
96 return doc;
97 }
98
99 }
100
SofCheck Inspector Build Version : 2.18479
| AtomService.java |
2009-Jan-02 14:24:46 |
| AtomService.class |
2009-Sep-04 03:12:46 |