1 18 package org.apache.roller.webservices.atomprotocol; 19 20 import java.util.ArrayList ; 21 import java.util.Iterator ; 22 import java.util.LinkedHashSet ; 23 import java.util.List ; 24 import java.util.Set ; 25 26 import org.jdom.Document; 27 import org.jdom.Element; 28 import org.jdom.Namespace; 29 30 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 workspaces = new ArrayList (); 92 93 public AtomService() { 94 } 95 96 public void addWorkspace(AtomService.Workspace workspace) { 97 workspaces.add(workspace); 98 } 99 100 public List getWorkspaces() { 101 return workspaces; 102 } 103 104 public void setWorkspaces(List workspaces) { 105 this.workspaces = workspaces; 106 } 107 108 120 public static class Workspace { 121 private String title = null; 122 private String titleType = null; private Set collections = new LinkedHashSet (); 124 125 public Workspace() { 126 } 127 128 129 public Iterator getCollections() { 130 return collections.iterator(); 131 } 132 133 134 public void addCollection(AtomService.Collection col) { 135 collections.add(col); 136 } 137 138 139 public String getTitle() { 140 return title; 141 } 142 143 public void setTitle(String title) { 144 this.title = title; 145 } 146 147 public String getTitleType() { 148 return titleType; 149 } 150 151 public void setTitleType(String titleType) { 152 this.titleType = titleType; 153 } 154 } 155 156 168 public static class Collection { 169 private String title = null; 170 private String titleType =null; private String accept = "entry"; 172 private String listTemplate = null; 173 private String href = null; 174 private Set categories = new LinkedHashSet (); 176 public Collection() { 177 } 178 179 184 public String getAccept() { 185 return accept; 186 } 187 188 public void setAccept(String accept) { 189 this.accept = accept; 190 } 191 192 193 public String getHref() { 194 return href; 195 } 196 197 public void setHref(String href) { 198 this.href = href; 199 } 200 201 202 public String getTitle() { 203 return title; 204 } 205 206 public void setTitle(String title) { 207 this.title = title; 208 } 209 210 public String getTitleType() { 211 return titleType; 212 } 213 214 public void setTitleType(String titleType) { 215 this.titleType = titleType; 216 } 217 218 219 public void addCategories(Categories cats) { 220 categories.add(cats); 221 } 222 223 224 public Iterator getCategories() { 225 return categories.iterator(); 226 } 227 } 228 229 239 public static class Categories { 240 private Set categories = new LinkedHashSet (); private String scheme = null; 242 private boolean fixed = false; 243 244 245 public void addCategory(Category cat) { 246 categories.add(cat); 247 } 248 249 250 public Iterator getCategories() { 251 return categories.iterator(); 252 } 253 254 255 public boolean isFixed() { 256 return fixed; 257 } 258 259 260 public void setFixed(boolean fixed) { 261 this.fixed = fixed; 262 } 263 264 265 public String getScheme() { 266 return scheme; 267 } 268 269 270 public void setScheme(String scheme) { 271 this.scheme = scheme; 272 } 273 } 274 275 276 288 public static class Category { 289 private String term; 290 private String scheme; 291 private String label; 292 293 public String getTerm() { 294 return term; 295 } 296 297 public void setTerm(String term) { 298 this.term = term; 299 } 300 301 public String getScheme() { 302 return scheme; 303 } 304 305 public void setScheme(String scheme) { 306 this.scheme = scheme; 307 } 308 309 public String getLabel() { 310 return label; 311 } 312 313 public void setLabel(String label) { 314 this.label = label; 315 } 316 } 317 318 319 public static AtomService documentToService(Document document) { 320 AtomService service = new AtomService(); 321 Element root = document.getRootElement(); 322 List spaces = root.getChildren("workspace", ns); 323 Iterator 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 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 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 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 collections = element.getChildren("collection", ns); 355 Iterator 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 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 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 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 List catsElems = element.getChildren("categories", ns); 402 for (Iterator 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 List catElems = catsElem.getChildren("category", atomns); 410 for (Iterator 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 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 for (Iterator 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 for (Iterator 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) { catElem.setAttribute("scheme", cat.getScheme(), atomns); 450 } 451 if (cat.getLabel() != null) { 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 |