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