You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-dev@db.apache.org by ar...@apache.org on 2006/05/13 22:32:59 UTC
svn commit: r406149 -
/db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/accesslayer/conversions/
Author: arminw
Date: Sat May 13 13:32:57 2006
New Revision: 406149
URL: http://svn.apache.org/viewcvs?rev=406149&view=rev
Log:
fix problems with 'null' entries, introduce base class
Added:
db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/accesslayer/conversions/AbstractList2VarcharConversion.java
Modified:
db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/accesslayer/conversions/IntList2VarcharFieldConversion.java
db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/accesslayer/conversions/StringList2VarcharFieldConversion.java
db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/accesslayer/conversions/StringVector2VarcharFieldConversion.java
db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/accesslayer/conversions/TimeList2VarcharFieldConversion.java
Added: db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/accesslayer/conversions/AbstractList2VarcharConversion.java
URL: http://svn.apache.org/viewcvs/db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/accesslayer/conversions/AbstractList2VarcharConversion.java?rev=406149&view=auto
==============================================================================
--- db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/accesslayer/conversions/AbstractList2VarcharConversion.java (added)
+++ db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/accesslayer/conversions/AbstractList2VarcharConversion.java Sat May 13 13:32:57 2006
@@ -0,0 +1,191 @@
+package org.apache.ojb.broker.accesslayer.conversions;
+
+/* Copyright 2002-2006 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import java.lang.Object;
+import java.util.List;
+import java.util.ArrayList;
+
+import org.apache.commons.lang.StringUtils;
+
+/**
+ * Base class to implement simple object List (e.g. list of strings or list of time values)
+ * to Varchar column conversions.
+ * <br/>
+ * Strings may not contain <em>"#"</em> (at the beginning and end of a string) as this is
+ * used as separator.
+ * Additionally this implementation use keywords for "blank string" and 'null' list entires,
+ * thus string based list entries may not contain keywords <em>"+.EmPtY.+"</em> and <em>"+.NuLl.+"</em>.
+ *
+ * @version $Id: $
+ */
+abstract class AbstractList2VarcharConversion implements FieldConversion
+{
+
+ protected static final String NULLVALUE = "#NULL#";
+ // don't fix the typo! otherwise we are no longer backward compatible
+ protected static final String EMPTYCOLLEC = "#EMTPY#";
+ protected static final String EMPTY_STR_ENTRY = "+.EmPtY.+";
+ protected static final String NULL_ENTRY = "+.NuLl.+";
+
+ public AbstractList2VarcharConversion()
+ {
+ }
+
+ /**
+ * Return a new instance of used {@link java.util.List} implementation.
+ */
+ protected List newList()
+ {
+ return new ArrayList();
+ }
+
+ /**
+ * Converts the list entry to a {@link String} representation. By default
+ * this method simply call 'toString' on specified entry. If entry is 'null'
+ * method returns 'null'.
+ */
+ protected String convertListEntryToString(Object entry)
+ {
+ String result = entry != null ? entry.toString() : null;
+ // if the separator keyword '#' is used within a string
+ // replace it by '##' to allow handle of '#' in strings
+ if(result != null)
+ {
+ result = StringUtils.replace(result, "#", "##");
+ }
+ return result;
+ }
+
+ /**
+ * Add a list entry (extracted from the datastore varchar column) to result list.
+ * By default this method simply add the string to the result list.
+ */
+ protected void addEntryToList(List resultList, String strEntry)
+ {
+ resultList.add(checkResult(strEntry));
+ }
+
+ /**
+ * Check method for String based list entries.
+ */
+ protected String checkResult(String result)
+ {
+ //System.out.println("check: " + result);
+ if(result.equals(NULL_ENTRY))
+ {
+ return null;
+}
+ else if(result.equals(EMPTY_STR_ENTRY))
+ {
+ return "";
+ }
+ else
+ {
+ return result;
+ }
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.ojb.broker.accesslayer.conversions.FieldConversion#javaToSql(java.lang.Object)
+ */
+ public Object javaToSql(Object source) throws ConversionException
+ {
+
+ if (source == null)
+ {
+ return NULLVALUE;
+ }
+
+ try
+ {
+ List sourceList = (List) source;
+ if (sourceList.isEmpty())
+ {
+ return EMPTYCOLLEC;
+ }
+
+ StringBuffer result = new StringBuffer();
+ for (int i = 0; i < sourceList.size(); i++)
+ {
+ String newSt = convertListEntryToString(sourceList.get(i));
+ if (i > 0)
+ {
+ result.append("#");
+ }
+ if(newSt == null) result.append(NULL_ENTRY);
+ else if(newSt.equals("")) result.append(EMPTY_STR_ENTRY);
+ else result.append(newSt);
+ }
+ return result.toString();
+ }
+ catch (ClassCastException e)
+ {
+ throw new ConversionException("Object is not a List of objects, class: "
+ + source.getClass().getName());
+ }
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.ojb.broker.accesslayer.conversions.FieldConversion#sqlToJava(java.lang.Object)
+ */
+ public Object sqlToJava(Object source) throws ConversionException
+ {
+ if (source == null)
+ {
+ return null;
+ }
+ if (source.toString().equals(NULLVALUE))
+ {
+ return null;
+ }
+ if (source.toString().equals(EMPTYCOLLEC))
+ {
+ return newList();
+ }
+
+ List resultList = newList();
+ int pos = 0;
+ int length;
+ StringBuffer input = new StringBuffer();
+ StringBuffer newString = new StringBuffer();
+ input.append(source.toString());
+ length = input.length();
+ while (pos < length)
+ {
+ if (input.charAt(pos) != '#')
+ {
+ newString.append(input.charAt(pos));
+ }
+ else
+ {
+ if (input.charAt(pos + 1) != '#')
+ {
+ addEntryToList(resultList, newString.toString());
+ newString = new StringBuffer();
+ }
+ else
+ {
+ newString.append('#');
+ ++pos;
+ }
+ }
+ ++pos;
+ }
+ addEntryToList(resultList, newString.toString());
+ return resultList;
+ }
+}
Modified: db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/accesslayer/conversions/IntList2VarcharFieldConversion.java
URL: http://svn.apache.org/viewcvs/db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/accesslayer/conversions/IntList2VarcharFieldConversion.java?rev=406149&r1=406148&r2=406149&view=diff
==============================================================================
--- db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/accesslayer/conversions/IntList2VarcharFieldConversion.java (original)
+++ db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/accesslayer/conversions/IntList2VarcharFieldConversion.java Sat May 13 13:32:57 2006
@@ -1,6 +1,6 @@
package org.apache.ojb.broker.accesslayer.conversions;
-/* Copyright 2004-2005 The Apache Software Foundation
+/* Copyright 2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -15,114 +15,24 @@
* limitations under the License.
*/
-import java.util.ArrayList;
import java.util.List;
-import org.apache.commons.lang.StringUtils;
-
/**
* This implementation of the {@link FieldConversion} interface converts
* between a {@link java.util.List} of {@link java.lang.Integer} objects and a database
* <em>varchar</em> field.
*
- * @author Guillaume Nodet
* @version $Id$
*/
-public class IntList2VarcharFieldConversion implements FieldConversion
+public class IntList2VarcharFieldConversion extends AbstractList2VarcharConversion
{
-
- private static final String NULLVALUE = "#NULL#";
- private static final String EMPTYCOLLEC = "#EMTPY#";
-
public IntList2VarcharFieldConversion()
{
}
- /* (non-Javadoc)
- * @see org.apache.ojb.broker.accesslayer.conversions.FieldConversion#javaToSql(java.lang.Object)
- */
- public Object javaToSql(Object source) throws ConversionException
- {
- if (source == null)
- {
- return NULLVALUE;
- }
-
- try
- {
- List intList = (List) source;
- if (intList.isEmpty())
- {
- return EMPTYCOLLEC;
- }
-
- StringBuffer result = new StringBuffer();
- for (int i = 0; i < intList.size(); i++)
- {
- Integer obj = (Integer) intList.get(i);
- String newSt = obj.toString();
- newSt = StringUtils.replace(newSt, "#", "##");
- result.append(newSt);
- result.append("#");
- }
- return result.toString();
- }
- catch (ClassCastException e)
- {
- throw new ConversionException("Object is not a List of Integer it is a"
- + source.getClass().getName());
- }
- }
-
- /* (non-Javadoc)
- * @see org.apache.ojb.broker.accesslayer.conversions.FieldConversion#sqlToJava(java.lang.Object)
- */
- public Object sqlToJava(Object source) throws ConversionException
+ protected void addEntryToList(List resultList, String strEntry)
{
-
- if (source == null)
- {
- return null;
- }
- if (!(source instanceof String))
- {
- throw new ConversionException("Object is not a String it is a"
- + source.getClass().getName());
- }
- if (source.toString().equals(NULLVALUE))
- {
- return null;
- }
- if (source.toString().equals(EMPTYCOLLEC))
- {
- return new ArrayList();
- }
-
- List v = new ArrayList();
- String input = source.toString();
- int pos = input.indexOf("#");
-
- while (pos >= 0)
- {
- if (pos == 0)
- {
- v.add("");
- }
- else
- {
- v.add(Integer.valueOf(input.substring(0, pos)));
- }
-
- if (pos + 1 > input.length())
- {
- //# at end causes outof bounds
- break;
- }
-
- input = input.substring(pos + 1, input.length());
- pos = input.indexOf("#");
- }
- return v;
+ String value = checkResult(strEntry);
+ resultList.add(value != null ? Integer.valueOf(value) : null);
}
-
}
Modified: db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/accesslayer/conversions/StringList2VarcharFieldConversion.java
URL: http://svn.apache.org/viewcvs/db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/accesslayer/conversions/StringList2VarcharFieldConversion.java?rev=406149&r1=406148&r2=406149&view=diff
==============================================================================
--- db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/accesslayer/conversions/StringList2VarcharFieldConversion.java (original)
+++ db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/accesslayer/conversions/StringList2VarcharFieldConversion.java Sat May 13 13:32:57 2006
@@ -1,6 +1,6 @@
package org.apache.ojb.broker.accesslayer.conversions;
-/* Copyright 2004-2005 The Apache Software Foundation
+/* Copyright 2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -15,119 +15,22 @@
* limitations under the License.
*/
-import java.util.ArrayList;
-import java.util.List;
-
-import org.apache.commons.lang.StringUtils;
/**
* This implementation of the {@link FieldConversion} interface converts
* between a {@link java.util.List} of {@link java.lang.String} objects and a database
* <em>varchar</em> field.
* <br/>
- * Strings may not contain "#" as this is used as separator.
+ * Strings may not contain <em>"#"</em> (at the beginning and end of a string) as this is
+ * used as separator.
+ * Additionally this implementation use keywords for "blank string" and 'null' list entires, thus Strings may
+ * not contain keywords <em>"+.EmPtY.+"</em> and <em>"+.NuLl.+"</em>.
*
- * @author Guillaume Nodet
* @version $Id$
*/
-public class StringList2VarcharFieldConversion implements FieldConversion
+public class StringList2VarcharFieldConversion extends AbstractList2VarcharConversion
{
-
- private static final String NULLVALUE = "#NULL#";
- private static final String EMPTYCOLLEC = "#EMTPY#";
-
public StringList2VarcharFieldConversion()
{
- }
-
- /* (non-Javadoc)
- * @see org.apache.ojb.broker.accesslayer.conversions.FieldConversion#javaToSql(java.lang.Object)
- */
- public Object javaToSql(Object source) throws ConversionException
- {
-
- if (source == null)
- {
- return NULLVALUE;
- }
-
- try
- {
- List stringList = (List) source;
- if (stringList.isEmpty())
- {
- return NULLVALUE;
- }
-
- StringBuffer result = new StringBuffer();
- for (int i = 0; i < stringList.size(); i++)
- {
- String newSt = (String) stringList.get(i);
- // introduced in JDK 1.4, replace with commons-lang
- // newSt = newSt.replaceAll("#", "##");
- newSt = StringUtils.replace(newSt, "#", "##");
- if (i > 0)
- {
- result.append("#");
- }
- result.append(newSt);
- }
- return result.toString();
- }
- catch (ClassCastException e)
- {
- throw new ConversionException("Object is not a List of String it is a"
- + source.getClass().getName());
- }
- }
-
- /* (non-Javadoc)
- * @see org.apache.ojb.broker.accesslayer.conversions.FieldConversion#sqlToJava(java.lang.Object)
- */
- public Object sqlToJava(Object source) throws ConversionException
- {
- if (source == null)
- {
- return null;
- }
- if (source.toString().equals(NULLVALUE))
- {
- return null;
- }
- if (source.toString().equals(EMPTYCOLLEC))
- {
- return new ArrayList();
- }
- List v = new ArrayList();
- StringBuffer input = new StringBuffer();
- StringBuffer newString = new StringBuffer();
- int pos = 0;
- int length;
-
- input.append(source.toString());
- length = input.length();
- while (pos < length)
- {
- if (input.charAt(pos) != '#')
- {
- newString.append(input.charAt(pos));
- }
- else
- {
- if (input.charAt(pos + 1) != '#')
- {
- v.add(newString.toString());
- newString = new StringBuffer();
- }
- else
- {
- newString.append('#');
- ++pos;
- }
- }
- ++pos;
- }
- v.add(newString.toString());
- return v;
}
}
Modified: db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/accesslayer/conversions/StringVector2VarcharFieldConversion.java
URL: http://svn.apache.org/viewcvs/db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/accesslayer/conversions/StringVector2VarcharFieldConversion.java?rev=406149&r1=406148&r2=406149&view=diff
==============================================================================
--- db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/accesslayer/conversions/StringVector2VarcharFieldConversion.java (original)
+++ db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/accesslayer/conversions/StringVector2VarcharFieldConversion.java Sat May 13 13:32:57 2006
@@ -1,6 +1,6 @@
package org.apache.ojb.broker.accesslayer.conversions;
-/* Copyright 2002-2005 The Apache Software Foundation
+/* Copyright 2002-2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,108 +16,33 @@
*/
import java.util.Vector;
+import java.util.List;
/**
* Converts a Vector of string elements back and forth from a database varchar field
* Strings may not contain "#" as this is used as separator.
- * This class maybe useful if it's important to have the string vector stored in a
+ * This class maybe useful if it's important to have the string vector stored in a
* human readable form that allows editing.
+ * <br/>
+ * Strings may not contain <em>"#"</em> (at the beginning and end of a string) as this is
+ * used as separator.
+ * Additionally this implementation use keywords for "blank string" and 'null' list entires, thus Strings may
+ * not contain keywords <em>"+.EmPtY.+"</em> and <em>"+.NuLl.+"</em>.
+ *
* @see Object2ByteArrFieldConversion uses Java serialization and is not suited for
- * this purpose.
- *
- * @author sschloesser mailto: stefan.schl@gmx.de
+ * this purpose.
* @version $Id$
*/
-public class StringVector2VarcharFieldConversion implements FieldConversion
+public class StringVector2VarcharFieldConversion extends AbstractList2VarcharConversion
{
- private static final String NULLVALUE = "#NULL#";
- private static final String EMPTYCOLLEC = "#EMTPY#";
- private static final String SEPARATOR = "#";
-
/** Creates a new instance of StringVector2VarcharFieldConversion */
public StringVector2VarcharFieldConversion()
{
}
- public Object javaToSql(Object obj)
- throws org.apache.ojb.broker.accesslayer.conversions.ConversionException
- {
-
- if (obj == null)
- {
- return NULLVALUE;
- }
-
- if (!(obj instanceof Vector))
- {
- throw new ConversionException(
- "Object is not a vector it is a" + obj.getClass().getName());
- }
-
- Vector v = (Vector) obj;
- if (v.size() == 0)
- {
- return EMPTYCOLLEC;
- }
-
- StringBuffer result = new StringBuffer();
- for (int i = 0; i < v.size(); i++)
- {
- String newSt = v.get(i).toString();
- if (newSt.indexOf(SEPARATOR) >= 0)
- {
- throw new ConversionException(
- "An entry in the Vector contains the forbidden "
- + SEPARATOR
- + " character used to separate the strings on the DB");
- }
- result.append(newSt);
- result.append(SEPARATOR);
- }
- return result.toString();
- }
-
- public Object sqlToJava(Object obj)
- throws org.apache.ojb.broker.accesslayer.conversions.ConversionException
+ protected List newList()
{
-
- if (obj == null)
- {
- return null;
- }
- if (obj.toString().equals(NULLVALUE))
- {
- return null;
- }
- if (obj.toString().equals(EMPTYCOLLEC))
- {
- return new Vector();
- }
-
- Vector v = new Vector();
- String input = obj.toString();
- int pos = input.indexOf(SEPARATOR);
-
- while (pos >= 0)
- {
- if (pos == 0)
- {
- v.add("");
- }
- else
- {
- v.add(input.substring(0, pos));
- }
-
- if (pos + 1 > input.length()) //# at end causes outof bounds
- {
- break;
- }
-
- input = input.substring(pos + 1, input.length());
- pos = input.indexOf(SEPARATOR);
- }
- return v;
+ return new Vector();
}
}
Modified: db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/accesslayer/conversions/TimeList2VarcharFieldConversion.java
URL: http://svn.apache.org/viewcvs/db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/accesslayer/conversions/TimeList2VarcharFieldConversion.java?rev=406149&r1=406148&r2=406149&view=diff
==============================================================================
--- db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/accesslayer/conversions/TimeList2VarcharFieldConversion.java (original)
+++ db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/accesslayer/conversions/TimeList2VarcharFieldConversion.java Sat May 13 13:32:57 2006
@@ -1,6 +1,6 @@
package org.apache.ojb.broker.accesslayer.conversions;
-/* Copyright 2004-2005 The Apache Software Foundation
+/* Copyright 2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,108 +16,47 @@
*/
import java.sql.Time;
-import java.util.ArrayList;
import java.util.List;
-import org.apache.commons.lang.StringUtils;
-
/**
* This implementation of the {@link FieldConversion} interface converts
* between a {@link java.util.List} of {@link java.sql.Time} objects and a database
* <em>varchar</em> field.
*
- * @author Guillaume Nodet
* @version $Id$
*/
-public class TimeList2VarcharFieldConversion implements FieldConversion
+public class TimeList2VarcharFieldConversion extends AbstractList2VarcharConversion
{
-
- private static final String NULLVALUE = "#NULL#";
- private static final String EMPTYCOLLEC = "#EMTPY#";
-
public TimeList2VarcharFieldConversion()
{
}
- /* (non-Javadoc)
- * @see org.apache.ojb.broker.accesslayer.conversions.FieldConversion#javaToSql(java.lang.Object)
- */
- public Object javaToSql(Object source) throws ConversionException
+ protected void addEntryToList(List resultList, String strEntry)
{
- if (source == null)
- {
- return NULLVALUE;
- }
-
- try
+ boolean old = strEntry.indexOf(":") > 0;
+ String value = checkResult(strEntry);
+ // backward compatible for string of form 01:00:00
+ if(old)
{
- List timeList = (List) source;
- if (timeList.isEmpty())
- {
- return NULLVALUE;
- }
-
- StringBuffer result = new StringBuffer();
- for (int i = 0; i < timeList.size(); i++)
- {
- Time obj = (Time) timeList.get(i);
- String newSt = obj.toString();
- // introduced in JDK 1.4, replace with commons-lang
- // newSt = newSt.replaceAll("#", "##");
- newSt = StringUtils.replace(newSt, "#", "##");
- result.append(newSt);
- result.append("#");
- }
- return result.toString();
+ resultList.add(value != null ? Time.valueOf(value) : null);
}
- catch (ClassCastException e)
+ // the new version with millisecond precision
+ else
{
- throw new ConversionException("Object is not a List of Time it is a" + source.getClass().getName());
+ resultList.add(value != null ? new Time(Long.parseLong(value)) : null);
}
}
- /* (non-Javadoc)
- * @see org.apache.ojb.broker.accesslayer.conversions.FieldConversion#sqlToJava(java.lang.Object)
- */
- public Object sqlToJava(Object source) throws ConversionException
+ protected String convertListEntryToString(Object entry)
{
- if (source == null)
+ if(entry != null)
{
- return null;
+ // use #getTime instead of #toString to avoid data corruption
+ return Long.toString(((Time) entry).getTime());
}
- if (source.toString().equals(NULLVALUE))
+ else
{
return null;
}
- if (source.toString().equals(EMPTYCOLLEC))
- {
- return new ArrayList();
- }
-
- List v = new ArrayList();
- String input = source.toString();
- int pos = input.indexOf("#");
-
- while (pos >= 0)
- {
- if (pos == 0)
- {
- v.add("");
- }
- else
- {
- v.add(Time.valueOf(input.substring(0, pos)));
- }
-
- if (pos + 1 > input.length())
- {
- //# at end causes outof bounds
- break;
- }
-
- input = input.substring(pos + 1, input.length());
- pos = input.indexOf("#");
- }
- return v;
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org