Coverage report

  %line %branch
org.apache.jetspeed.util.ojb.CSVtoCollectionFieldConversion
0% 
0% 

 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  * 
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  * 
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.apache.jetspeed.util.ojb;
 18  
 
 19  
 import java.io.IOException;
 20  
 import java.io.StreamTokenizer;
 21  
 import java.io.StringReader;
 22  
 import java.util.ArrayList;
 23  
 import java.util.Collection;
 24  
 import java.util.Iterator;
 25  
 
 26  
 import org.apache.commons.logging.Log;
 27  
 import org.apache.commons.logging.LogFactory;
 28  
 import org.apache.ojb.broker.accesslayer.conversions.ConversionException;
 29  
 import org.apache.ojb.broker.accesslayer.conversions.FieldConversion;
 30  
 
 31  
 /**
 32  
  * <p style="font-weight: bold">
 33  
  * ObjectRelationalBridge field conversion.
 34  
  * </p>
 35  
  * Converts from a comma-delimited field to a <code>java.util.collection</code>
 36  
  * 
 37  
  * @author <a href="mailto:weaver@apache.org">Scott T. Weaver</a>
 38  
  */
 39  0
 public class CSVtoCollectionFieldConversion implements FieldConversion
 40  
 {
 41  
     private static final String DELIM = ",";
 42  
     private static final String QUOTE = "\"";
 43  
     
 44  0
     private static final Log log = LogFactory.getLog(CSVtoCollectionFieldConversion.class);
 45  
     
 46  
     /**
 47  
      * @see org.apache.ojb.broker.accesslayer.conversions.FieldConversion#javaToSql(java.lang.Object)
 48  
      * @task Fix JDK 1.3 complient problem described in the FIXME
 49  
      */
 50  
     public Object javaToSql(Object arg0) throws ConversionException
 51  
     {
 52  0
         if (arg0 instanceof Collection)
 53  
         {
 54  0
             Collection col = ((Collection) arg0);
 55  0
             if (col.size() == 0)
 56  
             {
 57  0
                 return "";
 58  
             }
 59  
 
 60  0
             Iterator itr = col.iterator();
 61  
             // Estimate that the average word is going to be 5 characters long
 62  0
             StringBuffer buffer = new StringBuffer((col.size() * 5));
 63  0
             while (itr.hasNext())
 64  
             {
 65  0
                 buffer.append(QUOTE);
 66  0
                 String value = getNext(itr);
 67  
 
 68  
                 // FIXME: The following is not JDK1.3 complient. So I implement a warning 
 69  
                 //        message as a workaround until this field conversion is no longer
 70  
                 //        need and delete, or the code is made JDK 1.3 complient. (Paul Spencer)
 71  
 
 72  
                 // buffer.append(value.replaceAll("\"","\\\\\""));
 73  0
                 if(value != null && value.toString().indexOf("\"") >= 0)
 74  
                 {
 75  
                 //  FieldConversionLog.LOG.error("The string '" + value + 
 76  
                 // "' contains embeded '\"'.  It will not be converted to a CSV correctly.");
 77  0
                   log.warn("In CSVtoCollectionFieldConversion() - The string '" + value + 
 78  
                         "' contains embeded '\"'.  It will not be converted to a CSV correctly.");
 79  
                 }
 80  0
                 buffer.append(value);
 81  
                 // End of FIXME:
 82  0
                 buffer.append(QUOTE);
 83  
                 
 84  0
                 if (itr.hasNext())
 85  
                 {
 86  0
                     buffer.append(DELIM);
 87  
                 }
 88  0
             }
 89  
 
 90  0
             return buffer.toString();
 91  
         }
 92  0
         return arg0;
 93  
     }
 94  
 
 95  
     /**
 96  
      * @see org.apache.ojb.broker.accesslayer.conversions.FieldConversion#sqlToJava(java.lang.Object)
 97  
      */
 98  
     public Object sqlToJava(Object arg0) throws ConversionException
 99  
     {
 100  
 
 101  0
         if (arg0 instanceof String)
 102  
         {
 103  0
             StringReader sr = new StringReader((String) arg0);
 104  0
             StreamTokenizer st = new StreamTokenizer(sr);
 105  0
             st.resetSyntax();
 106  0
             st.whitespaceChars(',', ',');
 107  0
             st.quoteChar('"');
 108  0
             st.eolIsSignificant(false);
 109  
 
 110  
          
 111  0
             ArrayList list = new ArrayList();
 112  
             try
 113  
             {
 114  0
                 while (st.nextToken() != StreamTokenizer.TT_EOF)
 115  
                 {
 116  0
                     list.add(createObject(st.sval));
 117  
                 }
 118  
             }
 119  0
             catch (IOException e)
 120  
             {
 121  0
                 String message = "CSV parsing failed during field conversion.";
 122  0
                 log.error(message, e);
 123  0
                 throw new ConversionException("CSV parsing failed during field conversion.", e);
 124  0
             } 
 125  
 
 126  0
             return list;
 127  
         }
 128  
 
 129  0
         return arg0;
 130  
     }
 131  
 
 132  
     /**
 133  
      * Makes creation of objects created via csv fields extensible
 134  
      * By default simply return the string value.
 135  
      * 
 136  
      * @param name The string value
 137  
      * @return The string value
 138  
      */
 139  
     protected Object createObject(String name)
 140  
     {
 141  0
         return name;
 142  
     }
 143  
 
 144  
     /**
 145  
      * Makes getting objects via csv fields extensible
 146  
      * By default simply return the string value.
 147  
      * 
 148  
      * @param name The string value
 149  
      * @return The string value
 150  
      */
 151  
     protected String getNext(Iterator iterator)
 152  
     {
 153  0
         return (String) iterator.next();
 154  
     }
 155  
 
 156  
 }

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.