1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.myfaces.shared.renderkit.html; |
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
public class JavascriptContext |
27 | |
{ |
28 | 1 | private static final String LINE_SEPARATOR = System.getProperty( |
29 | |
"line.separator", "\r\n"); |
30 | |
private static final char TABULATOR = '\t'; |
31 | |
|
32 | |
private long currentIndentationLevel; |
33 | 18 | private StringBuilder buffer = new StringBuilder(); |
34 | 18 | private boolean prettyPrint = false; |
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | 18 | private boolean automaticFormatting = true; |
42 | |
|
43 | |
public JavascriptContext() |
44 | 18 | { |
45 | |
|
46 | 18 | } |
47 | |
|
48 | |
public JavascriptContext(boolean prettyPrint) |
49 | 0 | { |
50 | 0 | this.prettyPrint = prettyPrint; |
51 | 0 | } |
52 | |
|
53 | |
public JavascriptContext(StringBuilder buf, boolean prettyPrint) |
54 | 0 | { |
55 | 0 | this.prettyPrint = prettyPrint; |
56 | 0 | this.buffer = buf; |
57 | 0 | } |
58 | |
|
59 | |
public void increaseIndent() |
60 | |
{ |
61 | 0 | currentIndentationLevel++; |
62 | 0 | } |
63 | |
|
64 | |
public void decreaseIndent() |
65 | |
{ |
66 | 0 | currentIndentationLevel--; |
67 | |
|
68 | 0 | if (currentIndentationLevel < 0) |
69 | |
{ |
70 | 0 | currentIndentationLevel = 0; |
71 | |
} |
72 | 0 | } |
73 | |
|
74 | |
public void prettyLine() |
75 | |
{ |
76 | 0 | if (prettyPrint) |
77 | |
{ |
78 | 0 | append(LINE_SEPARATOR); |
79 | |
|
80 | 0 | for (int i = 0; i < getCurrentIndentationLevel(); i++) |
81 | |
{ |
82 | 0 | append(TABULATOR); |
83 | |
} |
84 | |
} |
85 | 0 | } |
86 | |
|
87 | |
public void prettyLineIncreaseIndent() |
88 | |
{ |
89 | 0 | increaseIndent(); |
90 | 0 | prettyLine(); |
91 | 0 | } |
92 | |
|
93 | |
public void prettyLineDecreaseIndent() |
94 | |
{ |
95 | 0 | decreaseIndent(); |
96 | 0 | prettyLine(); |
97 | 0 | } |
98 | |
|
99 | |
public long getCurrentIndentationLevel() |
100 | |
{ |
101 | 0 | return currentIndentationLevel; |
102 | |
} |
103 | |
|
104 | |
public void setCurrentIndentationLevel(long currentIndentationLevel) |
105 | |
{ |
106 | 0 | this.currentIndentationLevel = currentIndentationLevel; |
107 | 0 | } |
108 | |
|
109 | |
public JavascriptContext append(String str) |
110 | |
{ |
111 | |
|
112 | 40 | if (automaticFormatting && str.length() == 1) |
113 | |
{ |
114 | 0 | boolean openBlock = str.equals("{"); |
115 | 0 | boolean closeBlock = str.equals("}"); |
116 | |
|
117 | 0 | if (openBlock) |
118 | |
{ |
119 | 0 | prettyLine(); |
120 | |
} |
121 | 0 | else if (closeBlock) |
122 | |
{ |
123 | 0 | prettyLineDecreaseIndent(); |
124 | |
} |
125 | |
|
126 | 0 | buffer.append(str); |
127 | |
|
128 | 0 | if (openBlock) |
129 | |
{ |
130 | 0 | prettyLineIncreaseIndent(); |
131 | |
} |
132 | 0 | else if (closeBlock) |
133 | |
{ |
134 | 0 | prettyLine(); |
135 | |
} |
136 | 0 | } |
137 | |
else |
138 | |
{ |
139 | 40 | buffer.append(str); |
140 | |
} |
141 | 40 | return this; |
142 | |
} |
143 | |
|
144 | |
public JavascriptContext append(char c) |
145 | |
{ |
146 | 0 | buffer.append(c); |
147 | 0 | return this; |
148 | |
} |
149 | |
|
150 | |
public JavascriptContext append(int i) |
151 | |
{ |
152 | 0 | buffer.append(i); |
153 | 0 | return this; |
154 | |
} |
155 | |
|
156 | |
public String toString() |
157 | |
{ |
158 | 20 | return buffer.toString(); |
159 | |
} |
160 | |
} |