View Javadoc

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  	private static Map<String, ResourceTypeHandler> types = new HashMap<String, ResourceTypeHandler>(
35  	        4);
36  	private static Map<ResourceTypeHandler, String> paths = new HashMap<ResourceTypeHandler, String>(
37  	        4);
38  	public static final ResourceTypeHandler FILE = new ResourceTypeHandler("File");
39  	public static final ResourceTypeHandler FLASH = new ResourceTypeHandler("Flash");
40  	public static final ResourceTypeHandler IMAGE = new ResourceTypeHandler("Image");
41  	public static final ResourceTypeHandler MEDIA = new ResourceTypeHandler("Media");
42  
43  	static {
44  		// initialize the resource types
45  		types.put(FILE.getName(), FILE);
46  		types.put(FLASH.getName(), FLASH);
47  		types.put(IMAGE.getName(), IMAGE);
48  		types.put(MEDIA.getName(), MEDIA);
49  
50  		// initialize the sub folders for each resource type
51  		paths.put(FILE, PropertiesLoader.getProperty("connector.resourceType.file.path"));
52  		paths.put(IMAGE, PropertiesLoader.getProperty("connector.resourceType.image.path"));
53  		paths.put(FLASH, PropertiesLoader.getProperty("connector.resourceType.flash.path"));
54  		paths.put(MEDIA, PropertiesLoader.getProperty("connector.resourceType.media.path"));
55  	}
56  
57  	private ResourceTypeHandler(final String name) {
58  		this.name = name;
59  	}
60  
61  	/**
62  	 * Getter for the name.
63  	 * 
64  	 * @return name
65  	 */
66  	public String getName() {
67  		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  		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  		if (name == null)
90  			throw new IllegalArgumentException();
91  
92  		ResourceTypeHandler rt = types.get(name);
93  		if (rt == null)
94  			throw new IllegalArgumentException();
95  		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 		return types.containsKey(name);
106 	}
107 
108 	public static ResourceTypeHandler getDefaultResourceType(final String name) {
109 		ResourceTypeHandler rt = getResourceType(name);
110 		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 			return ResourceTypeHandler.valueOf(name);
123 		} catch (Exception e) {
124 			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 			ResourceTypeHandler rt = (ResourceTypeHandler) obj;
137 			return name.equals(rt.getName());
138 		} catch (ClassCastException e) {
139 			return false;
140 		}
141 	}
142 
143 	/*
144 	 * (non-Javadoc)
145 	 * 
146 	 * @see java.lang.Object#toString()
147 	 */
148 	@Override
149 	public int hashCode() {
150 		return name.hashCode();
151 	}
152 
153 	/*
154 	 * (non-Javadoc)
155 	 * 
156 	 * @see java.lang.Object#toString()
157 	 */
158 	@Override
159 	public String toString() {
160 		return name;
161 	}
162 }