Coverage Report - net.fckeditor.handlers.ResourceTypeHandler
 
Classes in this File Line Coverage Branch Coverage Complexity
ResourceTypeHandler
90%
36/40
75%
6/8
0
 
 1  
 /*
 2  
  * FCKeditor - The text editor for Internet - http://www.fckeditor.net
 3  
  * Copyright (C) 2003-2008 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.HashMap;
 24  
 import java.util.Map;
 25  
 
 26  
 import net.fckeditor.tool.Utils;
 27  
 
 28  
 /**
 29  
  * Handler for different resource types.
 30  
  * 
 31  
  * @version $Id: ResourceTypeHandler.java 2151 2008-07-02 22:03:15Z mosipov $
 32  
  */
 33  
 public class ResourceTypeHandler {
 34  
 
 35  
         private String name;
 36  1
         private static Map<String, ResourceTypeHandler> types = new HashMap<String, ResourceTypeHandler>(
 37  
                 4);
 38  1
         private static Map<ResourceTypeHandler, String> paths = new HashMap<ResourceTypeHandler, String>(
 39  
                 4);
 40  1
         public static final ResourceTypeHandler FILE = new ResourceTypeHandler("File");
 41  1
         public static final ResourceTypeHandler FLASH = new ResourceTypeHandler("Flash");
 42  1
         public static final ResourceTypeHandler IMAGE = new ResourceTypeHandler("Image");
 43  1
         public static final ResourceTypeHandler MEDIA = new ResourceTypeHandler("Media");
 44  
 
 45  
         static {
 46  
                 // initialize the resource types
 47  1
                 types.put(FILE.getName(), FILE);
 48  1
                 types.put(FLASH.getName(), FLASH);
 49  1
                 types.put(IMAGE.getName(), IMAGE);
 50  1
                 types.put(MEDIA.getName(), MEDIA);
 51  
 
 52  
                 // initialize the sub folders for each resource type
 53  1
                 paths.put(FILE, PropertiesLoader.getProperty("connector.resourceType.file.path"));
 54  1
                 paths.put(IMAGE, PropertiesLoader.getProperty("connector.resourceType.image.path"));
 55  1
                 paths.put(FLASH, PropertiesLoader.getProperty("connector.resourceType.flash.path"));
 56  1
                 paths.put(MEDIA, PropertiesLoader.getProperty("connector.resourceType.media.path"));
 57  1
         }
 58  
 
 59  4
         private ResourceTypeHandler(final String name) {
 60  4
                 this.name = name;
 61  4
         }
 62  
 
 63  
         /**
 64  
          * Getter for the name.
 65  
          * 
 66  
          * @return name
 67  
          */
 68  
         public String getName() {
 69  7
                 return name;
 70  
         }
 71  
 
 72  
         /**
 73  
          * Getter for the resource type path (sub folder).
 74  
          * 
 75  
          * @return The path (sub folder).
 76  
          */
 77  
         public String getPath() {
 78  2
                 return paths.get(this);
 79  
         }
 80  
 
 81  
         /**
 82  
          * Getter for a {@link ResourceTypeHandler} for a specified string.
 83  
          * 
 84  
          * @param name
 85  
          *            A resource type to retrieve.
 86  
          * @return A {@link ResourceTypeHandler} object holding the value
 87  
          *         represented by the string argument.
 88  
          * @throws IllegalArgumentException
 89  
          *            If 'name' is <code>null</code>, empty, or does not exist.
 90  
          */
 91  
         public static ResourceTypeHandler valueOf(final String name) throws IllegalArgumentException {
 92  7
                 if (Utils.isEmpty(name))
 93  1
                         throw new IllegalArgumentException();
 94  
 
 95  6
                 ResourceTypeHandler rt = types.get(name);
 96  6
                 if (rt == null)
 97  3
                         throw new IllegalArgumentException();
 98  3
                 return rt;
 99  
         }
 100  
 
 101  
         /**
 102  
          * 
 103  
          * Checks if a specfied string represents a valid resource type.
 104  
          * 
 105  
          * @param name
 106  
          *            A resource type string to check.
 107  
          * @return <code>true</code> if the string representation is valid else
 108  
          *         <code>false</code>.
 109  
          */
 110  
         public static boolean isValid(final String name) {
 111  3
                 return types.containsKey(name);
 112  
         }
 113  
 
 114  
         /**
 115  
          * Tries to determine ResourceType from string and return {@link #FILE} if
 116  
          * provided string is invalid.
 117  
          * 
 118  
          * @param name
 119  
          * @return resource type
 120  
          */
 121  
         public static ResourceTypeHandler getDefaultResourceType(final String name) {
 122  3
                 ResourceTypeHandler rt = getResourceType(name);
 123  3
                 return rt == null ? FILE : rt;
 124  
         }
 125  
 
 126  
         /**
 127  
          * A wrapper for {@link #valueOf(String)}. It returns <code>null</code>
 128  
          * instead of throwing an exception.
 129  
          * 
 130  
          * @param name
 131  
          *            A resource type string to check.
 132  
          * @return A {@link ResourceTypeHandler} object holding the value
 133  
          *         represented by the string argument, or <code>null</code>.
 134  
          */
 135  
         public static ResourceTypeHandler getResourceType(final String name) {
 136  
                 try {
 137  7
                         return ResourceTypeHandler.valueOf(name);
 138  4
                 } catch (Exception e) {
 139  4
                         return null;
 140  
                 }
 141  
         }
 142  
 
 143  
         /*
 144  
          * (non-Javadoc)
 145  
          * 
 146  
          * @see java.lang.Object#equals(java.lang.Object)
 147  
          */
 148  
         @Override
 149  
         public boolean equals(Object obj) {
 150  3
                 if (obj == null)
 151  0
                         return false;
 152  
                 try {
 153  3
                         ResourceTypeHandler rt = (ResourceTypeHandler) obj;
 154  3
                         return name.equals(rt.getName());
 155  0
                 } catch (ClassCastException e) {
 156  0
                         return false;
 157  
                 }
 158  
         }
 159  
 
 160  
         /*
 161  
          * (non-Javadoc)
 162  
          * 
 163  
          * @see java.lang.Object#toString()
 164  
          */
 165  
         @Override
 166  
         public int hashCode() {
 167  36
                 return name.hashCode();
 168  
         }
 169  
 
 170  
         /*
 171  
          * (non-Javadoc)
 172  
          * 
 173  
          * @see java.lang.Object#toString()
 174  
          */
 175  
         @Override
 176  
         public String toString() {
 177  0
                 return name;
 178  
         }
 179  
 }