[Csvtosql-cvs] csvtosql_jdk50/src/net/sf/csv2sql/grammars AbstractField.java,NONE,1.1 AbstractGramma
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 In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv15138/net/sf/csv2sql/grammars Added Files: AbstractField.java AbstractGrammarFactory.java Log Message: no message --- NEW FILE: AbstractField.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; import java.util.HashMap; import java.util.Properties; import net.sf.csv2sql.grammars.exceptions.FieldRenderException; import net.sf.csv2sql.grammars.exceptions.MissingRequiredParameterException; /** * The abstract field classes describe the type of the field * (like varchar, date, ...) in the table and contain information * like the name of the field and how to render rad data, * but doen't containt data itself. * <p> * Row data can be rendered with <code>render()</code> method. * <p> * <b>Extend this class</b> to define a new field, you must implement * the <code>doRender()</code> method, the argument of this method is raw data, * and the output is formatted data based on field type. * <p> * Implement methods * <code>requiredParameterList</code> and <code>optionalParameterList</code> * to return the list of required and the optional parameters * @author <a href="mailto:dco...@en...">Davide Consonni</a> */ public abstract class AbstractField { private String fieldName; private Properties fieldProperties = new Properties(); /** * render given value for insert. * <pre>example: * 01/10/2004 with oracle data field will be * to_date('01/10/2004', dd/MM/yyyy)<pre> * @throws FieldRenderException if value can be rendered to given datatype. */ public String render(Object value) throws FieldRenderException { String result = preFilter(value); result = doRender(result); result = postFilter(result); return result; } /** * automatically called BEFORE rendering. */ protected final String preFilter(Object value) throws FieldRenderException { String result = (String)value; //OPTION if ("true".equalsIgnoreCase(getFieldProperties().getProperty("tolowercase")) && result!=null) { result = result.toLowerCase(); } //OPTION if ("true".equalsIgnoreCase(getFieldProperties().getProperty("touppercase")) && result!=null) { result = result.toUpperCase(); } return result; } /** * rendering method. */ protected abstract String doRender(Object value) throws FieldRenderException; /** * automatically called AFTER rendering. */ protected final String postFilter(Object value) throws FieldRenderException { return (String)value; } /** * init fields' parameters * @see MissingRequiredParameterException MissingRequiredParameterException * @throws MissingRequiredParameterException if required parameter is missing */ public AbstractField(String fieldName, Properties properties) throws MissingRequiredParameterException { this.fieldName = fieldName; this.fieldProperties = properties; //check for required properties Object[] required = getRequiredParameterList().keySet().toArray(); for (int i=0; i< required.length; i++) { String parameter = (required[i]).toString(); String configParameter = properties.getProperty(parameter); if (configParameter==null) { throw new MissingRequiredParameterException(parameter); } } } public void setFieldName(String fieldName) { this.fieldName = fieldName; } public String getFieldName() { return this.fieldName; } public Properties getFieldProperties() { return this.fieldProperties; } public final String getDefaulNullValue() { //OPTION String notNullValue = getFieldProperties().getProperty("notnullvalue"); if (notNullValue!=null) { return notNullValue; } else { return "NULL"; } } public HashMap<String, String> getRequiredParameterList() { HashMap<String, String> hm = new HashMap<String, String>(); hm.putAll((commonRequiredParameterList()==null ? new HashMap<String, String>() : commonRequiredParameterList())); hm.putAll((requiredParameterList()==null ? new HashMap<String, String>() : requiredParameterList())); return hm; } public HashMap<String, String> getOptionalParameterList() { HashMap<String, String> hm = new HashMap<String, String>(); hm.putAll((commonOptionalParameterList()==null ? new HashMap<String, String>() : commonOptionalParameterList())); hm.putAll((optionalParameterList()==null ? new HashMap<String, String>() : optionalParameterList())); return hm; } private HashMap<String, String> commonRequiredParameterList() { return null; } private HashMap<String, String> commonOptionalParameterList() { HashMap<String, String> hm = new HashMap<String, String>(); hm.put("notnullvalue", "value to put when the culumn is 'not null'."); hm.put("tolowercase", "convert field to lower case."); hm.put("touppercase", "convert field to upper case."); return hm; } protected abstract HashMap<String, String> requiredParameterList(); protected abstract HashMap<String, String> optionalParameterList(); } --- NEW FILE: AbstractGrammarFactory.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; import java.util.Properties; import net.sf.csv2sql.grammars.exceptions.GrammarFactoryException; /** * The grammars factory classes match field type read * from configuration file and return an <code>AbstractField</code> * based on database type. * <p> * <b>Extend this class</b> to define a new grammar factory, * you must implement the <code>createField()</code> * method, the arguments of this method are type * (like varchar, string, integer) and the name of the * field in the table. * @author <a href="mailto:dco...@en...">Davide Consonni</a> */ public abstract class AbstractGrammarFactory { /** * factory method, create fields. */ public abstract AbstractField createField(String type, String name, Properties prop) throws GrammarFactoryException; } |