View Javadoc

1   /**
2    * Copyright 2005-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
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.kuali.rice.krad.uif.util;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.core.api.util.type.TypeUtils;
20  
21  import java.util.List;
22  import java.util.Map;
23  import java.util.Set;
24  
25  /**
26   * Utility class for generating JavaScript
27   *
28   * @author Kuali Rice Team (rice.collab@kuali.org)
29   */
30  public class ScriptUtils {
31  
32      /**
33       * Translates an Object to a String for representing the given Object as
34       * a JavaScript value
35       *
36       * <p>
37       * Handles null, List, Map, and Set collections, along with non quoting for numeric and
38       * boolean types. Complex types are treated as a String value using toString
39       * </p>
40       *
41       * @param value - Object instance to translate
42       * @return String JS value
43       */
44      public static String translateValue(Object value) {
45          String jsValue = "";
46  
47          if (value == null) {
48              jsValue = "null";
49              return jsValue;
50          }
51  
52          if (value instanceof List) {
53              jsValue = "[";
54  
55              List<Object> list = (List<Object>) value;
56              for (Object listItem : list) {
57                  jsValue += translateValue(listItem);
58                  jsValue += ",";
59              }
60              jsValue = StringUtils.removeEnd(jsValue, ",");
61  
62              jsValue += "]";
63          } else if (value instanceof Set) {
64              jsValue = "[";
65  
66              Set<Object> set = (Set<Object>) value;
67              for (Object setItem : set) {
68                  jsValue += translateValue(setItem);
69                  jsValue += ",";
70              }
71              jsValue = StringUtils.removeEnd(jsValue, ",");
72  
73              jsValue += "]";
74          } else if (value instanceof Map) {
75              jsValue = "{";
76  
77              Map<Object, Object> map = (Map<Object, Object>) value;
78              for (Map.Entry<Object, Object> mapEntry : map.entrySet()) {
79                  jsValue += mapEntry.getKey().toString() + ":";
80                  jsValue += translateValue(mapEntry.getValue());
81                  jsValue += ",";
82              }
83              jsValue = StringUtils.removeEnd(jsValue, ",");
84  
85              jsValue += "}";
86          } else {
87              boolean quoteValue = true;
88  
89              Class<?> valueClass = value.getClass();
90              if (TypeUtils.isBooleanClass(valueClass) || TypeUtils.isDecimalClass(valueClass) || TypeUtils
91                      .isIntegralClass(valueClass)) {
92                  quoteValue = false;
93              }
94  
95              if (quoteValue) {
96                  jsValue = "\"";
97              }
98  
99              // TODO: should this go through property editors?
100             jsValue += value.toString();
101 
102             if (quoteValue) {
103                 jsValue += "\"";
104             }
105         }
106 
107         return jsValue;
108     }
109 
110     /**
111      * Escapes the ' character present in collection names so it can be properly used in js without causing
112      * javascript errors due to an early completion of a ' string.
113      * @param name
114      * @return
115      */
116     public static String escapeName(String name) {
117         name = name.replace("'", "\\'");
118         return name;
119     }
120 }