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