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


001:        /*
002:         * Copyright 2001 Sun Microsystems, Inc.  All rights reserved.
003:         * PROPRIETARY/CONFIDENTIAL.  Use of this product is subject to license terms.
004:         */
005:        package com.sun.portal.rewriter.util;
006:
007:        import java.io.PrintWriter;
008:        import java.io.StringWriter;
009:        import java.util.ArrayList;
010:        import java.util.Arrays;
011:        import java.util.Collection;
012:        import java.util.Iterator;
013:        import java.util.StringTokenizer;
014:
015:        /**
016:         * Utility class which provide few commonly used methods to manipulate strings
017:         *
018:         * @version 1.0 12/15/2001
019:         * @author Raja Nagendra Kumar, Nagendra.Raja@sun.com
020:         */
021:        public final class StringHelper {
022:            public static String searchAndReplace(final String aSource,
023:                    final String[][] aFindReplaceMap) {
024:                if (aSource == null || aFindReplaceMap == null) {
025:                    return aSource;
026:                }
027:
028:                String lResult = aSource;
029:                for (int i = 0; i < aFindReplaceMap.length; i++) {
030:                    lResult = searchAndReplace(lResult, aFindReplaceMap[i][0],
031:                            aFindReplaceMap[i][1]);
032:                }
033:                return lResult;
034:            }//searchAndReplace()
035:
036:            public static String[] searchAndReplace(final String[] aSource,
037:                    final String aFind, final String aReplace) {
038:                if (aSource == null || aSource.length == 0) {
039:                    return aSource;
040:                }
041:
042:                String[] lResult = new String[aSource.length];
043:                for (int i = 0; i < aSource.length; i++) {
044:                    lResult[i] = searchAndReplace(aSource[i], aFind, aReplace);
045:                }
046:
047:                return lResult;
048:            }//searchAndReplace()
049:
050:            public static String searchAndReplace(final String aSource,
051:                    final String aFind, String aReplace) {
052:                int lFindLength;
053:                // the next statement has the side effect of throwing a null pointer
054:                // exception if s is null.
055:                if (aSource == null || aFind == null
056:                        || (lFindLength = aFind.length()) == 0) {
057:                    // If there is nothing to find, we won't try and find it.
058:                    return aSource;
059:                }
060:
061:                if (aReplace == null) {
062:                    // a null string and an empty string are the same
063:                    // for replacement purposes.
064:                    aReplace = "";
065:                }
066:
067:                int lSourceLength = aSource.length();
068:                int lReplaceLength = aReplace.length();
069:
070:                // We need to figure out how long our resulting string will be.
071:                // This is required because without it, the possible resizing
072:                // and copying of memory structures could lead to an unacceptable runtime.
073:                // In the worst case it would have to be resized n times with each
074:                // resize having a O(n) copy leading to an O(n^2) algorithm.
075:                int length;
076:                if (lFindLength == lReplaceLength) {
077:                    // special case in which we don't need to count the replacements
078:                    // because the count falls out of the length formula.
079:                    length = lSourceLength;
080:                } else {
081:                    int count;
082:                    int start;
083:                    int end;
084:
085:                    // Scan s and count the number of times we find our target.
086:                    count = 0;
087:                    start = 0;
088:                    while ((end = aSource.indexOf(aFind, start)) != -1) {
089:                        count++;
090:                        start = end + lFindLength;
091:                    }
092:
093:                    if (count == 0) {
094:                        // special case in which on first pass, we find there is nothing
095:                        // to be replaced.  No need to do a second pass or create a string buffer.
096:                        return aSource;
097:                    }
098:
099:                    length = lSourceLength
100:                            - (count * (lFindLength - lReplaceLength));
101:                }
102:
103:                int start = 0;
104:                int end = aSource.indexOf(aFind, start);
105:                if (end == -1) {
106:                    // nothing was found in the string to replace.
107:                    // we can get this if the find and replace strings
108:                    // are the same length because we didn't check before.
109:                    // in this case, we will return the original string
110:                    return aSource;
111:                }
112:                // it looks like we actually have something to replace
113:                // *sigh* allocate memory for it.
114:                StringBuffer sb = new StringBuffer(length);
115:
116:                // Scan s and do the replacements
117:                while (end != -1) {
118:                    sb.append(aSource.substring(start, end));
119:                    sb.append(aReplace);
120:                    start = end + lFindLength;
121:                    end = aSource.indexOf(aFind, start);
122:                }
123:                end = lSourceLength;
124:                sb.append(aSource.substring(start, end));
125:
126:                return (sb.toString());
127:            }//searchAndReplace()
128:
129:            public static String normalize(final String aSource) {
130:                return (aSource != null) ? aSource.trim() : "";
131:            }//normalize()
132:
133:            public static String normalize(final String aSource,
134:                    final String aDefault) {
135:                final String result = normalize(aSource);
136:                return (result.length() == 0) ? aDefault : result;
137:            }//normalize()
138:
139:            public static String exceptionStack2String(final Throwable t) {
140:                if (t != null) {
141:                    StringWriter sw = new StringWriter();
142:                    t.printStackTrace(new PrintWriter(sw));
143:                    return sw.toString();
144:                }
145:
146:                return null;
147:            }//exceptionStack2String()
148:
149:            public static String findMatchByIgnoringCase(
150:                    final Collection aCollection, final String aString) {
151:                final Iterator iterator = aCollection.iterator();
152:                String s;
153:                while (iterator.hasNext()) {
154:                    s = (String) iterator.next();
155:                    if (s.equalsIgnoreCase(aString)) {
156:                        return s;
157:                    }
158:                }//while loop
159:
160:                return null;
161:            }//findMatch()
162:
163:            public static final String[] tokenize(final String aString,
164:                    final String aTokens) {
165:                return tokenize(aString, aTokens, false);
166:            }//tokenize()
167:
168:            public static String[] tokenize(final String aSource,
169:                    final String aTokenList, final boolean aIncludeEmpty,
170:                    final int aEnsureLength) {
171:                final String[] result = tokenize(aSource, aTokenList,
172:                        aIncludeEmpty);
173:                return ensureLength(result, aEnsureLength);
174:            }//splitString()
175:
176:            public static final String[] tokenize(final String aString,
177:                    final String aTokens, final boolean aIncludeEmpty) {
178:                if (aTokens.length() == 1) {
179:                    return tokenize(aString, aTokens.charAt(0), aIncludeEmpty);
180:                } else {
181:                    return tokenizeNonPerfWay(aString, aTokens, aIncludeEmpty);
182:                }
183:            }//tokenize()
184:
185:            private static final String[] tokenize(final String aOrgString,
186:                    final char aToken, final boolean aIncludeEmpty) {
187:                final ArrayList lList = new ArrayList();
188:
189:                String aString = aOrgString;
190:                int i = 0;
191:                int j = aString.indexOf(aToken); // First substring
192:
193:                String sub;
194:                while (j >= 0) {
195:                    if (j != 0
196:                            && aString.charAt(j - 1) == Constants.ESCAPE_FORWARD_SLASH_CHAR) {
197:                        //BugNo:4929669
198:                        aString = aString.substring(0, j - 1)
199:                                + aString.substring(j);
200:                        j = aString.indexOf(aToken, j); //skip this token
201:                        continue;
202:                    }
203:
204:                    sub = aString.substring(i, j);
205:                    i = j + 1;
206:                    j = aString.indexOf(aToken, i); // Rest of substrings
207:                    if (aIncludeEmpty || sub.length() > 0) {
208:                        lList.add(sub);
209:                    }
210:                }
211:
212:                sub = aString.substring(i); //Last substring
213:                if (aString.length() > 0 && (aIncludeEmpty || sub.length() > 0)) {
214:                    lList.add(sub);
215:                }
216:
217:                return (String[]) lList.toArray(Constants.EMPTY_STRING_ARRAY);
218:            }//tokenize()
219:
220:            private static String[] tokenizeNonPerfWay(
221:                    final String aSourceString, final String aDelimList,
222:                    final boolean aBool) {
223:                final StringTokenizer st = new StringTokenizer(aSourceString,
224:                        aDelimList, aBool);
225:                final ArrayList lList = new ArrayList();
226:
227:                boolean wasPreviousOneADelim = false;
228:                for (int i = 0; st.hasMoreTokens(); i++) {
229:                    String bToken = st.nextToken();
230:                    boolean isPresentOneADelim = (aDelimList.indexOf(bToken) != -1) ? true
231:                            : false;
232:
233:                    if (!isPresentOneADelim) {
234:                        lList.add(bToken);
235:                    } else {
236:                        if (i == 0) {
237:                            lList.add("");
238:                        } else if (wasPreviousOneADelim) {
239:                            lList.add("");
240:                        }
241:                    }
242:
243:                    wasPreviousOneADelim = isPresentOneADelim;
244:                }//for loop
245:
246:                if (wasPreviousOneADelim) {
247:                    lList.add("");
248:                }
249:
250:                return (String[]) lList.toArray(Constants.EMPTY_STRING_ARRAY);
251:            }//parse()
252:
253:            private static String[] ensureLength(String[] result,
254:                    final int aLength) {
255:                //insert the dummy ones only if the lenght is less than the passed one
256:                if (aLength > result.length) {
257:                    String[] temp = new String[aLength];
258:                    Arrays.fill(temp, Constants.EMPTY_STRING);
259:                    System.arraycopy(result, 0, temp, 0, result.length);
260:                    result = temp;
261:                }
262:
263:                return result;
264:            }//ensureLength()
265:
266:            /**
267:             * Not implemented.. would be implemented when jdk1.4 is used
268:             */
269:            public static String[] regExpSplit(final String aOrgURL,
270:                    final String aRegExp) {
271:                if (aOrgURL == null) {
272:                    return Constants.EMPTY_STRING_ARRAY;
273:                }
274:
275:                String urlPart = aOrgURL.trim();
276:
277:                //remove the starting and ending ', " ,\t, \s chars
278:                while (urlPart.startsWith(Constants.DOUBLE_QUOTES)
279:                        || urlPart.startsWith(Constants.SINGLE_QUOTES)
280:                        || urlPart.startsWith(Constants.ESCAPE_SINGLE_QUOTES)
281:                        || urlPart.startsWith(Constants.ESCAPE_DOUBLE_QUOTES)) {
282:                    urlPart = urlPart.substring(1).trim();
283:                }
284:
285:                //BugNo:4784407
286:                while (urlPart.startsWith("&#")) {
287:                    int index = urlPart.indexOf(";");
288:                    if (index == -1) {
289:                        break;
290:                    }
291:
292:                    urlPart = urlPart.substring(index + 1).trim();
293:                }
294:
295:                int firstIndex = aOrgURL.indexOf(urlPart);
296:
297:                while (urlPart.endsWith(Constants.ESCAPE_SINGLE_QUOTES)
298:                        || urlPart.endsWith(Constants.ESCAPE_DOUBLE_QUOTES)
299:                        || urlPart.endsWith(Constants.DOUBLE_QUOTES)
300:                        || urlPart.endsWith(Constants.SINGLE_QUOTES)) {
301:                    urlPart = urlPart.substring(0, urlPart.length() - 1).trim();
302:                    if (urlPart.endsWith("\\")) {
303:                        urlPart = urlPart.substring(0, urlPart.length() - 1)
304:                                .trim();
305:                    }
306:                }
307:
308:                return new String[] { aOrgURL.substring(0, firstIndex),
309:                        urlPart,
310:                        aOrgURL.substring(firstIndex + urlPart.length()) };
311:            }//regExpSplit()
312:
313:            public static final String trimQuotes(String aString) {
314:                String lValue = aString;
315:                String[] splitted = StringHelper.regExpSplit(lValue,
316:                        Constants.REG_EX_FOR_TRIMING_QUOTES);
317:
318:                if (splitted.length == 3) {
319:                    lValue = splitted[1];
320:                }
321:
322:                return lValue;
323:            }//trimQuotes()
324:
325:            public static boolean startsWithIgnoreCase(String aSRC,
326:                    String aMache) {
327:                return aSRC.regionMatches(true, 0, aMache, 0, aMache.length());
328:            }//startsWithIgnoreCase()
329:
330:            public static void main(String[] args) {
331:                final String[][] lData = {
332:                        { "", ":" },
333:                        { ":::", ":" },
334:
335:                        {
336:                                "jini://raja.sun. com ;mahesh.sun.com;jini://srinu.sil.co.in",
337:                                ";" },
338:
339:                        { ",y,,", "," },
340:
341:                        { "test", " ", },
342:
343:                        { ",y,", "," },
344:
345:                        { ",y", "," },
346:
347:                        { " ,y", "," },
348:
349:                };
350:                for (int i = 0; i < lData.length; i++) {
351:                    System.out.println(Arrays.asList(tokenize(lData[i][0],
352:                            lData[i][1], true)));
353:                }
354:            }//main()
355:
356:        }//class StringHelper
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.