All Downloads are FREE. Search and download functionalities are using the official Maven repository.

dk.eobjects.metamodel.DataContextFactory Maven / Gradle / Ivy

Go to download

The full MetaModel package, including all datastore formats (jdbc, csv, excel, openoffice, xml)

There is a newer version: 1.2
Show newest version
/**
 *  This file is part of MetaModel.
 *
 *  MetaModel 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 3 of the License, or
 *  (at your option) any later version.
 *
 *  MetaModel 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 MetaModel.  If not, see .
 */
package dk.eobjects.metamodel;

import java.io.File;
import java.io.InputStream;
import java.net.URL;
import java.sql.Connection;

import javax.sql.DataSource;

import org.xml.sax.InputSource;

import dk.eobjects.metamodel.schema.TableType;

/**
 * A factory for DataContext objects. This class substantially easens the task
 * of creating and initializing DataContext objects and their strategies for
 * reading datastores.
 * 
 * A lot of the factory methods support column type detection, narrowing and
 * transformation. For details on this approach to datastores.
 * 
 * @see ColumnTypeTransformer
 * @see DataContext
 * @see IDataContextStrategy
 */
public class DataContextFactory {

	public static final TableType[] DEFAULT_JDBC_TABLE_TYPES = new TableType[] {
			TableType.TABLE, TableType.VIEW };
	public static final char DEFAULT_CSV_SEPARATOR_CHAR = ',';
	public static final char DEFAULT_CSV_QUOTE_CHAR = '\"';

	private DataContextFactory() {
		// Prevent instantiation
	}

	/**
	 * Creates a DataContext based on a CSV file
	 * 
	 * @param file
	 *            a CSV file
	 * @return a DataContext object that matches the request
	 */
	public static DataContext createCsvDataContext(File file) {
		return createCsvDataContext(file, DEFAULT_CSV_SEPARATOR_CHAR,
				DEFAULT_CSV_QUOTE_CHAR, false);
	}

	/**
	 * Creates a DataContext based on a CSV file
	 * 
	 * @param file
	 *            a CSV file
	 * @param transformColumnTypes
	 *            a boolean indicating if MetaModel should detect, narrow and
	 *            transform column types
	 * @return a DataContext object that matches the request
	 */
	public static DataContext createCsvDataContext(File file,
			boolean transformColumnTypes) {
		return createCsvDataContext(file, DEFAULT_CSV_SEPARATOR_CHAR,
				DEFAULT_CSV_QUOTE_CHAR, transformColumnTypes);
	}

	/**
	 * Creates a DataContext based on a CSV file
	 * 
	 * @param file
	 *            a CSV file
	 * @param separatorChar
	 *            the char to use for separating values
	 * @param quoteChar
	 *            the char used for quoting values (typically if they include
	 *            the separator char)
	 * @param transformColumnTypes
	 *            a boolean indicating if MetaModel should detect, narrow and
	 *            transform column types
	 * @return a DataContext object that matches the request
	 */
	public static DataContext createCsvDataContext(File file,
			char separatorChar, char quoteChar, boolean transformColumnTypes) {
		CsvDataContextStrategy strategy = new CsvDataContextStrategy(file,
				separatorChar, quoteChar);
		if (transformColumnTypes) {
			strategy.autoTransformColumnTypes();
		}
		return new DataContext(strategy);
	}

	/**
	 * Creates a DataContext based on a CSV file
	 * 
	 * @param file
	 *            a CSV file
	 * @param separatorChar
	 *            the char to use for separating values
	 * @param quoteChar
	 *            the char used for quoting values (typically if they include
	 *            the separator char)
	 * @param transformColumnTypes
	 *            a boolean indicating if MetaModel should detect, narrow and
	 *            transform column types
	 * @param encoding
	 *            the character encoding of the file
	 * @return a DataContext object that matches the request
	 */
	public static DataContext createCsvDataContext(File file,
			char separatorChar, char quoteChar, boolean transformColumnTypes,
			String encoding) {
		CsvDataContextStrategy strategy = new CsvDataContextStrategy(file,
				separatorChar, quoteChar, encoding);
		if (transformColumnTypes) {
			strategy.autoTransformColumnTypes();
		}
		return new DataContext(strategy);
	}

