01: /*
02: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
03: * Distributed under the terms of either:
04: * - the common development and distribution license (CDDL), v1.0; or
05: * - the GNU Lesser General Public License, v2.1 or later
06: * $Id: StateStoreTools.java 3634 2007-01-08 21:42:24Z gbevin $
07: */
08: package com.uwyn.rife.engine;
09:
10: import com.uwyn.rife.engine.exceptions.EngineException;
11:
12: public abstract class StateStoreTools {
13: private final static String HTML_PART1 = "<input name=\"";
14: private final static String HTML_PART2 = "\" type=\"hidden\" value=\"";
15: private final static String HTML_PART3 = "\" />";
16: private final static String JAVASCRIPT_PART1 = appendJavascriptAsciiArray(
17: new StringBuilder(), HTML_PART1).toString();
18: private final static String JAVASCRIPT_PART2 = appendJavascriptAsciiArray(
19: new StringBuilder(), HTML_PART2).toString();
20: private final static String JAVASCRIPT_PART3 = appendJavascriptAsciiArray(
21: new StringBuilder(), HTML_PART3).toString();
22:
23: public static void appendJavascriptHeader(StringBuilder state)
24: throws EngineException {
25: state.append("<script type=\"text/javascript\">\n");
26: state.append("{\n");
27: state.append("var part1 = ");
28: state.append(JAVASCRIPT_PART1);
29: state.append("; ");
30: state.append("var part2 = ");
31: state.append(JAVASCRIPT_PART2);
32: state.append("; ");
33: state.append("var part3 = ");
34: state.append(JAVASCRIPT_PART3);
35: state.append("; ");
36: state.append("var state = ''; ");
37: state.append("var intArrayToString = function(intArray) { ");
38: state.append(" var result = ''; ");
39: state
40: .append(" for (var i = 0; i < intArray.length; i++) result += String.fromCharCode(intArray[i]); ");
41: state.append(" return result; ");
42: state.append("}; ");
43: }
44:
45: public static void appendHtmlHiddenParam(StringBuilder state,
46: CharSequenceDeferred deferred, String name, String value)
47: throws EngineException {
48: state.append(HTML_PART1);
49: state.append(deferred.encode(name));
50: state.append(HTML_PART2);
51: state.append(deferred.encode(value));
52: state.append(HTML_PART3);
53: }
54:
55: public static StringBuilder appendJavascriptAsciiArray(
56: StringBuilder state, String value) {
57: if (null == value) {
58: state.append("[]");
59: }
60:
61: char seperator = ',';
62: int current_index = 0;
63: state.append("[");
64: while (current_index < value.length() - 1) {
65: state.append((int) value.charAt(current_index));
66: state.append(seperator);
67: current_index++;
68: }
69:
70: state.append((int) value.charAt(current_index));
71: state.append("]");
72:
73: return state;
74: }
75:
76: public static void appendJavascriptHiddenParam(StringBuilder state,
77: String name, String value) throws EngineException {
78: state.append("state += intArrayToString(part1); ");
79: state.append("state += intArrayToString(");
80: appendJavascriptAsciiArray(state, name);
81: state.append("); ");
82: state.append("state += intArrayToString(part2); ");
83: state.append("state += intArrayToString(");
84: appendJavascriptAsciiArray(state, value);
85: state.append("); ");
86: state.append("state += intArrayToString(part3); ");
87: }
88:
89: public static void appendJavascriptFooter(StringBuilder state)
90: throws EngineException {
91: state.append("document.write(state);\n");
92: state.append("}\n");
93: state.append("</script>");
94: }
95: }
|