Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ScriptUtils |
|
| 4.666666666666667;4.667 |
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 | 0 | 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 | 0 | _pool = new GenericKeyedObjectPool(_factory, MAX_ACTIVE, GenericKeyedObjectPool.WHEN_EXHAUSTED_BLOCK, -1); |
54 | ||
55 | 0 | _pool.setMaxIdle(MAX_ACTIVE / 2); |
56 | 0 | _pool.setMinEvictableIdleTimeMillis(MAX_ACTIVE); |
57 | 0 | _pool.setTimeBetweenEvictionRunsMillis(SLEEP_TIME); |
58 | 0 | } |
59 | ||
60 | /* defeat instantiation */ | |
61 | 0 | private ScriptUtils() { } |
62 | ||
63 | /** | |
64 | * Takes any <code><script>contents..</script></code> tags found in the specified | |
65 | * input string and replaces their contents into one large <code><script></script></code> | |
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 | 0 | if (input == null) |
77 | 0 | return null; |
78 | ||
79 | ||
80 | 0 | Pattern compiled = null; |
81 | ||
82 | try { | |
83 | ||
84 | 0 | compiled = (Pattern)_pool.borrowObject(SCRIPT_PATTERN); |
85 | ||
86 | 0 | Matcher matcher = compiled.matcher(input); |
87 | 0 | StringBuffer buffer = new StringBuffer(input.length()); |
88 | ||
89 | 0 | boolean matched = false; |
90 | 0 | int end = 0; |
91 | 0 | while (matcher.find()) { |
92 | ||
93 | 0 | matched = true; |
94 | 0 | String str = matcher.group(1); |
95 | 0 | int pos = matcher.start() - end; |
96 | 0 | end = matcher.end(); |
97 | ||
98 | 0 | 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 | 0 | StringBuffer matchLocal = new StringBuffer(); |
103 | 0 | 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 | 0 | String curr = matchLocal.toString(); |
107 | 0 | String prefix = curr.substring(0, pos); |
108 | 0 | String suffix = curr.substring(pos); |
109 | ||
110 | // the second part is in a script, so remove comments. | |
111 | 0 | suffix = StringUtils.replace(suffix, "<!--", ""); |
112 | 0 | suffix = StringUtils.replace(suffix, "// -->", ""); |
113 | 0 | buffer.append(prefix).append(suffix); |
114 | } | |
115 | 0 | } |
116 | ||
117 | 0 | if (!matched) |
118 | 0 | buffer.append(input); |
119 | else { | |
120 | //copies non matched character input, ie content after the last script. | |
121 | 0 | matcher.appendTail(buffer); |
122 | } | |
123 | ||
124 | 0 | return buffer.toString(); |
125 | ||
126 | 0 | } catch (Exception e) { |
127 | ||
128 | 0 | throw new ApplicationRuntimeException(e); |
129 | } finally { | |
130 | ||
131 | 0 | 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 | 0 | int hash = target.hashCode(); |
147 | 0 | if (hash < 0) // flip exponent if negative |
148 | 0 | hash = hash*-1; |
149 | ||
150 | 0 | return String.valueOf(hash); |
151 | } | |
152 | } |