View Javadoc
1   /*
2    * Copyright (c) 2011-2024 PrimeFaces Extensions
3    *
4    *  Permission is hereby granted, free of charge, to any person obtaining a copy
5    *  of this software and associated documentation files (the "Software"), to deal
6    *  in the Software without restriction, including without limitation the rights
7    *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    *  copies of the Software, and to permit persons to whom the Software is
9    *  furnished to do so, subject to the following conditions:
10   *
11   *  The above copyright notice and this permission notice shall be included in
12   *  all copies or substantial portions of the Software.
13   *
14   *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20   *  THE SOFTWARE.
21   */
22  package org.primefaces.extensions.util;
23  
24  import org.primefaces.util.EscapeUtils;
25  
26  /**
27   * Builds a JavaScript var object or array string. A simple way to generalized a lot of code used in renderers.
28   *
29   * @author Mark Lassiter / Melloware
30   * @since 6.2
31   */
32  public class JavascriptVarBuilder {
33  
34      private final StringBuilder sb = new StringBuilder();
35  
36      private final boolean isObject;
37  
38      private boolean firstValue = true;
39  
40      private final boolean isVar;
41  
42      /**
43       * Constructs an instance of the builder.
44       *
45       * @param varName the variable name
46       * @param isObject true if build an Object, false if an array.
47       */
48      public JavascriptVarBuilder(final String varName, final boolean isObject) {
49          this.isObject = isObject;
50          isVar = varName != null;
51          if (isVar) {
52              sb.append("var ");
53              sb.append(varName);
54              sb.append("=");
55          }
56          if (isObject) {
57              sb.append("{");
58          }
59          else {
60              sb.append("[");
61          }
62      }
63  
64      /**
65       * Called internally to prepare for next value
66       */
67      private void next() {
68          if (firstValue) {
69              firstValue = false;
70          }
71          else {
72              sb.append(",");
73          }
74      }
75  
76      /**
77       * Appends an Object name/value pair to the object.
78       *
79       * @param propertyName the property name
80       * @param propertyValue the property value
81       * @param quoted if true, the value is quoted and escaped.
82       * @return this builder
83       */
84      public JavascriptVarBuilder appendProperty(final String propertyName, final String propertyValue, final boolean quoted) {
85          next();
86          sb.append(propertyName);
87          sb.append(":");
88          appendText(propertyValue, quoted);
89          return this;
90      }
91  
92      /**
93       * appends a property with the name "rYY_cXX" where YY is the row and XX is he column.
94       *
95       * @param row
96       * @param col
97       * @param propertyValue
98       * @param quoted
99       * @return
100      */
101     public JavascriptVarBuilder appendRowColProperty(final int row, final int col, final String propertyValue, final boolean quoted) {
102         return appendProperty("r" + row + "_c" + col, propertyValue, quoted);
103     }
104 
105     /**
106      * Appends text to the var string
107      *
108      * @param value the value to append
109      * @param quoted if true, the value is quoted and escaped.
110      * @return this builder
111      */
112     public JavascriptVarBuilder appendText(final String value, final boolean quoted) {
113         if (quoted) {
114             sb.append("\"");
115             if (value != null) {
116                 sb.append(EscapeUtils.forJavaScript(value));
117             }
118             sb.append("\"");
119         }
120         else if (value != null) {
121             sb.append(value);
122         }
123         return this;
124     }
125 
126     /**
127      * Appends an array value.
128      *
129      * @param value
130      * @param quoted
131      * @return
132      */
133     public JavascriptVarBuilder appendArrayValue(final String value, final boolean quoted) {
134         next();
135         return appendText(value, quoted);
136     }
137 
138     /**
139      * Closes the array or object.
140      *
141      * @return
142      */
143     public JavascriptVarBuilder closeVar() {
144         if (isObject) {
145             sb.append("}");
146         }
147         else {
148             sb.append("]");
149         }
150 
151         if (isVar) {
152             sb.append(";");
153         }
154         return this;
155     }
156 
157     /**
158      * Returns the string for the var.
159      */
160     @Override
161     public String toString() {
162         return sb.toString();
163     }
164 }