Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
ScriptUtils |
|
| 4.333333333333333;4.333 |
1 | // Copyright May 8, 2006 The Apache Software Foundation. |
|
2 | // |
|
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
|
4 | // you may not use this file except in compliance with the License. |
|
5 | // You may obtain a copy of the License at |
|
6 | // |
|
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
|
8 | // |
|
9 | // Unless required by applicable law or agreed to in writing, software |
|
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
|
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
12 | // See the License for the specific language governing permissions and |
|
13 | // limitations under the License. |
|
14 | package org.apache.tapestry.util; |
|
15 | ||
16 | import org.apache.commons.lang.StringUtils; |
|
17 | import org.apache.commons.pool.KeyedPoolableObjectFactory; |
|
18 | import org.apache.commons.pool.impl.GenericKeyedObjectPool; |
|
19 | import org.apache.hivemind.ApplicationRuntimeException; |
|
20 | ||
21 | import java.util.regex.Matcher; |
|
22 | import java.util.regex.Pattern; |
|
23 | ||
24 | ||
25 | /** |
|
26 | * Various scripting utility methods. |
|
27 | */ |
|
28 | public final class ScriptUtils |
|
29 | { |
|
30 | /** |
|
31 | * XML cdata start. |
|
32 | */ |
|
33 | public static final String BEGIN_COMMENT = "\n<script>\n//<![CDATA[\n"; |
|
34 | /** |
|
35 | * XML character data end. |
|
36 | */ |
|
37 | public static final String END_COMMENT = "\n//]]>\n</script>\n"; |
|
38 | ||
39 | /** |
|
40 | * Regexp represenging javascript matches. |
|
41 | */ |
|
42 | public static final String SCRIPT_PATTERN = "(?:<script.*?>)(.*?)(?:<\\/script>)"; |
|
43 | ||
44 | 1 | private static final KeyedPoolableObjectFactory _factory = new RegexpPoolObjectFactory(); |
45 | ||
46 | private static final GenericKeyedObjectPool _pool; |
|
47 | ||
48 | private static final int MAX_ACTIVE = 100; |
|
49 | ||
50 | private static final long SLEEP_TIME = 1000 * 60 * 4; |
|
51 | ||
52 | static { |
|
53 | 1 | _pool = new GenericKeyedObjectPool(_factory, MAX_ACTIVE, GenericKeyedObjectPool.WHEN_EXHAUSTED_BLOCK, -1); |
54 | ||
55 | 1 | _pool.setMaxIdle(MAX_ACTIVE / 2); |
56 | 1 | _pool.setMinEvictableIdleTimeMillis(MAX_ACTIVE); |
57 | 1 | _pool.setTimeBetweenEvictionRunsMillis(SLEEP_TIME); |
58 | 1 | } |
59 | ||
60 | /* defeat instantiation */ |
|
61 | 0 | private ScriptUtils() { } |
62 | ||
63 | /** |
|
64 | * Takes any <script>contents..</script> tags found in the specified |
|
65 | * input string and replaces their contents into one large <script></script> |
|
66 | * block (meaning if multiple script blocks are found, they will be turned into one), |
|
67 | * with the addition of {@link #BEGIN_COMMENT} inserted before the logic block and |
|
68 | * {@link #END_COMMENT} inserted after the logic block. |
|
69 | * |
|
70 | * @param input |
|
71 | * The string to replace tags on |
|
72 | * @return The properly formatted string, if any formatting needed to occur. |
|
73 | */ |
|
74 | public static String ensureValidScriptTags(String input) { |
|
75 | ||
76 | 8 | if (input == null) |
77 | 1 | return null; |
78 | ||
79 | ||
80 | 7 | Pattern compiled = null; |
81 | ||
82 | try { |
|
83 | ||
84 | 7 | compiled = (Pattern)_pool.borrowObject(SCRIPT_PATTERN); |
85 | ||
86 | 7 | Matcher matcher = compiled.matcher(input); |
87 | 7 | StringBuffer buffer = new StringBuffer(input.length()); |
88 | ||
89 | 7 | boolean matched = false; |
90 | 7 | int end = 0; |
91 | 14 | while (matcher.find()) { |
92 | ||
93 | 7 | matched = true; |
94 | 7 | String str = matcher.group(1); |
95 | 7 | int pos = matcher.start() - end; |
96 | 7 | end = matcher.end(); |
97 | ||
98 | 7 | if (str == null || str.trim().equals("")) |
99 | 0 | matcher.appendReplacement(buffer, ""); |
100 | else { |
|
101 | // gather the text from the beggining to the match into a new buffer |
|
102 | 7 | StringBuffer matchLocal = new StringBuffer(); |
103 | 7 | matcher.appendReplacement(matchLocal, BEGIN_COMMENT + "$1" + END_COMMENT); |
104 | ||
105 | // the first part is always script-less, no need to remove comments from it. |
|
106 | 7 | String curr = matchLocal.toString(); |
107 | 7 | String prefix = curr.substring(0, pos); |
108 | 7 | String suffix = curr.substring(pos); |
109 | ||
110 | // the second part is in a script, so remove comments. |
|
111 | 7 | suffix = StringUtils.replace(suffix, "<!--", ""); |
112 | 7 | suffix = StringUtils.replace(suffix, "// -->", ""); |
113 | 7 | buffer.append(prefix).append(suffix); |
114 | } |
|
115 | 7 | } |
116 | ||
117 | 7 | if (!matched) |
118 | 2 | buffer.append(input); |
119 | else { |
|
120 | //copies non matched character input, ie content after the last script. |
|
121 | 5 | matcher.appendTail(buffer); |
122 | } |
|
123 | ||
124 | 7 | return buffer.toString(); |
125 | ||
126 | 0 | } catch (Exception e) { |
127 | ||
128 | 0 | throw new ApplicationRuntimeException(e); |
129 | } finally { |
|
130 | ||
131 | 7 | try { _pool.returnObject(SCRIPT_PATTERN, compiled); } catch (Throwable t) { } |
132 | } |
|
133 | } |
|
134 | ||
135 | /** |
|
136 | * Utility that will attempt to generate a unique hash string |
|
137 | * that is javascript client in a function name based on the inomcing |
|
138 | * object's {@link Object#hashCode()} return value. |
|
139 | * |
|
140 | * @param target The object to hash a string for. |
|
141 | * @return A string hash value, not necessarily exactly the same thing that would |
|
142 | * be returned by {@link Object#hashCode()}. |
|
143 | */ |
|
144 | public static String functionHash(Object target) |
|
145 | { |
|
146 | 16 | int hash = target.hashCode(); |
147 | 16 | if (hash < 0) // flip exponent if negative |
148 | 2 | hash = hash*-1; |
149 | ||
150 | 16 | return String.valueOf(hash); |
151 | } |
|
152 | } |