View Javadoc

1   /*
2    * FCKeditor - The text editor for Internet - http://www.fckeditor.net
3    * Copyright (C) 2004-2009 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 3840 2009-07-08 20:29:46Z mosipov $
32   */
33  public class ResourceTypeHandler {
34  
35  	private String name;
36  	private static Map<String, ResourceTypeHandler> types = new HashMap<String, ResourceTypeHandler>(
37  	        4);
38  	private static Map<ResourceTypeHandler, String> paths = new HashMap<ResourceTypeHandler, String>(
39  	        4);
40  	public static final ResourceTypeHandler FILE = new ResourceTypeHandler("File");
41  	public static final ResourceTypeHandler FLASH = new ResourceTypeHandler("Flash");
42  	public static final ResourceTypeHandler IMAGE = new ResourceTypeHandler("Image");
43  	public static final ResourceTypeHandler MEDIA = new ResourceTypeHandler("Media");
44  
45  	static {
46  		// initialize the resource types
47  		types.put(FILE.getName(), FILE);
48  		types.put(FLASH.getName(), FLASH);
49  		types.put(IMAGE.getName(), IMAGE);
50  		types.put(MEDIA.getName(), MEDIA);
51  
52  		// initialize the sub folders for each resource type
53  		paths.put(FILE, PropertiesLoader.getProperty("connector.resourceType.file.path"));
54  		paths.put(IMAGE, PropertiesLoader.getProperty("connector.resourceType.image.path"));
55  		paths.put(FLASH, PropertiesLoader.getProperty("connector.resourceType.flash.path"));
56  		paths.put(MEDIA, PropertiesLoader.getProperty("connector.resourceType.media.path"));
57  	}
58  
59  	private ResourceTypeHandler(final String name) {
60  		this.name = name;
61  	}
62  
63  	/**
64  	 * Getter for the name.
65  	 * 
66  	 * @return name
67  	 */
68  	public String getName() {
69  		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  		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  		if (Utils.isEmpty(name))
93  			throw new IllegalArgumentException();
94  
95  		ResourceTypeHandler rt = types.get(name);
96  		if (rt == null)
97  			throw new IllegalArgumentException();
98  		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 		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 		ResourceTypeHandler rt = getResourceType(name);
123 		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 			return ResourceTypeHandler.valueOf(name);
138 		} catch (Exception e) {
139 			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 		if (obj == null)
151 			return false;
152 		try {
153 			ResourceTypeHandler rt = (ResourceTypeHandler) obj;
154 			return name.equals(rt.getName());
155 		} catch (ClassCastException e) {
156 			return false;
157 		}
158 	}
159 
160 	/*
161 	 * (non-Javadoc)
162 	 * 
163 	 * @see java.lang.Object#toString()
164 	 */
165 	@Override
166 	public int hashCode() {
167 		return name.hashCode();
168 	}
169 
170 	/*
171 	 * (non-Javadoc)
172 	 * 
173 	 * @see java.lang.Object#toString()
174 	 */
175 	@Override
176 	public String toString() {
177 		return name;
178 	}
179 }