Source Code Cross Referenced for CSVtoCollectionFieldConversion.java in  » Portal » jetspeed-2.1.3 » org » apache » jetspeed » util » ojb » 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 » jetspeed 2.1.3 » org.apache.jetspeed.util.ojb 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Licensed to the Apache Software Foundation (ASF) under one or more
003:         * contributor license agreements.  See the NOTICE file distributed with
004:         * this work for additional information regarding copyright ownership.
005:         * The ASF licenses this file to You under the Apache License, Version 2.0
006:         * (the "License"); you may not use this file except in compliance with
007:         * the License.  You may obtain a copy of the License at
008:         * 
009:         *      http://www.apache.org/licenses/LICENSE-2.0
010:         * 
011:         * Unless required by applicable law or agreed to in writing, software
012:         * distributed under the License is distributed on an "AS IS" BASIS,
013:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         * See the License for the specific language governing permissions and
015:         * limitations under the License.
016:         */
017:        package org.apache.jetspeed.util.ojb;
018:
019:        import java.io.IOException;
020:        import java.io.StreamTokenizer;
021:        import java.io.StringReader;
022:        import java.util.ArrayList;
023:        import java.util.Collection;
024:        import java.util.Iterator;
025:
026:        import org.apache.commons.logging.Log;
027:        import org.apache.commons.logging.LogFactory;
028:        import org.apache.ojb.broker.accesslayer.conversions.ConversionException;
029:        import org.apache.ojb.broker.accesslayer.conversions.FieldConversion;
030:
031:        /**
032:         * <p style="font-weight: bold">
033:         * ObjectRelationalBridge field conversion.
034:         * </p>
035:         * Converts from a comma-delimited field to a <code>java.util.collection</code>
036:         * 
037:         * @author <a href="mailto:weaver@apache.org">Scott T. Weaver</a>
038:         */
039:        public class CSVtoCollectionFieldConversion implements  FieldConversion {
040:            private static final String DELIM = ",";
041:            private static final String QUOTE = "\"";
042:
043:            private static final Log log = LogFactory
044:                    .getLog(CSVtoCollectionFieldConversion.class);
045:
046:            /**
047:             * @see org.apache.ojb.broker.accesslayer.conversions.FieldConversion#javaToSql(java.lang.Object)
048:             * @task Fix JDK 1.3 complient problem described in the FIXME
049:             */
050:            public Object javaToSql(Object arg0) throws ConversionException {
051:                if (arg0 instanceof  Collection) {
052:                    Collection col = ((Collection) arg0);
053:                    if (col.size() == 0) {
054:                        return "";
055:                    }
056:
057:                    Iterator itr = col.iterator();
058:                    // Estimate that the average word is going to be 5 characters long
059:                    StringBuffer buffer = new StringBuffer((col.size() * 5));
060:                    while (itr.hasNext()) {
061:                        buffer.append(QUOTE);
062:                        String value = getNext(itr);
063:
064:                        // FIXME: The following is not JDK1.3 complient. So I implement a warning 
065:                        //        message as a workaround until this field conversion is no longer
066:                        //        need and delete, or the code is made JDK 1.3 complient. (Paul Spencer)
067:
068:                        // buffer.append(value.replaceAll("\"","\\\\\""));
069:                        if (value != null
070:                                && value.toString().indexOf("\"") >= 0) {
071:                            //  FieldConversionLog.LOG.error("The string '" + value + 
072:                            // "' contains embeded '\"'.  It will not be converted to a CSV correctly.");
073:                            log
074:                                    .warn("In CSVtoCollectionFieldConversion() - The string '"
075:                                            + value
076:                                            + "' contains embeded '\"'.  It will not be converted to a CSV correctly.");
077:                        }
078:                        buffer.append(value);
079:                        // End of FIXME:
080:                        buffer.append(QUOTE);
081:
082:                        if (itr.hasNext()) {
083:                            buffer.append(DELIM);
084:                        }
085:                    }
086:
087:                    return buffer.toString();
088:                }
089:                return arg0;
090:            }
091:
092:            /**
093:             * @see org.apache.ojb.broker.accesslayer.conversions.FieldConversion#sqlToJava(java.lang.Object)
094:             */
095:            public Object sqlToJava(Object arg0) throws ConversionException {
096:
097:                if (arg0 instanceof  String) {
098:                    StringReader sr = new StringReader((String) arg0);
099:                    StreamTokenizer st = new StreamTokenizer(sr);
100:                    st.resetSyntax();
101:                    st.whitespaceChars(',', ',');
102:                    st.quoteChar('"');
103:                    st.eolIsSignificant(false);
104:
105:                    ArrayList list = new ArrayList();
106:                    try {
107:                        while (st.nextToken() != StreamTokenizer.TT_EOF) {
108:                            list.add(createObject(st.sval));
109:                        }
110:                    } catch (IOException e) {
111:                        String message = "CSV parsing failed during field conversion.";
112:                        log.error(message, e);
113:                        throw new ConversionException(
114:                                "CSV parsing failed during field conversion.",
115:                                e);
116:                    }
117:
118:                    return list;
119:                }
120:
121:                return arg0;
122:            }
123:
124:            /**
125:             * Makes creation of objects created via csv fields extensible
126:             * By default simply return the string value.
127:             * 
128:             * @param name The string value
129:             * @return The string value
130:             */
131:            protected Object createObject(String name) {
132:                return name;
133:            }
134:
135:            /**
136:             * Makes getting objects via csv fields extensible
137:             * By default simply return the string value.
138:             * 
139:             * @param name The string value
140:             * @return The string value
141:             */
142:            protected String getNext(Iterator iterator) {
143:                return (String) iterator.next();
144:            }
145:
146:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.