1 16 package org.roller.presentation.atomapi; 17 18 import java.util.ArrayList ; 19 import java.util.Iterator ; 20 import java.util.List ; 21 22 import org.jdom.Document; 23 import org.jdom.Element; 24 import org.jdom.Namespace; 25 import org.jdom.filter.Filter; 26 27 32 61 public class AtomService 62 { 63 public static final Namespace ns = 64 Namespace.getNamespace("http://purl.org/atom/app#"); 65 private List workspaces = new ArrayList (); 66 67 public AtomService() 68 { 69 } 70 71 public void addWorkspace(AtomService.Workspace workspace) 72 { 73 workspaces.add(workspace); 74 } 75 76 public List getWorkspaces() 77 { 78 return workspaces; 79 } 80 81 public void setWorkspaces(List workspaces) 82 { 83 this.workspaces = workspaces; 84 } 85 86 91 95 public static class Workspace 96 { 97 private String title = null; 98 private List collections = new ArrayList (); 99 100 public Workspace() 101 { 102 } 103 104 public List getCollections() 105 { 106 return collections; 107 } 108 109 public void setCollections(List collections) 110 { 111 this.collections = collections; 112 } 113 114 public void addCollection(AtomService.Collection col) 115 { 116 collections.add(col); 117 } 118 119 120 public String getTitle() 121 { 122 return title; 123 } 124 125 public void setTitle(String title) 126 { 127 this.title = title; 128 } 129 } 130 131 136 140 public static class Collection 141 { 142 private String title; 143 private String contents = "generic"; 144 private String href; 145 146 public Collection() 147 { 148 } 149 150 154 public String getContents() 155 { 156 return contents; 157 } 158 159 public void setContents(String contents) 160 { 161 this.contents = contents; 162 } 163 164 165 public String getHref() 166 { 167 return href; 168 } 169 170 public void setHref(String href) 171 { 172 this.href = href; 173 } 174 175 176 public String getTitle() 177 { 178 return title; 179 } 180 181 public void setTitle(String title) 182 { 183 this.title = title; 184 } 185 } 186 187 188 public static AtomService documentToService(Document document) 189 { 190 AtomService service = new AtomService(); 191 Element root = document.getRootElement(); 192 List spaces = root.getChildren("workspace", ns); 193 Iterator 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 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 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 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 collections = element.getChildren("collection", ns); 223 Iterator 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 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 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 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 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 |