[Csvtosql-cvs] csvtosql_jdk50/src/net/sf/csv2sql/configuration Configurator.java,NONE,1.1 Configurat
Brought to you by:
davideconsonni
|
From: consonni d. <dav...@us...> - 2005-04-25 10:53:06
|
Update of /cvsroot/csvtosql/csvtosql_jdk50/src/net/sf/csv2sql/configuration In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv15138/net/sf/csv2sql/configuration Added Files: Configurator.java ConfiguratorException.java ManualConfigurator.java XmlConfigurator.java Log Message: no message --- NEW FILE: XmlConfigurator.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.configuration; import java.io.File; import java.util.Properties; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import net.sf.csv2sql.configuration.sections.TableStructure; import net.sf.csv2sql.configuration.sections.WriterList; import net.sf.csv2sql.grammars.AbstractGrammarFactory; import net.sf.csv2sql.renders.AbstractRender; import net.sf.csv2sql.writers.AbstractWriter; import net.sf.csv2sql.storage.Storage; /** * configure csvtosql from xmlfile * @author <a href="mailto:dco...@en...">Davide Consonni</a> */ public class XmlConfigurator implements Configurator { private Document document; private WriterList writers; private AbstractRender renderer; private AbstractGrammarFactory abstractGrammarFactory; private TableStructure structure; private Storage storage; private File configurationFile; public String getConfigurationDirectory() { return configurationFile.getParent(); } public void load(File configurationFile) throws ConfiguratorException { this.configurationFile = configurationFile; try { DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); Document doc = docBuilder.parse(configurationFile); this.document = doc; } catch (Exception e) { throw new ConfiguratorException(e.getMessage()); } } public TableStructure getTableStructure() throws ConfiguratorException { try { if (structure!=null) {return structure;} Node structureNode = document.getElementsByTagName("structure").item(0); structure = new TableStructure(structureNode.getAttributes().getNamedItem("tablename").getNodeValue()); AbstractGrammarFactory grammarFactory = this.getGrammar(); //read the fields NodeList list = structureNode.getChildNodes(); for (int i = 0; i<list.getLength(); i++) { if (list.item(i).getNodeType() == Node.ELEMENT_NODE) { //read parameter of childs Properties p = new Properties(); NodeList parameters = list.item(i).getChildNodes(); for (int j = 0; j<parameters.getLength(); j++) { if (parameters.item(j).getNodeType() == Node.ELEMENT_NODE) { p.setProperty( parameters.item(j).getAttributes().getNamedItem("name").getNodeValue(), parameters.item(j).getAttributes().getNamedItem("value").getNodeValue() ); } } //add field structure.addField(grammarFactory.createField( list.item(i).getAttributes().getNamedItem("type").getNodeValue(), list.item(i).getAttributes().getNamedItem("name").getNodeValue(), p )); } } return structure; } catch (Exception e) { e.printStackTrace(); throw new ConfiguratorException(e.getMessage()); } } public int getDescriptorVersion() throws ConfiguratorException { try { Node descriptorNode = document.getElementsByTagName("descriptor").item(0); return new Integer(descriptorNode.getAttributes().getNamedItem("version").getNodeValue()).intValue(); } catch (Exception e) { throw new ConfiguratorException("descriptor version can't be found"); } } /** grammars */ private String getGrammarClassName() throws ConfiguratorException { try { return document.getElementsByTagName("grammar").item(0). getAttributes().getNamedItem("class").getNodeValue(); } catch (Exception e) { throw new ConfiguratorException(e.getMessage()); } } public AbstractGrammarFactory getGrammar() throws ConfiguratorException { try { if (abstractGrammarFactory!=null) {return abstractGrammarFactory;} //instantiating grammar abstractGrammarFactory = (AbstractGrammarFactory)Class.forName(getGrammarClassName()).newInstance(); return abstractGrammarFactory; } catch (Exception e) { throw new ConfiguratorException(e.getMessage()); } } /** renderers */ private String getRenderClassName() throws ConfiguratorException { try { return document.getElementsByTagName("render").item(0). getAttributes().getNamedItem("class").getNodeValue(); } catch (Exception e) { throw new ConfiguratorException(e.getMessage()); } } public AbstractRender getRender() throws ConfiguratorException { try { if (renderer!=null) {return renderer;} //instantiating the renderer renderer = (AbstractRender)Class.forName(getRenderClassName()).newInstance(); //setting render properties Properties renderProps = new Properties(); Node renderNode = document.getElementsByTagName("render").item(0); NodeList list = renderNode.getChildNodes(); for (int i = 0; i<list.getLength(); i++) { if (list.item(i).getNodeType() == Node.ELEMENT_NODE) { renderProps.setProperty( list.item(i).getAttributes().getNamedItem("name").getNodeValue(), list.item(i).getAttributes().getNamedItem("value").getNodeValue() ); } } renderProps.setProperty("configdirectory", getConfigurationDirectory()+System.getProperty("file.separator")); renderer.configure(renderProps, this.getTableStructure(), getStorage()); return renderer; } catch (Exception e) { e.printStackTrace(); throw new ConfiguratorException(e.getMessage()); } } public WriterList getWriters() throws ConfiguratorException { try { if (writers!=null) {return writers;} writers = new WriterList(); NodeList writerAppenders = document.getElementsByTagName("writerAppender"); //all appenders for(int i=0; i<writerAppenders.getLength() ; i++){ Node writerAppenderNode = writerAppenders.item(i); if(writerAppenderNode.getNodeType() == Node.ELEMENT_NODE){ //the writer must be activated if ("true".equals(writerAppenderNode.getAttributes().getNamedItem("active").getNodeValue())) { String writerAppenderClassName = writerAppenderNode.getAttributes().getNamedItem("class").getNodeValue(); AbstractWriter writer = (AbstractWriter)Class.forName(writerAppenderClassName).newInstance(); //setting render properties Properties writerProps = new Properties(); NodeList list = writerAppenderNode.getChildNodes(); for (int j = 0; j<list.getLength(); j++) { if (list.item(j).getNodeType() == Node.ELEMENT_NODE) { writerProps.setProperty( list.item(j).getAttributes().getNamedItem("name").getNodeValue(), list.item(j).getAttributes().getNamedItem("value").getNodeValue() ); } } writer.configure(writerProps, getStorage()); //finally add it writers.addWriter(writer); } } } return writers; } catch (Exception e) {e.printStackTrace(); throw new ConfiguratorException(e.getMessage()); } } /** storage */ private String getStorageClassName() throws ConfiguratorException { try { return document.getElementsByTagName("storage").item(0). getAttributes().getNamedItem("class").getNodeValue(); } catch (Exception e) { throw new ConfiguratorException(e.getMessage()); } } public Storage getStorage() throws ConfiguratorException { try { if (storage!=null) {return storage;} //instantiating the renderer storage = (Storage)Class.forName(getStorageClassName()).newInstance(); //setting render properties Properties props = new Properties(); Node node = document.getElementsByTagName("storage").item(0); NodeList list = node.getChildNodes(); for (int i = 0; i<list.getLength(); i++) { if (list.item(i).getNodeType() == Node.ELEMENT_NODE) { props.setProperty( list.item(i).getAttributes().getNamedItem("name").getNodeValue(), list.item(i).getAttributes().getNamedItem("value").getNodeValue() ); } } props.setProperty("configdirectory", getConfigurationDirectory()+System.getProperty("file.separator")); storage.configure(props); return storage; } catch (Exception e) { e.printStackTrace(); throw new ConfiguratorException(e.getMessage()); } } } --- NEW FILE: ConfiguratorException.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.configuration; /** * @author <a href="mailto:dco...@en...">Davide Consonni</a> */ public class ConfiguratorException extends Exception { public ConfiguratorException(String message) { super(message); } } --- NEW FILE: ManualConfigurator.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.configuration; import net.sf.csv2sql.configuration.sections.TableStructure; import net.sf.csv2sql.configuration.sections.WriterList; import net.sf.csv2sql.grammars.AbstractGrammarFactory; import net.sf.csv2sql.renders.AbstractRender; import net.sf.csv2sql.storage.Storage; /** * configure csvtosql from code (very useful when you use it as library) * @author <a href="mailto:dco...@en...">Davide Consonni</a> */ public class ManualConfigurator implements Configurator { int descriptorVersion = -1 ; AbstractGrammarFactory grammarFactory; AbstractRender render; TableStructure tableStructure; WriterList writerList; Storage storage; public void setDescriptorVersion(int descriptorVersion) throws IllegalArgumentException { if (descriptorVersion != CURRENT_DESCRIPTOR_VERSION) { throw new IllegalArgumentException("invalid descriptor version. current version is "+CURRENT_DESCRIPTOR_VERSION); } this.descriptorVersion = descriptorVersion; } public int getDescriptorVersion() throws ConfiguratorException { if (this.descriptorVersion == -1) { throw new ConfiguratorException("descriptor version not set"); } return this.descriptorVersion; } public void setAbstractGrammarFactory(AbstractGrammarFactory grammarFactory) throws IllegalArgumentException { if (grammarFactory==null) { throw new IllegalArgumentException("null grammar factory"); } this.grammarFactory = grammarFactory; } public AbstractGrammarFactory getGrammar() throws ConfiguratorException { if (grammarFactory==null) { throw new ConfiguratorException("grammar factory not set"); } return this.grammarFactory; } public void setRender(AbstractRender render) throws IllegalArgumentException { if (render==null) { throw new IllegalArgumentException("null render"); } this.render = render; } public AbstractRender getRender() throws ConfiguratorException { if (render==null) { throw new ConfiguratorException("render not set"); } return this.render; } public void setTableStructure(TableStructure tableStructure) throws IllegalArgumentException { if (tableStructure==null) { throw new IllegalArgumentException("null tableStructure"); } this.tableStructure = tableStructure; } public TableStructure getTableStructure() throws ConfiguratorException { if (tableStructure==null) { throw new ConfiguratorException("table structure not set"); } return this.tableStructure; } public void setWriters(WriterList writerList) throws IllegalArgumentException { if (writerList==null) { throw new IllegalArgumentException("null writerList"); } this.writerList = writerList; } public WriterList getWriters() throws ConfiguratorException { if (writerList==null) { throw new ConfiguratorException("writerList not set"); } return this.writerList; } public void setStorage(Storage storage) throws IllegalArgumentException { if (storage==null) { throw new IllegalArgumentException("null storage"); } this.storage = storage; } public Storage getStorage() throws ConfiguratorException { if (storage==null) { throw new IllegalArgumentException("null storage"); } return this.storage = storage; } } --- NEW FILE: Configurator.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.configuration; import net.sf.csv2sql.configuration.sections.TableStructure; import net.sf.csv2sql.configuration.sections.WriterList; import net.sf.csv2sql.grammars.AbstractGrammarFactory; import net.sf.csv2sql.renders.AbstractRender; import net.sf.csv2sql.storage.Storage; /** * define the standard for all configurators. * @author <a href="mailto:dco...@en...">Davide Consonni</a> */ public interface Configurator { public TableStructure getTableStructure() throws ConfiguratorException; public AbstractGrammarFactory getGrammar() throws ConfiguratorException; public AbstractRender getRender() throws ConfiguratorException; public WriterList getWriters() throws ConfiguratorException; public int getDescriptorVersion() throws ConfiguratorException; public Storage getStorage() throws ConfiguratorException; /* * v1 - startup * v2 - storage support * v3 - port to jdk5.0 */ public static final int CURRENT_DESCRIPTOR_VERSION = 3; } |