Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
CommandHandler |
|
| 0.0;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 get and post commands. | |
28 | * | |
29 | * @version $Id: CommandHandler.java 1905 2008-04-10 15:32:00Z th-schwarz $ | |
30 | */ | |
31 | public class CommandHandler { | |
32 | ||
33 | private String name; | |
34 | 1 | private static Map<String, CommandHandler> getCommands = new HashMap<String, CommandHandler>( |
35 | 3); | |
36 | 1 | private static Map<String, CommandHandler> postCommands = new HashMap<String, CommandHandler>( |
37 | 2); | |
38 | 1 | public static final CommandHandler GET_FOLDERS = new CommandHandler("GetFolders"); |
39 | 1 | public static final CommandHandler GET_FOLDERS_AND_FILES = new CommandHandler("GetFoldersAndFiles"); |
40 | 1 | public static final CommandHandler CREATE_FOLDER = new CommandHandler("CreateFolder"); |
41 | 1 | public static final CommandHandler FILE_UPLOAD = new CommandHandler("FileUpload"); |
42 | 1 | public static final CommandHandler QUICK_UPLOAD = new CommandHandler("QuickUpload"); |
43 | ||
44 | static { | |
45 | // initialize the get commands | |
46 | 1 | getCommands.put(GET_FOLDERS.getName(), GET_FOLDERS); |
47 | 1 | getCommands.put(GET_FOLDERS_AND_FILES.getName(), GET_FOLDERS_AND_FILES); |
48 | 1 | getCommands.put(CREATE_FOLDER.getName(), CREATE_FOLDER); |
49 | ||
50 | // initialize the post commands | |
51 | 1 | postCommands.put(FILE_UPLOAD.getName(), FILE_UPLOAD); |
52 | 1 | postCommands.put(QUICK_UPLOAD.getName(), QUICK_UPLOAD); |
53 | 1 | } |
54 | ||
55 | 5 | private CommandHandler(final String name) { |
56 | 5 | this.name = name; |
57 | 5 | } |
58 | ||
59 | /** | |
60 | * Getter for the name. | |
61 | * | |
62 | * @return name | |
63 | */ | |
64 | public String getName() { | |
65 | 12 | return name; |
66 | } | |
67 | ||
68 | /** | |
69 | * Getter for an {@link CommandHandler} of a specified string. | |
70 | * | |
71 | * @param name | |
72 | * @return A {@link CommandHandler} object holding the value represented by the string | |
73 | * argument. | |
74 | * @throws IllegalArgumentException | |
75 | * If 'name' is null or can't be parsed. | |
76 | */ | |
77 | public static CommandHandler valueOf(final String name) throws IllegalArgumentException { | |
78 | 10 | if (name == null) |
79 | 2 | throw new IllegalArgumentException(); |
80 | ||
81 | 8 | if (!isValidForGet(name) && !isValidForPost(name)) |
82 | 2 | throw new IllegalArgumentException(); |
83 | 6 | return (getCommands.get(name) != null) ? getCommands.get(name) : postCommands.get(name); |
84 | } | |
85 | ||
86 | ||
87 | /** | |
88 | * Checks, if a specfied string is a valid representation of a get command. | |
89 | * | |
90 | * @param name | |
91 | * @return True, if the string representation is valid, or false. | |
92 | */ | |
93 | public static boolean isValidForGet(final String name) { | |
94 | 8 | return getCommands.containsKey(name); |
95 | } | |
96 | ||
97 | /** | |
98 | * Checks, if a specfied string is a valid representation of a post command. | |
99 | * | |
100 | * @param name | |
101 | * @return True, if the string representation is valid, or false. | |
102 | */ | |
103 | public static boolean isValidForPost(final String name) { | |
104 | 5 | return postCommands.containsKey(name); |
105 | } | |
106 | ||
107 | ||
108 | /** | |
109 | * A wrapper for {@link #valueOf(String)}. It returns null instead of throwing an exception. | |
110 | * | |
111 | * @param name | |
112 | * @return A {@link CommandHandler} object holding the value represented by the string | |
113 | * argument, or null. | |
114 | */ | |
115 | public static CommandHandler getCommand(final String name) { | |
116 | try { | |
117 | 3 | return CommandHandler.valueOf(name); |
118 | 2 | } catch (Exception e) { |
119 | 2 | return null; |
120 | } | |
121 | } | |
122 | ||
123 | /* | |
124 | * (non-Javadoc) | |
125 | * | |
126 | * @see java.lang.Object#equals(java.lang.Object) | |
127 | */ | |
128 | @Override | |
129 | public boolean equals(Object obj) { | |
130 | try { | |
131 | 8 | CommandHandler rt = (CommandHandler) obj; |
132 | 7 | return name.equals(rt.getName()); |
133 | 1 | } catch (ClassCastException e) { |
134 | 1 | return false; |
135 | } | |
136 | } | |
137 | ||
138 | /* | |
139 | * (non-Javadoc) | |
140 | * | |
141 | * @see java.lang.Object#toString() | |
142 | */ | |
143 | @Override | |
144 | public int hashCode() { | |
145 | 2 | return name.hashCode(); |
146 | } | |
147 | ||
148 | /* | |
149 | * (non-Javadoc) | |
150 | * | |
151 | * @see java.lang.Object#toString() | |
152 | */ | |
153 | @Override | |
154 | public String toString() { | |
155 | 1 | return name; |
156 | } | |
157 | } |