Source Code Cross Referenced for Strip.java in  » Database-ORM » MMBase » org » mmbase » 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 » Database ORM » MMBase » org.mmbase.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:
003:        This software is OSI Certified Open Source Software.
004:        OSI Certified is a certification mark of the Open Source Initiative.
005:
006:        The license (Mozilla version 1.0) can be read at the MMBase site.
007:        See http://www.MMBase.org/license
008:
009:         */
010:        package org.mmbase.util;
011:
012:        import org.mmbase.util.logging.Logger;
013:        import org.mmbase.util.logging.Logging;
014:
015:        /**
016:         * Class to strip characters from the beginning and end of strings.
017:         *
018:         * <PRE>
019:         * Example1: Strip.Char("..dfld..",'.',Strip.TRAILING) yields "..dlfd."
020:         * Example2: Strip.Chars("..dfld..",".",Strip.TRAILING) yields "..dlfd"
021:         * Example3: Strip.Chars(". .. dfld. , .","., ",Strip.BOTH) yields "dfld"
022:         * </PRE>
023:         *
024:         * @author Rico Jansen
025:         * @version $Id: Strip.java,v 1.7 2004/09/30 14:07:13 pierre Exp $
026:         */
027:        public class Strip {
028:
029:            // logger
030:            private static Logger log = Logging.getLoggerInstance(Strip.class
031:                    .getName());
032:
033:            /**
034:             * Strip nothing, a rather ineffecient form of a copy
035:             */
036:            public static final int NOTHING = 0;
037:
038:            /**
039:             * Strip leading, only characters at begin of string are checked
040:             */
041:            public static final int LEADING = 1;
042:
043:            /**
044:             * Strip trailing, only characters at end of string are checked
045:             */
046:            public static final int TRAILING = 2;
047:
048:            /**
049:             * Strip both, characters at begin and end of string are checked
050:             */
051:            public static final int BOTH = 3;
052:
053:            /**
054:             * Strip double quotes from beginning, end or both, only once.
055:             * @param str the string to strip
056:             * @param where one of {@link #NOTHING}, {@link #LEADING}, {@link #TRAILING}
057:             * or {@link #BOTH}
058:             * @return the stripped String
059:             */
060:            public static String DoubleQuote(String str, int where) {
061:                return Char(str, '"', where);
062:            }
063:
064:            /**
065:             * Strip single quotes from beginning, end or both, only once.
066:             * @param str the string to strip
067:             * @param where one of {@link #NOTHING}, {@link #LEADING}, {@link #TRAILING}
068:             * or {@link #BOTH}
069:             * @return the stripped String
070:             */
071:            public static String SingleQuote(String str, int where) {
072:                return Char(str, '\'', where);
073:            }
074:
075:            /**
076:             * Strip multiple whitespace characters from beginning, end or both, that
077:             * means keep on stripping util a non-whitespace character is found.
078:             * @param str the string to strip
079:             * @param where one of {@link #NOTHING}, {@link #LEADING}, {@link #TRAILING}
080:             * or {@link #BOTH}
081:             * @return the stripped String
082:             */
083:            public static String Whitespace(String str, int where) {
084:                return Chars(str, " \t\n\r", where);
085:            }
086:
087:            /**
088:             * Strip all of the specified character from beginning, end or both.
089:             * @param str the string to strip
090:             * @param chr the character to strip from the string
091:             * @param where one of {@link #NOTHING}, {@link #LEADING}, {@link #TRAILING}
092:             * or {@link #BOTH}
093:             * @return the stripped String
094:             */
095:            public static String Char(String str, char chr, int where) {
096:                if (str != null && str.length() > 0) {
097:                    int lead = 0;
098:                    int trail = str.length() - 1;
099:
100:                    switch (where) {
101:                    case LEADING:
102:                        if (str.charAt(lead) == chr)
103:                            lead++;
104:                        break;
105:                    case TRAILING:
106:                        if (str.charAt(trail) == chr)
107:                            trail--;
108:                        break;
109:                    case BOTH:
110:                        if (str.charAt(lead) == chr)
111:                            lead++;
112:                        if (str.charAt(trail) == chr)
113:                            trail--;
114:                        break;
115:                    default:
116:                        break;
117:                    }
118:                    str = str.substring(lead, trail + 1);
119:                }
120:                return str;
121:            }
122:
123:            /**
124:             * Strip multiple characters contained in the set given as second parameter
125:             * until a non-set character.
126:             * @param str the string to strip
127:             * @param chars a string containing all characters to strip from the string
128:             * @param where one of {@link #NOTHING}, {@link #LEADING}, {@link #TRAILING}
129:             * or {@link #BOTH}
130:             * @return the stripped String
131:             */
132:            public static String Chars(String str, String chars, int where) {
133:
134:                if (str != null && str.length() > 0) {
135:                    int lead = 0;
136:                    int trail = str.length() - 1;
137:
138:                    if (trail < 1) {
139:                        where = LEADING;
140:                    } else {
141:                        switch (where) {
142:                        case LEADING:
143:                            while (chars.indexOf(str.charAt(lead)) != -1
144:                                    && (lead < str.length() - 1))
145:                                lead++;
146:                            break;
147:                        case TRAILING:
148:                            while (chars.lastIndexOf(str.charAt(trail)) != -1
149:                                    && trail > 0)
150:                                trail--;
151:                            break;
152:                        case BOTH:
153:                            while (chars.indexOf(str.charAt(lead)) != -1
154:                                    && lead < (str.length() - 1))
155:                                lead++;
156:                            while (chars.lastIndexOf(str.charAt(trail)) != -1
157:                                    && trail >= lead)
158:                                trail--;
159:                            break;
160:                        default:
161:                            break;
162:                        }
163:                    }
164:                    if (lead <= trail) {
165:                        str = str.substring(lead, trail + 1);
166:                    } else {
167:                        str = "";
168:                    }
169:                }
170:                return str;
171:            }
172:
173:            /**
174:             * Test the class
175:             */
176:            public static void main(String args[]) {
177:                log.info("Double "
178:                        + Strip.DoubleQuote("\"double\"", Strip.BOTH));
179:                log.info("Single " + Strip.SingleQuote("'single'", Strip.BOTH));
180:                log.info("White |"
181:                        + Strip.Whitespace("   white         \n", Strip.BOTH)
182:                        + "|");
183:            }
184:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.