Source Code Cross Referenced for JavascriptUtils.java in  » J2EE » myfaces-core-1.2.0 » org » apache » myfaces » shared_impl » renderkit » html » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » J2EE » myfaces core 1.2.0 » org.apache.myfaces.shared_impl.renderkit.html.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
w___w__w__.___jav___a___2s.__c___o__m__ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.