[Csvtosql-cvs] csvtosql_jdk50/src/net/sf/csv2sql/grammars/db2 DB2DateField.java,NONE,1.1 DB2FloatFie
Brought to you by:
davideconsonni
|
From: consonni d. <dav...@us...> - 2005-04-25 10:53:27
|
Update of /cvsroot/csvtosql/csvtosql_jdk50/src/net/sf/csv2sql/grammars/db2 In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv15138/net/sf/csv2sql/grammars/db2 Added Files: DB2DateField.java DB2FloatField.java DB2IntegerField.java DB2StringField.java GrammarFactory.java Log Message: no message --- NEW FILE: GrammarFactory.java --- /* Copyright (C) 2004 Dana Cordes <blo...@us...> This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ package net.sf.csv2sql.grammars.db2; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.util.Hashtable; import java.util.Properties; import net.sf.csv2sql.grammars.AbstractField; import net.sf.csv2sql.grammars.AbstractGrammarFactory; import net.sf.csv2sql.grammars.exceptions.GrammarFactoryException; /** * @see net.sf.csv2sql.grammars.AbstractGrammarFactory AbstractGrammarFactory * @author <a href="mailto:blo...@us...">Dana Cordes</a> */ public final class GrammarFactory extends AbstractGrammarFactory { public static final Hashtable<String, String> mapping = new Hashtable<String, String>(); static { //strings mapping.put("VARCHAR", "net.sf.csv2sql.grammars.db2.DB2StringField"); mapping.put("STRING", "net.sf.csv2sql.grammars.db2.DB2StringField"); //numbers mapping.put("FLOAT", "net.sf.csv2sql.grammars.db2.DB2FloatField"); mapping.put("INTEGER", "net.sf.csv2sql.grammars.db2.DB2IntegerField"); //datetime mapping.put("DATE", "net.sf.csv2sql.grammars.db2.DB2DateField"); } public AbstractField createField(String type, String name, Properties prop) throws GrammarFactoryException { AbstractField field = null; try { String className = (String)mapping.get(type.toUpperCase()); if (className==null || "".equals(className)) { throw new ClassNotFoundException("datatype not found in mapping"); } Constructor c = Class.forName(className). getConstructor(new Class[] { String.class, Properties.class }); Object o = c.newInstance(new Object[] {name, prop }); field = (AbstractField)o; } catch (ClassNotFoundException e) { throw new GrammarFactoryException("cannot create field. type: "+type+", name: "+name+")", e.getException()); } catch (NoSuchMethodException e) { throw new GrammarFactoryException("cannot create field. type: "+type+", name: "+name+")", e.getCause()); } catch (InstantiationException e) { throw new GrammarFactoryException("cannot create field. type: "+type+", name: "+name+")", e.getCause()); } catch (IllegalAccessException e) { throw new GrammarFactoryException("cannot create field. type: "+type+", name: "+name+")", e.getCause()); } catch (InvocationTargetException e) { throw new GrammarFactoryException("cannot create field. type: "+type+", name: "+name+")", e.getCause()); } if (field==null) { throw new GrammarFactoryException("field type ["+type+"] doesn't exist. (field name: "+name+")", null); } else { return field; } } } --- NEW FILE: DB2FloatField.java --- /* Copyright (C) 2004 Davide Consonni <dco...@en...> This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ package net.sf.csv2sql.grammars.db2; import java.util.HashMap; import java.util.Properties; import net.sf.csv2sql.grammars.AbstractField; import net.sf.csv2sql.grammars.exceptions.FieldRenderException; import net.sf.csv2sql.grammars.exceptions.MissingRequiredParameterException; /** * @see AbstractField * @author <a href="mailto:dco...@en...">Davide Consonni</a> */ public final class DB2FloatField extends AbstractField { public DB2FloatField(String fieldName, Properties properties) throws MissingRequiredParameterException { super(fieldName, properties); } /** * @see AbstractField#render */ protected String doRender(Object value) throws FieldRenderException { String result = null; try { if ( (value == null) || ("".equals((String)value)) ) { result = getDefaulNullValue(); } else { String strFloat = ((String)value).replaceAll(",", "."); result = String.valueOf(new Float(Float.parseFloat(strFloat))); } } catch (NumberFormatException e) { throw new FieldRenderException(this, value, "not an number", e); } catch (Exception e) { throw new FieldRenderException(this, value, "generic error", e); } return result; } protected HashMap<String, String> requiredParameterList() { return null; } protected HashMap<String, String> optionalParameterList() { return null; } } --- NEW FILE: DB2IntegerField.java --- /* Copyright (C) 2004 Davide Consonni <dco...@en...> This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ package net.sf.csv2sql.grammars.db2; import java.util.HashMap; import java.util.Properties; import net.sf.csv2sql.grammars.AbstractField; import net.sf.csv2sql.grammars.exceptions.FieldRenderException; import net.sf.csv2sql.grammars.exceptions.MissingRequiredParameterException; /** * @see AbstractField * @author <a href="mailto:dco...@en...">Davide Consonni</a> */ public final class DB2IntegerField extends AbstractField { public DB2IntegerField(String fieldName, Properties properties) throws MissingRequiredParameterException { super(fieldName, properties); } /** * @see AbstractField#render */ protected String doRender(Object value) throws FieldRenderException { String result = null; try { if ( (value == null) || ("".equals((String)value))) { result = getDefaulNullValue(); } else { result = String.valueOf(new Integer(Integer.parseInt((String)value)).intValue()); } } catch (NumberFormatException e) { throw new FieldRenderException(this, value, "not an number", e); } catch (Exception e) { throw new FieldRenderException(this, value, "generic error", e); } return result; } protected HashMap<String, String> requiredParameterList() { return null; } protected HashMap<String, String> optionalParameterList() { return null; } } --- NEW FILE: DB2StringField.java --- /* Copyright (C) 2004 Dana Cordes <blo...@us...> This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ package net.sf.csv2sql.grammars.db2; import java.util.HashMap; import java.util.Properties; import net.sf.csv2sql.grammars.AbstractField; import net.sf.csv2sql.grammars.exceptions.FieldRenderException; import net.sf.csv2sql.grammars.exceptions.MissingRequiredParameterException; /** * @see AbstractField * @author <a href="mailto:blo...@us...">Dana Cordes</a> */ public final class DB2StringField extends AbstractField { public static final String PRE = "\'"; public static final String POST = "\'"; public DB2StringField(String fieldName, Properties properties) throws MissingRequiredParameterException { super(fieldName, properties); } /** * @see AbstractField#render */ protected String doRender(Object value) throws FieldRenderException { String result = null; try { if ((value == null) || ("".equals(value))) { result = getDefaulNullValue(); } else { value = this.truncateValue(value); value = this.escapeValue(value); result = PRE + value + POST; } } catch (ClassCastException e) { throw new FieldRenderException(this, value, "not an string", e); } catch (Exception e) { throw new FieldRenderException(this, value, "generic error", e); } return result; } protected HashMap<String, String> requiredParameterList() { return null; } protected HashMap<String, String> optionalParameterList() { HashMap<String, String> hm = new HashMap<String, String>(); hm.put("truncatelength","truncate the string."); return hm; } protected Object truncateValue(Object value){ if(getFieldProperties().getProperty("truncatelength")!=null){ try{ int length = Integer.parseInt(getFieldProperties().getProperty("truncatelength")); if(((String)value).length()>length) value = ((String)value).substring(0,length); } catch (NumberFormatException nfe){ //if it's not a valid number, ignore it. } } return value; } protected Object escapeValue(Object value){ return ((String)value).replaceAll("'", "''"); } } --- NEW FILE: DB2DateField.java --- /* Copyright (C) 2004 Davide Consonni <dco...@en...> This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ package net.sf.csv2sql.grammars.db2; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.Properties; import net.sf.csv2sql.grammars.AbstractField; import net.sf.csv2sql.grammars.exceptions.FieldRenderException; import net.sf.csv2sql.grammars.exceptions.MissingRequiredParameterException; /** * @see AbstractField * @author <a href="mailto:dco...@en...">Davide Consonni</a> */ public final class DB2DateField extends AbstractField { public DB2DateField(String fieldName, Properties properties) throws MissingRequiredParameterException { super(fieldName, properties); } /** * @see AbstractField#render */ protected String doRender(Object value) throws FieldRenderException { String result = null; try { if ( (value == null) || ("".equals((String)value)) ) { result = getDefaulNullValue(); } else { String inputFormat = this.getFieldProperties().getProperty("inputformat"); if (inputFormat == null) { inputFormat = "dd/MM/yy"; } String outputFormat = this.getFieldProperties().getProperty("outputformat"); if (outputFormat == null) { outputFormat = "dd/MM/yy"; } //check if Date is valid SimpleDateFormat df = new SimpleDateFormat(inputFormat); Date date = df.parse( (String)value ); //custom format df = new SimpleDateFormat(outputFormat); //format Date result = "\""+df.format(date)+"\""; } } catch (ParseException e) { throw new FieldRenderException(this, value, "not a date", e); } catch (Exception e) { throw new FieldRenderException(this, value, "generic error", e); } return result; } protected HashMap<String, String> requiredParameterList() { return null; } protected HashMap<String, String> optionalParameterList() { HashMap<String, String> hm = new HashMap<String, String>(); hm.put("inputformat","format of date in input."); hm.put("outputformat","format of date in output."); return hm; } } |