001: /*
002: * Copyright 2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.myfaces.shared_impl.renderkit.html.util;
017:
018: import org.apache.commons.logging.Log;
019: import org.apache.commons.logging.LogFactory;
020: import org.apache.myfaces.shared_impl.config.MyfacesConfig;
021:
022: import javax.faces.context.ExternalContext;
023: import javax.servlet.http.HttpSession;
024: import java.io.UnsupportedEncodingException;
025: import java.util.Arrays;
026: import java.util.HashSet;
027: import java.util.Set;
028:
029: /**
030: * @author Manfred Geiler (latest modification by $Author: baranda $)
031: * @author Anton Koinov
032: * @version $Revision: 544646 $ $Date: 2007-06-05 23:51:27 +0200 (Di, 05 Jun 2007) $
033: */
034: public final class JavascriptUtils {
035: private static final Log log = LogFactory
036: .getLog(JavascriptUtils.class);
037:
038: public static final String JAVASCRIPT_DETECTED = JavascriptUtils.class
039: .getName()
040: + ".JAVASCRIPT_DETECTED";
041:
042: private static final String AUTO_SCROLL_PARAM = "autoScroll";
043: private static final String AUTO_SCROLL_FUNCTION = "getScrolling()";
044:
045: private static final String OLD_VIEW_ID = JavascriptUtils.class
046: + ".OLD_VIEW_ID";
047:
048: private JavascriptUtils() {
049: // utility class, do not instantiate
050: }
051:
052: private static final Set RESERVED_WORDS = new HashSet(Arrays
053: .asList(new String[] { "abstract", "boolean", "break",
054: "byte", "case", "catch", "char", "class", "const",
055: "continue", "default", "delete", "do", "double",
056: "else", "export", "extends", "false", "final",
057: "finally", "float", "for", "function", "goto",
058: "if", "implements", "in", "instanceof", "int",
059: "long", "native", "new", "null", "package",
060: "private", "protected", "public", "return",
061: "short", "static", "super", "switch",
062: "synchronized", "this", "throw", "throws",
063: "transient", "true", "try", "typeof", "var",
064: "void", "while", "with" }));
065:
066: /**Don't use this function - except when compatibility with the RI is a must,
067: * as in the name for the clear form parameters script.
068: */
069: public static String getValidJavascriptNameAsInRI(
070: String origIdentifier) {
071: return origIdentifier.replaceAll("-", "\\$_");
072: }
073:
074: public static String getValidJavascriptName(String s,
075: boolean checkForReservedWord) {
076: if (checkForReservedWord && RESERVED_WORDS.contains(s)) {
077: return s + "_";
078: }
079:
080: StringBuffer buf = null;
081: for (int i = 0, len = s.length(); i < len; i++) {
082: char c = s.charAt(i);
083:
084: if (Character.isLetterOrDigit(c)) {
085: // allowed char
086: if (buf != null)
087: buf.append(c);
088: } else {
089: if (buf == null) {
090: buf = new StringBuffer(s.length() + 10);
091: buf.append(s.substring(0, i));
092: }
093:
094: buf.append('_');
095: if (c < 16) {
096: // pad single hex digit values with '0' on the left
097: buf.append('0');
098: }
099:
100: if (c < 128) {
101: // first 128 chars match their byte representation in UTF-8
102: buf.append(Integer.toHexString(c).toUpperCase());
103: } else {
104: byte[] bytes;
105: try {
106: bytes = Character.toString(c).getBytes("UTF-8");
107: } catch (UnsupportedEncodingException e) {
108: throw new RuntimeException(e);
109: }
110:
111: for (int j = 0; j < bytes.length; j++) {
112: int intVal = bytes[j];
113: if (intVal < 0) {
114: // intVal will be >= 128
115: intVal = 256 + intVal;
116: } else if (intVal < 16) {
117: // pad single hex digit values with '0' on the left
118: buf.append('0');
119: }
120: buf.append(Integer.toHexString(intVal)
121: .toUpperCase());
122: }
123: }
124: }
125:
126: }
127:
128: return buf == null ? s : buf.toString();
129: }
130:
131: public static String encodeString(String string) {
132: if (string == null) {
133: return "";
134: }
135: StringBuffer sb = null; //create later on demand
136: String app;
137: char c;
138: for (int i = 0; i < string.length(); ++i) {
139: app = null;
140: c = string.charAt(i);
141: switch (c) {
142: case '\\':
143: app = "\\";
144: break;
145: case '"':
146: app = "\\\"";
147: break;
148: case '\'':
149: app = "\\'";
150: break;
151: case '\n':
152: app = "\\n";
153: break;
154: case '\r':
155: app = "\\r";
156: break;
157: }
158: if (app != null) {
159: if (sb == null) {
160: sb = new StringBuffer(string.substring(0, i));
161: }
162: sb.append(app);
163: } else {
164: if (sb != null) {
165: sb.append(c);
166: }
167: }
168: }
169:
170: if (sb == null) {
171: return string;
172: } else {
173: return sb.toString();
174: }
175: }
176:
177: public static boolean isJavascriptAllowed(
178: ExternalContext externalContext) {
179: MyfacesConfig myfacesConfig = MyfacesConfig
180: .getCurrentInstance(externalContext);
181: if (myfacesConfig.isAllowJavascript()) {
182: if (myfacesConfig.isDetectJavascript()) {
183: return isJavascriptDetected(externalContext);
184: } else {
185: return true;
186: }
187: } else {
188: return false;
189: }
190: }
191:
192: public static void setJavascriptDetected(HttpSession session,
193: boolean value) {
194: session.setAttribute(JAVASCRIPT_DETECTED, Boolean
195: .valueOf(value));
196: }
197:
198: public static boolean isJavascriptDetected(
199: ExternalContext externalContext) {
200: //TODO/FIXME (manolito): This info should be better stored in the viewroot component and not in the session
201: Boolean sessionValue = (Boolean) externalContext
202: .getSessionMap().get(JAVASCRIPT_DETECTED);
203: return sessionValue != null && sessionValue.booleanValue();
204: }
205:
206: public static void setOldViewId(ExternalContext externalContext,
207: String viewId) {
208: externalContext.getRequestMap().put(OLD_VIEW_ID, viewId);
209: }
210:
211: public static String getOldViewId(ExternalContext externalContext) {
212: return (String) externalContext.getRequestMap()
213: .get(OLD_VIEW_ID);
214: }
215: }
|