From: <zep...@us...> - 2012-04-04 14:53:50
|
Revision: 436 http://flatpack.svn.sourceforge.net/flatpack/?rev=436&view=rev Author: zepernick Date: 2012-04-04 14:53:43 +0000 (Wed, 04 Apr 2012) Log Message: ----------- Enhancements to the Writer: No column mapping required on delimiter writer. Ability to write different record types. Modified Paths: -------------- trunk/flatpack/src/main/java/net/sf/flatpack/writer/AbstractWriter.java trunk/flatpack/src/main/java/net/sf/flatpack/writer/DelimiterWriter.java trunk/flatpack/src/main/java/net/sf/flatpack/writer/DelimiterWriterFactory.java trunk/flatpack/src/main/java/net/sf/flatpack/writer/FixedLengthWriter.java trunk/flatpack/src/main/java/net/sf/flatpack/writer/Writer.java trunk/flatpack/src/main/java/net/sf/flatpack/xml/MapParser.java trunk/flatpack/src/test/java/net/sf/flatpack/pzparser/PZParserOptsTest.java trunk/flatpack/src/test/java/net/sf/flatpack/writer/DelimiterWriterTestCase.java trunk/flatpack/src/test/java/net/sf/flatpack/writer/FixedLengthWriterTestCase.java Added Paths: ----------- trunk/flatpack/src/main/java/net/sf/flatpack/writer/DelimiterWriterOptions.java Modified: trunk/flatpack/src/main/java/net/sf/flatpack/writer/AbstractWriter.java =================================================================== --- trunk/flatpack/src/main/java/net/sf/flatpack/writer/AbstractWriter.java 2012-04-04 14:51:39 UTC (rev 435) +++ trunk/flatpack/src/main/java/net/sf/flatpack/writer/AbstractWriter.java 2012-04-04 14:53:43 UTC (rev 436) @@ -4,14 +4,21 @@ import java.io.IOException; import java.util.HashMap; import java.util.Map; +import java.util.Set; +import net.sf.flatpack.util.FPConstants; + /** * This class encapsulates the writer that's used to output the data. * @author Dirk Holmes and Holger Holger Hoffstatte */ public abstract class AbstractWriter extends Object implements Writer { private final BufferedWriter writer; - private Map rowMap; + + private Map rowMap; //used when using column headers + + //the record ID that is currently being written. The default is the DETAIL_ID which are <columns> that are mapped outside of <record> elements + private String recordId = FPConstants.DETAIL_ID; public AbstractWriter(final java.io.Writer output) { super(); @@ -28,6 +35,20 @@ } rowMap.put(columnName, value); } + + //TODO implement writing for no column titles +// public void addRecordEntry(final Object value) { +// if ( +// +// if (rowMap == null) { +// rowMap = new HashMap(); +// } +// +// if (!validateColumnTitle(columnName)) { +// throw new IllegalArgumentException("unknown column: \"" + columnName + "\""); +// } +// rowMap.put(columnName, value); +// } /** * Subclasses must implement this method to perform validation of @@ -50,6 +71,8 @@ // the row should have been written out by the subclass so it's safe to // discard it here rowMap = null; + //default the recordId when we go to the next record + recordId = FPConstants.DETAIL_ID; writer.newLine(); } @@ -78,8 +101,30 @@ writer.flush(); writer.close(); } + protected Map getRowMap() { return rowMap; } + + /** + * @return the recordId + */ + public String getRecordId() { + return recordId; + } + + /** + * Sets the record ID for the record that is being written. This should be used when mapping <record> elements. + * The "id" attribute of the record element should be specified here and needs to be called before calling addRecordEntry(). + * This will throw an exception if addRecordEntry() has been called for the record currently being processed. + * + * @param recordId the recordId to set + */ + public void setRecordId(String recordId) { + if (rowMap != null && !rowMap.isEmpty()) { + throw new RuntimeException("addRecordEntry() has already been called for this row. Please set the record id prior to adding data to this row."); + } + this.recordId = recordId; + } } Modified: trunk/flatpack/src/main/java/net/sf/flatpack/writer/DelimiterWriter.java =================================================================== --- trunk/flatpack/src/main/java/net/sf/flatpack/writer/DelimiterWriter.java 2012-04-04 14:51:39 UTC (rev 435) +++ trunk/flatpack/src/main/java/net/sf/flatpack/writer/DelimiterWriter.java 2012-04-04 14:53:43 UTC (rev 436) @@ -1,14 +1,14 @@ package net.sf.flatpack.writer; import java.io.IOException; -import java.util.ArrayList; +import java.math.BigDecimal; import java.util.Iterator; import java.util.List; import java.util.Map; -import java.math.BigDecimal; import net.sf.flatpack.structure.ColumnMetaData; import net.sf.flatpack.util.FPConstants; +import net.sf.flatpack.xml.XMLRecordElement; /** * @@ -16,26 +16,50 @@ */ public class DelimiterWriter extends AbstractWriter { private char delimiter; + private char qualifier; - private List columnTitles = null; + + //private List columnTitles = null; + private boolean columnTitlesWritten = false; + + private Map columnMapping; + + private DelimiterWriterOptions writerOptions; + + //this is used when using addRecordEntry() and no column names + private boolean delimitNextEntry = false; protected DelimiterWriter(final Map columnMapping, final java.io.Writer output, final char delimiter, final char qualifier) throws IOException { + + this(columnMapping, output, delimiter, qualifier, new DelimiterWriterOptions()); + } + + protected DelimiterWriter(final Map columnMapping, + final java.io.Writer output, final char delimiter, + final char qualifier, DelimiterWriterOptions writerOptions) throws IOException { super(output); this.delimiter = delimiter; this.qualifier = qualifier; - columnTitles = new ArrayList(); - final List columns = (List) columnMapping.get(FPConstants.DETAIL_ID); - final Iterator columnIter = columns.iterator(); - while (columnIter.hasNext()) { - final ColumnMetaData element = (ColumnMetaData) columnIter.next(); - columnTitles.add(element.getColName()); - } - // write the column headers - this.nextRecord(); +// columnTitles = new ArrayList(); +// final List columns = (List) columnMapping.get(FPConstants.DETAIL_ID); +// final Iterator columnIter = columns.iterator(); +// while (columnIter.hasNext()) { +// final ColumnMetaData element = (ColumnMetaData) columnIter.next(); +// columnTitles.add(element.getColName()); +// } + this.columnMapping = columnMapping; + this.writerOptions = writerOptions; + if (!writerOptions.isNoColumnMappings()) { + // write the column headers + this.nextRecord(); + } else { + //flag the headers as written so that we don't try to write them when calling nextRecord() + columnTitlesWritten = true; + } } protected void writeWithDelimiter(final Object value) throws IOException { @@ -68,18 +92,75 @@ super.write(qualifier); } } - - protected void addColumnTitle(final String string) { - if (string == null) { - throw new IllegalArgumentException("column title may not be null"); + + public void addRecordEntry(String columnName, Object value) { + if (writerOptions.isNoColumnMappings()) { + throw new UnsupportedOperationException("Column Names Cannot Be Bound To Values When Column Mappings Have Been Turned Off..."); } - columnTitles.add(string); + super.addRecordEntry(columnName, value); } + + /** + * Adds a record entry when not using column names in the file + * + * @param value + * @throws IOException + */ + public void addRecordEntry(Object value) throws IOException{ + if (!writerOptions.isNoColumnMappings()) { + throw new UnsupportedOperationException("Must use addRecordEntry(String,Object) When Using Column Names..."); + } + + if (delimitNextEntry) { + //need to use the super write here. The write in this class will try and qualify the delimiter. + //we also cannot use the writeWithDelimiter() here. It puts the delimiter after the value. We don't + //know the last column to be written so we cannot utilize that here as we did with the mappings + super.write(this.delimiter); + + } + + this.write(value.toString()); + delimitNextEntry = true; + } + //TODO find out if these are needed since the titles can already be added from the Factory +// protected void addColumnTitle(final String string) { +// addColumnTitle(string, FPConstants.DETAIL_ID); +// } +// +// protected void addColumnTitle(final String string, final String recordId) { +// if (string == null) { +// throw new IllegalArgumentException("column title may not be null"); +// } +// List cols = null; +// if (FPConstants.DETAIL_ID.equals(recordId)) { +// //the key for the detail key contains a List in the Map +// cols = (List)this.columnMapping.get(FPConstants.DETAIL_ID); +// if (cols == null) { +// cols = new ArrayList(); +// this.columnMapping.put(FPConstants.DETAIL_ID, cols); +// } +// +// } else { +// //the map contains an XMLRecord element and then the list is contained under that +// XMLRecordElement xmlRec = (XMLRecordElement)this.columnMapping.get(recordId); +// if (xmlRec == null) { +// cols = new ArrayList(); +// xmlRec = new XMLRecordElement(); +// xmlRec.setColumns(cols, null); +// this.columnMapping.put(recordId, xmlRec); +// } else { +// cols = xmlRec.getColumns(); +// } +// } +// +// cols.add(string); +// } + protected void writeColumnTitles() throws IOException { - final Iterator titleIter = columnTitles.iterator(); + final Iterator titleIter = ((List)this.columnMapping.get(FPConstants.DETAIL_ID)).iterator(); while (titleIter.hasNext()) { - final String title = (String) titleIter.next(); + final String title = ((ColumnMetaData)titleIter.next()).getColName(); if (titleIter.hasNext()) { this.writeWithDelimiter(title); @@ -90,9 +171,15 @@ } protected void writeRow() throws IOException { - final Iterator titlesIter = columnTitles.iterator(); + Iterator titlesIter = null; + if (FPConstants.DETAIL_ID.equals(getRecordId())) { + titlesIter = ((List)this.columnMapping.get(FPConstants.DETAIL_ID)).iterator(); + } else { + final XMLRecordElement xmlRec = (XMLRecordElement)this.columnMapping.get(getRecordId()); + titlesIter = xmlRec.getColumns().iterator(); + } while (titlesIter.hasNext()) { - final String columnTitle = (String) titlesIter.next(); + final String columnTitle = ((ColumnMetaData)titlesIter.next()).getColName(); if (getRowMap() != null) { if (titlesIter.hasNext()) { writeWithDelimiter(getRowMap().get(columnTitle)); @@ -102,27 +189,33 @@ } } } + public final void nextRecord() throws IOException { - if (!columnTitlesWritten) { - this.writeColumnTitles(); - columnTitlesWritten = true; - } else { - this.writeRow(); - } - + if (!writerOptions.isNoColumnMappings()) { + if (!columnTitlesWritten) { + this.writeColumnTitles(); + columnTitlesWritten = true; + } else { + this.writeRow(); + } + } + + delimitNextEntry = false; super.nextRecord(); } - public void printFooter() { - // TODO DO: implement footer handling - } - - public void printHeader() { - // TODO DO: implement header handling - } - protected boolean validateColumnTitle(final String columnTitle) { - return columnTitles.contains(columnTitle); + //return columnTitles.contains(columnTitle); + + //the column index for <record> elements is stored in the Map as the COL_IDX constant_<record id attribute> + final Map columnNameToIndex = (Map) columnMapping + .get(getRecordId().equals(FPConstants.DETAIL_ID) ? FPConstants.COL_IDX : FPConstants.COL_IDX + "_" + getRecordId()); + if (columnNameToIndex == null) { + //TODO this needs to be moved to the AbstractWriter, but the columnMapping is not exposed to it currently + //This should only happen if the getRecordId() contained an invalid record id + throw new RuntimeException("The record ID[" + getRecordId() + "] Is Not Mapped"); + } + return columnNameToIndex.keySet().contains(columnTitle); } } Modified: trunk/flatpack/src/main/java/net/sf/flatpack/writer/DelimiterWriterFactory.java =================================================================== --- trunk/flatpack/src/main/java/net/sf/flatpack/writer/DelimiterWriterFactory.java 2012-04-04 14:51:39 UTC (rev 435) +++ trunk/flatpack/src/main/java/net/sf/flatpack/writer/DelimiterWriterFactory.java 2012-04-04 14:53:43 UTC (rev 436) @@ -2,11 +2,14 @@ import java.io.IOException; import java.io.Reader; +import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import java.util.Map; import net.sf.flatpack.structure.ColumnMetaData; import net.sf.flatpack.util.FPConstants; +import net.sf.flatpack.xml.XMLRecordElement; import org.jdom.JDOMException; @@ -20,6 +23,7 @@ private final char delimiter; private final char qualifier; + public DelimiterWriterFactory(final char delimiter, final char qualifier) { super(); @@ -62,22 +66,76 @@ public char getQualifier() { return qualifier; } + - public Writer createWriter(final java.io.Writer out) throws IOException { - return new DelimiterWriter(this.getColumnMapping(), out, delimiter, qualifier); + public Writer createWriter(final java.io.Writer out) throws IOException { + return createWriter(out, new DelimiterWriterOptions()); + } + + /** + * Create an instance of the DelimterWriter + * + * @param out + * @param delimiterWriterOptions + * Options for Writing + * @return {@link DelimiterWriter} + * @throws IOException + */ + public Writer createWriter(final java.io.Writer out, final DelimiterWriterOptions delimiterWriterOptions) throws IOException { + return new DelimiterWriter(this.getColumnMapping(), out, delimiter, qualifier, delimiterWriterOptions); + } - // TODO DO: check that no column titles can be added after first nextRecord - public void addColumnTitle(final String columnTitle) { + /** + * Add column titles for mapping. This can be done in lieu of using an XML Mapping. This needs to be done before calling createWriter() + * + * @param columnTitle + * @param recordId + */ + public void addColumnTitle(final String columnTitle, final String recordId) { + final String colIdxKey = FPConstants.DETAIL_ID.equals(recordId) ? FPConstants.COL_IDX : FPConstants.COL_IDX + "_" + recordId; final Map columnMapping = this.getColumnMapping(); - final List columnMetaDatas = (List) columnMapping.get(FPConstants.DETAIL_ID); - final Map columnIndices = (Map) columnMapping.get(FPConstants.COL_IDX); + List columnMetaDatas = null; + if (FPConstants.DETAIL_ID.equals(recordId)) { + //the detail record will always be there. It is being setup by the AbstractWriter if it + //is not coming off of the XML + columnMetaDatas = (List) columnMapping.get(FPConstants.DETAIL_ID); + } else { + //this has a XMLRecord in the map, for anything other than the detail id + XMLRecordElement xmlRec = (XMLRecordElement)columnMapping.get(recordId); + if (xmlRec == null) { + columnMetaDatas = new ArrayList(); + xmlRec = new XMLRecordElement(); + xmlRec.setColumns(columnMetaDatas, null); + columnMapping.put(recordId, xmlRec); + } else { + columnMetaDatas = xmlRec.getColumns(); + } + } + + Map columnIndices = (Map) columnMapping.get(colIdxKey); + if (columnIndices == null) { + columnIndices = new HashMap(); + columnMapping.put(recordId, columnIndices); + } final ColumnMetaData metaData = new ColumnMetaData(); metaData.setColName(columnTitle); columnMetaDatas.add(metaData); final Integer columnIndex = Integer.valueOf(columnMetaDatas.indexOf(metaData)); - columnIndices.put(columnIndex, columnTitle); + columnIndices.put(columnTitle, columnIndex); } + + /** + * Add column titles for mapping. This can be done in lieu of using an XML Mapping. This needs to be done before calling createWriter() + * + * @param string + */ + public void addColumnTitle(final String string) { + addColumnTitle(string, FPConstants.DETAIL_ID); + } + + + } Added: trunk/flatpack/src/main/java/net/sf/flatpack/writer/DelimiterWriterOptions.java =================================================================== --- trunk/flatpack/src/main/java/net/sf/flatpack/writer/DelimiterWriterOptions.java (rev 0) +++ trunk/flatpack/src/main/java/net/sf/flatpack/writer/DelimiterWriterOptions.java 2012-04-04 14:53:43 UTC (rev 436) @@ -0,0 +1,44 @@ +package net.sf.flatpack.writer; + +/** + * Defines options for various DelimiterWriter behaviors + * + * @author Paul Zepernick + */ +public class DelimiterWriterOptions { + + private boolean noColumnMappings; + + + + /** + * Returns a DelimiterWriterOptions instance + * + * @return DelimiterWriterOptions + */ + public static DelimiterWriterOptions getInstance() { + return new DelimiterWriterOptions(); + } + + + + /** + * @return the noColumnMappings + */ + public boolean isNoColumnMappings() { + return noColumnMappings; + } + + + + /** + * When this is set to true, the addRecordEntry(column, value) will throw an exception. You + * must use addRecordEntry(value). + * + * @param noColumnMappings the noColumnMappings to set + */ + public void setNoColumnMappings(boolean noColumnMappings) { + this.noColumnMappings = noColumnMappings; + } + +} Modified: trunk/flatpack/src/main/java/net/sf/flatpack/writer/FixedLengthWriter.java =================================================================== --- trunk/flatpack/src/main/java/net/sf/flatpack/writer/FixedLengthWriter.java 2012-04-04 14:51:39 UTC (rev 435) +++ trunk/flatpack/src/main/java/net/sf/flatpack/writer/FixedLengthWriter.java 2012-04-04 14:53:43 UTC (rev 436) @@ -9,6 +9,7 @@ import net.sf.flatpack.structure.ColumnMetaData; import net.sf.flatpack.util.FPConstants; +import net.sf.flatpack.xml.XMLRecordElement; /** * @@ -96,27 +97,27 @@ } protected boolean validateColumnTitle(final String columnTitle) { + //the column index for <record> elements is stored in the Map as the COL_IDX constant_<record id attribute> final Map columnNameToIndex = (Map) columnMapping - .get(FPConstants.COL_IDX); + .get(getRecordId().equals(FPConstants.DETAIL_ID) ? FPConstants.COL_IDX : FPConstants.COL_IDX + "_" + getRecordId()); + if (columnNameToIndex == null) { + //TODO this needs to be moved to the AbstractWriter, but the columnMapping is not exposed to it currently + //This should only happen if the getRecordId() contained an invalid record id + throw new RuntimeException("The record ID[" + getRecordId() + "] Is Not Mapped"); + } return columnNameToIndex.keySet().contains(columnTitle); } - public void printFooter() { - // TODO DO: implement footer handling - - } - - public void printHeader() { - // TODO DO: implement header handling - - } - /** * @return List of ColumnMetaData objects describing the mapping defined in * the XML file. */ private List getColumnMetaData() { - return (List) columnMapping.get(FPConstants.DETAIL_ID); + if (getRecordId().equals(FPConstants.DETAIL_ID)) { + return (List) columnMapping.get(getRecordId()); + } + + return ((XMLRecordElement)columnMapping.get(getRecordId())).getColumns(); } private ColumnMetaData getColumnMetaData(final String columnName) { @@ -128,7 +129,7 @@ } } - throw new IllegalArgumentException("Column \"" + columnName + throw new IllegalArgumentException("Record ID[" + getRecordId() + "] Column \"" + columnName + "\" unknown"); } } Modified: trunk/flatpack/src/main/java/net/sf/flatpack/writer/Writer.java =================================================================== --- trunk/flatpack/src/main/java/net/sf/flatpack/writer/Writer.java 2012-04-04 14:51:39 UTC (rev 435) +++ trunk/flatpack/src/main/java/net/sf/flatpack/writer/Writer.java 2012-04-04 14:53:43 UTC (rev 436) @@ -7,17 +7,21 @@ * @author Dirk Holmes and Holger Holger Hoffstatte */ public interface Writer { - /** put writer into header mode. TODO: how to handle multiple header lines? */ - void printHeader(); - /** put writer into footer mode. TODO: how to handle multiple footer lines? */ - void printFooter(); + void addRecordEntry(String columnName, Object value); - void addRecordEntry(String columnName, Object value); - void nextRecord() throws IOException; void flush() throws IOException; void close() throws IOException; + + /** + * Sets the record ID for the record that is being written. This should be used when mapping <record> elements. + * The "id" attribute of the record element should be specified here and needs to be called before calling addRecordEntry(). + * This will throw an exception if addRecordEntry() has been called for the record currently being processed. + * + * @param recordId the recordId to set + */ + void setRecordId(String recordId); } Modified: trunk/flatpack/src/main/java/net/sf/flatpack/xml/MapParser.java =================================================================== --- trunk/flatpack/src/main/java/net/sf/flatpack/xml/MapParser.java 2012-04-04 14:51:39 UTC (rev 435) +++ trunk/flatpack/src/main/java/net/sf/flatpack/xml/MapParser.java 2012-04-04 14:53:43 UTC (rev 436) @@ -174,12 +174,14 @@ xmlre.setEndPositition(convertAttributeToInt(xmlElement.getAttribute("endPosition"))); xmlre.setElementCount(convertAttributeToInt(xmlElement.getAttribute("elementCount"))); mdIndex.put(xmlElement.getAttributeValue("id"), xmlre); + //make a column index for non detail records + mdIndex.put(FPConstants.COL_IDX + "_" + xmlElement.getAttributeValue("id"), ParserUtils.buidColumnIndexMap(columns, pzparser)); } if (showDebug) { setShowDebug(mdIndex); } - + return mdIndex; } @@ -292,6 +294,20 @@ final Map m = (Map) map.get(FPConstants.COL_IDX); map.remove(FPConstants.COL_IDX); + + //loop through the map and remove anything else that is an index of FPConstancts.COL_IDX + _ + //these were put in for the writer. + //TODO maybe these shoudld be thrown into the MetaData instead of just discarded, but they are unused + //in the Reader the moment. This parseMap is not utilized in the writer so it is safe to remove them here + final Iterator entrySetIt = map.entrySet().iterator(); + while (entrySetIt.hasNext()) { + final Entry e = (Entry)entrySetIt.next(); + if (((String)e.getKey()).startsWith(FPConstants.COL_IDX + "_")) { + entrySetIt.remove(); + } + } + + // System.out.println(map); return new MetaData(col, m, map); } } Modified: trunk/flatpack/src/test/java/net/sf/flatpack/pzparser/PZParserOptsTest.java =================================================================== --- trunk/flatpack/src/test/java/net/sf/flatpack/pzparser/PZParserOptsTest.java 2012-04-04 14:51:39 UTC (rev 435) +++ trunk/flatpack/src/test/java/net/sf/flatpack/pzparser/PZParserOptsTest.java 2012-04-04 14:53:43 UTC (rev 436) @@ -218,6 +218,38 @@ assertEquals(true, ds.next()); } + public void testFixedWidthMultipleRecordElementsInMapping() { + DataSet ds; + String xml = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?> \r\n" + + "<!DOCTYPE PZMAP SYSTEM \"pzfilereader.dtd\" > \r\n" + + " <PZMAP>\r\n" + + " <RECORD id=\"header\" startPosition=\"1\" endPosition=\"1\" indicator=\"H\">" + + " <COLUMN name=\"recordtype\" length=\"1\" /> \r\n" + + " <COLUMN name=\"headerdata1\" length=\"20\" /> \r\n" + + " </RECORD>" + + " <COLUMN name=\"recordtype\" length=\"1\" /> \r\n" + + " <COLUMN name=\"detaildata1\" length=\"20\" /> \r\n" + + " </PZMAP>"; + + String cols = "HHEADER DATA \r\n" + + "DDETAIL DATA \r\n"; + + Parser p = DefaultParserFactory.getInstance().newFixedLengthParser(new StringReader(xml), new StringReader(cols)); + ds = p.parse(); + int i = 0; + while (ds.next()) { + if (i == 0 ) { + assertEquals("Checking to see if we have the header record...", ds.isRecordID("header"), true); + assertEquals("Checking header data", ds.getString("headerdata1"), "HEADER DATA"); + } else { + assertEquals("Checking detail data", ds.getString("detaildata1"), "DETAIL DATA"); + } + + i++; + } + + } + public void testSorting() { DataSet ds; String cols = "fname,lname,dob,anumber\r\npaul,zepernick,06/21/1981,2\r\nbenoit,xhenseval,05/01/1970,12"; Modified: trunk/flatpack/src/test/java/net/sf/flatpack/writer/DelimiterWriterTestCase.java =================================================================== --- trunk/flatpack/src/test/java/net/sf/flatpack/writer/DelimiterWriterTestCase.java 2012-04-04 14:51:39 UTC (rev 435) +++ trunk/flatpack/src/test/java/net/sf/flatpack/writer/DelimiterWriterTestCase.java 2012-04-04 14:53:43 UTC (rev 436) @@ -101,6 +101,29 @@ // this one was expected } } + + public void testAllowWriteWithNoMapping() throws Exception { + final StringWriter sw = new StringWriter(); + + final DelimiterWriterOptions writerOpts = new DelimiterWriterOptions(); + writerOpts.setNoColumnMappings(true); + final DelimiterWriterFactory factory = new DelimiterWriterFactory(',', '"'); + final DelimiterWriter dwriter = (DelimiterWriter)factory.createWriter(sw, writerOpts); + + dwriter.addRecordEntry("Column1Data"); + dwriter.addRecordEntry("Column2,Data"); + dwriter.addRecordEntry("Column3Data"); + dwriter.nextRecord(); + dwriter.flush(); + + final String ls = System.getProperty("line.separator"); + final StringBuilder expected = new StringBuilder(); + expected.append("Column1Data").append(","); + expected.append("\"").append("Column2,Data").append("\"").append(","); + expected.append("Column3Data").append(ls); + + assertEquals("Testing writer with no colum mapping", expected.toString(), sw.toString()); + } public void testWriteValueWithQualifier() throws Exception { final DelimiterWriterFactory factory = new DelimiterWriterFactory(';', '"'); Modified: trunk/flatpack/src/test/java/net/sf/flatpack/writer/FixedLengthWriterTestCase.java =================================================================== --- trunk/flatpack/src/test/java/net/sf/flatpack/writer/FixedLengthWriterTestCase.java 2012-04-04 14:51:39 UTC (rev 435) +++ trunk/flatpack/src/test/java/net/sf/flatpack/writer/FixedLengthWriterTestCase.java 2012-04-04 14:53:43 UTC (rev 436) @@ -3,6 +3,7 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; +import java.io.StringReader; import java.io.StringWriter; import junit.framework.Assert; @@ -95,9 +96,48 @@ .normalizeLineEnding(" DOE 1234 CIRCLE CT ELYRIA OH44035"); Assert.assertEquals(expected, out.toString()); } + + public void testWriteDifferentRecords() throws Exception{ + final String ls = System.getProperty("line.separator"); + final StringWriter out = new StringWriter(); + final Writer writer = new FixedWriterFactory(getMappingDiffRecordTypes()).createWriter(out); + writer.setRecordId("header"); + writer.addRecordEntry("recordtype", "H"); + writer.addRecordEntry("headerdata1", "header data"); + writer.nextRecord(); + + writer.addRecordEntry("recordtype", "D"); + writer.addRecordEntry("detaildata1", "detail data"); + writer.nextRecord(); + writer.flush(); + + final StringBuilder expected = new StringBuilder(); + expected.append("H"); + expected.append("header data ").append(ls); + expected.append("D"); + expected.append("detail data ").append(ls); + + assertEquals("Checking writer for different record types...", expected.toString(), out.toString()); + + } private Reader getMapping() { final InputStream mapping = this.getClass().getClassLoader().getResourceAsStream("FixedLength.pzmap.xml"); return new InputStreamReader(mapping); } + + private Reader getMappingDiffRecordTypes() { + String xml = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?> \r\n" + + "<!DOCTYPE PZMAP SYSTEM \"pzfilereader.dtd\" > \r\n" + + " <PZMAP>\r\n" + + " <RECORD id=\"header\" startPosition=\"1\" endPosition=\"1\" indicator=\"H\">" + + " <COLUMN name=\"recordtype\" length=\"1\" /> \r\n" + + " <COLUMN name=\"headerdata1\" length=\"20\" /> \r\n" + + " </RECORD>" + + " <COLUMN name=\"recordtype\" length=\"1\" /> \r\n" + + " <COLUMN name=\"detaildata1\" length=\"20\" /> \r\n" + + " </PZMAP>"; + + return new StringReader(xml); + } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |