File Source: Workspace.java
/*
P/P * Method: org.apache.roller.weblogger.webservices.atomprotocol.Workspace__static_init
*/
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 import java.util.ArrayList;
21 import java.util.Iterator;
22 import java.util.List;
23 import org.jdom.Element;
24
25
26 /**
27 * This class models an Atom workspace.
28 * @author Dave Johnson
29 *//*
30 appWorkspace =
31 element app:workspace {
32 appCommonAttributes,
33 ( appCollection*
34 & extensionElement* )
35 }
36 atomTitle = element atom:title { atomTextConstruct }
37 */
38 public class Workspace {
39 private String title = null;
40 private String titleType = null; // may be TEXT, HTML, XHTML
41 private List collections = new ArrayList();
42
43 /**
44 * Collection MUST have title.
45 * @param title Title for collection
46 * @param typeType Content type of title (null for plain text)
47 */
/*
P/P * Method: void org.apache.roller.weblogger.webservices.atomprotocol.Workspace(String, String)
*
* Postconditions:
* this.collections == &new ArrayList(Workspace#1)
* this.title == title
* init'ed(this.title)
* this.titleType == titleType
* init'ed(this.titleType)
* new ArrayList(Workspace#1) num objects == 1
*/
48 public Workspace(String title, String titleType) {
49 this.title = title;
50 this.titleType = titleType;
51 }
52
53 /** Iterate over collections in workspace */
54 public List getCollections() {
/*
P/P * Method: List getCollections()
*
* Preconditions:
* init'ed(this.collections)
*
* Postconditions:
* return_value == this.collections
* init'ed(return_value)
*/
55 return collections;
56 }
57
58 /** Add new collection to workspace */
59 public void addCollection(Collection col) {
/*
P/P * Method: void addCollection(Collection)
*
* Preconditions:
* this.collections != null
*/
60 collections.add(col);
61 }
62
63 /**
64 * DefaultWorkspace must have a human readable title
65 */
66 public String getTitle() {
/*
P/P * Method: String getTitle()
*
* Preconditions:
* init'ed(this.title)
*
* Postconditions:
* return_value == this.title
* init'ed(return_value)
*/
67 return title;
68 }
69
70 public void setTitle(String title) {
/*
P/P * Method: void setTitle(String)
*
* Postconditions:
* this.title == title
* init'ed(this.title)
*/
71 this.title = title;
72 }
73
74 public String getTitleType() {
/*
P/P * Method: String getTitleType()
*
* Preconditions:
* init'ed(this.titleType)
*
* Postconditions:
* return_value == this.titleType
* init'ed(return_value)
*/
75 return titleType;
76 }
77
78 public void setTitleType(String titleType) {
/*
P/P * Method: void setTitleType(String)
*
* Postconditions:
* this.titleType == titleType
* init'ed(this.titleType)
*/
79 this.titleType = titleType;
80 }
81
82 public Collection findCollection(String title, String contentType) {
/*
P/P * Method: Collection findCollection(String, String)
*
* Preconditions:
* this.collections != null
* (soft) contentType != null
*
* Presumptions:
* col.accepts@84 != null
* java.util.Iterator:next(...)@84 != null
*
* Postconditions:
* (soft) init'ed(return_value)
*
* Test Vectors:
* title: Addr_Set{null}, Inverse{null}
* java.util.Iterator:hasNext(...)@83: {0}, {1}
*/
83 for (Iterator it = collections.iterator(); it.hasNext();) {
84 Collection col = (Collection) it.next();
85 if (title != null && col.accepts(contentType)) {
86 return col;
87 } else if (col.accepts(contentType)) {
88 return col;
89 }
90 }
91 return null;
92 }
93
94 /** Deserialize a Atom workspace XML element into an object */
95 public static Workspace elementToWorkspace(Element element) {
/*
P/P * Method: Workspace elementToWorkspace(Element)
*
* Preconditions:
* element != null
* init'ed(org/apache/roller/weblogger/webservices/atomprotocol/AtomService.ATOM_FORMAT)
* init'ed(org/apache/roller/weblogger/webservices/atomprotocol/AtomService.ATOM_PROTOCOL)
*
* Presumptions:
* java.util.Iterator:next(...)@106 != null
* org.jdom.Element:getAttribute(...)@100 != null
* org.jdom.Element:getChild(...)@96 != null
* org.jdom.Element:getChildren(...)@103 != null
*
* Postconditions:
* return_value == &new Workspace(elementToWorkspace#1)
* new ArrayList(Workspace#1) num objects == 1
* new Workspace(elementToWorkspace#1) num objects == 1
* return_value.collections == &new ArrayList(Workspace#1)
* init'ed(return_value.title)
* init'ed(return_value.titleType)
*
* Test Vectors:
* java.util.Iterator:hasNext(...)@105: {0}, {1}
* org.jdom.Element:getAttribute(...)@99: Addr_Set{null}, Inverse{null}
*/
96 Element titleElem = element.getChild("title", AtomService.ATOM_FORMAT);
97 String newTitle = titleElem.getText();
98 String newType = null;
99 if (titleElem.getAttribute("type", AtomService.ATOM_FORMAT) != null) {
100 newType = titleElem.getAttribute("type", AtomService.ATOM_FORMAT).getValue();
101 }
102 Workspace space = new Workspace(newTitle, newType);
103 List collections = element.getChildren("collection", AtomService.ATOM_PROTOCOL);
104 Iterator iter = collections.iterator();
105 while (iter.hasNext()) {
106 Element e = (Element) iter.next();
107 space.addCollection(Collection.elementToCollection(e));
108 }
109 return space;
110 }
111
112 /**
113 * Serialize an AtomService.DefaultWorkspace object into an XML element
114 */
115 public static Element workspaceToElement(Workspace space) {
/*
P/P * Method: Element workspaceToElement(Workspace)
*
* Preconditions:
* init'ed(org/apache/roller/weblogger/webservices/atomprotocol/AtomService.ATOM_FORMAT)
* init'ed(org/apache/roller/weblogger/webservices/atomprotocol/AtomService.ATOM_PROTOCOL)
* space != null
* space.collections != null
* init'ed(space.title)
* init'ed(space.titleType)
*
* Presumptions:
* col.accepts@127 != null
* col.categories@127 != null
* java.util.Iterator:next(...)@127 != null
*
* Postconditions:
* return_value == &new Element(workspaceToElement#1)
* new Element(workspaceToElement#1) num objects == 1
*
* Test Vectors:
* space.titleType: Addr_Set{null}, Inverse{null}
* java.lang.String:equals(...)@121: {1}, {0}
* java.util.Iterator:hasNext(...)@126: {0}, {1}
*/
116 Element element = new Element("workspace", AtomService.ATOM_PROTOCOL);
117
118 Element title = new Element("title", AtomService.ATOM_FORMAT);
119 title.setText(space.getTitle());
120 element.addContent(title);
121 if (space.getTitleType() != null && !space.getTitleType().equals("TEXT")) {
122 title.setAttribute("type", space.getTitleType()); //, AtomService.ATOM_FORMAT);
123 }
124
125 Iterator iter = space.getCollections().iterator();
126 while (iter.hasNext()) {
127 Collection col = (Collection) iter.next();
128 element.addContent(Collection.collectionToElement(col));
129 }
130 return element;
131 }
132
133 }
SofCheck Inspector Build Version : 2.18479
| Workspace.java |
2009-Jan-02 14:25:10 |
| Workspace.class |
2009-Sep-04 03:12:46 |