Source Code Cross Referenced for XMLEscaper.java in  » Portal » uPortal_rel-2-6-1-GA » org » jasig » portal » utils » 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 » Portal » uPortal_rel 2 6 1 GA » org.jasig.portal.utils 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /* Copyright 2001 The JA-SIG Collaborative.  All rights reserved.
002:         *  See license distributed with this file and
003:         *  available online at http://www.uportal.org/license.html
004:         */
005:
006:        package org.jasig.portal.utils;
007:
008:        /**
009:         * This utility provides a simple way of escaping the special or
010:         * reserved characters in XML that serve as delimiters so that
011:         * a string of characters can be left untouched by an XML parser.
012:         * See http://www.w3.org/TR/2000/REC-xml-20001006#syntax
013:         * and http://www.w3.org/TR/2000/REC-xml-20001006#sec-predefined-ent
014:         * and http://www.w3.org/TR/2000/REC-xml-20001006#sec-entexpand
015:         * Most of the code was borrowed from Xerces serializer classes.
016:         * If anyone finds a useable method in a standard XML API
017:         * that escapes XML strings, we should use it in place of this class.
018:         * @author Ken Weiner, kweiner@unicon.net
019:         * @version $Revision: 34797 $
020:         */
021:        public class XMLEscaper {
022:            /**
023:             * Escapes an XML string
024:             * @param source a String to be escaped
025:             * @return an escaped String
026:             */
027:            public static String escape(String source) {
028:                String result = null;
029:
030:                if (source != null) {
031:                    StringBuffer sb = new StringBuffer(source.length() + 256);
032:
033:                    for (int i = 0; i < source.length(); i++)
034:                        sb.append(escape(source.charAt(i)));
035:
036:                    result = sb.toString();
037:
038:                }
039:
040:                return (result);
041:            }
042:
043:            /**
044:             * Escapes an XML character
045:             * @param ch a char to be escaped
046:             * @return an escaped char
047:             */
048:            public static String escape(char ch) {
049:                StringBuffer sb = new StringBuffer(10);
050:                String charRef;
051:
052:                // If there is a suitable entity reference for this character, print it.
053:                charRef = getEntityRef(ch);
054:
055:                if (charRef != null) {
056:                    sb.append('&');
057:                    sb.append(charRef);
058:                    sb.append(';');
059:                } else if ((ch >= ' ' && ch <= 0x7E && ch != 0xF7)
060:                        || ch == '\n' || ch == '\r' || ch == '\t') {
061:                    // If the character is not printable, print as character reference.
062:                    // Non printables are below ASCII space but not tab or line
063:                    // terminator, ASCII delete, or above a certain Unicode threshold.
064:                    sb.append(ch);
065:                } else {
066:                    sb.append("&#");
067:                    sb.append(Integer.toString(ch));
068:                    sb.append(';');
069:                }
070:
071:                return sb.toString();
072:            }
073:
074:            private static String getEntityRef(char ch) {
075:                // Encode special XML characters into the equivalent character references.
076:                // These five are defined by default for all XML documents.
077:                switch (ch) {
078:                case '<':
079:                    return "lt";
080:                case '>':
081:                    return "gt";
082:                case '"':
083:                    return "quot";
084:                case '\'':
085:                    return "apos";
086:                case '&':
087:                    return "amp";
088:                }
089:                return null;
090:            }
091:
092:            /**
093:             * This method is provided to test out the escape method.
094:             * @param args the command line arguments
095:             */
096:            public static void main(String args[]) {
097:                if (args.length < 1) {
098:                    System.out
099:                            .println("Usage: XMLEscaper \"<string to escape>\"");
100:                } else {
101:                    String before = args[0];
102:                    String after = escape(before);
103:                    System.out.println("Before escaping: " + before);
104:                    System.out.println(" After escaping: " + after);
105:                }
106:            }
107:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.