From: <zep...@us...> - 2011-06-08 20:07:04
|
Revision: 426 http://flatpack.svn.sourceforge.net/flatpack/?rev=426&view=rev Author: zepernick Date: 2011-06-08 20:06:57 +0000 (Wed, 08 Jun 2011) Log Message: ----------- Added mappings from DB using the BufferedReader classes Modified Paths: -------------- trunk/flatpack/src/main/java/net/sf/flatpack/FixedLengthParser.java trunk/flatpack/src/main/java/net/sf/flatpack/brparse/BuffReaderDataSet.java trunk/flatpack/src/main/java/net/sf/flatpack/brparse/BuffReaderDelimParser.java trunk/flatpack/src/main/java/net/sf/flatpack/brparse/BuffReaderFixedParser.java trunk/flatpack/src/main/java/net/sf/flatpack/brparse/BuffReaderParseFactory.java Added Paths: ----------- trunk/flatpack/src/main/java/net/sf/flatpack/brparse/DBBuffReaderDelimParser.java trunk/flatpack/src/main/java/net/sf/flatpack/brparse/DBBuffReaderFixedParser.java trunk/flatpack/src/main/java/net/sf/flatpack/brparse/InterfaceBuffReaderParse.java Modified: trunk/flatpack/src/main/java/net/sf/flatpack/FixedLengthParser.java =================================================================== --- trunk/flatpack/src/main/java/net/sf/flatpack/FixedLengthParser.java 2011-03-05 11:46:45 UTC (rev 425) +++ trunk/flatpack/src/main/java/net/sf/flatpack/FixedLengthParser.java 2011-06-08 20:06:57 UTC (rev 426) @@ -78,6 +78,10 @@ super(dataSourceReader); this.pzmapReader = pzmapReader; } + + protected FixedLengthParser(final Reader dataSourceReader, final String dataDefinition) { + super(dataSourceReader, dataDefinition); + } protected void init() { try { Modified: trunk/flatpack/src/main/java/net/sf/flatpack/brparse/BuffReaderDataSet.java =================================================================== --- trunk/flatpack/src/main/java/net/sf/flatpack/brparse/BuffReaderDataSet.java 2011-03-05 11:46:45 UTC (rev 425) +++ trunk/flatpack/src/main/java/net/sf/flatpack/brparse/BuffReaderDataSet.java 2011-06-08 20:06:57 UTC (rev 426) @@ -32,9 +32,9 @@ */ package net.sf.flatpack.brparse; -import java.io.IOException; - +import net.sf.flatpack.AbstractParser; import net.sf.flatpack.DefaultDataSet; +import net.sf.flatpack.Parser; import net.sf.flatpack.ordering.OrderBy; import net.sf.flatpack.structure.Row; import net.sf.flatpack.xml.MetaData; @@ -42,56 +42,48 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +/** + * + * + * @author Paul Zepernick + */ public class BuffReaderDataSet extends DefaultDataSet { private static final Logger LOGGER = LoggerFactory.getLogger(BuffReaderDataSet.class); - private final BuffReaderDelimParser brDelimPzParser; - private final BuffReaderFixedParser brFixedPzParser; + + private final InterfaceBuffReaderParse brParser; - public BuffReaderDataSet(final MetaData columnMD2, final BuffReaderDelimParser brDelimPzParser) { - super(columnMD2, brDelimPzParser); + /** + * + * @param columnMD2 + * @param brParser + */ + public BuffReaderDataSet(final MetaData columnMD2, final InterfaceBuffReaderParse brParser) { + super(columnMD2, (Parser)brParser); //register the parser with the dataset so we can fetch rows from //the bufferedreader as needed - this.brDelimPzParser = brDelimPzParser; - this.brFixedPzParser = null; + this.brParser = brParser; } - public BuffReaderDataSet(final MetaData columnMD2, final BuffReaderFixedParser brFixedPzParser) { - super(columnMD2, brFixedPzParser); - //register the parser with the dataset so we can fetch rows from - //the bufferedreader as needed - this.brFixedPzParser = brFixedPzParser; - this.brDelimPzParser = null; - } - public boolean next() { - try { + Row r = null; - - if (brDelimPzParser != null) { - r = brDelimPzParser.buildRow(this); - } else if (brFixedPzParser != null) { - r = brFixedPzParser.buildRow(this); + + if (brParser != null) { + r = brParser.buildRow(this); } else { //this should not happen, throw exception throw new RuntimeException("No parser available to fetch row"); } + if (getMetaData() == null) { + setMetaData(((AbstractParser)brParser).getPzMetaData()); + } + if (r == null) { setPointer(-1); return false; } - - //make sure we have some MD - // if (getColumnMD() == null) { - // //create a new map so the user cannot change the internal - // //DataSet representation of the MD through the parser - // setColumnMD(new LinkedHashMap(brDelimPzParser.getColumnMD())); - // } - - if (getMetaData() == null) { - setMetaData(brDelimPzParser.getPzMetaData()); - } - + clearRows(); addRow(r); @@ -99,11 +91,6 @@ return true; - } catch (final IOException ex) { - LOGGER.error("error building Row on next()", ex); - } - - return false; } /** Modified: trunk/flatpack/src/main/java/net/sf/flatpack/brparse/BuffReaderDelimParser.java =================================================================== --- trunk/flatpack/src/main/java/net/sf/flatpack/brparse/BuffReaderDelimParser.java 2011-03-05 11:46:45 UTC (rev 425) +++ trunk/flatpack/src/main/java/net/sf/flatpack/brparse/BuffReaderDelimParser.java 2011-06-08 20:06:57 UTC (rev 426) @@ -49,7 +49,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public class BuffReaderDelimParser extends DelimiterParser { +public class BuffReaderDelimParser extends DelimiterParser implements InterfaceBuffReaderParse{ private BufferedReader br; private boolean processedFirst = false; @@ -108,10 +108,15 @@ * @return Row * @throws IOException */ - public Row buildRow(final DefaultDataSet ds) throws IOException { + public Row buildRow(final DefaultDataSet ds) { /** loop through each line in the file */ while (true) { - final String line = fetchNextRecord(br, getQualifier(), getDelimiter()); + String line; + try { + line = fetchNextRecord(br, getQualifier(), getDelimiter()); + } catch (IOException e) { + throw new RuntimeException("Error Fetching Record From File...", e); + } if (line == null) { return null; Modified: trunk/flatpack/src/main/java/net/sf/flatpack/brparse/BuffReaderFixedParser.java =================================================================== --- trunk/flatpack/src/main/java/net/sf/flatpack/brparse/BuffReaderFixedParser.java 2011-03-05 11:46:45 UTC (rev 425) +++ trunk/flatpack/src/main/java/net/sf/flatpack/brparse/BuffReaderFixedParser.java 2011-06-08 20:06:57 UTC (rev 426) @@ -51,7 +51,13 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public class BuffReaderFixedParser extends FixedLengthParser { + +/** + * + * + * @author Paul Zepernick + */ +public class BuffReaderFixedParser extends FixedLengthParser implements InterfaceBuffReaderParse{ private BufferedReader br = null; private int lineCount = 0; @@ -60,17 +66,39 @@ private static final Logger LOGGER = LoggerFactory.getLogger(BuffReaderFixedParser.class); + + /** + * + * @param pzmapXMLStream + * @param dataSourceStream + */ public BuffReaderFixedParser(final InputStream pzmapXMLStream, final InputStream dataSourceStream) { super(pzmapXMLStream, dataSourceStream); } + + /** + * + * @param pzmapXML + * @param dataSource + */ public BuffReaderFixedParser(final File pzmapXML, final File dataSource) { super(pzmapXML, dataSource); } + /** + * + * + * @param pzmapXML + * @param dataSource + */ public BuffReaderFixedParser(final Reader pzmapXML, final Reader dataSource) { super(pzmapXML, dataSource); } + + protected BuffReaderFixedParser(final Reader dataSourceReader, final String dataDefinition) { + super(dataSourceReader, dataDefinition); + } protected DataSet doParse() { final DataSet ds = new BuffReaderDataSet(getPzMetaData(), this); @@ -98,67 +126,71 @@ * @return Row * @throws IOException */ - public Row buildRow(final DefaultDataSet ds) throws IOException { + public Row buildRow(final DefaultDataSet ds) { String line = null; - - while ((line = br.readLine()) != null) { - lineCount++; - // empty line skip past it - if (line.trim().length() == 0) { - continue; - } - - final String mdkey = FixedWidthParserUtils.getCMDKey(getPzMetaData(), line); - final int recordLength = ((Integer) recordLengths.get(mdkey)).intValue(); - - if (line.length() > recordLength) { - // Incorrect record length on line log the error. Line will not - // be included in the - // dataset - if (isIgnoreExtraColumns()) { - //user has chosen to ignore the fact that we have too many bytes in the fixed - //width file. Truncate the line to the correct length - line = line.substring(0, recordLength); - addError(ds, "TRUNCATED LINE TO CORRECT LENGTH", lineCount, 1); - } else { - addError(ds, "LINE TOO LONG. LINE IS " + line.length() + " LONG. SHOULD BE " + recordLength, lineCount, 2, - isStoreRawDataToDataError() ? line : null); - continue; - } - } else if (line.length() < recordLength) { - if (isHandlingShortLines()) { - // We can pad this line out - line += ParserUtils.padding(recordLength - line.length(), ' '); - - // log a warning - addError(ds, "PADDED LINE TO CORRECT RECORD LENGTH", lineCount, 1); - - } else { - addError(ds, "LINE TOO SHORT. LINE IS " + line.length() + " LONG. SHOULD BE " + recordLength, lineCount, 2, - isStoreRawDataToDataError() ? line : null); - continue; - } - } - - final Row row = new Row(); - row.setMdkey(mdkey.equals(FPConstants.DETAIL_ID) ? null : mdkey); - - final List cmds = ParserUtils.getColumnMetaData(mdkey, getPzMetaData()); - row.addColumn(FixedWidthParserUtils.splitFixedText(cmds, line)); - - row.setRowNumber(lineCount); - - if (isFlagEmptyRows()) { - //user has elected to have the parser flag rows that are empty - row.setEmpty(ParserUtils.isListElementsEmpty(row.getCols())); - } - if (isStoreRawDataToDataSet()) { - //user told the parser to keep a copy of the raw data in the row - //WARNING potential for high memory usage here - row.setRawData(line); - } - - return row; + try { + while ((line = br.readLine()) != null) { + lineCount++; + // empty line skip past it + if (line.trim().length() == 0) { + continue; + } + + final String mdkey = FixedWidthParserUtils.getCMDKey(getPzMetaData(), line); + final int recordLength = ((Integer) recordLengths.get(mdkey)).intValue(); + + if (line.length() > recordLength) { + // Incorrect record length on line log the error. Line will not + // be included in the + // dataset + if (isIgnoreExtraColumns()) { + //user has chosen to ignore the fact that we have too many bytes in the fixed + //width file. Truncate the line to the correct length + line = line.substring(0, recordLength); + addError(ds, "TRUNCATED LINE TO CORRECT LENGTH", lineCount, 1); + } else { + addError(ds, "LINE TOO LONG. LINE IS " + line.length() + " LONG. SHOULD BE " + recordLength, lineCount, 2, + isStoreRawDataToDataError() ? line : null); + continue; + } + } else if (line.length() < recordLength) { + if (isHandlingShortLines()) { + // We can pad this line out + line += ParserUtils.padding(recordLength - line.length(), ' '); + + // log a warning + addError(ds, "PADDED LINE TO CORRECT RECORD LENGTH", lineCount, 1); + + } else { + addError(ds, "LINE TOO SHORT. LINE IS " + line.length() + " LONG. SHOULD BE " + recordLength, lineCount, 2, + isStoreRawDataToDataError() ? line : null); + continue; + } + } + + final Row row = new Row(); + row.setMdkey(mdkey.equals(FPConstants.DETAIL_ID) ? null : mdkey); + + final List cmds = ParserUtils.getColumnMetaData(mdkey, getPzMetaData()); + row.addColumn(FixedWidthParserUtils.splitFixedText(cmds, line)); + + row.setRowNumber(lineCount); + + if (isFlagEmptyRows()) { + //user has elected to have the parser flag rows that are empty + row.setEmpty(ParserUtils.isListElementsEmpty(row.getCols())); + } + if (isStoreRawDataToDataSet()) { + //user told the parser to keep a copy of the raw data in the row + //WARNING potential for high memory usage here + row.setRawData(line); + } + + return row; + } + + } catch(IOException e) { + throw new RuntimeException("Error Fetching Record From File...", e); } return null; Modified: trunk/flatpack/src/main/java/net/sf/flatpack/brparse/BuffReaderParseFactory.java =================================================================== --- trunk/flatpack/src/main/java/net/sf/flatpack/brparse/BuffReaderParseFactory.java 2011-03-05 11:46:45 UTC (rev 425) +++ trunk/flatpack/src/main/java/net/sf/flatpack/brparse/BuffReaderParseFactory.java 2011-06-08 20:06:57 UTC (rev 426) @@ -84,11 +84,9 @@ return new BuffReaderFixedParser(pzmapXML, dataSource); } - /** - * Not supported at this time. - */ + public Parser newFixedLengthParser(final Connection con, final Reader dataSource, final String dataDefinition) { - throw new UnsupportedOperationException("Not supported..."); + return new DBBuffReaderFixedParser(con, dataSource, dataDefinition); } public Parser newFixedLengthParser(final Reader pzmapXMLStream, final Reader dataSource) { @@ -105,12 +103,10 @@ return new BuffReaderFixedParser(pzmapXMLStream, dataSourceStream); } - /** - * Not supported at this time. - */ + public Parser newDelimitedParser(final Connection con, final InputStream dataSourceStream, final String dataDefinition, final char delimiter, final char qualifier, final boolean ignoreFirstRecord) { - throw new UnsupportedOperationException("Not supported..."); + throw new UnsupportedOperationException("Not supported. Use 'Reader' Constructor Instead Of InputStream."); } /* @@ -160,7 +156,8 @@ */ public Parser newDelimitedParser(final Connection con, final Reader dataSource, final String dataDefinition, final char delimiter, final char qualifier, final boolean ignoreFirstRecord) { - throw new UnsupportedOperationException("Not supported..."); + //throw new UnsupportedOperationException("Not supported..."); + return new DBBuffReaderDelimParser(con, dataSource, dataDefinition, delimiter, qualifier, ignoreFirstRecord); } public Parser newDelimitedParser(final Reader dataSource, final char delimiter, final char qualifier) { Added: trunk/flatpack/src/main/java/net/sf/flatpack/brparse/DBBuffReaderDelimParser.java =================================================================== --- trunk/flatpack/src/main/java/net/sf/flatpack/brparse/DBBuffReaderDelimParser.java (rev 0) +++ trunk/flatpack/src/main/java/net/sf/flatpack/brparse/DBBuffReaderDelimParser.java 2011-06-08 20:06:57 UTC (rev 426) @@ -0,0 +1,64 @@ +package net.sf.flatpack.brparse; + +import java.io.FileNotFoundException; +import java.io.Reader; +import java.sql.Connection; +import java.sql.SQLException; +import java.util.List; + +import net.sf.flatpack.InitialisationException; +import net.sf.flatpack.util.ParserUtils; + +/** + * + * + * @author Paul Zepernick + */ +public class DBBuffReaderDelimParser extends BuffReaderDelimParser implements InterfaceBuffReaderParse{ + + private Connection con; + + /** + * + * + * + * @param con + * @param dataSourceReader + * @param dataDefinition + * @param delimiter + * @param qualifier + * @param ignoreFirstRecord + */ + public DBBuffReaderDelimParser(Connection con, Reader dataSourceReader, String dataDefinition, char delimiter, char qualifier, + boolean ignoreFirstRecord) { + super(dataSourceReader, delimiter, qualifier, ignoreFirstRecord); + setDataDefinition(dataDefinition); + this.con = con; + } + + protected void init() { + try { + + final List cmds = ParserUtils.buildMDFromSQLTable(con, getDataDefinition(), this); + addToMetaData(cmds); + // addToColumnMD(PZConstants.DETAIL_ID, cmds); + // addToColumnMD(PZConstants.COL_IDX, ParserUtils.buidColumnIndexMap(cmds, this)); + + if (cmds.isEmpty()) { + throw new FileNotFoundException("DATA DEFINITION CAN NOT BE FOUND IN THE DATABASE " + getDataDefinition()); + } + setInitialised(true); + } catch (final SQLException e) { + throw new InitialisationException(e); + } catch (final FileNotFoundException e) { + throw new InitialisationException(e); + } + } + + protected boolean shouldCreateMDFromFile() { + //The MetaData should always be pulled from the DB for this implementation + return false; + } + + +} Added: trunk/flatpack/src/main/java/net/sf/flatpack/brparse/DBBuffReaderFixedParser.java =================================================================== --- trunk/flatpack/src/main/java/net/sf/flatpack/brparse/DBBuffReaderFixedParser.java (rev 0) +++ trunk/flatpack/src/main/java/net/sf/flatpack/brparse/DBBuffReaderFixedParser.java 2011-06-08 20:06:57 UTC (rev 426) @@ -0,0 +1,44 @@ +package net.sf.flatpack.brparse; + +import java.io.FileNotFoundException; +import java.io.Reader; +import java.sql.Connection; +import java.sql.SQLException; +import java.util.List; + +import net.sf.flatpack.InitialisationException; +import net.sf.flatpack.util.ParserUtils; + +public class DBBuffReaderFixedParser extends BuffReaderFixedParser implements InterfaceBuffReaderParse { + private Connection con; + + + public DBBuffReaderFixedParser(final Connection con, final Reader dataSourceReader, final String dataDefinition) { + super(dataSourceReader, dataDefinition); + this.con = con; + } + + protected void init() { + try { + + final List cmds = ParserUtils.buildMDFromSQLTable(con, getDataDefinition(), this); + addToMetaData(cmds); + // addToColumnMD(PZConstants.DETAIL_ID, cmds); + // addToColumnMD(PZConstants.COL_IDX, ParserUtils.buidColumnIndexMap(cmds, this)); + + if (cmds.isEmpty()) { + throw new FileNotFoundException("DATA DEFINITION CAN NOT BE FOUND IN THE DATABASE " + getDataDefinition()); + } + setInitialised(true); + } catch (final SQLException e) { + throw new InitialisationException(e); + } catch (final FileNotFoundException e) { + throw new InitialisationException(e); + } + } + + protected boolean shouldCreateMDFromFile() { + //The MetaData should always be pulled from the DB for this implementation + return false; + } +} Added: trunk/flatpack/src/main/java/net/sf/flatpack/brparse/InterfaceBuffReaderParse.java =================================================================== --- trunk/flatpack/src/main/java/net/sf/flatpack/brparse/InterfaceBuffReaderParse.java (rev 0) +++ trunk/flatpack/src/main/java/net/sf/flatpack/brparse/InterfaceBuffReaderParse.java 2011-06-08 20:06:57 UTC (rev 426) @@ -0,0 +1,22 @@ +package net.sf.flatpack.brparse; + +import net.sf.flatpack.DefaultDataSet; +import net.sf.flatpack.structure.Row; + +/** + * All buffered reader parsers should implement this interface and provide + * an implementation for the buildRow + * + * @author Paul Zepernick + */ +public interface InterfaceBuffReaderParse { + + /** + * Builds a row into the DataSet using the current record from the File + * + * @param ds + * @return Row object + */ + public Row buildRow(final DefaultDataSet ds); + +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |