1   /*
2    * Confluence BibSonomy plugin
3    * Copyright (C) 2007 Christian Schenk
4    *
5    * This program is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU General Public License
7    * as published by the Free Software Foundation; either version 2
8    * of the License, or (at your option) any later version.
9    * 
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   * 
15   * You should have received a copy of the GNU General Public License
16   * along with this program; if not, write to the Free Software
17   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
18   */
19  package org.christianschenk.confluence.plugin.bibsonomy.helper;
20  
21  import java.util.Map;
22  
23  import com.atlassian.renderer.v2.macro.MacroException;
24  
25  import static org.christianschenk.confluence.plugin.bibsonomy.helper.ValidationUtils.present;
26  
27  /**
28   * Some helper methods for Confluence.
29   * 
30   * @author Christian Schenk
31   */
32  public class ConfluenceHelper {
33  
34  	/**
35  	 * Convenience method.
36  	 * 
37  	 * @see ConfluenceHelper.getParam(Map params, String[] keys, String defaultValue, boolean required); 
38  	 */
39  	public static String getParam(@SuppressWarnings("unchecked") final Map params, final String[] keys) throws MacroException {
40  		return getParam(params, keys, null, false);
41  	}
42  
43  	/**
44  	 * Convenience method.
45  	 * 
46  	 * @see ConfluenceHelper.getParam(Map params, String[] keys, String defaultValue, boolean required); 
47  	 */
48  	public static String getParam(@SuppressWarnings("unchecked") final Map params, final String[] keys, final String defaultValue) throws MacroException {
49  		return getParam(params, keys, defaultValue, false);
50  	}
51  
52  	/**
53  	 * Convenience method.
54  	 * 
55  	 * @see ConfluenceHelper.getParam(Map params, String[] keys, String defaultValue, boolean required); 
56  	 */
57  	public static String getParam(@SuppressWarnings("unchecked") final Map params, final String[] keys, final boolean required) throws MacroException {
58  		return getParam(params, keys, null, required);
59  	}
60  
61  	/**
62  	 * Returns the value of a specific key from the given map. If the key isn't found and required
63  	 * is set to true, it will throw a MacroException saying that this parameter is missing. If
64  	 * required isn't set to true and the key isn't found in the map, we can fall back to the
65  	 * defaultValue; providing a default value and setting required to true doesn't make sense.
66  	 * 
67  	 * @param params The parameter map
68  	 * @param keys We'll search for these keys in the parameter map
69  	 * @param defaultValue Fall back default value
70  	 * @param required If this is set to true and we don't find the key in the map, we'll throw a MacroException
71  	 * @return The corresponding value for one of the keys
72  	 * @throws MacroException
73  	 */
74  	public static String getParam(@SuppressWarnings("unchecked") final Map params, final String[] keys, final String defaultValue, final boolean required) throws MacroException {
75  		String rVal = defaultValue;
76  
77  		for (final String paramName : keys) {
78  			final String param = (String) params.get(paramName);
79  			if (present(param)) {
80  				rVal = (String) params.get(paramName);
81  				break;
82  			}
83  		}
84  
85  		if (required == true && rVal == null) {
86  			final String msg = "Missing required parameter" + ((keys.length >= 2) ? " '" + keys[1] + "'" : "");
87  			throw new MacroException(msg);
88  		}
89  
90  		return rVal;
91  	}
92  }