File Source: Collection.java

         /* 
    P/P   *  Method: org.apache.roller.weblogger.webservices.atomprotocol.Collection__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 com.sun.syndication.feed.atom.Category;
    21  import java.util.ArrayList;
    22  import java.util.Iterator;
    23  import java.util.List;
    24  import org.jdom.Element;
    25  
    26  
    27  /**
    28   * This class models an Atom workspace collection.    
    29   *//* 
    30  	appCollection =
    31  	   element app:collection {
    32     appCommonAttributes,
    33     attribute href { atomURI  },
    34     ( appAccept?	
    35       & appCategories*
    36       & extensionElement* )
    37  	   }
    38   */
    39  public class Collection {
    40      private String title = null;
    41      private String titleType = null; // may be TEXT, HTML, XHTML
    42      private String accept = "entry";
    43      private String listTemplate = null;
    44      private String href = null;
    45      private List categories = new ArrayList(); // of Categories objects    
    46      private List accepts = new ArrayList(); // of Strings
    47      
    48      /**
    49       * Collection MUST have title and href.
    50       * @param title    Title for collection
    51       * @param typeType Content type of title (null for plain text)
    52       * @param href     Collection URI.
    53       */
             /* 
    P/P       *  Method: void org.apache.roller.weblogger.webservices.atomprotocol.Collection(String, String, String)
              * 
              *  Postconditions:
              *    this.accept == &"entry"
              *    this.accepts == &new ArrayList(Collection#2)
              *    this.categories == &new ArrayList(Collection#1)
              *    this.href == href
              *    init'ed(this.href)
              *    this.listTemplate == null
              *    this.title == title
              *    init'ed(this.title)
              *    this.titleType == titleType
              *    init'ed(this.titleType)
              *    ...
              */
    54      public Collection(String title, String titleType, String href) {
    55          this.title = title;
    56          this.titleType = titleType;
    57          this.href = href;
    58      }
    59      
    60      /**
    61       * Comma separated list of media-ranges accepted by collection.
    62       */
    63      public List getAccepts() {
                 /* 
    P/P           *  Method: List getAccepts()
                  * 
                  *  Preconditions:
                  *    init'ed(this.accepts)
                  * 
                  *  Postconditions:
                  *    return_value == this.accepts
                  *    init'ed(return_value)
                  */
    64          return accepts;
    65      }
    66      
    67      public void addAccept(String accept) {
                 /* 
    P/P           *  Method: void addAccept(String)
                  * 
                  *  Preconditions:
                  *    this.accepts != null
                  */
    68          this.accepts.add(accept);
    69      }
    70      
    71      public void setAccepts(List accepts) {
                 /* 
    P/P           *  Method: void setAccepts(List)
                  * 
                  *  Postconditions:
                  *    this.accepts == accepts
                  *    init'ed(this.accepts)
                  */
    72          this.accepts = accepts;
    73      }
    74      
    75      /** The URI of the collection */
    76      public String getHref() {
                 /* 
    P/P           *  Method: String getHref()
                  * 
                  *  Preconditions:
                  *    init'ed(this.href)
                  * 
                  *  Postconditions:
                  *    return_value == this.href
                  *    init'ed(return_value)
                  */
    77          return href;
    78      }
    79      
    80      public void setHref(String href) {
                 /* 
    P/P           *  Method: void setHref(String)
                  * 
                  *  Postconditions:
                  *    this.href == href
                  *    init'ed(this.href)
                  */
    81          this.href = href;
    82      }
    83      
    84      /** Must have human readable title */
    85      public String getTitle() {
                 /* 
    P/P           *  Method: String getTitle()
                  * 
                  *  Preconditions:
                  *    init'ed(this.title)
                  * 
                  *  Postconditions:
                  *    return_value == this.title
                  *    init'ed(return_value)
                  */
    86          return title;
    87      }
    88      
    89      public void setTitle(String title) {
                 /* 
    P/P           *  Method: void setTitle(String)
                  * 
                  *  Postconditions:
                  *    this.title == title
                  *    init'ed(this.title)
                  */
    90          this.title = title;
    91      }
    92  
    93      public String getTitleType() {
                 /* 
    P/P           *  Method: String getTitleType()
                  * 
                  *  Preconditions:
                  *    init'ed(this.titleType)
                  * 
                  *  Postconditions:
                  *    return_value == this.titleType
                  *    init'ed(return_value)
                  */
    94          return titleType;
    95      }
    96  
    97      public void setTitleType(String titleType) {
                 /* 
    P/P           *  Method: void setTitleType(String)
                  * 
                  *  Postconditions:
                  *    this.titleType == titleType
                  *    init'ed(this.titleType)
                  */
    98          this.titleType = titleType;
    99      }
   100  
   101      /** Workspace can have multiple Categories objects */
   102      public void addCategories(Categories cats) {
                 /* 
    P/P           *  Method: void addCategories(Categories)
                  * 
                  *  Preconditions:
                  *    this.categories != null
                  */
   103          categories.add(cats);
   104      }
   105      
   106      public List getCategories() {
                 /* 
    P/P           *  Method: List getCategories()
                  * 
                  *  Preconditions:
                  *    init'ed(this.categories)
                  * 
                  *  Postconditions:
                  *    return_value == this.categories
                  *    init'ed(return_value)
                  */
   107          return categories;
   108      }
   109      
   110      /**
   111       * Returns true if contentType is accepted by collection.
   112       */
   113      public boolean accepts(String ct) {
                 /* 
    P/P           *  Method: bool accepts(String)
                  * 
                  *  Preconditions:
                  *    init'ed(this.accept)
                  *    (soft) ct != null
                  *    (soft) this.accepts != null
                  * 
                  *  Presumptions:
                  *    java.lang.String:indexOf(...)@128 <= 232-2
                  *    java.util.List:size(...)@124 >= 0
                  *    java.util.List:toArray(...)@124 != null
                  *    rules.length@124 <= 232-1
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  * 
                  *  Test Vectors:
                  *    this.accept: Addr_Set{null}, Inverse{null}
                  *    java.lang.String:equals(...)@114: {0}, {1}
                  *    java.lang.String:equals(...)@116: {0}, {1}
                  *    java.lang.String:equals(...)@119: {0}, {1}
                  *    java.lang.String:equals(...)@121: {0}, {1}
                  *    java.lang.String:equals(...)@127: {0}, {1}
                  *    java.lang.String:indexOf(...)@128: {-231..0}, {1..232-2}
                  *    java.lang.String:startsWith(...)@131: {0}, {1}
                  */
   114          if (accept != null && accept.trim().equals("*/*")) return true;
   115          String entryType = "application/atom+xml";
   116          boolean entry = entryType.equals(ct);
   117          if (entry && null == accept) {
   118              return true;
   119          } else if (entry && "entry".equals(accept)) {
   120              return true;
   121          } else if (entry && entryType.equals(accept)) {
   122              return true;
   123          } else {
   124              String[] rules = (String[])accepts.toArray(new String[accepts.size()]);
   125              for (int i=0; i<rules.length; i++) {
+  126                  String rule = rules[i].trim();
   127                  if (rule.equals(ct)) return true;
   128                  int slashstar = rule.indexOf("/*");
   129                  if (slashstar > 0) {
   130                      rule = rule.substring(0, slashstar + 1);
   131                      if (ct.startsWith(rule)) return true;
   132                  }
   133              }
   134          }
   135          return false;
   136      }
   137      
   138      /**
   139       * Serialize an AtomService.Collection into an XML element
   140       */
   141      public static Element collectionToElement(Collection collection) {
                 /* 
    P/P           *  Method: Element collectionToElement(Collection)
                  * 
                  *  Preconditions:
                  *    collection != null
                  *    collection.accepts != null
                  *    collection.categories != null
                  *    init'ed(collection.href)
                  *    init'ed(collection.title)
                  *    init'ed(collection.titleType)
                  *    init'ed(org/apache/roller/weblogger/webservices/atomprotocol/AtomService.ATOM_FORMAT)
                  *    init'ed(org/apache/roller/weblogger/webservices/atomprotocol/AtomService.ATOM_PROTOCOL)
                  * 
                  *  Presumptions:
                  *    cats.categories@154 != null
                  *    java.util.Iterator:next(...)@154 != null
                  *    java.util.Iterator:next(...)@162 != null
                  * 
                  *  Postconditions:
                  *    return_value == &new Element(collectionToElement#1)
                  *    new Element(collectionToElement#1) num objects == 1
                  * 
                  *  Test Vectors:
                  *    collection.titleType: Addr_Set{null}, Inverse{null}
                  *    cats.scheme@154: Addr_Set{null}, Inverse{null}
                  *    com.sun.syndication.feed.atom.Category:getLabel(...)@168: Addr_Set{null}, Inverse{null}
                  *    com.sun.syndication.feed.atom.Category:getScheme(...)@165: Addr_Set{null}, Inverse{null}
                  *    java.lang.String:equals(...)@148: {1}, {0}
                  *    java.util.Iterator:hasNext(...)@153: {0}, {1}
                  *    java.util.Iterator:hasNext(...)@161: {0}, {1}
                  *    java.util.Iterator:hasNext(...)@176: {0}, {1}
                  */
   142          Element element = new Element("collection", AtomService.ATOM_PROTOCOL);
   143          element.setAttribute("href", collection.getHref());
   144                         
   145          Element title = new Element("title", AtomService.ATOM_FORMAT);
   146          title.setText(collection.getTitle());
   147          element.addContent(title);
   148          if (collection.getTitleType() != null && !collection.getTitleType().equals("TEXT")) {
   149              element.setAttribute("type", collection.getTitleType()); //, AtomService.ATOM_FORMAT);
   150          }
   151                      
   152          // Loop to create <app:categories> elements            
   153          for (Iterator it = collection.getCategories().iterator(); it.hasNext();) {
   154              Categories cats = (Categories)it.next();
   155              Element catsElem = new Element("categories", AtomService.ATOM_PROTOCOL);
   156              catsElem.setAttribute("fixed", cats.isFixed() ? "yes" : "no"); //, AtomService.ATOM_PROTOCOL);
   157              if (cats.getScheme() != null) {
   158                  catsElem.setAttribute("scheme", cats.getScheme()); //, AtomService.ATOM_PROTOCOL);
   159              }
   160              // Loop to create <atom:category> elements
   161              for (Iterator catIter = cats.getCategories().iterator(); catIter.hasNext();) {
   162                  Category cat = (Category) catIter.next();
   163                  Element catElem = new Element("category", AtomService.ATOM_FORMAT);
   164                  catElem.setAttribute("term", cat.getTerm()); //, AtomService.ATOM_FORMAT);
   165                  if (cat.getScheme() != null) { // optional
   166                      catElem.setAttribute("scheme", cat.getScheme()); //, AtomService.ATOM_FORMAT);
   167                  }
   168                  if (cat.getLabel() != null) { // optional
   169                      catElem.setAttribute("label", cat.getLabel()); //, AtomService.ATOM_FORMAT);
   170                  }
   171                  catsElem.addContent(catElem);
   172              }
   173              element.addContent(catsElem);
   174          }
   175          
   176          for (Iterator it = collection.getAccepts().iterator(); it.hasNext();) {
   177              String range = (String)it.next();
   178              Element acceptElem = new Element("accept", AtomService.ATOM_PROTOCOL);
   179              acceptElem.setText(range);
   180              element.addContent(acceptElem);
   181          }
   182          
   183          return element;
   184      }
   185      
   186      /** Deserialize an Atom service collection XML element into an object */
   187      public static Collection elementToCollection(Element element) {
                 /* 
    P/P           *  Method: Collection elementToCollection(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(...)@200 != null
                  *    java.util.Iterator:next(...)@208 != null
                  *    java.util.Iterator:next(...)@216 != null
                  *    org.jdom.Element:getAttribute(...)@188 != null
                  *    org.jdom.Element:getAttribute(...)@193 != null
                  *    ...
                  * 
                  *  Postconditions:
                  *    return_value == &new Collection(elementToCollection#1)
                  *    new ArrayList(Collection#1) num objects == 1
                  *    new ArrayList(Collection#2) num objects == 1
                  *    new Collection(elementToCollection#1) num objects == 1
                  *    return_value.accept == &"entry"
                  *    return_value.accepts == &new ArrayList(Collection#2)
                  *    return_value.categories == &new ArrayList(Collection#1)
                  *    init'ed(return_value.href)
                  *    return_value.listTemplate == null
                  *    init'ed(return_value.title)
                  *    ...
                  * 
                  *  Test Vectors:
                  *    java.lang.String:equals(...)@210: {0}, {1}
                  *    java.util.Iterator:hasNext(...)@199: {0}, {1}
                  *    java.util.Iterator:hasNext(...)@207: {0}, {1}
                  *    java.util.Iterator:hasNext(...)@215: {0}, {1}
                  *    java.util.List:size(...)@198: {-231..0}, {1..232-1}
                  *    org.jdom.Element:getAttribute(...)@192: Addr_Set{null}, Inverse{null}
                  *    org.jdom.Element:getChildren(...)@197: Addr_Set{null}, Inverse{null}
                  */
   188          String newHref = element.getAttribute("href").getValue();
   189          Element titleElem = element.getChild("title", AtomService.ATOM_FORMAT);
   190          String newTitle = titleElem.getText();
   191          String newType = null;
   192          if (titleElem.getAttribute("type", AtomService.ATOM_FORMAT) != null) {
   193              newType = titleElem.getAttribute("type", AtomService.ATOM_FORMAT).getValue();
   194          }
   195          Collection collection = new Collection(newTitle, newType, newHref);
   196                  
   197          List acceptElems = element.getChildren("accept",  AtomService.ATOM_PROTOCOL);
   198          if (acceptElems != null && acceptElems.size() > 0) {
   199              for (Iterator it = acceptElems.iterator(); it.hasNext();) {
   200                  Element acceptElem = (Element)it.next();
   201                  collection.addAccept(acceptElem.getTextTrim());
   202              }
   203          }
   204          
   205          // Loop to parse <app:categories> element to Categories objects
   206          List catsElems = element.getChildren("categories", AtomService.ATOM_PROTOCOL);
   207          for (Iterator catsIter = catsElems.iterator(); catsIter.hasNext();) {
   208              Element catsElem = (Element) catsIter.next();  
   209              Categories cats = new Categories();
   210              if ("yes".equals(catsElem.getAttribute("fixed", AtomService.ATOM_PROTOCOL))) {
   211                  cats.setFixed(true);
   212              }
   213              // Loop to parse <atom:category> elemenents to Category objects
   214              List catElems = catsElem.getChildren("category", AtomService.ATOM_FORMAT);
   215              for (Iterator catIter = catElems.iterator(); catIter.hasNext();) {                
   216                  Element catElem = (Element) catIter.next();
   217                  Category cat = new Category();
   218                  cat.setTerm(catElem.getAttributeValue("term"));                
   219                  cat.setLabel(catElem.getAttributeValue("label")); 
   220                  cat.setScheme(catElem.getAttributeValue("scheme"));
   221                  cats.addCategory(cat);
   222              }
   223              collection.addCategories(cats);
   224          }
   225          return collection;
   226      }
   227  
   228  }








SofCheck Inspector Build Version : 2.18479
Collection.java 2009-Jan-02 14:25:38
Collection.class 2009-Sep-04 03:12:46