KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ajaxtags > tags > OptionsBuilder


1 /**
2  * Copyright 2005 Darren L. Spurgeon
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.ajaxtags.tags;
17
18 import java.util.HashMap JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.Map JavaDoc;
21
22 /**
23  * Helper class to assist in building options passed to JavaScript method.
24  *
25  * @author Darren Spurgeon
26  * @version $Revision: 1.2 $ $Date: 2007/06/20 20:55:56 $ $Author: jenskapitza $
27  */

28 public class OptionsBuilder {
29
30     private Map JavaDoc<String JavaDoc, String JavaDoc> parameters = new HashMap JavaDoc<String JavaDoc, String JavaDoc>();
31
32     private Map JavaDoc<String JavaDoc, Boolean JavaDoc> parameterQuotes = new HashMap JavaDoc<String JavaDoc, Boolean JavaDoc>();
33
34     public OptionsBuilder add(String JavaDoc parameter, String JavaDoc value, boolean quoted) {
35         this.parameters.put(parameter, value);
36         this.parameterQuotes.put(parameter, Boolean.valueOf(quoted));
37         return this;
38     }
39
40     public OptionsBuilder remove(String JavaDoc parameter) {
41         this.parameters.remove(parameter);
42         this.parameterQuotes.remove(parameter);
43         return this;
44     }
45
46     @Override JavaDoc
47     public String JavaDoc toString() {
48         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
49         for (Iterator JavaDoc<String JavaDoc> iter = this.parameters.keySet().iterator(); iter
50                 .hasNext();) {
51             String JavaDoc key = iter.next();
52             String JavaDoc value = this.parameters.get(key);
53             boolean quoted = this.parameterQuotes.get(key).booleanValue();
54             sb.append(key).append(": ");
55             if (quoted) {
56                 sb.append("\"").append(value).append("\"");
57             } else {
58                 sb.append(value);
59             }
60             if (iter.hasNext()) {
61                 sb.append(",");
62             }
63             sb.append("\n");
64         }
65         return sb.toString();
66     }
67
68 }
69
Popular Tags