File Source: URLUtilities.java

         /* 
    P/P   *  Method: org.apache.roller.weblogger.util.URLUtilities__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  
    19  package org.apache.roller.weblogger.util;
    20  
    21  import java.io.UnsupportedEncodingException;
    22  import java.net.URLDecoder;
    23  import java.net.URLEncoder;
    24  import java.util.Iterator;
    25  import java.util.List;
    26  import java.util.Map;
    27  
    28  
    29  /**
    30   * Some utility methods used when dealing with urls.
    31   */
    32  public final class URLUtilities {
    33      
    34      // non-intantiable
             /* 
    P/P       *  Method: void org.apache.roller.weblogger.util.URLUtilities()
              */
    35      private URLUtilities() {}
    36      
    37      
    38      /**
    39       * Compose a map of key=value params into a query string.
    40       */
    41      public static final String getQueryString(Map params) {
    42          
                 /* 
    P/P           *  Method: String getQueryString(Map)
                  * 
                  *  Presumptions:
                  *    java.util.Map:keySet(...)@49 != null
                  * 
                  *  Postconditions:
                  *    java.lang.StringBuffer:toString(...)._tainted == 0
                  *    return_value in Addr_Set{null,&java.lang.StringBuffer:toString(...)}
                  * 
                  *  Test Vectors:
                  *    params: Inverse{null}, Addr_Set{null}
                  *    java.lang.StringBuffer:length(...)@53: {-231..-1, 1..232-1}, {0}
                  *    java.util.Iterator:hasNext(...)@49: {0}, {1}
                  */
    43          if(params == null) {
    44              return null;
    45          }
    46          
    47          StringBuffer queryString = new StringBuffer();
    48          
    49          for(Iterator keys = params.keySet().iterator(); keys.hasNext();) {
    50              String key = (String) keys.next();
    51              String value = (String) params.get(key);
    52              
    53              if (queryString.length() == 0) {
    54                  queryString.append("?");
    55              } else {
    56                  queryString.append("&");
    57              }
    58              
    59              queryString.append(key);
    60              queryString.append("=");
    61              queryString.append(value);
    62          }
    63          
    64          return queryString.toString();
    65      }
    66      
    67      
    68      /**
    69       * URL encode a string using UTF-8.
    70       */
    71      public static final String encode(String str) {
                 /* 
    P/P           *  Method: String encode(String)
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
    72          String encodedStr = str;
    73          try {
    74              encodedStr = URLEncoder.encode(str, "UTF-8");
    75          } catch (UnsupportedEncodingException ex) {
    76              // ignored
    77          }
    78          return encodedStr;
    79      }
    80      
    81      
    82      /**
    83       * URL decode a string using UTF-8.
    84       */
    85      public static final String decode(String str) {
                 /* 
    P/P           *  Method: String decode(String)
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
    86          String decodedStr = str;
    87          try {
    88              decodedStr = URLDecoder.decode(str, "UTF-8");
    89          } catch (UnsupportedEncodingException ex) {
    90              // ignored
    91          }
    92          return decodedStr;
    93      }
    94      
    95      
    96      public static final String getEncodedTagsString(List tags) {
                 /* 
    P/P           *  Method: String getEncodedTagsString(List)
                  * 
                  *  Postconditions:
                  *    java.lang.StringBuffer:toString(...)._tainted == 0
                  *    return_value == &java.lang.StringBuffer:toString(...)
                  * 
                  *  Test Vectors:
                  *    tags: Addr_Set{null}, Inverse{null}
                  *    java.util.Iterator:hasNext(...)@107: {0}, {1}
                  *    java.util.List:size(...)@98: {-231..0}, {1..232-1}
                  */
    97          StringBuffer tagsString = new StringBuffer();
    98          if(tags != null && tags.size() > 0) {
    99              String tag = null;
   100              Iterator tagsIT = tags.iterator();
   101              
   102              // do first tag
   103              tag = (String) tagsIT.next();
   104              tagsString.append(encode(tag));
   105              
   106              // do rest of tags, joining them with a '+'
   107              while(tagsIT.hasNext()) {
   108                  tag = (String) tagsIT.next();
   109                  tagsString.append("+");
   110                  tagsString.append(encode(tag));
   111              }
   112          }
   113          return tagsString.toString();
   114      }
   115      
   116          
   117      /**
   118       * URL encode a path string using UTF-8. The path seprator '/' will not be encoded
   119       */
   120      public static final String encodePath(String path) {
                 /* 
    P/P           *  Method: String encodePath(String)
                  * 
                  *  Preconditions:
                  *    path != null
                  * 
                  *  Presumptions:
                  *    java.lang.String:indexOf(...)@121 <= 232-2
                  * 
                  *  Postconditions:
                  *    init'ed(java.lang.StringBuffer:toString(...)._tainted)
                  *    return_value == &java.lang.StringBuffer:toString(...)
                  */
   121          int i = path.indexOf('/');
   122          StringBuffer sb = new StringBuffer();
   123          while (i != -1) {
   124              sb.append(encode(path.substring(0, i))).append('/');
   125              path = path.substring(i + 1);
   126              i = path.indexOf('/');
   127          }
   128          sb.append(encode(path));
   129          return sb.toString();
   130      }
   131  }
   132  
   133  








SofCheck Inspector Build Version : 2.18479
URLUtilities.java 2009-Jan-02 14:24:44
URLUtilities.class 2009-Sep-04 03:12:32