Clover coverage report - Code Coverage for tapestry release 4.0.2
Coverage timestamp: Thu Apr 13 2006 10:52:06 EDT
file stats: LOC: 217   Methods: 14
NCLOC: 105   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
ContentType.java 72.2% 97.8% 100% 92.3%
coverage coverage
 1    // Copyright 2004, 2005 The Apache Software Foundation
 2    //
 3    // Licensed under the Apache License, Version 2.0 (the "License");
 4    // you may not use this file except in compliance with the License.
 5    // You may obtain a copy of the License at
 6    //
 7    // http://www.apache.org/licenses/LICENSE-2.0
 8    //
 9    // Unless required by applicable law or agreed to in writing, software
 10    // distributed under the License is distributed on an "AS IS" BASIS,
 11    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12    // See the License for the specific language governing permissions and
 13    // limitations under the License.
 14   
 15    package org.apache.tapestry.util;
 16   
 17    import java.util.HashMap;
 18    import java.util.Map;
 19    import java.util.Set;
 20    import java.util.StringTokenizer;
 21   
 22    import org.apache.hivemind.util.Defense;
 23   
 24    /**
 25    * Represents an HTTP content type. Allows to set various elements like the mime type, the character
 26    * set, and other parameters. This is similar to a number of other implementations of the same
 27    * concept in JAF, etc. We have created this simple implementation to avoid including the whole
 28    * libraries.
 29    *
 30    * @author mindbridge
 31    * @since 3.0
 32    */
 33    public class ContentType
 34    {
 35    private String _baseType = "";
 36   
 37    private String _subType = "";
 38   
 39    private final Map _parameters = new HashMap();
 40   
 41    /**
 42    * Creates a new empty content type
 43    */
 44  288 public ContentType()
 45    {
 46    }
 47   
 48    /**
 49    * Creates a new content type from the argument. The format of the argument has to be
 50    * basetype/subtype(;key=value)*
 51    *
 52    * @param contentType
 53    * the content type that needs to be represented
 54    */
 55  286 public ContentType(String contentType)
 56    {
 57  286 this();
 58  286 parse(contentType);
 59    }
 60   
 61    /**
 62    * Returns true only if the other object is another instance of ContentType, and has the ssame
 63    * baseType, subType and set of parameters.
 64    */
 65  15 public boolean equals(Object o)
 66    {
 67  15 if (o == null)
 68  1 return false;
 69   
 70  14 if (o.getClass() != this.getClass())
 71  1 return false;
 72   
 73  13 ContentType ct = (ContentType) o;
 74   
 75  13 return _baseType.equals(ct._baseType) && _subType.equals(ct._subType)
 76    && _parameters.equals(ct._parameters);
 77    }
 78   
 79    /**
 80    * @return the base type of the content type
 81    */
 82  2 public String getBaseType()
 83    {
 84  2 return _baseType;
 85    }
 86   
 87    /**
 88    * @param baseType
 89    */
 90  288 public void setBaseType(String baseType)
 91    {
 92  288 Defense.notNull(baseType, "baseType");
 93   
 94  288 _baseType = baseType;
 95    }
 96   
 97    /**
 98    * @return the sub-type of the content type
 99    */
 100  2 public String getSubType()
 101    {
 102  2 return _subType;
 103    }
 104   
 105    /**
 106    * @param subType
 107    */
 108  288 public void setSubType(String subType)
 109    {
 110  288 Defense.notNull(subType, "subType");
 111   
 112  288 _subType = subType;
 113    }
 114   
 115    /**
 116    * @return the MIME type of the content type
 117    */
 118  430 public String getMimeType()
 119    {
 120  430 return _baseType + "/" + _subType;
 121    }
 122   
 123    /**
 124    * @return the list of names of parameters in this content type
 125    */
 126  404 public String[] getParameterNames()
 127    {
 128  404 Set parameterNames = _parameters.keySet();
 129  404 return (String[]) parameterNames.toArray(new String[parameterNames.size()]);
 130    }
 131   
 132    /**
 133    * @param key
 134    * the name of the content type parameter
 135    * @return the value of the content type parameter
 136    */
 137  654 public String getParameter(String key)
 138    {
 139  654 Defense.notNull(key, "key");
 140   
 141  654 return (String) _parameters.get(key);
 142    }
 143   
 144    /**
 145    * @param key
 146    * the name of the content type parameter
 147    * @param value
 148    * the value of the content type parameter
 149    */
 150  275 public void setParameter(String key, String value)
 151    {
 152  275 Defense.notNull(key, "key");
 153  275 Defense.notNull(value, "value");
 154   
 155  275 _parameters.put(key.toLowerCase(), value);
 156    }
 157   
 158    /**
 159    * Parses the argument and configures the content type accordingly. The format of the argument
 160    * has to be type/subtype(;key=value)*
 161    *
 162    * @param contentType
 163    * the content type that needs to be represented
 164    */
 165  286 public void parse(String contentType)
 166    {
 167  286 _baseType = "";
 168  286 _subType = "";
 169  286 _parameters.clear();
 170   
 171  286 StringTokenizer tokens = new StringTokenizer(contentType, ";");
 172  286 if (!tokens.hasMoreTokens())
 173  0 return;
 174   
 175  286 String mimeType = tokens.nextToken();
 176  286 StringTokenizer mimeTokens = new StringTokenizer(mimeType, "/");
 177  286 setBaseType(mimeTokens.hasMoreTokens() ? mimeTokens.nextToken() : "");
 178  286 setSubType(mimeTokens.hasMoreTokens() ? mimeTokens.nextToken() : "");
 179   
 180  286 while (tokens.hasMoreTokens())
 181    {
 182  144 String parameter = tokens.nextToken();
 183   
 184  144 StringTokenizer parameterTokens = new StringTokenizer(parameter, "=");
 185  144 String key = parameterTokens.hasMoreTokens() ? parameterTokens.nextToken() : "";
 186  144 String value = parameterTokens.hasMoreTokens() ? parameterTokens.nextToken() : "";
 187  144 setParameter(key, value);
 188    }
 189    }
 190   
 191    /**
 192    * @return the string representation of this content type
 193    */
 194  402 public String unparse()
 195    {
 196  402 StringBuffer buf = new StringBuffer(getMimeType());
 197   
 198  402 String[] parameterNames = getParameterNames();
 199  402 for (int i = 0; i < parameterNames.length; i++)
 200    {
 201  391 String key = parameterNames[i];
 202  391 String value = getParameter(key);
 203  391 buf.append(";" + key + "=" + value);
 204    }
 205   
 206  402 return buf.toString();
 207    }
 208   
 209    /**
 210    * @return the string representation of this content type. Same as unparse().
 211    */
 212  400 public String toString()
 213    {
 214  400 return unparse();
 215    }
 216   
 217    }