Source Code Cross Referenced for StringUtil.java in  » Portal » Open-Portal » com » sun » portal » proxylet » client » common » 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 » Open Portal » com.sun.portal.proxylet.client.common 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package com.sun.portal.proxylet.client.common;
002:
003:        public class StringUtil {
004:
005:            public static String replaceAll(final String aSource,
006:                    final String aFind, String aReplace) {
007:                int lFindLength;
008:                // the next statement has the side effect of throwing a null pointer
009:                // exception if s is null.
010:                if (aSource == null || aFind == null
011:                        || (lFindLength = aFind.length()) == 0) {
012:                    // If there is nothing to find, we won't try and find it.
013:                    return aSource;
014:                }
015:
016:                if (aReplace == null) {
017:                    // a null string and an empty string are the same
018:                    // for replacement purposes.
019:                    aReplace = "";
020:                }
021:
022:                int lSourceLength = aSource.length();
023:                int lReplaceLength = aReplace.length();
024:
025:                // We need to figure out how long our resulting string will be.
026:                // This is required because without it, the possible resizing
027:                // and copying of memory structures could lead to an unacceptable runtime.
028:                // In the worst case it would have to be resized n times with each
029:                // resize having a O(n) copy leading to an O(n^2) algorithm.
030:                int length;
031:                if (lFindLength == lReplaceLength) {
032:                    // special case in which we don't need to count the replacements
033:                    // because the count falls out of the length formula.
034:                    length = lSourceLength;
035:                } else {
036:                    int count;
037:                    int start;
038:                    int end;
039:
040:                    // Scan s and count the number of times we find our target.
041:                    count = 0;
042:                    start = 0;
043:                    while ((end = aSource.indexOf(aFind, start)) != -1) {
044:                        count++;
045:                        start = end + lFindLength;
046:                    }
047:
048:                    if (count == 0) {
049:                        // special case in which on first pass, we find there is nothing
050:                        // to be replaced.  No need to do a second pass or create a string buffer.
051:                        return aSource;
052:                    }
053:
054:                    length = lSourceLength
055:                            - (count * (lFindLength - lReplaceLength));
056:                }
057:
058:                int start = 0;
059:                int end = aSource.indexOf(aFind, start);
060:                if (end == -1) {
061:                    // nothing was found in the string to replace.
062:                    // we can get this if the find and replace strings
063:                    // are the same length because we didn't check before.
064:                    // in this case, we will return the original string
065:                    return aSource;
066:                }
067:                // it looks like we actually have something to replace
068:                // *sigh* allocate memory for it.
069:                StringBuffer sb = new StringBuffer(length);
070:
071:                // Scan s and do the replacements
072:                while (end != -1) {
073:                    sb.append(aSource.substring(start, end));
074:                    sb.append(aReplace);
075:                    start = end + lFindLength;
076:                    end = aSource.indexOf(aFind, start);
077:                }
078:                end = lSourceLength;
079:                sb.append(aSource.substring(start, end));
080:
081:                return (sb.toString());
082:            }
083:
084:            public static String replaceFirst(final String aSource,
085:                    final String aFind, String aReplace) {
086:                int lFindLength;
087:                // the next statement has the side effect of throwing a null pointer
088:                // exception if s is null.
089:                if (aSource == null || aFind == null
090:                        || (lFindLength = aFind.length()) == 0) {
091:                    // If there is nothing to find, we won't try and find it.
092:                    return aSource;
093:                }
094:
095:                if (aReplace == null) {
096:                    // a null string and an empty string are the same
097:                    // for replacement purposes.
098:                    aReplace = "";
099:                }
100:
101:                int lSourceLength = aSource.length();
102:                int lReplaceLength = aReplace.length();
103:
104:                // We need to figure out how long our resulting string will be.
105:                // This is required because without it, the possible resizing
106:                // and copying of memory structures could lead to an unacceptable runtime.
107:                // In the worst case it would have to be resized n times with each
108:                // resize having a O(n) copy leading to an O(n^2) algorithm.
109:                int length;
110:                if (lFindLength == lReplaceLength) {
111:                    // special case in which we don't need to count the replacements
112:                    // because the count falls out of the length formula.
113:                    length = lSourceLength;
114:                } else {
115:                    int count;
116:                    int start;
117:                    int end;
118:
119:                    // Scan s and count the number of times we find our target.
120:                    count = 0;
121:                    start = 0;
122:                    while ((end = aSource.indexOf(aFind, start)) != -1) {
123:                        count++;
124:                        start = end + lFindLength;
125:                    }
126:
127:                    if (count == 0) {
128:                        // special case in which on first pass, we find there is nothing
129:                        // to be replaced.  No need to do a second pass or create a string buffer.
130:                        return aSource;
131:                    }
132:
133:                    length = lSourceLength
134:                            - (count * (lFindLength - lReplaceLength));
135:                }
136:
137:                int start = 0;
138:                int end = aSource.indexOf(aFind, start);
139:                if (end == -1) {
140:                    // nothing was found in the string to replace.
141:                    // we can get this if the find and replace strings
142:                    // are the same length because we didn't check before.
143:                    // in this case, we will return the original string
144:                    return aSource;
145:                }
146:                // it looks like we actually have something to replace
147:                // *sigh* allocate memory for it.
148:                StringBuffer sb = new StringBuffer(length);
149:
150:                // Scan s and do the replacements
151:                if (end != -1) {
152:                    sb.append(aSource.substring(start, end));
153:                    sb.append(aReplace);
154:                    start = end + lFindLength;
155:                    end = aSource.indexOf(aFind, start);
156:                }
157:                end = lSourceLength;
158:                sb.append(aSource.substring(start, end));
159:
160:                return (sb.toString());
161:            }
162:
163:            public static void main(String[] args) {
164:                String configList = "http=www.google.com:80;https=www.s.s.c;gopher=ss.w.s.c";
165:
166:                String out = StringUtil.replaceAll(configList, "=", "://");
167:                System.out.println(out);
168:                out = StringUtil.replaceFirst(configList, "=", "://");
169:                System.out.println(out);
170:
171:            }
172:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.