Source Code Cross Referenced for CSVString.java in  » Scripting » jython » com » ziclix » python » sql » pipe » csv » 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 » Scripting » jython » com.ziclix.python.sql.pipe.csv 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Jython Database Specification API 2.0
003:         *
004:         * $Id: CSVString.java 2414 2005-02-23 04:26:23Z bzimmer $
005:         *
006:         * Copyright (c) 2001 brian zimmer <bzimmer@ziclix.com>
007:         *
008:         */
009:        package com.ziclix.python.sql.pipe.csv;
010:
011:        /**
012:         * A utility class to aide in quoting CSV strings.
013:         *
014:         * @author brian zimmer
015:         * @version $Revision: 2414 $
016:         */
017:        public class CSVString {
018:
019:            /**
020:             * The default delimiter.
021:             */
022:            public static final String DELIMITER = ",";
023:
024:            private CSVString() {
025:            }
026:
027:            /**
028:             * Escape the string as needed using the default delimiter.
029:             */
030:            public static String toCSV(String string) {
031:                return toCSV(string, CSVString.DELIMITER);
032:            }
033:
034:            /**
035:             * Escape the string as needed using the given delimiter.
036:             */
037:            public static String toCSV(String string, String delimiter) {
038:
039:                String res = replace(string, "\"", "\"\"");
040:
041:                if ((res.indexOf("\"") >= 0)
042:                        || (string.indexOf(delimiter) >= 0)) {
043:                    res = "\"" + res + "\"";
044:                }
045:
046:                return res;
047:            }
048:
049:            /**
050:             * Returns a new string resulting from replacing the first occurrence, or all occurrences,
051:             * of search string in this string with replace string.
052:             * If the string search does not occur in the character sequence represented by this object,
053:             * then this string is returned.
054:             *
055:             * @param search    the old string
056:             * @param replace   the new string
057:             * @param all=true  all occurrences of the search string are replaced
058:             * @param all=false only the first occurrence of the search string is replaced
059:             * @return a string derived from this string by replacing the first occurrence,
060:             *         or every occurrence of search with replace.
061:             */
062:            public static String replace(String original, String search,
063:                    String replace, boolean all) {
064:
065:                String valReturn = new String("");
066:                int l = original.length();
067:                int lo = search.length();
068:                int i = 0;
069:                int j;
070:
071:                while (i <= l) {
072:                    j = original.indexOf(search, i);
073:
074:                    if (j == -1) {
075:                        valReturn = valReturn.concat(original.substring(i, l));
076:                        i = l + 1; // Stop, no more occurrence.
077:                    } else {
078:                        valReturn = valReturn.concat(original.substring(i, j));
079:                        valReturn = valReturn.concat(replace);
080:                        i = j + lo;
081:
082:                        if (!all) { // Stop, replace the first occurrence only.
083:                            valReturn = valReturn.concat(original.substring(i,
084:                                    l));
085:                            i = l + 1; // Stop, replace the first occurrence only.
086:                        }
087:                    }
088:                }
089:
090:                return valReturn;
091:            }
092:
093:            /**
094:             * Returns a new string resulting from replacing all occurrences,
095:             * of search string in this string with replace string.
096:             * If the string search does not occur in the character sequence represented by this object,
097:             * then this string is returned.
098:             *
099:             * @param search  the old string
100:             * @param replace the new string
101:             * @return a string derived from this string by replacing every occurrence of search with replace.
102:             */
103:            public static String replace(String original, String search,
104:                    String replace) {
105:                return replace(original, search, replace, true);
106:            }
107:
108:            /**
109:             * Returns a new string resulting from replacing the end of this String
110:             * from oldSuffix to newSuffix.
111:             * The original string is returned if it does not end with oldSuffix.
112:             *
113:             * @param oldSuffix the old suffix
114:             * @param newSuffix the new suffix
115:             * @return a string derived from this string by replacing the end oldSuffix by newSuffix
116:             */
117:            public static String replaceEndWith(String original,
118:                    String oldSuffix, String newSuffix) {
119:
120:                if (original.endsWith(oldSuffix)) {
121:                    String st = original.substring(0, original.length()
122:                            - oldSuffix.length());
123:
124:                    return st.concat(newSuffix);
125:                } else {
126:                    return original;
127:                }
128:            }
129:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.