Source Code Cross Referenced for TextUtil.java in  » J2EE » webwork-2.2.6 » com » opensymphony » webwork » views » 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 » webwork 2.2.6 » com.opensymphony.webwork.views.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright (c) 2002-2003 by OpenSymphony
003:         * All rights reserved.
004:         */
005:        /*
006:         * WebWork, Web Application Framework
007:         *
008:         * Distributable under Apache license.
009:         * See terms of license at opensource.org
010:         */
011:        package com.opensymphony.webwork.views.util;
012:
013:        /**
014:         * This class handles HTML escaping of text.
015:         * It was written and optimized to be as fast as possible.
016:         *
017:         * @author Dick Zetterberg (dick@transitor.se)
018:         * @version $Revision: 1282 $
019:         */
020:        public class TextUtil {
021:
022:            protected static final int MAX_LENGTH = 300;
023:
024:            /**
025:             * We use arrays of char in the lookup table because it is faster
026:             * appending this to a StringBuffer than appending a String
027:             */
028:            protected static final char[][] _stringChars = new char[MAX_LENGTH][];
029:
030:            static {
031:                // Initialize the mapping table
032:                initMapping();
033:            }
034:
035:            /**
036:             * Call escapeHTML(s, false)
037:             */
038:            public static final String escapeHTML(String s) {
039:                return escapeHTML(s, false);
040:            }
041:
042:            /**
043:             * Escape HTML.
044:             *
045:             * @param s           string to be escaped
046:             * @param escapeEmpty if true, then empty string will be escaped.
047:             */
048:            public static final String escapeHTML(String s, boolean escapeEmpty) {
049:                int len = s.length();
050:
051:                if (len == 0) {
052:                    return s;
053:                }
054:
055:                if (!escapeEmpty) {
056:                    String trimmed = s.trim();
057:
058:                    if ((trimmed.length() == 0) || ("\"\"").equals(trimmed)) {
059:                        return s;
060:                    }
061:                }
062:
063:                int i = 0;
064:
065:                // First loop through String and check if escaping is needed at all
066:                // No buffers are copied at this time
067:                do {
068:                    int index = s.charAt(i);
069:
070:                    if (index >= MAX_LENGTH) {
071:                        if (index != 0x20AC) { // If not euro symbol
072:
073:                            continue;
074:                        }
075:
076:                        break;
077:                    } else if (_stringChars[index] != null) {
078:                        break;
079:                    }
080:                } while (++i < len);
081:
082:                // If the check went to the end with no escaping then i should be == len now
083:                // otherwise we must continue escaping for real
084:                if (i == len) {
085:                    return s;
086:                }
087:
088:                // We found a character to escape and broke out at position i
089:                // Now copy all characters before that to StringBuffer sb
090:                // Since a char[] will be used for copying we might as well get
091:                // a complete copy of it so that we can use array indexing instead of charAt
092:                StringBuffer sb = new StringBuffer(len + 40);
093:                char[] chars = new char[len];
094:
095:                // Copy all chars from the String s to the chars buffer
096:                s.getChars(0, len, chars, 0);
097:
098:                // Append the first i characters that we have checked to the resulting StringBuffer
099:                sb.append(chars, 0, i);
100:
101:                int last = i;
102:                char[] subst;
103:
104:                for (; i < len; i++) {
105:                    char c = chars[i];
106:                    int index = c;
107:
108:                    if (index < MAX_LENGTH) {
109:                        subst = _stringChars[index];
110:
111:                        // It is faster to append a char[] than a String which is why we use this
112:                        if (subst != null) {
113:                            if (i > last) {
114:                                sb.append(chars, last, i - last);
115:                            }
116:
117:                            sb.append(subst);
118:                            last = i + 1;
119:                        }
120:                    }
121:                    // Check if it is the euro symbol. This could be changed to check in a second lookup
122:                    // table in case one wants to convert more characters in that area
123:                    else if (index == 0x20AC) {
124:                        if (i > last) {
125:                            sb.append(chars, last, i - last);
126:                        }
127:
128:                        sb.append("&euro;");
129:                        last = i + 1;
130:                    }
131:                }
132:
133:                if (i > last) {
134:                    sb.append(chars, last, i - last);
135:                }
136:
137:                return sb.toString();
138:            }
139:
140:            protected static void addMapping(int c, String txt, String[] strings) {
141:                strings[c] = txt;
142:            }
143:
144:            protected static void initMapping() {
145:                String[] strings = new String[MAX_LENGTH];
146:
147:                addMapping(0x22, "&quot;", strings); // "
148:                addMapping(0x26, "&amp;", strings); // &
149:                addMapping(0x3c, "&lt;", strings); // <
150:                addMapping(0x3e, "&gt;", strings); // >
151:
152:                addMapping(0xa1, "&iexcl;", strings); //
153:                addMapping(0xa2, "&cent;", strings); //
154:                addMapping(0xa3, "&pound;", strings); //
155:                addMapping(0xa9, "&copy;", strings); // ©
156:                addMapping(0xae, "&reg;", strings); // ®
157:                addMapping(0xbf, "&iquest;", strings); //
158:
159:                addMapping(0xc0, "&Agrave;", strings); // À
160:                addMapping(0xc1, "&Aacute;", strings); // Á
161:                addMapping(0xc2, "&Acirc;", strings); // Â
162:                addMapping(0xc3, "&Atilde;", strings); // Â
163:                addMapping(0xc4, "&Auml;", strings); // Ä
164:                addMapping(0xc5, "&Aring;", strings); // Å
165:                addMapping(0xc6, "&AElig;", strings); // Æ
166:                addMapping(0xc7, "&Ccedil;", strings); // Ç
167:                addMapping(0xc8, "&Egrave;", strings); //
168:                addMapping(0xc9, "&Eacute;", strings); //
169:                addMapping(0xca, "&Ecirc;", strings); //
170:                addMapping(0xcb, "&Euml;", strings); //
171:                addMapping(0xcc, "&Igrave;", strings); //
172:                addMapping(0xcd, "&Iacute;", strings); //
173:                addMapping(0xce, "&Icirc;", strings); //
174:                addMapping(0xcf, "&Iuml;", strings); //
175:
176:                addMapping(0xd0, "&ETH;", strings); //
177:                addMapping(0xd1, "&Ntilde;", strings); //
178:                addMapping(0xd2, "&Ograve;", strings); //
179:                addMapping(0xd3, "&Oacute;", strings); //
180:                addMapping(0xd4, "&Ocirc;", strings); //
181:                addMapping(0xd5, "&Otilde;", strings); //
182:                addMapping(0xd6, "&Ouml;", strings); // Ö
183:                addMapping(0xd7, "&times;", strings); //
184:                addMapping(0xd8, "&Oslash;", strings); //
185:                addMapping(0xd9, "&Ugrave;", strings); //
186:                addMapping(0xda, "&Uacute;", strings); //
187:                addMapping(0xdb, "&Ucirc;", strings); //
188:                addMapping(0xdc, "&Uuml;", strings); //
189:                addMapping(0xdd, "&Yacute;", strings); //
190:                addMapping(0xde, "&THORN;", strings); //
191:                addMapping(0xdf, "&szlig;", strings); //
192:
193:                addMapping(0xe0, "&agrave;", strings); //
194:                addMapping(0xe1, "&aacute;", strings); //
195:                addMapping(0xe2, "&acirc;", strings); //
196:                addMapping(0xe3, "&atilde;", strings); //
197:                addMapping(0xe4, "&auml;", strings); // ä
198:                addMapping(0xe5, "&aring;", strings); // å
199:                addMapping(0xe6, "&aelig;", strings); //
200:                addMapping(0xe7, "&ccedil;", strings); //
201:                addMapping(0xe8, "&egrave;", strings); //
202:                addMapping(0xe9, "&eacute;", strings); //
203:                addMapping(0xea, "&ecirc;", strings); //
204:                addMapping(0xeb, "&euml;", strings); //
205:                addMapping(0xec, "&igrave;", strings); //
206:                addMapping(0xed, "&iacute;", strings); //
207:                addMapping(0xee, "&icirc;", strings); //
208:                addMapping(0xef, "&iuml;", strings); //
209:
210:                addMapping(0xf0, "&eth;", strings); //
211:                addMapping(0xf1, "&ntilde;", strings); //
212:                addMapping(0xf2, "&ograve;", strings); //
213:                addMapping(0xf3, "&oacute;", strings); //
214:                addMapping(0xf4, "&ocirc;", strings); //
215:                addMapping(0xf5, "&otilde;", strings); //
216:                addMapping(0xf6, "&ouml;", strings); // ö
217:                addMapping(0xf7, "&divide;", strings); //
218:                addMapping(0xf8, "&oslash;", strings); //
219:                addMapping(0xf9, "&ugrave;", strings); //
220:                addMapping(0xfa, "&uacute;", strings); //
221:                addMapping(0xfb, "&ucirc;", strings); //
222:                addMapping(0xfc, "&uuml;", strings); //
223:                addMapping(0xfd, "&yacute;", strings); //
224:                addMapping(0xfe, "&thorn;", strings); //
225:                addMapping(0xff, "&yuml;", strings); //
226:
227:                for (int i = 0; i < strings.length; i++) {
228:                    String str = strings[i];
229:
230:                    if (str != null) {
231:                        _stringChars[i] = str.toCharArray();
232:                    }
233:                }
234:            }
235:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.