	/**
	 * Creates a DataContext based on CSV-content through an input stream
	 * 
	 * @param inputStream
	 *            the input stream to read from
	 * @param separatorChar
	 *            the char to use for separating values
	 * @param quoteChar
	 *            the char used for quoting values (typically if they include
	 *            the separator char)
	 * @param transformColumnTypes
	 *            a boolean indicating if MetaModel should detect, narrow and
	 *            transform column types
	 * @return a DataContext object that matches the request
	 */
	public static DataContext createCsvDataContext(InputStream inputStream,
			char separatorChar, char quoteChar, boolean transformColumnTypes) {
		CsvDataContextStrategy strategy = new CsvDataContextStrategy(
				inputStream, separatorChar, quoteChar);
		if (transformColumnTypes) {
			strategy.autoTransformColumnTypes();
		}
		return new DataContext(strategy);
	}

	/**
	 * Creates a DataContet based on an Excel spreadsheet file
	 * 
	 * @param file
	 *            an excel spreadsheet file
	 * @return a DataContext object that matches the request
	 */
	public static DataContext createExcelDataContext(File file) {
		return createExcelDataContext(file, false);
	}

	/**
	 * Creates a DataContext based on an Excel spreadsheet file
	 * 
	 * @param file
	 *            an Excel spreadsheet file
	 * @param transformColumnTypes
	 *            a boolean indicating if MetaModel should detect, narrow and
	 *            transform column types
	 * @return a DataContext object that matches the request
	 */
	public static DataContext createExcelDataContext(File file,
			boolean transformColumnTypes) {
		ExcelDataContextStrategy strategy = new ExcelDataContextStrategy(file);
		if (transformColumnTypes) {
			strategy.autoTransformColumnTypes();
		}
		return new DataContext(strategy);
	}

	/**
	 * Creates a DataContext based on a XML file.
	 * 
	 * Tables are created by examining the data in the XML file, NOT by reading
	 * XML Schemas (xsd/dtd's). This enables compliancy with ALL xml formats but
	 * also raises a risk that two XML files with the same format wont
	 * nescesarily yield the same table model if some optional attributes or
	 * tags are omitted in one of the files.
	 * 
	 * @param file
	 *            a XML file
	 * @param autoFlattenTables
	 *            a boolean indicating if MetaModel should flatten very simple
	 *            table structures (where tables only contain a single
	 *            data-carrying column) for greater usability of the generated
	 *            table-based model
	 * @param transformColumnTypes
	 *            a boolean indicating if MetaModel should detect, narrow and
	 *            transform column types
	 * @return a DataContext object that matches the request
	 */
	public static DataContext createXmlDataContext(File file,
			boolean autoFlattenTables, boolean transformColumnTypes) {
		XmlDataContextStrategy strategy = new XmlDataContextStrategy(file,
				autoFlattenTables);
		if (transformColumnTypes) {
			strategy.autoTransformColumnTypes();
		}
		return new DataContext(strategy);
	}

	/**
	 * Creates a DataContext based on XML-content from an input source.
	 * 
	 * Tables are created by examining the data in the XML file, NOT by reading
	 * XML Schemas (xsd/dtd's). This enables compliancy with ALL xml formats but
	 * also raises a risk that two XML files with the same format wont
	 * nescesarily yield the same table model if some optional attributes or
	 * tags are omitted in one of the files.
	 * 
	 * @param inputSource
	 *            an input source feeding XML content
	 * @param schemaName
	 *            the name to be used for the main schema
	 * @param autoFlattenTables
	 *            a boolean indicating if MetaModel should flatten very simple
	 *            table structures (where tables only contain a single
	 *            data-carrying column) for greater usability of the generated
	 *            table-based model
	 * @param transformColumnTypes
	 *            a boolean indicating if MetaModel should detect, narrow and
	 *            transform column types
	 * @return a DataContext object that matches the request
	 */
	public static DataContext createXmlDataContext(InputSource inputSource,
			String schemaName, boolean autoFlattenTables,
			boolean transformColumnTypes) {
		XmlDataContextStrategy strategy = new XmlDataContextStrategy(
				inputSource, schemaName, autoFlattenTables);
		if (transformColumnTypes) {
			strategy.autoTransformColumnTypes();
		}
		return new DataContext(strategy);
	}

	/**
	 * Creates a DataContext based on XML-content from a URL.
	 * 
	 * Tables are created by examining the data in the XML file, NOT by reading
	 * XML Schemas (xsd/dtd's). This enables compliancy with ALL xml formats but
	 * also raises a risk that two XML files with the same format wont
	 * nescesarily yield the same table model if some optional attributes or
	 * tags are omitted in one of the files.
	 * 
	 * @param url
	 *            the URL to use for feeding XML content
	 * @param autoFlattenTables
	 *            a boolean indicating if MetaModel should flatten very simple
	 *            table structures (where tables only contain a single
	 *            data-carrying column) for greater usability of the generated
	 *            table-based model
	 * @param transformColumnTypes
	 *            a boolean indicating if MetaModel should detect, narrow and
	 *            transform column types
	 * @return a DataContext object that matches the request
	 */
	public static DataContext createXmlDataContext(URL url,
			boolean autoFlattenTables, boolean transformColumnTypes) {
		XmlDataContextStrategy strategy = new XmlDataContextStrategy(url,
				autoFlattenTables);
		if (transformColumnTypes) {
			strategy.autoTransformColumnTypes();
		}
		return new DataContext(strategy);
	}

