Source Code Cross Referenced for StringUtility.java in  » Science » Cougaar12_4 » org » cougaar » 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 » Science » Cougaar12_4 » org.cougaar.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * <copyright>
003:         *  
004:         *  Copyright 1997-2004 BBNT Solutions, LLC
005:         *  under sponsorship of the Defense Advanced Research Projects
006:         *  Agency (DARPA).
007:         * 
008:         *  You can redistribute this software and/or modify it under the
009:         *  terms of the Cougaar Open Source License as published on the
010:         *  Cougaar Open Source Website (www.cougaar.org).
011:         * 
012:         *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013:         *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014:         *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015:         *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016:         *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017:         *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018:         *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019:         *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020:         *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021:         *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022:         *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023:         *  
024:         * </copyright>
025:         */
026:
027:        package org.cougaar.util;
028:
029:        import java.util.Vector;
030:
031:        /** Utility knife for efficiently parsing stuff out of strings.
032:         **/
033:
034:        public class StringUtility {
035:
036:            /** an empty String instance **/
037:            public static final String emptyString = "";
038:
039:            /** @return an array of strings which represents the comma-separated-
040:             * values list from the range of characters in buf. 
041:             * @deprecated Use {@link CSVUtility#parse(String)} 
042:             **/
043:            public static Vector parseCSV(String buf, int start, int end,
044:                    char comma) {
045:                Vector tmp = new Vector();
046:
047:                StringBuffer sb = new StringBuffer(); // temp storage
048:
049:                int i = start;
050:                int lc = -1; // last comma index
051:                boolean white = true;
052:                for (; i < end; i++) {
053:                    char c = buf.charAt(i);
054:                    if (c == comma) {
055:                        tmp.addElement(trimStringBufferRight(sb));
056:                        sb.setLength(0);
057:                        lc = i;
058:                        white = true;
059:                    } else if (c == '\\') {
060:                        i++;
061:                        c = buf.charAt(i);
062:                        sb.append(c);
063:                        white = false;
064:                    } else if (!white) {
065:                        sb.append(c);
066:                    } else if (!Character.isWhitespace(c)) {
067:                        sb.append(c);
068:                        white = false;
069:                    } else {
070:                        // white and still reading initial white space
071:                    }
072:                }
073:                if (sb.length() > 0) {
074:                    // still chars in the buffer?
075:                    tmp.addElement(trimStringBufferRight(sb));
076:                } else if (lc > -1 && i > lc) {
077:                    tmp.addElement(emptyString);
078:                }
079:
080:                return tmp;
081:            }
082:
083:            public static String trimStringBufferRight(StringBuffer sb) {
084:                int l = sb.length();
085:
086:                // if empty, return an empty string
087:                if (l == 0)
088:                    return emptyString;
089:
090:                // skip trailing whitespace
091:                int j;
092:                for (j = (l - 1); j >= 0; j--) {
093:                    if (!Character.isWhitespace(sb.charAt(j))) {
094:                        break;
095:                    }
096:                }
097:
098:                // if only whitespace return empty
099:                if (j < 0)
100:                    return emptyString;
101:
102:                sb.setLength(j + 1);
103:
104:                return sb.toString();
105:            }
106:
107:            static Vector toVector(String[] ss) {
108:                Vector r = new Vector(ss.length);
109:                for (int i = 0; i < ss.length; i++) {
110:                    r.add(ss[i]);
111:                }
112:                return r;
113:            }
114:
115:            /** @deprecated Use {@link CSVUtility#parse(String)} **/
116:            public static Vector parseCSV(String buf) {
117:                return toVector(CSVUtility.parse(buf));
118:            }
119:
120:            /** @deprecated Use {@link CSVUtility#parse(String)} **/
121:            public static Vector parseCSV(String buf, int start) {
122:                return toVector(CSVUtility.parse(buf.substring(start)));
123:            }
124:
125:            /** @deprecated Use {@link CSVUtility#parse(String)} **/
126:            public static Vector parseCSV(String buf, int start, int end) {
127:                return toVector(CSVUtility.parse(buf.substring(start, end)));
128:            }
129:
130:            /** @deprecated Use {@link CSVUtility#parse(String)} or {@link String#split(String)} **/
131:            public static Vector parseCSV(String buf, char comma) {
132:                if (comma == ',') {
133:                    return parseCSV(buf);
134:                } else {
135:                    return toVector(buf.split("\\s*" + comma + "\\s*"));
136:                }
137:            }
138:
139:            /** @deprecated Use {@link String#split(String)} **/
140:            public static Vector explode(String s) {
141:                Vector v = new Vector();
142:                int k = 0; // char after last white
143:                int l = s.length();
144:                int i = 0;
145:                while (i < l) {
146:                    if (Character.isWhitespace(s.charAt(i))) {
147:                        // is white - what do we do?
148:                        if (i == k) { // skipping contiguous white
149:                            k++;
150:                        } else { // last char wasn't white - word boundary!
151:                            v.addElement(s.substring(k, i));
152:                            k = i + 1;
153:                        }
154:                    } else { // nonwhite
155:                        // let it advance
156:                    }
157:                    i++;
158:                }
159:                if (k != i) { // leftover non-white chars
160:                    v.addElement(s.substring(k, i));
161:                }
162:                return v;
163:            }
164:
165:            /** 
166:             * Prefix all lines of "s" with "prefix", e.g.<br>
167:             *   <code>prefixLines("X: ", "a\nb")</code><br>
168:             * returns<br>
169:             *   <code>"X: a\nA: b"</code><br>
170:             */
171:            public static String prefixLines(String prefix, String s) {
172:                StringBuffer sb = new StringBuffer();
173:                int j = s.indexOf('\n');
174:                if (j < 0) {
175:                    sb.append(prefix).append(s);
176:                } else {
177:                    int i = 0;
178:                    do {
179:                        sb.append(prefix).append(s.substring(i, ++j));
180:                        i = j;
181:                        j = s.indexOf('\n', i);
182:                    } while (j >= 0);
183:                    sb.append(prefix).append(s.substring(i));
184:                }
185:                return sb.toString();
186:            }
187:
188:            public static String arrayToString(Object[] array) {
189:                return appendArray(new StringBuffer(), array).substring(0);
190:            }
191:
192:            public static StringBuffer appendArray(StringBuffer buf,
193:                    Object[] array) {
194:                buf.append('[');
195:                for (int i = 0; i < array.length; i++) {
196:                    if (i > 0)
197:                        buf.append(',');
198:                    buf.append(array[i]);
199:                }
200:                buf.append(']');
201:                return buf;
202:            }
203:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.