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: }
|