	/**
	 * Creates a DataContext based on an OpenOffice.org database file.
	 * 
	 * @param file
	 *            an OpenOffice.org database file
	 * @return a DataContext object that matches the request
	 */
	public static DataContext createOpenOfficeDataContext(File file) {
		return new DataContext(new OpenOfficeDataContextStrategy(file));
	}

	/**
	 * Creates a DataContext based on a JDBC connection
	 * 
	 * @param connection
	 *            a JDBC connection
	 * @return a DataContext object that matches the request
	 */
	public static DataContext createJdbcDataContext(Connection connection) {
		return new DataContext(new JdbcDataContextStrategy(connection,
				DEFAULT_JDBC_TABLE_TYPES, null));
	}

	/**
	 * Creates a DataContext based on a JDBC datasource
	 * 
	 * @param ds
	 *            a JDBC datasource
	 * @return a DataContext object that matches the request
	 */
	public static DataContext createJdbcDataContext(DataSource ds) {
		return new DataContext(new JdbcDataContextStrategy(ds,
				DEFAULT_JDBC_TABLE_TYPES, null));
	}

	/**
	 * Creates a DataContext based on a JDBC connection
	 * 
	 * @param connection
	 *            a JDBC connection
	 * @param catalogName
	 *            a catalog name to use
	 * @return a DataContext object that matches the request
	 */
	public static DataContext createJdbcDataContext(Connection connection,
			String catalogName) {
		return new DataContext(new JdbcDataContextStrategy(connection,
				DEFAULT_JDBC_TABLE_TYPES, catalogName));
	}

	/**
	 * Creates a DataContext based on a JDBC connection
	 * 
	 * @param connection
	 *            a JDBC connection
	 * @param tableTypes
	 *            the types of tables to include in the generated schemas
	 * @return a DataContext object that matches the request
	 */
	public static DataContext createJdbcDataContext(Connection connection,
			TableType... tableTypes) {
		return new DataContext(new JdbcDataContextStrategy(connection,
				tableTypes, null));
	}

	/**
	 * Creates a DataContext based on a JDBC connection
	 * 
	 * @param connection
	 *            a JDBC connection
	 * @param catalogName
	 *            a catalog name to use
	 * @param tableTypes
	 *            the types of tables to include in the generated schemas
	 * @return a DataContext object that matches the request
	 */
	public static DataContext createJdbcDataContext(Connection connection,
			String catalogName, TableType[] tableTypes) {
		return new DataContext(new JdbcDataContextStrategy(connection,
				tableTypes, catalogName));
	}

	/**
	 * Creates a DataContext based on a JDBC datasource
	 * 
	 * @param ds
	 *            a JDBC datasource
	 * @param tableTypes
	 *            the types of tables to include in the generated schemas
	 * @return a DataContext object that matches the request
	 */
	public static DataContext createJdbcDataContext(DataSource ds,
			TableType... tableTypes) {
		return new DataContext(
				new JdbcDataContextStrategy(ds, tableTypes, null));
	}

	/**
	 * Creates a DataContext based on a JDBC datasource
	 * 
	 * @param ds
	 *            a JDBC datasource
	 * @param catalogName
	 *            a catalog name to use
	 * @param tableTypes
	 *            the types of tables to include in the generated schemas
	 * @return a DataContext object that matches the request
	 */
	public static DataContext createJdbcDataContext(DataSource ds,
			String catalogName, TableType[] tableTypes) {
		return new DataContext(new JdbcDataContextStrategy(ds, tableTypes,
				catalogName));
	}

	/**
	 * Creates a DataContext based on a JDBC datasource
	 * 
	 * @param ds
	 *            a JDBC datasource
	 * @param catalogName
	 *            a catalog name to use
	 * @return a DataContext object that matches the request
	 */
	public static DataContext createJdbcDataContext(DataSource ds,
			String catalogName) {
		return new DataContext(new JdbcDataContextStrategy(ds,
				DEFAULT_JDBC_TABLE_TYPES, catalogName));
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy