Coverage Report - net.fckeditor.handlers.ResourceType
 
Classes in this File Line Coverage Branch Coverage Complexity
ResourceType
63%
33/52
33%
10/30
2,846
 
 1  
 /*
 2  
  * FCKeditor - The text editor for Internet - http://www.fckeditor.net
 3  
  * Copyright (C) 2004-2010 Frederico Caldeira Knabben
 4  
  * 
 5  
  * == BEGIN LICENSE ==
 6  
  * 
 7  
  * Licensed under the terms of any of the following licenses at your
 8  
  * choice:
 9  
  * 
 10  
  *  - GNU General Public License Version 2 or later (the "GPL")
 11  
  *    http://www.gnu.org/licenses/gpl.html
 12  
  * 
 13  
  *  - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
 14  
  *    http://www.gnu.org/licenses/lgpl.html
 15  
  * 
 16  
  *  - Mozilla Public License Version 1.1 or later (the "MPL")
 17  
  *    http://www.mozilla.org/MPL/MPL-1.1.html
 18  
  * 
 19  
  * == END LICENSE ==
 20  
  */
 21  
 package net.fckeditor.handlers;
 22  
 
 23  
 import java.util.Collections;
 24  
 import java.util.HashMap;
 25  
 import java.util.Map;
 26  
 import java.util.Set;
 27  
 
 28  
 import net.fckeditor.tool.Utils;
 29  
 
 30  
 /**
 31  
  * File Browser resource types. The File Browser provides a specific resource
 32  
  * type for each and every request. This class is intended to reflect these in
 33  
  * an Enum-like manner.
 34  
  * <p>
 35  
  * The resource types are:
 36  
  * <ul>
 37  
  * <li>File</li>
 38  
  * <li>Flash</li>
 39  
  * <li>Image</li>
 40  
  * <li>Media</li>
 41  
  * </ul>
 42  
  * 
 43  
  * @version $Id: ResourceType.java 4785 2009-12-21 20:10:28Z mosipov $
 44  
  */
 45  
 public class ResourceType {
 46  
 
 47  
         /** The name of the resource type */
 48  
         private String name;
 49  
         /** The absolute path of the resource type */
 50  
         private String path;
 51  
         /** The allowed extensions */
 52  
         private Set<String> allowedEextensions;
 53  
         /** The denied extensions */
 54  
         private Set<String> deniedExtensions;
 55  
 
 56  
         /** Map holding a String to ResourceType reference */
 57  1
         private static Map<String, ResourceType> types = new HashMap<String, ResourceType>(
 58  
                         4);
 59  
 
 60  
         /** Resource type <code>File</code> */
 61  1
         public static final ResourceType FILE = new ResourceType("File",
 62  
                         PropertiesLoader.getFileResourceTypePath(), Utils
 63  
                                         .getSet(PropertiesLoader
 64  
                                                         .getFileResourceTypeAllowedExtensions()), Utils
 65  
                                         .getSet(PropertiesLoader
 66  
                                                         .getFileResourceTypeDeniedExtensions()));
 67  
         /** Resource type <code>Flash</code> */
 68  1
         public static final ResourceType FLASH = new ResourceType("Flash",
 69  
                         PropertiesLoader.getFlashResourceTypePath(), Utils
 70  
                                         .getSet(PropertiesLoader
 71  
                                                         .getFlashResourceTypeAllowedExtensions()), Utils
 72  
                                         .getSet(PropertiesLoader
 73  
                                                         .getFlashResourceTypeDeniedExtensions()));
 74  
         /** Resource type <code>Image</code> */
 75  1
         public static final ResourceType IMAGE = new ResourceType("Image",
 76  
                         PropertiesLoader.getImageResourceTypePath(), Utils
 77  
                                         .getSet(PropertiesLoader
 78  
                                                         .getImageResourceTypeAllowedExtensions()), Utils
 79  
                                         .getSet(PropertiesLoader
 80  
                                                         .getImageResourceTypeDeniedExtensions()));
 81  
         /** Resource type <code>Media</code> */
 82  1
         public static final ResourceType MEDIA = new ResourceType("Media",
 83  
                         PropertiesLoader.getMediaResourceTypePath(), Utils
 84  
                                         .getSet(PropertiesLoader
 85  
                                                         .getMediaResourceTypeAllowedExtensions()), Utils
 86  
                                         .getSet(PropertiesLoader
 87  
                                                         .getMediaResourceTypeDeniedExtensions()));
 88  
 
 89  
         static {
 90  1
                 types.put(FILE.getName(), FILE);
 91  1
                 types.put(FLASH.getName(), FLASH);
 92  1
                 types.put(IMAGE.getName(), IMAGE);
 93  1
                 types.put(MEDIA.getName(), MEDIA);
 94  1
         }
 95  
 
 96  
         /**
 97  
          * This constructor has been made intentionally made private to provide
 98  
          * pre-defined types only.
 99  
          * 
 100  
          * @param name
 101  
          *            the name of the new resource type
 102  
          * @param path
 103  
          *            the absolute path of the new resource type
 104  
          * @param allowedEextensions
 105  
          *            the allowed extensions set of the new resource type
 106  
          * @param deniedExtensions
 107  
          *            the denied extensions set of the new resource type
 108  
          * @throws IllegalArgumentException
 109  
          *             if both sets are empty
 110  
          * @throws IllegalArgumentException
 111  
          *             if both sets contain extensions
 112  
          */
 113  
         private ResourceType(final String name, final String path,
 114  
                         final Set<String> allowedEextensions,
 115  4
                         final Set<String> deniedExtensions) {
 116  4
                 this.name = name;
 117  4
                 this.path = path;
 118  
 
 119  4
                 if (allowedEextensions.isEmpty() && deniedExtensions.isEmpty())
 120  0
                         throw new IllegalArgumentException(
 121  
                                         "Both sets are empty, one has always to be filled");
 122  
 
 123  4
                 if (!allowedEextensions.isEmpty() && !deniedExtensions.isEmpty())
 124  0
                         throw new IllegalArgumentException(
 125  
                                         "Both sets contain extensions, only one can be filled at the same time");
 126  
 
 127  4
                 this.allowedEextensions = allowedEextensions;
 128  4
                 this.deniedExtensions = deniedExtensions;
 129  4
         }
 130  
 
 131  
         /**
 132  
          * Returns the name of this resource type.
 133  
          * 
 134  
          * @return the name of this resource type
 135  
          */
 136  
         public String getName() {
 137  4
                 return name;
 138  
         }
 139  
 
 140  
         /**
 141  
          * Returns the absolute path of this resource type. This path is absolute to
 142  
          * the userfiles path. To set this path, see the <a
 143  
          * href="http://java.fckeditor.net/properties.html">configuration</a>.
 144  
          * 
 145  
          * @return the absolute path of this resource type
 146  
          */
 147  
         public String getPath() {
 148  0
                 return path;
 149  
         }
 150  
 
 151  
         /**
 152  
          * Returns a read-only reference to the allowed extensions set.
 153  
          * 
 154  
          * @return the read-only allowed extensions set of this resource type
 155  
          */
 156  
         public Set<String> getAllowedEextensions() {
 157  0
                 return Collections.unmodifiableSet(allowedEextensions);
 158  
         }
 159  
 
 160  
         /**
 161  
          * Returns a read-only reference to the denied extensions set.
 162  
          * 
 163  
          * @return the read-only denied extensions set of this resource type
 164  
          */
 165  
         public Set<String> getDeniedExtensions() {
 166  0
                 return Collections.unmodifiableSet(deniedExtensions);
 167  
         }
 168  
 
 169  
         /**
 170  
          * Returns the resource type constant with the specified name.
 171  
          * 
 172  
          * @param name
 173  
          *            the name of the constant to return
 174  
          * @return the resource type constant with the specified name
 175  
          * @throws IllegalArgumentException
 176  
          *             if this class has no constant with the specified name
 177  
          * @throws NullPointerException
 178  
          *             if <code>name</code> is null or empty
 179  
          */
 180  
         public static ResourceType valueOf(final String name) {
 181  10
                 if (Utils.isEmpty(name))
 182  4
                         throw new NullPointerException("Name is null or empty");
 183  
 
 184  6
                 ResourceType rt = types.get(name);
 185  6
                 if (rt == null)
 186  4
                         throw new IllegalArgumentException("No resource type const " + name);
 187  2
                 return rt;
 188  
         }
 189  
 
 190  
         /**
 191  
          * Returns <code>true</code> if name represents a valid resource type
 192  
          * constant.
 193  
          * 
 194  
          * @param name
 195  
          *            the resource type to check
 196  
          * @return <code>true</code> if name represents a valid resource type, else
 197  
          *         <code>false</code>
 198  
          */
 199  
         public static boolean isValidType(final String name) {
 200  
                 // HashMap permits null keys, so it will return false in that case
 201  3
                 return types.containsKey(name);
 202  
         }
 203  
 
 204  
         /**
 205  
          * Returns the resource type constant with the specified name. In contrast
 206  
          * to {@link #valueOf(String)} it returns a null instead of throwing an
 207  
          * exception if a resource type constant was not found.
 208  
          * 
 209  
          * @param name
 210  
          *            the name of the constant to return
 211  
          * @return the resource type constant with the specified name, else
 212  
          *         <code>null</code>
 213  
          */
 214  
         public static ResourceType getResourceType(final String name) {
 215  
                 try {
 216  10
                         return ResourceType.valueOf(name);
 217  8
                 } catch (Exception e) {
 218  8
                         return null;
 219  
                 }
 220  
         }
 221  
 
 222  
         /**
 223  
          * Returns the resource type constant with the specified name. In contrast
 224  
          * to {@link #getResourceType(String)} it returns {@link #FILE} instead of
 225  
          * returning <code>null</code>.
 226  
          * 
 227  
          * @param name
 228  
          *            the name of the constant to return
 229  
          * @return the resource type constant with the specified name, else
 230  
          *         <code>FILE</code>
 231  
          */
 232  
         public static ResourceType getDefaultResourceType(final String name) {
 233  5
                 ResourceType rt = getResourceType(name);
 234  5
                 return rt == null ? FILE : rt;
 235  
         }
 236  
 
 237  
         /**
 238  
          * Returns <code>true</code> if extension is allowed. Denied extensions set
 239  
          * takes precedence over allowed extensions set, in other words a negative
 240  
          * check is made against denied set and if this fails, allowed set is
 241  
          * checked.
 242  
          * 
 243  
          * @param extension
 244  
          *            the extension to check, empty will fail
 245  
          * @return <code>true</code> if extension is allowed, else
 246  
          *         <code>false</code>
 247  
          */
 248  
         public boolean isAllowedExtension(final String extension) {
 249  0
                 if (Utils.isEmpty(extension))
 250  0
                         return false;
 251  0
                 String ext = extension.toLowerCase();
 252  0
                 if (allowedEextensions.isEmpty())
 253  0
                         return !deniedExtensions.contains(ext);
 254  0
                 if (deniedExtensions.isEmpty())
 255  0
                         return allowedEextensions.contains(ext);
 256  0
                 return false;
 257  
         }
 258  
 
 259  
         /**
 260  
          * Returns <code>true</code> if extension is denied. This method simply
 261  
          * negates {@link #isAllowedExtension(String)}.
 262  
          * 
 263  
          * @param extension
 264  
          *            the extension to check, empty will fail
 265  
          * @return <code>true</code> if extension is denied, else <code>false</code>
 266  
          */
 267  
         public boolean isDeniedExtension(final String extension) {
 268  0
                 return !isAllowedExtension(extension);
 269  
         }
 270  
 
 271  
         /**
 272  
          * Compares the specified object with this resource type for equality. The
 273  
          * comparison is based on class and name only.
 274  
          * 
 275  
          * @param obj
 276  
          *            Object to be compared with this resource type.
 277  
          * @return <code>true</code> if the specified object is equal to this
 278  
          *         resource type
 279  
          */
 280  
         @Override
 281  
         public boolean equals(Object obj) {
 282  4
                 if (this == obj)
 283  4
                         return true;
 284  
 
 285  0
                 if (obj == null || this.getClass() != obj.getClass())
 286  0
                         return false;
 287  
 
 288  0
                 final ResourceType rt = (ResourceType) obj;
 289  0
                 return name.equals(rt.getName());
 290  
         }
 291  
 
 292  
         /**
 293  
          * Returns the hash code value for this resource type. The hash code equals
 294  
          * the hash code of the name field.
 295  
          * 
 296  
          * @return the hash code value for this resource type
 297  
          */
 298  
         @Override
 299  
         public int hashCode() {
 300  0
                 return name.hashCode();
 301  
         }
 302  
 }