001: /*
002: * Version: MPL 1.1/GPL 2.0/LGPL 2.1
003: *
004: * "The contents of this file are subject to the Mozilla Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License at
007: * http://www.mozilla.org/MPL/
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
011: * License for the specific language governing rights and limitations under
012: * the License.
013: *
014: * The Original Code is ICEfaces 1.5 open source software code, released
015: * November 5, 2006. The Initial Developer of the Original Code is ICEsoft
016: * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)
017: * 2004-2006 ICEsoft Technologies Canada, Corp. All Rights Reserved.
018: *
019: * Contributor(s): _____________________.
020: *
021: * Alternatively, the contents of this file may be used under the terms of
022: * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"
023: * License), in which case the provisions of the LGPL License are
024: * applicable instead of those above. If you wish to allow use of your
025: * version of this file only under the terms of the LGPL License and not to
026: * allow others to use your version of this file under the MPL, indicate
027: * your decision by deleting the provisions above and replace them with
028: * the notice and other provisions required by the LGPL License. If you do
029: * not delete the provisions above, a recipient may use your version of
030: * this file under either the MPL or the LGPL License."
031: *
032: */
033:
034: package com.icesoft.faces.context.effects;
035:
036: import java.text.DecimalFormat;
037: import java.text.NumberFormat;
038: import java.util.*;
039:
040: /**
041: * Utility class to set arguments for effects
042: */
043: public class EffectsArguments {
044:
045: private Map map = new HashMap();
046: private List parameter = new ArrayList();
047: private NumberFormat floatFormat = NumberFormat
048: .getNumberInstance(Locale.US);
049: private String options = null;
050:
051: public EffectsArguments() {
052: floatFormat.setGroupingUsed(false);
053: }
054:
055: public void setParameter(int i, float f) {
056: addParameter(i, floatFormat.format(f));
057: }
058:
059: public void addParameter(int i, String value) {
060: if (i == parameter.size()) {
061: parameter.add(i, value);
062: } else {
063: parameter.set(i, value);
064: }
065: }
066:
067: public void add(String argument, String value) {
068: if (value == null) {
069: return;
070: }
071: map.put(argument, "'" + value + "'");
072: }
073:
074: public void setOptions(String value) {
075: options = value;
076: }
077:
078: public void add(String argument, float value) {
079: map.put(argument, floatFormat.format(value));
080: }
081:
082: public void addFunction(String argument, String function) {
083: map.put(argument, function);
084: }
085:
086: public void add(String arg, boolean value) {
087:
088: map.put(arg, value + "");
089: }
090:
091: public boolean isEmpty() {
092: return map.isEmpty();
093: }
094:
095: public String toString() {
096: if (options != null) {
097: // To prevent javascript injection
098: options = options.replace(')', ' ');
099: options = options.replace('}', ' ');
100: }
101: StringBuffer sb = new StringBuffer(",{");
102: Iterator iter = parameter.iterator();
103: if (iter.hasNext()) {
104: sb.append(", ");
105: }
106: while (iter.hasNext()) {
107: String value = (String) iter.next();
108: sb.append("'").append(value).append("'");
109: if (iter.hasNext()) {
110: sb.append(",");
111: }
112: }
113: if (map.isEmpty()) {
114: if (options != null) {
115: return ",{ " + options + "})";
116: }
117: return ");";
118: }
119:
120: iter = map.keySet().iterator();
121: while (iter.hasNext()) {
122: String arg = (String) iter.next();
123: String value = (String) map.get(arg);
124: sb.append(arg).append(":").append(value);
125: if (iter.hasNext()) {
126: sb.append(",");
127: }
128: }
129: if (options != null) {
130: sb.append(",").append(options);
131: }
132: sb.append("});");
133:
134: return sb.toString();
135: }
136: }
|