You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jetspeed-dev@portals.apache.org by we...@apache.org on 2004/02/24 01:31:47 UTC

cvs commit: jakarta-jetspeed-2/components/persistence/src/java/org/apache/jetspeed/util/ojb LocaleFieldConversion.java ObjectIDtoStringFieldConversion.java ObjectIDtoLongFieldConversion.java FieldConversionLog.java CollectionDebugger.java CSVtoCollectionFieldConversion.java CSVtoPortletModeFieldConversion.java

weaver      2004/02/23 16:31:47

  Added:       components/persistence/src/java/org/apache/jetspeed/util/ojb
                        LocaleFieldConversion.java
                        ObjectIDtoStringFieldConversion.java
                        ObjectIDtoLongFieldConversion.java
                        FieldConversionLog.java CollectionDebugger.java
                        CSVtoCollectionFieldConversion.java
                        CSVtoPortletModeFieldConversion.java
  Log:
  moved ojb uitlities into components/persistence
  
  Revision  Changes    Path
  1.1                  jakarta-jetspeed-2/components/persistence/src/java/org/apache/jetspeed/util/ojb/LocaleFieldConversion.java
  
  Index: LocaleFieldConversion.java
  ===================================================================
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2000-2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache Jetspeed" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache Jetspeed", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  package org.apache.jetspeed.util.ojb;
  
  import java.util.Locale;
  import java.util.StringTokenizer;
  
  import org.apache.ojb.broker.accesslayer.conversions.ConversionException;
  import org.apache.ojb.broker.accesslayer.conversions.FieldConversion;
  
  /**
   * <p style="font-weight: bold">
   * ObjectRelationalBridge field conversion.
   * </p>
   * Helps transparently map Locale objects into a database table
   * that contains country, langauge and variant field and vice versa.
   * 
   * field should be tokenized with commas
   */
  public class LocaleFieldConversion implements FieldConversion
  {
      private static final String DELIM = ",";
  
      /**
       * @see org.apache.ojb.broker.accesslayer.conversions.FieldConversion#javaToSql(java.lang.Object)
       */
      public Object javaToSql(Object arg0) throws ConversionException
      {
          if (arg0 instanceof Locale)
          {
  
              Locale locale = (Locale) arg0;
              String country = locale.getCountry();
              String language = locale.getLanguage();
              String variant = locale.getVariant();
              StringBuffer buffer = new StringBuffer(40);
              if (language != null)
              {
                  buffer.append(language);
              }
              buffer.append(DELIM);
  
              if (country != null)
              {
                  buffer.append(country);
              }
              buffer.append(DELIM);
  
              if (variant != null)
              {
                  buffer.append(variant);
              }
  
              return buffer.toString().trim();
          }
          else
          {
              return arg0;
          }
      }
  
      /**
       * @see org.apache.ojb.broker.accesslayer.conversions.FieldConversion#sqlToJava(java.lang.Object)
       */
      public Object sqlToJava(Object arg0) throws ConversionException
      {
          if (arg0 instanceof String)
          {
  
              String localeString = (String) arg0;
              StringTokenizer tokenizer = new StringTokenizer(localeString, DELIM);
  
              
              String language = tokenizer.nextToken().trim();
              String country = null;
              String variant = null;
              if(tokenizer.hasMoreTokens())
              {
                  country = tokenizer.nextToken().trim();
              }
              
              if(tokenizer.hasMoreTokens())
              {
                  variant = tokenizer.nextToken().trim();
              }
              
  
              if (country != null && language != null && variant != null)
              {
                  return new Locale (language, country, variant);
              }
              else if (country != null && language != null)
              {
                  return new Locale(language, country);
              }
              else if (language != null)
              {
                  return new Locale(language, ""); // JDK 1.3 compatibility
              }
              else
              {
                  return Locale.getDefault();
              }
          }
          else
          {
              return arg0;
          }
  
      }
  
  }
  
  
  
  1.1                  jakarta-jetspeed-2/components/persistence/src/java/org/apache/jetspeed/util/ojb/ObjectIDtoStringFieldConversion.java
  
  Index: ObjectIDtoStringFieldConversion.java
  ===================================================================
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2000-2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache Jetspeed" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache Jetspeed", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  package org.apache.jetspeed.util.ojb;
  
  import org.apache.jetspeed.util.JetspeedObjectID;
  import org.apache.ojb.broker.accesslayer.conversions.ConversionException;
  import org.apache.ojb.broker.accesslayer.conversions.FieldConversion;
  
  /**
    * <p style="font-weight: bold">
   * ObjectRelationalBridge field conversion.
   * </p>
   * 
   * Converts between <code>long</code> and <code>ObjectID</code>
   * 
   * @author <a href="mailto:weaver@apache.org">Scott T. Weaver</a>
   */
  public class ObjectIDtoStringFieldConversion implements FieldConversion
  {
  
      /**
       * @see org.apache.ojb.broker.accesslayer.conversions.FieldConversion#javaToSql(java.lang.Object)
       */
      public Object javaToSql(Object arg0) throws ConversionException
      {
          if (arg0 instanceof JetspeedObjectID)
          {
              JetspeedObjectID oid = (JetspeedObjectID) arg0;
  
              return oid.toString();
          }
          else
          {
              return arg0;
          }
  
      }
  
      /**
       * @see org.apache.ojb.broker.accesslayer.conversions.FieldConversion#sqlToJava(java.lang.Object)
       */
      public Object sqlToJava(Object arg0) throws ConversionException
      {
          if (arg0 instanceof String)
          {            
              return  JetspeedObjectID.createFromString((String)arg0);
          }
          else
          {
              return arg0;
          }
  
      }
  
  }
  
  
  
  1.1                  jakarta-jetspeed-2/components/persistence/src/java/org/apache/jetspeed/util/ojb/ObjectIDtoLongFieldConversion.java
  
  Index: ObjectIDtoLongFieldConversion.java
  ===================================================================
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2000-2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache Jetspeed" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache Jetspeed", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  package org.apache.jetspeed.util.ojb;
  
  import org.apache.jetspeed.util.JetspeedObjectID;
  import org.apache.ojb.broker.accesslayer.conversions.ConversionException;
  import org.apache.ojb.broker.accesslayer.conversions.FieldConversion;
  
  /**
    * <p style="font-weight: bold">
   * ObjectRelationalBridge field conversion.
   * </p>
   * 
   * Converts between <code>long</code> and <code>ObjectID</code>
   * 
   * @author <a href="mailto:weaver@apache.org">Scott T. Weaver</a>
   */
  public class ObjectIDtoLongFieldConversion implements FieldConversion
  {
  
      /**
       * @see org.apache.ojb.broker.accesslayer.conversions.FieldConversion#javaToSql(java.lang.Object)
       */
      public Object javaToSql(Object arg0) throws ConversionException
      {
          if (arg0 instanceof JetspeedObjectID)
          {
              JetspeedObjectID oid = (JetspeedObjectID) arg0;
  
              return new Integer(oid.intValue());
          }
          else
          {
              return arg0;
          }
  
      }
  
      /**
       * @see org.apache.ojb.broker.accesslayer.conversions.FieldConversion#sqlToJava(java.lang.Object)
       */
      public Object sqlToJava(Object arg0) throws ConversionException
      {
          if (arg0 instanceof Number)
          {
              
              return new JetspeedObjectID(((Number)arg0).intValue());
          }
          else
          {
              return arg0;
          }
  
      }
  
  }
  
  
  
  1.1                  jakarta-jetspeed-2/components/persistence/src/java/org/apache/jetspeed/util/ojb/FieldConversionLog.java
  
  Index: FieldConversionLog.java
  ===================================================================
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2000-2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache Jetspeed" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache Jetspeed", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  package org.apache.jetspeed.util.ojb;
  
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  
  /**
   * Utility class that allows convenient access to commons=logging for OJB 
   * FiledConversions without having to define a 
   * <code>org.apache.commons.logging.Log</code> in each of conversion
   * class.
   *
   *@author <a href="mailto:weaver@apache.org">Scott T. Weaver</a>
   */
  public abstract class FieldConversionLog
  {
      /**
       * There is only default ("package") access to this Log only as
       * all OJB FieldConversions should be located here.
       */
      static final Log LOG = LogFactory.getLog("org.apache.jetspeed.util.ojb");
  }
  
  
  
  1.1                  jakarta-jetspeed-2/components/persistence/src/java/org/apache/jetspeed/util/ojb/CollectionDebugger.java
  
  Index: CollectionDebugger.java
  ===================================================================
  /**
   * Created on Jan 22, 2004
   *
   * 
   * @author
   */
  package org.apache.jetspeed.util.ojb;
  
  
  import org.apache.ojb.broker.PersistenceBroker;
  import org.apache.ojb.broker.accesslayer.QueryCustomizer;
  import org.apache.ojb.broker.metadata.CollectionDescriptor;
  
  import org.apache.ojb.broker.query.Query;
  import org.apache.ojb.broker.query.QueryByCriteria;
  
  
  /**
   * <p>
   * CollectionDebugger
   * </p>
   * 
   * @author <a href="mailto:weaver@apache.org">Scott T. Weaver</a>
   * @version $Id: CollectionDebugger.java,v 1.1 2004/02/24 00:31:46 weaver Exp $
   *
   */
  public class CollectionDebugger implements QueryCustomizer
  {
  
      /**
       * @see org.apache.ojb.broker.accesslayer.QueryCustomizer#customizeQuery(java.lang.Object, org.apache.ojb.broker.PersistenceBroker, org.apache.ojb.broker.metadata.CollectionDescriptor, org.apache.ojb.broker.query.QueryByCriteria)
       */
      public Query customizeQuery(Object arg0, PersistenceBroker pb, CollectionDescriptor arg2, QueryByCriteria arg3)
      {        
          return arg3;
      }
  
      /**
       * @see org.apache.ojb.broker.metadata.AttributeContainer#addAttribute(java.lang.String, java.lang.String)
       */
      public void addAttribute(String arg0, String arg1)
      {
          // TODO Auto-generated method stub
  
      }
  
      /**
       * @see org.apache.ojb.broker.metadata.AttributeContainer#getAttribute(java.lang.String, java.lang.String)
       */
      public String getAttribute(String arg0, String arg1)
      {
          // TODO Auto-generated method stub
          return null;
      }
  
      /**
       * @see org.apache.ojb.broker.metadata.AttributeContainer#getAttribute(java.lang.String)
       */
      public String getAttribute(String arg0)
      {
          // TODO Auto-generated method stub
          return null;
      }
  
  }
  
  
  
  1.1                  jakarta-jetspeed-2/components/persistence/src/java/org/apache/jetspeed/util/ojb/CSVtoCollectionFieldConversion.java
  
  Index: CSVtoCollectionFieldConversion.java
  ===================================================================
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2000-2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache Jetspeed" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache Jetspeed", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  package org.apache.jetspeed.util.ojb;
  
  import java.io.IOException;
  import java.io.StreamTokenizer;
  import java.io.StringReader;
  import java.util.ArrayList;
  import java.util.Collection;
  import java.util.Iterator;
  
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  import org.apache.ojb.broker.accesslayer.conversions.ConversionException;
  import org.apache.ojb.broker.accesslayer.conversions.FieldConversion;
  
  /**
   * <p style="font-weight: bold">
   * ObjectRelationalBridge field conversion.
   * </p>
   * Converts from a comma-delimited field to a <code>java.util.collection</code>
   * 
   * @author <a href="mailto:weaver@apache.org">Scott T. Weaver</a>
   */
  public class CSVtoCollectionFieldConversion implements FieldConversion
  {
      private static final String DELIM = ",";
      private static final String QUOTE = "\"";
      
      private static final Log log = LogFactory.getLog(CSVtoCollectionFieldConversion.class);
      
      /**
       * @see org.apache.ojb.broker.accesslayer.conversions.FieldConversion#javaToSql(java.lang.Object)
       * @task Fix JDK 1.3 complient problem described in the FIXME
       */
      public Object javaToSql(Object arg0) throws ConversionException
      {
          if (arg0 instanceof Collection)
          {
              Collection col = ((Collection) arg0);
              if (col.size() == 0)
              {
                  return "";
              }
  
              Iterator itr = col.iterator();
              // Estimate that the average word is going to be 5 characters long
              StringBuffer buffer = new StringBuffer((col.size() * 5));
              while (itr.hasNext())
              {
                  buffer.append(QUOTE);
                  String value = getNext(itr);
  
                  // FIXME: The following is not JDK1.3 complient. So I implement a warning 
                  //        message as a workaround until this field conversion is no longer
                  //        need and delete, or the code is made JDK 1.3 complient. (Paul Spencer)
  
                  // buffer.append(value.replaceAll("\"","\\\\\""));
                  if(value != null && value.toString().indexOf("\"") >= 0)
                  {
                  //  FieldConversionLog.LOG.error("The string '" + value + 
                  // "' contains embeded '\"'.  It will not be converted to a CSV correctly.");
                    log.warn("In CSVtoCollectionFieldConversion() - The string '" + value + 
                          "' contains embeded '\"'.  It will not be converted to a CSV correctly.");
                  }
                  buffer.append(value);
                  // End of FIXME:
                  buffer.append(QUOTE);
                  
                  if (itr.hasNext())
                  {
                      buffer.append(DELIM);
                  }
              }
  
              return buffer.toString();
          }
          return arg0;
      }
  
      /**
       * @see org.apache.ojb.broker.accesslayer.conversions.FieldConversion#sqlToJava(java.lang.Object)
       */
      public Object sqlToJava(Object arg0) throws ConversionException
      {
  
          if (arg0 instanceof String)
          {
              StringReader sr = new StringReader((String) arg0);
              StreamTokenizer st = new StreamTokenizer(sr);
              st.resetSyntax();
              st.whitespaceChars(',', ',');
              st.quoteChar('"');
              st.eolIsSignificant(false);
  
           
              ArrayList list = new ArrayList();
              try
              {
                  while (st.nextToken() != StreamTokenizer.TT_EOF)
                  {
                      list.add(createObject(st.sval));
                      log.debug("Parsed token value: "+st.sval);
                  }
              }
              catch (IOException e)
              {
                  String message = "CSV parsing failed during field conversion.";
                  log.error(message, e);
                  throw new ConversionException("CSV parsing failed during field conversion.", e);
              } 
  
              return list;
          }
  
          return arg0;
      }
  
      /**
       * Makes creation of objects created via csv fields extensible
       * By default simply return the string value.
       * 
       * @param name The string value
       * @return The string value
       */
      protected Object createObject(String name)
      {
          return name;
      }
  
      /**
       * Makes getting objects via csv fields extensible
       * By default simply return the string value.
       * 
       * @param name The string value
       * @return The string value
       */
      protected String getNext(Iterator iterator)
      {
          return (String) iterator.next();
      }
  
  }
  
  
  
  1.1                  jakarta-jetspeed-2/components/persistence/src/java/org/apache/jetspeed/util/ojb/CSVtoPortletModeFieldConversion.java
  
  Index: CSVtoPortletModeFieldConversion.java
  ===================================================================
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2000-2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache Jetspeed" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache Jetspeed", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  package org.apache.jetspeed.util.ojb;
  
  import java.util.Iterator;
  
  import javax.portlet.PortletMode;
  
  import org.apache.ojb.broker.accesslayer.conversions.FieldConversion;
  
  /**
   * CSVtoPortletModeFieldConversion
   *
   * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
   * @version $Id: CSVtoPortletModeFieldConversion.java,v 1.1 2004/02/24 00:31:46 weaver Exp $
   */
  public class CSVtoPortletModeFieldConversion extends CSVtoCollectionFieldConversion implements FieldConversion
  {
      /* (non-Javadoc)
       * @see org.apache.jetspeed.util.ojb.CSVtoCollectionFieldConversion#getNext(java.util.Iterator)
       */
      protected String getNext(Iterator iterator)
      {
          PortletMode mode = (PortletMode)iterator.next();
          return mode.toString();
      }
      
      /* (non-Javadoc)
       * @see org.apache.jetspeed.util.ojb.CSVtoCollectionFieldConversion#createObject(java.lang.String)
       */
      protected Object createObject(String name)
      {
          return new PortletMode(name);
      }
      
      
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: jetspeed-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: jetspeed-dev-help@jakarta.apache.org