From: <be...@us...> - 2006-10-30 16:46:45
|
Revision: 151 http://svn.sourceforge.net/pzfilereader/?rev=151&view=rev Author: benoitx Date: 2006-10-30 08:46:27 -0800 (Mon, 30 Oct 2006) Log Message: ----------- first cut at reg expressions... Added Paths: ----------- trunk/PZFileReader/src/main/java/net/sf/pzfilereader/util/RegExParser.java trunk/PZFileReader/src/test/java/net/sf/pzfilereader/parserutils/RegExParserTest.java Added: trunk/PZFileReader/src/main/java/net/sf/pzfilereader/util/RegExParser.java =================================================================== --- trunk/PZFileReader/src/main/java/net/sf/pzfilereader/util/RegExParser.java (rev 0) +++ trunk/PZFileReader/src/main/java/net/sf/pzfilereader/util/RegExParser.java 2006-10-30 16:46:27 UTC (rev 151) @@ -0,0 +1,114 @@ +/** + * + */ +package net.sf.pzfilereader.util; + +import java.util.ArrayList; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * @author xhensevb + * + */ +public class RegExParser { + + // /////////////////////////// + /** + * The rather involved pattern used to match CSV's consists of three + * alternations: the first matches aquoted field, the second unquoted, the + * third a null field. + */ + public static final String CSV_PATTERN = "\"(.*?)\",|(\\w+),|\"(.*?)\"|(\\w+),|,"; + public static final String ORIGINAL_CSV_PATTERN = "\"([^\"]+?)\",?|([^,]+),?|,"; + + // public static final String CSV_PATTERN = "\"([^\"]+?)\",?|([^,]+),?|,"; + +// private static Pattern csvRE = Pattern.compile(CSV_PATTERN); + + public static List splitLine(String line, final char delimiter, char qualifier) { + StringBuilder patternBuilder = new StringBuilder(); + + if (qualifier == 0) { + qualifier = '\"'; + } + + String qualif = escapeIfRequired(qualifier); + String delim = escapeIfRequired(delimiter); + + // first Pattern + if (qualifier != 0) { + patternBuilder.append(qualif); + } + patternBuilder.append("(.*?)"); + if (qualifier != 0) { + patternBuilder.append(qualif); + } + patternBuilder.append(delim); + + // second Pattern + patternBuilder.append("|(\\w+)"); + patternBuilder.append(delim); + + // Third Pattern + patternBuilder.append("|"); + if (qualifier != 0) { + patternBuilder.append(qualif); + } + patternBuilder.append("(.*?)"); + if (qualifier != 0) { + patternBuilder.append(qualif); + } + patternBuilder.append("|(\\w+)"); + patternBuilder.append(delim); + + // Fourth Pattern + patternBuilder.append("|").append(delim); + + String pat = patternBuilder.toString(); + + System.out.println(pat); + + Pattern pattern = Pattern.compile(pat); + + return parse(pattern, line, String.valueOf(delimiter), String.valueOf(qualifier)); + } + + private static String escapeIfRequired(final char c) { + if (c == 0) { + return ""; + } + if ("([{\\^-$|]})?*+.\"\'".indexOf(c) >= 0) { + return "\\" + c; + } + return String.valueOf(c); + } + + /** + * Parse one line. + * + * @return List of Strings, minus their double quotes + */ + public static List parse(Pattern pattern, String line, String delimiter, String qualifier) { + List list = new ArrayList(); + Matcher m = pattern.matcher(line); + // For each field + while (m.find()) { + String match = m.group(); + if (match == null) + break; + if (match.endsWith(delimiter)) { // trim trailing , + match = match.substring(0, match.length() - 1); + } + if (match.startsWith(qualifier)) { // assume also ends with + match = match.substring(1, match.length() - 1); + } + if (match.length() == 0) + match = null; + list.add(match); + } + return list; + } + +} Added: trunk/PZFileReader/src/test/java/net/sf/pzfilereader/parserutils/RegExParserTest.java =================================================================== --- trunk/PZFileReader/src/test/java/net/sf/pzfilereader/parserutils/RegExParserTest.java (rev 0) +++ trunk/PZFileReader/src/test/java/net/sf/pzfilereader/parserutils/RegExParserTest.java 2006-10-30 16:46:27 UTC (rev 151) @@ -0,0 +1,144 @@ +package net.sf.pzfilereader.parserutils; + +import java.util.List; + +import junit.framework.TestCase; +import net.sf.pzfilereader.util.ParserUtils; +import net.sf.pzfilereader.util.RegExParser; +import net.sf.pzfilereader.utilities.UnitTestUtils; + +/** + * Test the functionality of the splitLine method. This method returns a List of + * Strings. Each element of the list represents a column created by the parser + * from the delimited String. + * + * @author Paul Zepernick + */ +public class RegExParserTest extends TestCase { + private static final String[] DELIMITED_DATA_NO_BREAKS = { "Column 1", "Column 2", "Column 3", "Column 4", "Column 5" }; + + private static final String[] DELIMITED_DATA_WITH_BREAKS = { "Column 1 \r\n\r\n Test After Break \r\n Another Break", + "Column 2", "Column 3 \r\n\r\n Test After Break", "Column 4", "Column 5 \r\n\r\n Test After Break\r\n Another Break" }; + + // TODO think of a situation that actually breaks the parse. This still + // works because of the way it is coded + // to handle the excel CSV. Excel CSV has some elements qualified and others + // not + private static final String DELIMITED_BAD_DATA = "\"column 1\",\"column 2 ,\"column3\""; + + // 0 = delimiter + // 1 = qualifier + private static final char[][] DELIM_QUAL_PAIR = { { ',', '\"' }, { '\t', '\"' }, { '|', '\"' }, { '_', '\"' }, { ',', 0 }, + { '|', 0 }, { '\t', 0 } }; + + /** + * Test without any line breaks + * + */ + public void testNoLineBreaks() { + // loop down all delimiter qualifier pairs to test + for (int i = 0; i < DELIM_QUAL_PAIR.length; i++) { + final char d = DELIM_QUAL_PAIR[i][0]; + final char q = DELIM_QUAL_PAIR[i][1]; + + final String txtToParse = UnitTestUtils.buildDelimString(DELIMITED_DATA_NO_BREAKS, d, q); + + final List splitLineResults = RegExParser.splitLine(txtToParse, d, q); + + // check to make sure we have the same amount of elements which were + // expected + assertEquals("Expected size (d = [" + d + "] q = [" + (q != 0 ? String.valueOf(q) : "") + "] txt [" + txtToParse + + "])", DELIMITED_DATA_NO_BREAKS.length, splitLineResults.size()); + + // loop through each value and compare what came back + for (int j = 0; j < DELIMITED_DATA_NO_BREAKS.length; j++) { + assertEquals("Data Element Value Does Not Match (d = [" + d + "] q = [" + q + "] txt [" + txtToParse + "])", + DELIMITED_DATA_NO_BREAKS[j], (String) splitLineResults.get(j)); + } + } + + } + + /** + * Test with any line breaks + * + */ + public void testLineBreaks() { + // loop down all delimiter qualifier pairs to test + for (int i = 0; i < DELIM_QUAL_PAIR.length; i++) { + final char d = DELIM_QUAL_PAIR[i][0]; + final char q = DELIM_QUAL_PAIR[i][1]; + + final String txtToParse = UnitTestUtils.buildDelimString(DELIMITED_DATA_WITH_BREAKS, d, q); + + final List splitLineResults = RegExParser.splitLine(txtToParse, d, q); + + // check to make sure we have the same amount of elements which were + // expected + assertEquals("Did Not Get Amount Of Elements Expected (d = " + d + " q = " + q + ")", + DELIMITED_DATA_WITH_BREAKS.length, splitLineResults.size()); + + // loop through each value and compare what came back + for (int j = 0; j < DELIMITED_DATA_WITH_BREAKS.length; j++) { + assertEquals("Data Element Value Does Not Match (d = " + d + " q = " + q + ")", DELIMITED_DATA_WITH_BREAKS[j], + (String) splitLineResults.get(j)); + } + } + } + + /** + * Test to make sure we get the correct amount of elements for malformed + * data + */ + public void testMalformedData() { + final List splitLineResults = ParserUtils.splitLine(DELIMITED_BAD_DATA, ',', '\"'); + + assertEquals("Expecting 2 Data Elements From The Malformed Data", 2, splitLineResults.size()); + } + + /** + * Test some extreme cases + */ + public void testSomeExtremeCases() { + check("\"a,b,c\"", ',', '\"', new String[] { "a,b,c" }); + check("\"a,b\",\"c\"", ',', '\"', new String[] { "a,b", "c" }); + check("a,b,c", ',', '\"', new String[] { "a", "b", "c" }); + check(" a,b,c", ',', '\"', new String[] { "a", "b", "c" }); + check(" a,b,c", ',', '\"', new String[] { "a", "b", "c" }); + + // what would you expect of these ones? + + // +++++The parser allows qualified and unqualified elements to be + // contained + // on the same line. so it should break the elements down like so + // 1 = a" -->" is part of the data since the element did not start with + // a qualifier + // 2 = b + // 3 = c" --> same as #1 + check("a\",b,c\"", ',', '\"', new String[] { "a\"", "b", "c\"" }); + + check("\" a,b,c\"", ',', '\"', new String[] { "a,b,c" }); + // check(" a, b ,c ", ',', '\"', new String[] { "a","b","c" }); + // ++++++I think this should probably generate this + check(" a, b ,c ", ',', '\"', new String[] { "a, b ,c" }); + + // Paul... please put some more whacky stuff here... + + } + + private void check(final String txtToParse, final char delim, final char qualifier, final String[] expected) { + final List splitLineResults = ParserUtils.splitLine(txtToParse, delim, qualifier); + + assertEquals( + "Did Not Get Amount Of Elements Expected (d = " + delim + " q = " + qualifier + ") txt [" + txtToParse + "]", + expected.length, splitLineResults.size()); + + for (int i = 0; i < expected.length; i++) { + assertEquals("expecting...", expected[i], splitLineResults.get(i)); + } + } + + public static void main(final String[] args) { + junit.textui.TestRunner.run(RegExParserTest.class); + } +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <be...@us...> - 2006-10-31 13:38:17
|
Revision: 154 http://svn.sourceforge.net/pzfilereader/?rev=154&view=rev Author: benoitx Date: 2006-10-31 05:38:04 -0800 (Tue, 31 Oct 2006) Log Message: ----------- added a few more tests. Modified Paths: -------------- trunk/PZFileReader/src/main/java/net/sf/pzfilereader/util/RegExParser.java trunk/PZFileReader/src/test/java/net/sf/pzfilereader/parserutils/ParserUtilsSplitLineTest.java trunk/PZFileReader/src/test/java/net/sf/pzfilereader/parserutils/RegExParserTest.java Modified: trunk/PZFileReader/src/main/java/net/sf/pzfilereader/util/RegExParser.java =================================================================== --- trunk/PZFileReader/src/main/java/net/sf/pzfilereader/util/RegExParser.java 2006-10-31 11:44:42 UTC (rev 153) +++ trunk/PZFileReader/src/main/java/net/sf/pzfilereader/util/RegExParser.java 2006-10-31 13:38:04 UTC (rev 154) @@ -4,6 +4,7 @@ package net.sf.pzfilereader.util; import java.util.ArrayList; +import java.util.Iterator; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -21,11 +22,13 @@ * third a null field. */ public static final String CSV_PATTERN = "\"(.*?)\",|(\\w+),|\"(.*?)\"|(\\w+),|,"; + public static final String CSV_PATTERN2 = "([^\"]+?),|\"(.*?)\",|(\\w+),|\"(.*?)\"|(\\w+)"; + public static final String ORIGINAL_CSV_PATTERN = "\"([^\"]+?)\",?|([^,]+),?|,"; // public static final String CSV_PATTERN = "\"([^\"]+?)\",?|([^,]+),?|,"; -// private static Pattern csvRE = Pattern.compile(CSV_PATTERN); + // private static Pattern csvRE = Pattern.compile(CSV_PATTERN); public static List splitLine(String line, final char delimiter, char qualifier) { StringBuilder patternBuilder = new StringBuilder(); @@ -68,11 +71,21 @@ String pat = patternBuilder.toString(); + pat = CSV_PATTERN2; + System.out.println(pat); + System.out.println("Input: [" + line + "]"); Pattern pattern = Pattern.compile(pat); - return parse(pattern, line, String.valueOf(delimiter), String.valueOf(qualifier)); + List l = parse(pattern, line, String.valueOf(delimiter), String.valueOf(qualifier)); + + int u = 1; + for (Iterator i = l.iterator(); i.hasNext(); u++) { + System.out.println(u + "/" + l.size() + " -- [" + i.next() + "]"); + } + + return l; } private static String escapeIfRequired(final char c) { @@ -104,6 +117,9 @@ if (match.startsWith(qualifier)) { // assume also ends with match = match.substring(1, match.length() - 1); } + + match = match.trim(); + if (match.length() == 0) match = null; list.add(match); Modified: trunk/PZFileReader/src/test/java/net/sf/pzfilereader/parserutils/ParserUtilsSplitLineTest.java =================================================================== --- trunk/PZFileReader/src/test/java/net/sf/pzfilereader/parserutils/ParserUtilsSplitLineTest.java 2006-10-31 11:44:42 UTC (rev 153) +++ trunk/PZFileReader/src/test/java/net/sf/pzfilereader/parserutils/ParserUtilsSplitLineTest.java 2006-10-31 13:38:04 UTC (rev 154) @@ -46,8 +46,8 @@ // check to make sure we have the same amount of elements which were // expected - assertEquals("Did Not Get Amount Of Elements Expected (d = [" + d + "] q = [" + q + "] txt [" + txtToParse + "])", - DELIMITED_DATA_NO_BREAKS.length, splitLineResults.size()); + assertEquals("Expected size (d = [" + d + "] q = [" + (q != 0 ? String.valueOf(q) : "") + "] txt [" + txtToParse + + "])", DELIMITED_DATA_NO_BREAKS.length, splitLineResults.size()); // loop through each value and compare what came back for (int j = 0; j < DELIMITED_DATA_NO_BREAKS.length; j++) { @@ -103,21 +103,26 @@ check("\"a,b\",\"c\"", ',', '\"', new String[] { "a,b", "c" }); check("a,b,c", ',', '\"', new String[] { "a", "b", "c" }); check(" a,b,c", ',', '\"', new String[] { "a", "b", "c" }); - check(" a,b,c", ',', '\"', new String[] { "a","b","c" }); - + check(" a,b,c", ',', '\"', new String[] { "a", "b", "c" }); + + // example typically from Excel. + check("\"test1\",test2,\"0.00\",\"another, element here\",lastone", ',', '\"', new String[] { "test1", "test2", "0.00", + "another, element here", "lastone" }); + // what would you expect of these ones? - - //+++++The parser allows qualified and unqualified elements to be contained - //on the same line. so it should break the elements down like so - //1 = a" -->" is part of the data since the element did not start with a qualifier - //2 = b - //3 = c" --> same as #1 + + // +++++The parser allows qualified and unqualified elements to be + // contained + // on the same line. so it should break the elements down like so + // 1 = a" -->" is part of the data since the element did not start with + // a qualifier + // 2 = b + // 3 = c" --> same as #1 + // a",b,c" check("a\",b,c\"", ',', '\"', new String[] { "a\"", "b", "c\"" }); //should not trim leading space inside of a qualified element check("\" a,b,c\"", ',', '\"', new String[] { " a,b,c" }); check(" a, b ,c ", ',', '\"', new String[] { "a","b","c" }); - - // Paul... please put some more whacky stuff here... check("\"a\", b , \"c\"", ',', '\"', new String[] {"a","b","c"}); //check malformed data //TODO - I believe this should be producing 2 elements. As soon as their is a @@ -129,6 +134,37 @@ } + /** + * Test some extreme cases + */ + public void testSomeExtremeCases2() { + check("\"a,b,c\"", ',', '\'', new String[] { "\"a", "b", "c\"" }); + check("\"a,b\",\"c\"", ',', '\'', new String[] { "\"a", "b\"", "\"c\"" }); + check("a,b,c", ',', '\'', new String[] { "a", "b", "c" }); + check(" a,b,c", ',', '\'', new String[] { "a", "b", "c" }); + check(" a,b,c", ',', '\'', new String[] { "a", "b", "c" }); + + // example typically from Excel. + check("\"test1\",test2,\"0.00\",\"another, element here\",lastone", ',', '\'', new String[] { "\"test1\"", "test2", + "\"0.00\"", "\"another", "element here\"", "lastone" }); + + // what would you expect of these ones? + + // +++++The parser allows qualified and unqualified elements to be + // contained + // on the same line. so it should break the elements down like so + // 1 = a" -->" is part of the data since the element did not start with + // a qualifier + // 2 = b + // 3 = c" --> same as #1 + // a",b,c" + check("a\",b,c\"", ',', '\'', new String[] { "a\"", "b", "c\"" }); + + check("\" a,b,c\"", ',', '\'', new String[] { "\" a", "b", "c\"" }); + check(" a, b ,c ", ',', '\'', new String[] { "a", "b", "c" }); + + } + private void check(final String txtToParse, final char delim, final char qualifier, final String[] expected) { final List splitLineResults = ParserUtils.splitLine(txtToParse, delim, qualifier); Modified: trunk/PZFileReader/src/test/java/net/sf/pzfilereader/parserutils/RegExParserTest.java =================================================================== --- trunk/PZFileReader/src/test/java/net/sf/pzfilereader/parserutils/RegExParserTest.java 2006-10-31 11:44:42 UTC (rev 153) +++ trunk/PZFileReader/src/test/java/net/sf/pzfilereader/parserutils/RegExParserTest.java 2006-10-31 13:38:04 UTC (rev 154) @@ -102,7 +102,9 @@ public void testSomeExtremeCases() { check("\"a,b,c\"", ',', '\"', new String[] { "a,b,c" }); check("\"a,b\",\"c\"", ',', '\"', new String[] { "a,b", "c" }); + check("\"a , b\",\"c\"", ',', '\"', new String[] { "a , b", "c" }); check("a,b,c", ',', '\"', new String[] { "a", "b", "c" }); + check("a b,c", ',', '\"', new String[] { "a b", "c" }); check(" a,b,c", ',', '\"', new String[] { "a", "b", "c" }); check(" a,b,c", ',', '\"', new String[] { "a", "b", "c" }); @@ -127,7 +129,7 @@ } private void check(final String txtToParse, final char delim, final char qualifier, final String[] expected) { - final List splitLineResults = ParserUtils.splitLine(txtToParse, delim, qualifier); + final List splitLineResults = RegExParser.splitLine(txtToParse, delim, qualifier); assertEquals( "Did Not Get Amount Of Elements Expected (d = " + delim + " q = " + qualifier + ") txt [" + txtToParse + "]", This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <be...@us...> - 2006-10-31 17:12:27
|
Revision: 160 http://svn.sourceforge.net/pzfilereader/?rev=160&view=rev Author: benoitx Date: 2006-10-31 09:12:14 -0800 (Tue, 31 Oct 2006) Log Message: ----------- Paul, I've added some basic tests for null, empty, ",,," kind of things. I've also had a go at a parser, the regular expression is a dead-end or will become **extremely** complex due to our special and whacky cases... The basic tests make quite a few things break in the current version. I'll run a couple of speed tests to see where we're going... Modified Paths: -------------- trunk/PZFileReader/src/test/java/net/sf/pzfilereader/parserutils/ParserUtilsSplitLineTest.java Added Paths: ----------- trunk/PZFileReader/src/main/java/net/sf/pzfilereader/util/BXParser.java trunk/PZFileReader/src/test/java/net/sf/pzfilereader/parserutils/BXParserTest.java Added: trunk/PZFileReader/src/main/java/net/sf/pzfilereader/util/BXParser.java =================================================================== --- trunk/PZFileReader/src/main/java/net/sf/pzfilereader/util/BXParser.java (rev 0) +++ trunk/PZFileReader/src/main/java/net/sf/pzfilereader/util/BXParser.java 2006-10-31 17:12:14 UTC (rev 160) @@ -0,0 +1,110 @@ +/** + * + */ +package net.sf.pzfilereader.util; + +import java.util.ArrayList; +import java.util.List; + +/** + * @author xhensevb + * + */ +public class BXParser { + public static List splitLine(String line, final char delimiter, char qualifier) { + List list = new ArrayList(); + + if (delimiter == 0) { + list.add(line); + return list; + } else if (line == null) { + return list; + } + + final String trimmedLine = line.trim(); + int size = trimmedLine.length(); + + if (size == 0) { + list.add(null); + return list; + } + + boolean insideQualifier = false; + char previousChar = 0; + int startBlock = 0; + int endBlock = 0; + boolean blockWasInQualifier = false; + + final String doubleQualifier = String.valueOf(qualifier) + String.valueOf(qualifier); + for (int i = 0; i < size; i++) { + + final char currentChar = trimmedLine.charAt(i); + + if (currentChar != delimiter && currentChar != qualifier) { + previousChar = currentChar; + endBlock = i + 1; + continue; + } + + if (currentChar == delimiter) { + // we've found the delimiter (eg ,) + if (!insideQualifier) { + String trimmed = trimmedLine.substring(startBlock, endBlock > startBlock ? endBlock : startBlock + 1); + if (!blockWasInQualifier) { + trimmed = trimmed.trim(); + } + + trimmed = trimmed.replaceAll(doubleQualifier, String.valueOf(qualifier)); + + if (trimmed.length() == 1 && trimmed.charAt(0) == delimiter) { + list.add(null); + } else if (trimmed.length() == 1 && trimmed.charAt(0) == qualifier) { + list.add(""); + } else { + list.add(trimmed); + } + blockWasInQualifier = false; + startBlock = i + 1; + } + } else if (currentChar == qualifier) { + if (!insideQualifier && previousChar != qualifier) { + if (previousChar == delimiter || previousChar == 0 || previousChar == ' ') { + insideQualifier = true; + startBlock = i + 1; + } else { + endBlock = i + 1; + } + } else { + insideQualifier = false; + blockWasInQualifier = true; + endBlock = i; + // last column (e.g. finishes with ") + if (i == size - 1) { + list.add(trimmedLine.substring(startBlock, size - 1)); + startBlock = i + 1; + } + } + } + // antepenultimateChar = previousChar; + previousChar = currentChar; + } + + if (startBlock < size) { + String str = trimmedLine.substring(startBlock, size); + str = str.replaceAll(doubleQualifier, String.valueOf(qualifier)); + if (blockWasInQualifier) { + if (str.charAt(str.length() - 1) == qualifier) { + list.add(str.substring(0, str.length() - 1)); + } else { + list.add(str); + } + } else { + list.add(str.trim()); + } + } else if (trimmedLine.charAt(size - 1) == delimiter) { + list.add(null); + } + + return list; + } +} Added: trunk/PZFileReader/src/test/java/net/sf/pzfilereader/parserutils/BXParserTest.java =================================================================== --- trunk/PZFileReader/src/test/java/net/sf/pzfilereader/parserutils/BXParserTest.java (rev 0) +++ trunk/PZFileReader/src/test/java/net/sf/pzfilereader/parserutils/BXParserTest.java 2006-10-31 17:12:14 UTC (rev 160) @@ -0,0 +1,151 @@ +package net.sf.pzfilereader.parserutils; + +import java.util.List; + +import junit.framework.TestCase; +import net.sf.pzfilereader.util.BXParser; +import net.sf.pzfilereader.util.ParserUtils; +import net.sf.pzfilereader.util.RegExParser; +import net.sf.pzfilereader.utilities.UnitTestUtils; + +/** + * Test the functionality of the splitLine method. This method returns a List of + * Strings. Each element of the list represents a column created by the parser + * from the delimited String. + * + * @author Paul Zepernick + */ +public class BXParserTest extends TestCase { + private static final String[] DELIMITED_DATA_NO_BREAKS = { "Column 1", "Column 2", "Column 3", "Column 4", "Column 5" }; + + private static final String[] DELIMITED_DATA_WITH_BREAKS = { "Column 1 \r\n\r\n Test After Break \r\n Another Break", + "Column 2", "Column 3 \r\n\r\n Test After Break", "Column 4", "Column 5 \r\n\r\n Test After Break\r\n Another Break" }; + + // TODO think of a situation that actually breaks the parse. This still + // works because of the way it is coded + // to handle the excel CSV. Excel CSV has some elements qualified and others + // not + private static final String DELIMITED_BAD_DATA = "\"column 1\",\"column 2 ,\"column3\""; + + // 0 = delimiter + // 1 = qualifier + private static final char[][] DELIM_QUAL_PAIR = { { ',', '\"' }, { '\t', '\"' }, { '|', '\"' }, { '_', '\"' }, { ',', 0 }, + { '|', 0 }, { '\t', 0 } }; + + /** + * Test without any line breaks + * + */ + public void testNoLineBreaks() { + // loop down all delimiter qualifier pairs to test + for (int i = 0; i < DELIM_QUAL_PAIR.length; i++) { + final char d = DELIM_QUAL_PAIR[i][0]; + final char q = DELIM_QUAL_PAIR[i][1]; + + final String txtToParse = UnitTestUtils.buildDelimString(DELIMITED_DATA_NO_BREAKS, d, q); + + final List splitLineResults = BXParser.splitLine(txtToParse, d, q); + + // check to make sure we have the same amount of elements which were + // expected + assertEquals("Expected size (d = [" + d + "] q = [" + (q != 0 ? String.valueOf(q) : "") + "] txt [" + txtToParse + + "])", DELIMITED_DATA_NO_BREAKS.length, splitLineResults.size()); + + // loop through each value and compare what came back + for (int j = 0; j < DELIMITED_DATA_NO_BREAKS.length; j++) { + assertEquals("Data Element Value Does Not Match (d = [" + d + "] q = [" + q + "] txt [" + txtToParse + "])", + DELIMITED_DATA_NO_BREAKS[j], (String) splitLineResults.get(j)); + } + } + + } + + /** + * Test with any line breaks + * + */ + public void NOtestLineBreaks() { + // loop down all delimiter qualifier pairs to test + for (int i = 0; i < DELIM_QUAL_PAIR.length; i++) { + final char d = DELIM_QUAL_PAIR[i][0]; + final char q = DELIM_QUAL_PAIR[i][1]; + + final String txtToParse = UnitTestUtils.buildDelimString(DELIMITED_DATA_WITH_BREAKS, d, q); + + final List splitLineResults = BXParser.splitLine(txtToParse, d, q); + + // check to make sure we have the same amount of elements which were + // expected + assertEquals("Did Not Get Amount Of Elements Expected (d = " + d + " q = " + q + ")", + DELIMITED_DATA_WITH_BREAKS.length, splitLineResults.size()); + + // loop through each value and compare what came back + for (int j = 0; j < DELIMITED_DATA_WITH_BREAKS.length; j++) { + assertEquals("Data Element Value Does Not Match (d = " + d + " q = " + q + ")", DELIMITED_DATA_WITH_BREAKS[j], + (String) splitLineResults.get(j)); + } + } + } + + /** + * Test to make sure we get the correct amount of elements for malformed + * data + */ + public void testMalformedData() { + final List splitLineResults = BXParser.splitLine(DELIMITED_BAD_DATA, ',', '\"'); + + assertEquals("Expecting 2 Data Elements From The Malformed Data", 2, splitLineResults.size()); + } + + /** + * Test some extreme cases + */ + public void testSomeExtremeCases() { + check(null, ',', '\"', new String[] {}); + check("a", ',', '\"', new String[] { "a" }); + check("", ',', '\"', new String[] { null }); + check(" ", ',', '\"', new String[] { null }); + check(" ", ',', '\"', new String[] { null }); + check(",", ',', '\"', new String[] { null, null }); + check(",,", ',', '\"', new String[] { null, null, null }); + check(",a,", ',', '\"', new String[] { null, "a", null }); + + check("\"a,b,c\"", ',', '\"', new String[] { "a,b,c" }); + check("\"a,b\",\"c\"", ',', '\"', new String[] { "a,b", "c" }); + check("\"a , b\",\"c\"", ',', '\"', new String[] { "a , b", "c" }); + check("a,b,c", ',', '\"', new String[] { "a", "b", "c" }); + check("a b,c", ',', '\"', new String[] { "a b", "c" }); + check(" a,b,c ", ',', '\"', new String[] { "a", "b", "c" }); + check(" a, b ,c", ',', '\"', new String[] { "a", "b", "c" }); + + // example typically from Excel. + check("\"test1\",test2,\"0.00\",\"another, element here\",lastone", ',', '\"', new String[] { "test1", "test2", "0.00", + "another, element here", "lastone" }); + + check("a\",b,c\"", ',', '\"', new String[] { "a\"", "b", "c\"" }); + check(" a, b ,c ", ',', '\"', new String[] { "a", "b", "c" }); + check("\"a\", b , \"c\"", ',', '\"', new String[] { "a", "b", "c" }); + + check("\"\",,,,\"last one\"", ',', '\"', new String[] { "", null, null, null, "last one" }); + check("\"first\",\"second\",", ',', '\"', new String[] { "first", "second", null }); + check("\" a,b,c\"", ',', '\"', new String[] { " a,b,c" }); + check("\" a,b,c\",d", ',', '\"', new String[] { " a,b,c", "d" }); + check("\"a, b,\"\"c\"", ',', '\"', new String[] { "a, b,\"c" }); + } + + private void check(final String txtToParse, final char delim, final char qualifier, final String[] expected) { + final List splitLineResults = BXParser.splitLine(txtToParse, delim, qualifier); + + assertEquals( + "Did Not Get Amount Of Elements Expected (d = " + delim + " q = " + qualifier + ") txt [" + txtToParse + "]", + expected.length, splitLineResults.size()); + + for (int i = 0; i < expected.length; i++) { + assertEquals("expecting...", expected[i], splitLineResults.get(i)); + } + } + + public static void main(final String[] args) { + junit.textui.TestRunner.run(BXParserTest.class); + } +} Modified: trunk/PZFileReader/src/test/java/net/sf/pzfilereader/parserutils/ParserUtilsSplitLineTest.java =================================================================== --- trunk/PZFileReader/src/test/java/net/sf/pzfilereader/parserutils/ParserUtilsSplitLineTest.java 2006-10-31 16:07:23 UTC (rev 159) +++ trunk/PZFileReader/src/test/java/net/sf/pzfilereader/parserutils/ParserUtilsSplitLineTest.java 2006-10-31 17:12:14 UTC (rev 160) @@ -99,49 +99,36 @@ * Test some extreme cases */ public void testSomeExtremeCases() { - // back to Basic... check(null, ',', '\"', new String[] {}); check("a", ',', '\"', new String[] { "a" }); check("", ',', '\"', new String[] { null }); + check(" ", ',', '\"', new String[] { null }); + check(" ", ',', '\"', new String[] { null }); check(",", ',', '\"', new String[] { null, null }); check(",,", ',', '\"', new String[] { null, null, null }); check(",a,", ',', '\"', new String[] { null, "a", null }); - // - check("\"a,b,c\"", ',', '\"', new String[] { "a,b,c" }); check("\"a,b\",\"c\"", ',', '\"', new String[] { "a,b", "c" }); + check("\"a , b\",\"c\"", ',', '\"', new String[] { "a , b", "c" }); check("a,b,c", ',', '\"', new String[] { "a", "b", "c" }); - check(" a,b,c", ',', '\"', new String[] { "a", "b", "c" }); - check(" a,b,c", ',', '\"', new String[] { "a", "b", "c" }); + check("a b,c", ',', '\"', new String[] { "a b", "c" }); + check(" a,b,c ", ',', '\"', new String[] { "a", "b", "c" }); + check(" a, b ,c", ',', '\"', new String[] { "a", "b", "c" }); // example typically from Excel. check("\"test1\",test2,\"0.00\",\"another, element here\",lastone", ',', '\"', new String[] { "test1", "test2", "0.00", "another, element here", "lastone" }); - // what would you expect of these ones? - - // +++++The parser allows qualified and unqualified elements to be - // contained - // on the same line. so it should break the elements down like so - // 1 = a" -->" is part of the data since the element did not start with - // a qualifier - // 2 = b - // 3 = c" --> same as #1 - // a",b,c" check("a\",b,c\"", ',', '\"', new String[] { "a\"", "b", "c\"" }); - //should not trim leading space inside of a qualified element - check("\" a,b,c\"", ',', '\"', new String[] { " a,b,c" }); - check(" a, b ,c ", ',', '\"', new String[] { "a","b","c" }); - check("\"a\", b , \"c\"", ',', '\"', new String[] {"a","b","c"}); - //check malformed data - //TODO - I believe this should be producing 2 elements. As soon as their is a - //delimter followed by a qualifier a new element shoudl be created - //+++Any thoughts Benoit? - check("\"a, b,\"c\"", ',', '\"', new String[] {"a, b,\"c"}); - check("\"\",,,,\"last one\"", ',', '\"', new String[] {"","","","","last one"}); - check("\"first\",\"second\",", ',', '\"', new String[] {"first","second",""}); + check(" a, b ,c ", ',', '\"', new String[] { "a", "b", "c" }); + check("\"a\", b , \"c\"", ',', '\"', new String[] { "a", "b", "c" }); + check("\"\",,,,\"last one\"", ',', '\"', new String[] { "", null, null, null, "last one" }); + check("\"first\",\"second\",", ',', '\"', new String[] { "first", "second", null }); + check("\" a,b,c\"", ',', '\"', new String[] { " a,b,c" }); + check("\" a,b,c\",d", ',', '\"', new String[] { " a,b,c", "d" }); + check("\"a, b,\"\"c\"", ',', '\"', new String[] { "a, b,\"c" }); } /** This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <be...@us...> - 2006-10-31 20:47:17
|
Revision: 164 http://svn.sourceforge.net/pzfilereader/?rev=164&view=rev Author: benoitx Date: 2006-10-31 12:47:02 -0800 (Tue, 31 Oct 2006) Log Message: ----------- do not use null for empty fields, use an empty String. Modified Paths: -------------- trunk/PZFileReader/src/main/java/net/sf/pzfilereader/util/BXParser.java trunk/PZFileReader/src/test/java/net/sf/pzfilereader/parserutils/BXParserTest.java Modified: trunk/PZFileReader/src/main/java/net/sf/pzfilereader/util/BXParser.java =================================================================== --- trunk/PZFileReader/src/main/java/net/sf/pzfilereader/util/BXParser.java 2006-10-31 20:02:48 UTC (rev 163) +++ trunk/PZFileReader/src/main/java/net/sf/pzfilereader/util/BXParser.java 2006-10-31 20:47:02 UTC (rev 164) @@ -25,7 +25,7 @@ int size = trimmedLine.length(); if (size == 0) { - list.add(null); + list.add(""); return list; } @@ -56,9 +56,7 @@ trimmed = trimmed.replaceAll(doubleQualifier, String.valueOf(qualifier)); - if (trimmed.length() == 1 && trimmed.charAt(0) == delimiter) { - list.add(null); - } else if (trimmed.length() == 1 && trimmed.charAt(0) == qualifier) { + if (trimmed.length() == 1 && (trimmed.charAt(0) == delimiter || trimmed.charAt(0) == qualifier)) { list.add(""); } else { list.add(trimmed); @@ -102,7 +100,7 @@ list.add(str.trim()); } } else if (trimmedLine.charAt(size - 1) == delimiter) { - list.add(null); + list.add(""); } return list; Modified: trunk/PZFileReader/src/test/java/net/sf/pzfilereader/parserutils/BXParserTest.java =================================================================== --- trunk/PZFileReader/src/test/java/net/sf/pzfilereader/parserutils/BXParserTest.java 2006-10-31 20:02:48 UTC (rev 163) +++ trunk/PZFileReader/src/test/java/net/sf/pzfilereader/parserutils/BXParserTest.java 2006-10-31 20:47:02 UTC (rev 164) @@ -103,12 +103,12 @@ public void testSomeExtremeCases() { check(null, ',', '\"', new String[] {}); check("a", ',', '\"', new String[] { "a" }); - check("", ',', '\"', new String[] { null }); - check(" ", ',', '\"', new String[] { null }); - check(" ", ',', '\"', new String[] { null }); - check(",", ',', '\"', new String[] { null, null }); - check(",,", ',', '\"', new String[] { null, null, null }); - check(",a,", ',', '\"', new String[] { null, "a", null }); + check("", ',', '\"', new String[] { "" }); + check(" ", ',', '\"', new String[] { "" }); + check(" ", ',', '\"', new String[] { "" }); + check(",", ',', '\"', new String[] { "", "" }); + check(",,", ',', '\"', new String[] { "", "", "" }); + check(",a,", ',', '\"', new String[] { "", "a", "" }); check("\"a,b,c\"", ',', '\"', new String[] { "a,b,c" }); check("\"a,b\",\"c\"", ',', '\"', new String[] { "a,b", "c" }); @@ -126,8 +126,8 @@ check(" a, b ,c ", ',', '\"', new String[] { "a", "b", "c" }); check("\"a\", b , \"c\"", ',', '\"', new String[] { "a", "b", "c" }); - check("\"\",,,,\"last one\"", ',', '\"', new String[] { "", null, null, null, "last one" }); - check("\"first\",\"second\",", ',', '\"', new String[] { "first", "second", null }); + check("\"\",,,,\"last one\"", ',', '\"', new String[] { "", "", "", "", "last one" }); + check("\"first\",\"second\",", ',', '\"', new String[] { "first", "second", "" }); check("\" a,b,c\"", ',', '\"', new String[] { " a,b,c" }); check("\" a,b,c\",d", ',', '\"', new String[] { " a,b,c", "d" }); check("\"a, b,\"\"c\"", ',', '\"', new String[] { "a, b,\"c" }); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <zep...@us...> - 2006-11-27 13:22:41
|
Revision: 206 http://svn.sourceforge.net/pzfilereader/?rev=206&view=rev Author: zepernick Date: 2006-11-27 05:22:39 -0800 (Mon, 27 Nov 2006) Log Message: ----------- renamed IDataSet to DataSet Modified Paths: -------------- trunk/PZFileReader/src/main/java/net/sf/pzfilereader/AbstractDelimiterPZParser.java trunk/PZFileReader/src/main/java/net/sf/pzfilereader/AbstractFixedLengthPZParser.java trunk/PZFileReader/src/main/java/net/sf/pzfilereader/AbstractPZParser.java trunk/PZFileReader/src/main/java/net/sf/pzfilereader/DBFixedLengthPZParser.java trunk/PZFileReader/src/main/java/net/sf/pzfilereader/DefaultDataSet.java trunk/PZFileReader/src/main/java/net/sf/pzfilereader/PZParser.java trunk/PZFileReader/src/main/java/net/sf/pzfilereader/util/ExcelTransformer.java trunk/PZFileReader/src/test/java/net/sf/pzfilereader/columninfile/DelimitedColumnNamesInFile.java trunk/PZFileReader/src/test/java/net/sf/pzfilereader/columninfile/DelimitedColumnNamesInFileTest.java trunk/PZFileReader/src/test/java/net/sf/pzfilereader/delim/tab/TabDelimited.java trunk/PZFileReader/src/test/java/net/sf/pzfilereader/delim/tab/TabDelimitedTest.java Added Paths: ----------- trunk/PZFileReader/src/main/java/net/sf/pzfilereader/DataSet.java Removed Paths: ------------- trunk/PZFileReader/src/main/java/net/sf/pzfilereader/IDataSet.java Modified: trunk/PZFileReader/src/main/java/net/sf/pzfilereader/AbstractDelimiterPZParser.java =================================================================== --- trunk/PZFileReader/src/main/java/net/sf/pzfilereader/AbstractDelimiterPZParser.java 2006-11-26 14:01:12 UTC (rev 205) +++ trunk/PZFileReader/src/main/java/net/sf/pzfilereader/AbstractDelimiterPZParser.java 2006-11-27 13:22:39 UTC (rev 206) @@ -81,7 +81,7 @@ this.ignoreFirstRecord = ignoreFirstRecord; } - public IDataSet doParse() { + public DataSet doParse() { try { if (getDataSourceStream() != null) { return doDelimitedFile(getDataSourceStream(), getDelimiter(), getQualifier(), isIgnoreFirstRecord(), @@ -144,7 +144,7 @@ * puts together the dataset for a DELIMITED file. This is used for PZ XML * mappings, and SQL table mappings */ - private IDataSet doDelimitedFile(final InputStream dataSource, final char delimiter, final char qualifier, + private DataSet doDelimitedFile(final InputStream dataSource, final char delimiter, final char qualifier, final boolean ignoreFirstRecord, final boolean createMDFromFile) throws IOException, Exception { if (dataSource == null) { throw new NullPointerException("dataSource is null"); Modified: trunk/PZFileReader/src/main/java/net/sf/pzfilereader/AbstractFixedLengthPZParser.java =================================================================== --- trunk/PZFileReader/src/main/java/net/sf/pzfilereader/AbstractFixedLengthPZParser.java 2006-11-26 14:01:12 UTC (rev 205) +++ trunk/PZFileReader/src/main/java/net/sf/pzfilereader/AbstractFixedLengthPZParser.java 2006-11-27 13:22:39 UTC (rev 206) @@ -70,7 +70,7 @@ super(dataSourceStream); } - public IDataSet doParse() { + public DataSet doParse() { try { if (getDataSourceStream() != null) { return doFixedLengthFile(getDataSourceStream()); @@ -105,7 +105,7 @@ * puts together the dataset for fixed length file. This is used for PZ XML * mappings, and SQL table mappings */ - private IDataSet doFixedLengthFile(final InputStream dataSource) throws IOException { + private DataSet doFixedLengthFile(final InputStream dataSource) throws IOException { InputStreamReader isr = null; BufferedReader br = null; Modified: trunk/PZFileReader/src/main/java/net/sf/pzfilereader/AbstractPZParser.java =================================================================== --- trunk/PZFileReader/src/main/java/net/sf/pzfilereader/AbstractPZParser.java 2006-11-26 14:01:12 UTC (rev 205) +++ trunk/PZFileReader/src/main/java/net/sf/pzfilereader/AbstractPZParser.java 2006-11-27 13:22:39 UTC (rev 206) @@ -92,14 +92,14 @@ this.handlingShortLines = handleShortLines; } - public final IDataSet parse() { + public final DataSet parse() { if (!initialised) { init(); } return doParse(); } - protected abstract IDataSet doParse(); + protected abstract DataSet doParse(); protected abstract void init(); Modified: trunk/PZFileReader/src/main/java/net/sf/pzfilereader/DBFixedLengthPZParser.java =================================================================== --- trunk/PZFileReader/src/main/java/net/sf/pzfilereader/DBFixedLengthPZParser.java 2006-11-26 14:01:12 UTC (rev 205) +++ trunk/PZFileReader/src/main/java/net/sf/pzfilereader/DBFixedLengthPZParser.java 2006-11-27 13:22:39 UTC (rev 206) @@ -124,7 +124,7 @@ } } - public IDataSet doParse() { + public DataSet doParse() { return null; } } Copied: trunk/PZFileReader/src/main/java/net/sf/pzfilereader/DataSet.java (from rev 205, trunk/PZFileReader/src/main/java/net/sf/pzfilereader/IDataSet.java) =================================================================== --- trunk/PZFileReader/src/main/java/net/sf/pzfilereader/DataSet.java (rev 0) +++ trunk/PZFileReader/src/main/java/net/sf/pzfilereader/DataSet.java 2006-11-27 13:22:39 UTC (rev 206) @@ -0,0 +1,284 @@ +/* + * ObjectLab, http://www.objectlab.co.uk/open is supporting PZFileReader. + * + * Based in London, we are world leaders in the design and development + * of bespoke applications for the securities financing markets. + * + * <a href="http://www.objectlab.co.uk/open">Click here to learn more</a> + * ___ _ _ _ _ _ + * / _ \| |__ (_) ___ ___| |_| | __ _| |__ + * | | | | '_ \| |/ _ \/ __| __| | / _` | '_ \ + * | |_| | |_) | | __/ (__| |_| |__| (_| | |_) | + * \___/|_.__// |\___|\___|\__|_____\__,_|_.__/ + * |__/ + * + * www.ObjectLab.co.uk + * + * $Id: ColorProvider.java 74 2006-10-24 22:19:05Z benoitx $ + * + * Copyright 2006 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package net.sf.pzfilereader; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.List; +import java.util.Properties; + +import net.sf.pzfilereader.ordering.OrderBy; + +public interface DataSet { + + /** + * Goes to the top of the data set. This will put the pointer one record + * before the first in the set. Next() will have to be called to get the + * first record after this call. + */ + void goTop(); + + /** + * Goes to the last record in the dataset + */ + void goBottom(); + + /** + * Moves to the next record in the set. Returns true if move was a success, + * false if not + * + * @return boolean + */ + boolean next(); + + /** + * Moves back to the previous record in the set return true if move was a + * success, false if not + * + * @return boolean + */ + boolean previous(); + + /** + * Returns the string value of a specified column + * + * @param column - + * Name of the column + * @exception NoSuchElementException + * @return String + */ + String getString(final String column); + + /** + * Returns the double value of a specified column + * + * @param column - + * Name of the column + * @exception NoSuchElementException + * @exception NumberFormatException + * @return double + */ + double getDouble(final String column); + + /** + * Returns the interger value of a specified column + * + * @param column - + * Name of the column + * @exception NoSuchElementException + * @exception NumberFormatException + * @return double + */ + int getInt(final String column); + + /** + * Returns the date value of a specified column. This assumes the date is in + * yyyyMMdd. If your date is not in this format, see + * getDate(String,SimpleDateFormat) + * + * @param column - + * Name of the column + * @exception ParseException + * @return Date + */ + Date getDate(final String column) throws ParseException; + + /** + * Returns the date value of a specified column. This should be used if the + * date is NOT in yyyyMMdd format. The SimpleDateFormat object will specify + * what kind of format the date is in. + * + * @param column - + * Name of the column + * @param sdf - + * SimpleDateFormat of the date + * @exception ParseException + * @see java.text.SimpleDateFormat + * @return Date + */ + Date getDate(final String column, final SimpleDateFormat sdf) throws ParseException; + + + /** + * Returns the value of the column with the type of object + * specified + * + * @param column + * Name of the column + * @param classToConvertTo + * Class type to convert to + * @return Object + * Value of the column in the specified object + */ + Object getObject(final String column, final Class classToConvertTo); + + /** + * Returns a String array of column names in the DataSet. This will assume + * 'detail' <RECORD> ID. + * + * @return String[] + */ + String[] getColumns(); + + /** + * Returns a String array of column names in the DataSet for a given + * <RECORD> id + * + * @param recordID + * @return String[] + */ + String[] getColumns(final String recordID); + + /** + * Returns the line number the pointer is on. These are the actual line + * numbers from the flat file, before any sorting. + * + * @exception NoSuchElementException + * @exception NumberFormatException + * @return int + */ + int getRowNo(); + + /** + * Returns A Collection Of DataErrors that happened during processing + * + * @return Vector + */ + List getErrors(); + + /** + * Removes a row from the dataset. Once the row is removed the pointer will + * be sitting on the record previous to the deleted row. + */ + void remove(); + + /** + * Returns the index the pointer is on for the array + * + * @return int + */ + int getIndex(); + + /** + * Returns the total number of rows parsed in from the file + * + * + * @return int - Row Count + */ + int getRowCount(); + + /** + * Returns total number of records which contained a parse error in the + * file. + * + * @return int - Record Error Count + */ + int getErrorCount(); + + /** + * Returns true or false as to whether or not the line number contains an + * error. The import will skip the line if it contains an error and it will + * not be processed + * + * @param lineNo - + * int line number + * @return boolean + */ + boolean isAnError(final int lineNo); + + /** + * Orders the data by column(s) specified. This will reposition the cursor + * to the top of the DataSet when executed. This is currently not supported + * when specying <RECORD> elements in the mapping. An exception will be + * thrown if this situation occurs + * + * @param ob - + * OrderBy object + * @exception Exception + * @see net.sf.pzfilereader.ordering.OrderBy + * @see net.sf.pzfilereader.ordering.OrderColumn + */ + void orderRows(final OrderBy ob) throws Exception; + + List getRows(); + + /** + * Sets data in the DataSet to lowercase + */ + void setLowerCase(); + + /** + * Sets data in the DataSet to uppercase + */ + void setUpperCase(); + + /** + * Checks to see if the row has the given <RECORD> id + * + * @param recordID + * @return boolean + */ + boolean isRecordID(final String recordID); + + /** + * Sets the absolute position of the record pointer + * + * @param localPointer - + * int + * @exception IndexOutOfBoundsException + */ + void absolute(final int localPointer); + + /** + * Setting this to True will parse text as is and throw a + * NumberFormatException. Setting to false, which is the default, will + * remove any non numeric charcter from the field. The remaining numeric + * chars's will be returned. If it is an empty string,or there are no + * numeric chars, 0 will be returned for getInt() and getDouble() + * + * @param strictNumericParse + * The strictNumericParse to set. + */ + void setStrictNumericParse(final boolean strictNumericParse); + + /** + * Sets the properties from the pzconvert.properties file. + * This file specifies the PZConverter implementation to use + * for a particular class + * + * @param props + * Property mapping for String to Object conversion + */ + void setPZConvertProps(Properties props); +} \ No newline at end of file Modified: trunk/PZFileReader/src/main/java/net/sf/pzfilereader/DefaultDataSet.java =================================================================== --- trunk/PZFileReader/src/main/java/net/sf/pzfilereader/DefaultDataSet.java 2006-11-26 14:01:12 UTC (rev 205) +++ trunk/PZFileReader/src/main/java/net/sf/pzfilereader/DefaultDataSet.java 2006-11-27 13:22:39 UTC (rev 206) @@ -54,7 +54,7 @@ * @author xhensevb * */ -public class DefaultDataSet implements IDataSet { +public class DefaultDataSet implements DataSet { private final List rows = new ArrayList(); private final List errors = new ArrayList(); Deleted: trunk/PZFileReader/src/main/java/net/sf/pzfilereader/IDataSet.java =================================================================== --- trunk/PZFileReader/src/main/java/net/sf/pzfilereader/IDataSet.java 2006-11-26 14:01:12 UTC (rev 205) +++ trunk/PZFileReader/src/main/java/net/sf/pzfilereader/IDataSet.java 2006-11-27 13:22:39 UTC (rev 206) @@ -1,284 +0,0 @@ -/* - * ObjectLab, http://www.objectlab.co.uk/open is supporting PZFileReader. - * - * Based in London, we are world leaders in the design and development - * of bespoke applications for the securities financing markets. - * - * <a href="http://www.objectlab.co.uk/open">Click here to learn more</a> - * ___ _ _ _ _ _ - * / _ \| |__ (_) ___ ___| |_| | __ _| |__ - * | | | | '_ \| |/ _ \/ __| __| | / _` | '_ \ - * | |_| | |_) | | __/ (__| |_| |__| (_| | |_) | - * \___/|_.__// |\___|\___|\__|_____\__,_|_.__/ - * |__/ - * - * www.ObjectLab.co.uk - * - * $Id: ColorProvider.java 74 2006-10-24 22:19:05Z benoitx $ - * - * Copyright 2006 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package net.sf.pzfilereader; - -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.Date; -import java.util.List; -import java.util.Properties; - -import net.sf.pzfilereader.ordering.OrderBy; - -public interface IDataSet { - - /** - * Goes to the top of the data set. This will put the pointer one record - * before the first in the set. Next() will have to be called to get the - * first record after this call. - */ - void goTop(); - - /** - * Goes to the last record in the dataset - */ - void goBottom(); - - /** - * Moves to the next record in the set. Returns true if move was a success, - * false if not - * - * @return boolean - */ - boolean next(); - - /** - * Moves back to the previous record in the set return true if move was a - * success, false if not - * - * @return boolean - */ - boolean previous(); - - /** - * Returns the string value of a specified column - * - * @param column - - * Name of the column - * @exception NoSuchElementException - * @return String - */ - String getString(final String column); - - /** - * Returns the double value of a specified column - * - * @param column - - * Name of the column - * @exception NoSuchElementException - * @exception NumberFormatException - * @return double - */ - double getDouble(final String column); - - /** - * Returns the interger value of a specified column - * - * @param column - - * Name of the column - * @exception NoSuchElementException - * @exception NumberFormatException - * @return double - */ - int getInt(final String column); - - /** - * Returns the date value of a specified column. This assumes the date is in - * yyyyMMdd. If your date is not in this format, see - * getDate(String,SimpleDateFormat) - * - * @param column - - * Name of the column - * @exception ParseException - * @return Date - */ - Date getDate(final String column) throws ParseException; - - /** - * Returns the date value of a specified column. This should be used if the - * date is NOT in yyyyMMdd format. The SimpleDateFormat object will specify - * what kind of format the date is in. - * - * @param column - - * Name of the column - * @param sdf - - * SimpleDateFormat of the date - * @exception ParseException - * @see java.text.SimpleDateFormat - * @return Date - */ - Date getDate(final String column, final SimpleDateFormat sdf) throws ParseException; - - - /** - * Returns the value of the column with the type of object - * specified - * - * @param column - * Name of the column - * @param classToConvertTo - * Class type to convert to - * @return Object - * Value of the column in the specified object - */ - Object getObject(final String column, final Class classToConvertTo); - - /** - * Returns a String array of column names in the DataSet. This will assume - * 'detail' <RECORD> ID. - * - * @return String[] - */ - String[] getColumns(); - - /** - * Returns a String array of column names in the DataSet for a given - * <RECORD> id - * - * @param recordID - * @return String[] - */ - String[] getColumns(final String recordID); - - /** - * Returns the line number the pointer is on. These are the actual line - * numbers from the flat file, before any sorting. - * - * @exception NoSuchElementException - * @exception NumberFormatException - * @return int - */ - int getRowNo(); - - /** - * Returns A Collection Of DataErrors that happened during processing - * - * @return Vector - */ - List getErrors(); - - /** - * Removes a row from the dataset. Once the row is removed the pointer will - * be sitting on the record previous to the deleted row. - */ - void remove(); - - /** - * Returns the index the pointer is on for the array - * - * @return int - */ - int getIndex(); - - /** - * Returns the total number of rows parsed in from the file - * - * - * @return int - Row Count - */ - int getRowCount(); - - /** - * Returns total number of records which contained a parse error in the - * file. - * - * @return int - Record Error Count - */ - int getErrorCount(); - - /** - * Returns true or false as to whether or not the line number contains an - * error. The import will skip the line if it contains an error and it will - * not be processed - * - * @param lineNo - - * int line number - * @return boolean - */ - boolean isAnError(final int lineNo); - - /** - * Orders the data by column(s) specified. This will reposition the cursor - * to the top of the DataSet when executed. This is currently not supported - * when specying <RECORD> elements in the mapping. An exception will be - * thrown if this situation occurs - * - * @param ob - - * OrderBy object - * @exception Exception - * @see net.sf.pzfilereader.ordering.OrderBy - * @see net.sf.pzfilereader.ordering.OrderColumn - */ - void orderRows(final OrderBy ob) throws Exception; - - List getRows(); - - /** - * Sets data in the DataSet to lowercase - */ - void setLowerCase(); - - /** - * Sets data in the DataSet to uppercase - */ - void setUpperCase(); - - /** - * Checks to see if the row has the given <RECORD> id - * - * @param recordID - * @return boolean - */ - boolean isRecordID(final String recordID); - - /** - * Sets the absolute position of the record pointer - * - * @param localPointer - - * int - * @exception IndexOutOfBoundsException - */ - void absolute(final int localPointer); - - /** - * Setting this to True will parse text as is and throw a - * NumberFormatException. Setting to false, which is the default, will - * remove any non numeric charcter from the field. The remaining numeric - * chars's will be returned. If it is an empty string,or there are no - * numeric chars, 0 will be returned for getInt() and getDouble() - * - * @param strictNumericParse - * The strictNumericParse to set. - */ - void setStrictNumericParse(final boolean strictNumericParse); - - /** - * Sets the properties from the pzconvert.properties file. - * This file specifies the PZConverter implementation to use - * for a particular class - * - * @param props - * Property mapping for String to Object conversion - */ - void setPZConvertProps(Properties props); -} \ No newline at end of file Modified: trunk/PZFileReader/src/main/java/net/sf/pzfilereader/PZParser.java =================================================================== --- trunk/PZFileReader/src/main/java/net/sf/pzfilereader/PZParser.java 2006-11-26 14:01:12 UTC (rev 205) +++ trunk/PZFileReader/src/main/java/net/sf/pzfilereader/PZParser.java 2006-11-27 13:22:39 UTC (rev 206) @@ -46,7 +46,7 @@ * * @return the data set resulting from parsing */ - IDataSet parse(); + DataSet parse(); /** * @return true, lines with less columns then the amount of column headers Modified: trunk/PZFileReader/src/main/java/net/sf/pzfilereader/util/ExcelTransformer.java =================================================================== --- trunk/PZFileReader/src/main/java/net/sf/pzfilereader/util/ExcelTransformer.java 2006-11-26 14:01:12 UTC (rev 205) +++ trunk/PZFileReader/src/main/java/net/sf/pzfilereader/util/ExcelTransformer.java 2006-11-27 13:22:39 UTC (rev 206) @@ -22,7 +22,7 @@ import jxl.write.WritableFont; import jxl.write.WritableSheet; import jxl.write.WritableWorkbook; -import net.sf.pzfilereader.IDataSet; +import net.sf.pzfilereader.DataSet; /** * @author Paul Zepernick @@ -31,7 +31,7 @@ */ public class ExcelTransformer { - private IDataSet ds; + private DataSet ds; private File xlsFile; @@ -43,7 +43,7 @@ * @param xlsFile * Excel file to be created */ - public ExcelTransformer(final IDataSet ds, final File xlsFile) { + public ExcelTransformer(final DataSet ds, final File xlsFile) { this.ds = ds; this.xlsFile = xlsFile; } Modified: trunk/PZFileReader/src/test/java/net/sf/pzfilereader/columninfile/DelimitedColumnNamesInFile.java =================================================================== --- trunk/PZFileReader/src/test/java/net/sf/pzfilereader/columninfile/DelimitedColumnNamesInFile.java 2006-11-26 14:01:12 UTC (rev 205) +++ trunk/PZFileReader/src/test/java/net/sf/pzfilereader/columninfile/DelimitedColumnNamesInFile.java 2006-11-27 13:22:39 UTC (rev 206) @@ -9,7 +9,7 @@ import net.sf.pzfilereader.DataError; import net.sf.pzfilereader.DefaultPZParserFactory; -import net.sf.pzfilereader.IDataSet; +import net.sf.pzfilereader.DataSet; import net.sf.pzfilereader.PZParser; import net.sf.pzfilereader.ordering.OrderBy; import net.sf.pzfilereader.ordering.OrderColumn; @@ -29,7 +29,7 @@ // text qualified by double quotes // ignore first record final PZParser pzparser = DefaultPZParserFactory.getInstance().newDelimitedParser(new File("net/sf/pzfilereader/columninfile/PEOPLE-CommaDelimitedWithQualifier.txt"), ',', '\"'); - final IDataSet ds = pzparser.parse(); + final DataSet ds = pzparser.parse(); // re order the data set by last name orderby = new OrderBy(); @@ -59,7 +59,7 @@ // used for Junit test - public IDataSet getDsForTest() throws Exception { + public DataSet getDsForTest() throws Exception { final PZParser parser = DefaultPZParserFactory.getInstance().newDelimitedParser( new File("src/test/java/net/sf/pzfilereader/columninfile/PEOPLE-CommaDelimitedWithQualifier.txt"), ',', '\"'); Modified: trunk/PZFileReader/src/test/java/net/sf/pzfilereader/columninfile/DelimitedColumnNamesInFileTest.java =================================================================== --- trunk/PZFileReader/src/test/java/net/sf/pzfilereader/columninfile/DelimitedColumnNamesInFileTest.java 2006-11-26 14:01:12 UTC (rev 205) +++ trunk/PZFileReader/src/test/java/net/sf/pzfilereader/columninfile/DelimitedColumnNamesInFileTest.java 2006-11-27 13:22:39 UTC (rev 206) @@ -7,7 +7,7 @@ package net.sf.pzfilereader.columninfile; import junit.framework.TestCase; -import net.sf.pzfilereader.IDataSet; +import net.sf.pzfilereader.DataSet; /** * @author zepernick @@ -22,7 +22,7 @@ // tests to make sure we have 0 errors public void testErrorCount() { - IDataSet ds = null; + DataSet ds = null; try { final DelimitedColumnNamesInFile testDelimted = new DelimitedColumnNamesInFile(); @@ -41,7 +41,7 @@ // test to make sure we parsed the correct number // of rows in the file public void testRowCount() { - IDataSet ds = null; + DataSet ds = null; try { final DelimitedColumnNamesInFile testDelimted = new DelimitedColumnNamesInFile(); @@ -59,7 +59,7 @@ // test to make sure we have the right number of column names from the file public void testColumnNameCount() { - IDataSet ds = null; + DataSet ds = null; try { final DelimitedColumnNamesInFile testDelimted = new DelimitedColumnNamesInFile(); Modified: trunk/PZFileReader/src/test/java/net/sf/pzfilereader/delim/tab/TabDelimited.java =================================================================== --- trunk/PZFileReader/src/test/java/net/sf/pzfilereader/delim/tab/TabDelimited.java 2006-11-26 14:01:12 UTC (rev 205) +++ trunk/PZFileReader/src/test/java/net/sf/pzfilereader/delim/tab/TabDelimited.java 2006-11-27 13:22:39 UTC (rev 206) @@ -9,7 +9,7 @@ import net.sf.pzfilereader.DataError; import net.sf.pzfilereader.DefaultPZParserFactory; -import net.sf.pzfilereader.IDataSet; +import net.sf.pzfilereader.DataSet; import net.sf.pzfilereader.PZParser; /** @@ -28,7 +28,7 @@ // ignore first record tmpFile = new File("net/sf/pzfilereader/delim/tab/PEOPLE-TabDelimitedWithQualifier.txt"); final PZParser pzparser = DefaultPZParserFactory.getInstance().newDelimitedParser(tmpFile, '\t', '\"'); - final IDataSet ds = pzparser.parse(); + final DataSet ds = pzparser.parse(); // re order the data set by last name /* @@ -59,7 +59,7 @@ // used for Junit test - public IDataSet getDsForTest() throws Exception { + public DataSet getDsForTest() throws Exception { final PZParser parser = DefaultPZParserFactory.getInstance().newDelimitedParser( new File("src/test/java/net/sf/pzfilereader/delim/tab/PEOPLE-TabDelimitedWithQualifier.txt"), '\t', '\"'); Modified: trunk/PZFileReader/src/test/java/net/sf/pzfilereader/delim/tab/TabDelimitedTest.java =================================================================== --- trunk/PZFileReader/src/test/java/net/sf/pzfilereader/delim/tab/TabDelimitedTest.java 2006-11-26 14:01:12 UTC (rev 205) +++ trunk/PZFileReader/src/test/java/net/sf/pzfilereader/delim/tab/TabDelimitedTest.java 2006-11-27 13:22:39 UTC (rev 206) @@ -7,7 +7,7 @@ package net.sf.pzfilereader.delim.tab; import junit.framework.TestCase; -import net.sf.pzfilereader.IDataSet; +import net.sf.pzfilereader.DataSet; /** * @author zepernick @@ -22,7 +22,7 @@ // tests to make sure we have 0 errors public void testErrorCount() { - IDataSet ds = null; + DataSet ds = null; try { final TabDelimited testTab = new TabDelimited(); @@ -41,7 +41,7 @@ // test to make sure we parsed the correct number // of rows in the file public void testRowCount() { - IDataSet ds = null; + DataSet ds = null; try { final TabDelimited testTab = new TabDelimited(); @@ -59,7 +59,7 @@ // test to make sure we have the right number of column names from the file public void testColumnNameCount() { - IDataSet ds = null; + DataSet ds = null; try { final TabDelimited testTab = new TabDelimited(); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <be...@us...> - 2007-05-24 22:51:50
|
Revision: 310 http://svn.sourceforge.net/pzfilereader/?rev=310&view=rev Author: benoitx Date: 2007-05-24 15:51:48 -0700 (Thu, 24 May 2007) Log Message: ----------- Created PZMetaData that removes the use of some magic keys in a map This also provides a bit more semantic in the use of meta data. The old code is simply commented out or deprecated for now. Modified Paths: -------------- trunk/PZFileReader/src/main/java/net/sf/pzfilereader/AbstractDelimiterPZParser.java trunk/PZFileReader/src/main/java/net/sf/pzfilereader/AbstractFixedLengthPZParser.java trunk/PZFileReader/src/main/java/net/sf/pzfilereader/AbstractPZParser.java trunk/PZFileReader/src/main/java/net/sf/pzfilereader/DBDelimiterPZParser.java trunk/PZFileReader/src/main/java/net/sf/pzfilereader/DBFixedLengthPZParser.java trunk/PZFileReader/src/main/java/net/sf/pzfilereader/DataSet.java trunk/PZFileReader/src/main/java/net/sf/pzfilereader/DefaultDataSet.java trunk/PZFileReader/src/main/java/net/sf/pzfilereader/DefaultPZParserFactory.java trunk/PZFileReader/src/main/java/net/sf/pzfilereader/DelimiterPZParser.java trunk/PZFileReader/src/main/java/net/sf/pzfilereader/FixedLengthPZParser.java trunk/PZFileReader/src/main/java/net/sf/pzfilereader/PZParser.java trunk/PZFileReader/src/main/java/net/sf/pzfilereader/PZParserFactory.java trunk/PZFileReader/src/main/java/net/sf/pzfilereader/brparse/BuffReaderDelimPZParser.java trunk/PZFileReader/src/main/java/net/sf/pzfilereader/brparse/BuffReaderFixedPZParser.java trunk/PZFileReader/src/main/java/net/sf/pzfilereader/brparse/BuffReaderPZDataSet.java trunk/PZFileReader/src/main/java/net/sf/pzfilereader/brparse/BuffReaderPZParseFactory.java trunk/PZFileReader/src/main/java/net/sf/pzfilereader/converter/ConvertBigDecimal.java trunk/PZFileReader/src/main/java/net/sf/pzfilereader/converter/ConvertDouble.java trunk/PZFileReader/src/main/java/net/sf/pzfilereader/converter/ConvertInteger.java trunk/PZFileReader/src/main/java/net/sf/pzfilereader/converter/PZConvertException.java trunk/PZFileReader/src/main/java/net/sf/pzfilereader/converter/PZConverter.java trunk/PZFileReader/src/main/java/net/sf/pzfilereader/ordering/OrderBy.java trunk/PZFileReader/src/main/java/net/sf/pzfilereader/structure/Row.java trunk/PZFileReader/src/main/java/net/sf/pzfilereader/util/ExcelTransformer.java trunk/PZFileReader/src/main/java/net/sf/pzfilereader/util/FixedWidthParserUtils.java trunk/PZFileReader/src/main/java/net/sf/pzfilereader/util/PZConstants.java trunk/PZFileReader/src/main/java/net/sf/pzfilereader/util/ParserUtils.java trunk/PZFileReader/src/main/java/net/sf/pzfilereader/xml/PZMapParser.java trunk/PZFileReader/src/main/java/net/sf/pzfilereader/xml/XMLRecordElement.java trunk/PZFileReader/src/test/java/net/sf/pzfilereader/columninfile/DelimitedColumnNamesInFile.java trunk/PZFileReader/src/test/java/net/sf/pzfilereader/columninfile/DelimitedColumnNamesInFileTest.java trunk/PZFileReader/src/test/java/net/sf/pzfilereader/delim/tab/TabDelimited.java trunk/PZFileReader/src/test/java/net/sf/pzfilereader/delim/tab/TabDelimitedTest.java trunk/PZFileReader/src/test/java/net/sf/pzfilereader/parserutils/FixedWidthParserUtilsTest.java trunk/PZFileReader/src/test/java/net/sf/pzfilereader/parserutils/ParserUtilsLTrimTest.java trunk/PZFileReader/src/test/java/net/sf/pzfilereader/parserutils/ParserUtilsSplitLineTest.java trunk/PZFileReader/src/test/java/net/sf/pzfilereader/parserutils/ParserUtilsTest.java trunk/PZFileReader/src/test/java/net/sf/pzfilereader/pzparser/PZParserOptsTest.java Modified: trunk/PZFileReader/src/main/java/net/sf/pzfilereader/AbstractDelimiterPZParser.java =================================================================== --- trunk/PZFileReader/src/main/java/net/sf/pzfilereader/AbstractDelimiterPZParser.java 2007-05-01 09:13:20 UTC (rev 309) +++ trunk/PZFileReader/src/main/java/net/sf/pzfilereader/AbstractDelimiterPZParser.java 2007-05-24 22:51:48 UTC (rev 310) @@ -33,20 +33,17 @@ package net.sf.pzfilereader; import java.io.BufferedReader; -import java.io.File; import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; import java.io.Reader; import java.util.List; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import net.sf.pzfilereader.structure.Row; import net.sf.pzfilereader.util.PZConstants; import net.sf.pzfilereader.util.ParserUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + /** * @author xhensevb * @author zepernick @@ -54,50 +51,45 @@ */ public abstract class AbstractDelimiterPZParser extends AbstractPZParser { private char delimiter = 0; - private char qualifier = 0; - private boolean ignoreFirstRecord = false; - private int lineCount = 0; - + private final Logger logger = LoggerFactory.getLogger(AbstractDelimiterPZParser.class); - + /*public AbstractDelimiterPZParser(final InputStream dataSourceStream, final String dataDefinition, final char delimiter, - final char qualifier, final boolean ignoreFirstRecord) { - super(dataSourceStream, dataDefinition); - this.delimiter = delimiter; - this.qualifier = qualifier; - this.ignoreFirstRecord = ignoreFirstRecord; - } + final char qualifier, final boolean ignoreFirstRecord) { + super(dataSourceStream, dataDefinition); + this.delimiter = delimiter; + this.qualifier = qualifier; + this.ignoreFirstRecord = ignoreFirstRecord; + } - public AbstractDelimiterPZParser(final File dataSource, final char delimiter, final char qualifier, - final boolean ignoreFirstRecord) { - super(dataSource); - this.delimiter = delimiter; - this.qualifier = qualifier; - this.ignoreFirstRecord = ignoreFirstRecord; - } + public AbstractDelimiterPZParser(final File dataSource, final char delimiter, final char qualifier, + final boolean ignoreFirstRecord) { + super(dataSource); + this.delimiter = delimiter; + this.qualifier = qualifier; + this.ignoreFirstRecord = ignoreFirstRecord; + } - public AbstractDelimiterPZParser(final InputStream dataSourceStream, final char delimiter, final char qualifier, + public AbstractDelimiterPZParser(final InputStream dataSourceStream, final char delimiter, final char qualifier, + final boolean ignoreFirstRecord) { + super(dataSourceStream); + this.delimiter = delimiter; + this.qualifier = qualifier; + this.ignoreFirstRecord = ignoreFirstRecord; + }*/ + + public AbstractDelimiterPZParser(final Reader dataSourceReader, final String dataDefinition, final char delimiter, final char qualifier, final boolean ignoreFirstRecord) { - super(dataSourceStream); - this.delimiter = delimiter; - this.qualifier = qualifier; - this.ignoreFirstRecord = ignoreFirstRecord; - }*/ - - public AbstractDelimiterPZParser(final Reader dataSourceReader, final String dataDefinition, final char delimiter, - final char qualifier, final boolean ignoreFirstRecord) { super(dataSourceReader, dataDefinition); this.delimiter = delimiter; this.qualifier = qualifier; this.ignoreFirstRecord = ignoreFirstRecord; } - - - public AbstractDelimiterPZParser(final Reader dataSourceReader, final char delimiter, final char qualifier, - final boolean ignoreFirstRecord) { + + public AbstractDelimiterPZParser(final Reader dataSourceReader, final char delimiter, final char qualifier, final boolean ignoreFirstRecord) { super(dataSourceReader); this.delimiter = delimiter; this.qualifier = qualifier; @@ -108,22 +100,22 @@ try { lineCount = 0; return doDelimitedFile(getDataSourceReader(), shouldCreateMDFromFile()); - /* if (getDataSourceStream() != null) { - return doDelimitedFile(getDataSourceStream(), shouldCreateMDFromFile()); - } else { - InputStream stream = null; - try { - stream = ParserUtils.createInputStream(getDataSource()); - return doDelimitedFile(stream, shouldCreateMDFromFile()); - } finally { - if (stream != null) { - stream.close(); - } - } - }*/ + /* if (getDataSourceStream() != null) { + return doDelimitedFile(getDataSourceStream(), shouldCreateMDFromFile()); + } else { + InputStream stream = null; + try { + stream = ParserUtils.createInputStream(getDataSource()); + return doDelimitedFile(stream, shouldCreateMDFromFile()); + } finally { + if (stream != null) { + stream.close(); + } + } + }*/ } catch (final IOException e) { logger.error("error accessing/creating inputstream", e); - } + } return null; } @@ -156,7 +148,7 @@ protected int getLineCount() { return lineCount; } - + /* * This is the new version of doDelimitedFile using InputStrem instead of * File. This is more flexible especially it is working with WebStart. @@ -169,11 +161,12 @@ throw new NullPointerException("dataSource is null"); } BufferedReader br = null; - final DefaultDataSet ds = new DefaultDataSet(getColumnMD(), this); + // final DefaultDataSet ds = new DefaultDataSet(getColumnMD(), this); + final DefaultDataSet ds = new DefaultDataSet(getPzMetaData(), this); try { //gather the conversion properties ds.setPZConvertProps(ParserUtils.loadConvertProperties()); - + // get the total column count // columnCount = columnMD.size(); @@ -189,17 +182,21 @@ continue; } else if (!processedFirst && createMDFromFile) { processedFirst = true; - setColumnMD(ParserUtils.getColumnMDFromFile(line, delimiter, qualifier, this)); - ds.setColumnMD(getColumnMD()); + // setColumnMD(ParserUtils.getColumnMDFromFile(line, delimiter, qualifier, this)); + setPzMetaData(ParserUtils.getPZMetaDataFromFile(line, delimiter, qualifier, this)); + // ds.setColumnMD(getColumnMD()); + ds.setPzMetaData(getPzMetaData()); continue; } // column values List columns = ParserUtils.splitLine(line, getDelimiter(), getQualifier(), PZConstants.SPLITLINE_SIZE_INIT); - final String mdkey = ParserUtils.getCMDKeyForDelimitedFile(getColumnMD(), columns); - final List cmds = ParserUtils.getColumnMetaData(mdkey, getColumnMD()); + final String mdkey = ParserUtils.getCMDKeyForDelimitedFile(getPzMetaData(), columns); + // final String mdkey = ParserUtils.getCMDKeyForDelimitedFile(getColumnMD(), columns); + // final List cmds = ParserUtils.getColumnMetaData(mdkey, getColumnMD()); + final List cmds = ParserUtils.getColumnMetaData(mdkey, getPzMetaData()); final int columnCount = cmds.size(); - + if (columns.size() > columnCount) { // Incorrect record length on line log the error. Line // will not be included in the dataset log the error @@ -244,7 +241,7 @@ } return ds; } - + /** * Reads a record from a delimited file. This will account for records which * could span multiple lines. @@ -260,12 +257,11 @@ * Record from delimited file * */ - protected String fetchNextRecord(final BufferedReader br, final char qual, - final char delim) throws IOException{ + protected String fetchNextRecord(final BufferedReader br, final char qual, final char delim) throws IOException { String line = null; - StringBuffer lineData = new StringBuffer(); + final StringBuffer lineData = new StringBuffer(); boolean processingMultiLine = false; - + while ((line = br.readLine()) != null) { lineCount++; final String trimmed = line.trim(); @@ -344,15 +340,15 @@ // the data on the next line } } - + break; } - + if (line == null && lineData.length() == 0) { //eof return null; } - + return lineData.toString(); } } Modified: trunk/PZFileReader/src/main/java/net/sf/pzfilereader/AbstractFixedLengthPZParser.java =================================================================== --- trunk/PZFileReader/src/main/java/net/sf/pzfilereader/AbstractFixedLengthPZParser.java 2007-05-01 09:13:20 UTC (rev 309) +++ trunk/PZFileReader/src/main/java/net/sf/pzfilereader/AbstractFixedLengthPZParser.java 2007-05-24 22:51:48 UTC (rev 310) @@ -38,37 +38,37 @@ import java.util.List; import java.util.Map; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import net.sf.pzfilereader.structure.Row; import net.sf.pzfilereader.util.FixedWidthParserUtils; import net.sf.pzfilereader.util.PZConstants; import net.sf.pzfilereader.util.ParserUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + /** * @author xhensevb * */ public abstract class AbstractFixedLengthPZParser extends AbstractPZParser { private final Logger logger = LoggerFactory.getLogger(AbstractFixedLengthPZParser.class); - - /* protected AbstractFixedLengthPZParser(final File dataSource, final String dataDefinition) { - super(dataSource, dataDefinition); - } - protected AbstractFixedLengthPZParser(final File dataSource) { - super(dataSource); - } + /* protected AbstractFixedLengthPZParser(final File dataSource, final String dataDefinition) { + super(dataSource, dataDefinition); + } - protected AbstractFixedLengthPZParser(final InputStream dataSourceStream, final String dataDefinition) { - super(dataSourceStream, dataDefinition); - } + protected AbstractFixedLengthPZParser(final File dataSource) { + super(dataSource); + } - protected AbstractFixedLengthPZParser(final InputStream dataSourceStream) { - super(dataSourceStream); - }*/ - + protected AbstractFixedLengthPZParser(final InputStream dataSourceStream, final String dataDefinition) { + super(dataSourceStream, dataDefinition); + } + + protected AbstractFixedLengthPZParser(final InputStream dataSourceStream) { + super(dataSourceStream); + }*/ + protected AbstractFixedLengthPZParser(final Reader dataSourceReader, final String dataDefinition) { super(dataSourceReader, dataDefinition); } @@ -79,19 +79,19 @@ public DataSet doParse() { try { - /* if (getDataSourceStream() != null) { - return doFixedLengthFile(getDataSourceStream()); - } else { - InputStream stream; - stream = ParserUtils.createInputStream(getDataSource()); - try { - return doFixedLengthFile(stream); - } finally { - if (stream != null) { - stream.close(); - } - } - }*/ + /* if (getDataSourceStream() != null) { + return doFixedLengthFile(getDataSourceStream()); + } else { + InputStream stream; + stream = ParserUtils.createInputStream(getDataSource()); + try { + return doFixedLengthFile(stream); + } finally { + if (stream != null) { + stream.close(); + } + } + }*/ return doFixedLengthFile(getDataSourceReader()); } catch (final IOException e) { logger.error("error accessing/reading data", e); @@ -109,13 +109,15 @@ private DataSet doFixedLengthFile(final Reader dataSource) throws IOException { BufferedReader br = null; - final DefaultDataSet ds = new DefaultDataSet(getColumnMD(), this); + // final DefaultDataSet ds = new DefaultDataSet(getColumnMD(), this); + final DefaultDataSet ds = new DefaultDataSet(getPzMetaData(), this); try { //gather the conversion properties ds.setPZConvertProps(ParserUtils.loadConvertProperties()); - final Map recordLengths = ParserUtils.calculateRecordLengths(getColumnMD()); + // final Map recordLengths = ParserUtils.calculateRecordLengths(getColumnMD()); + final Map recordLengths = ParserUtils.calculateRecordLengths(getPzMetaData()); // Read in the flat file br = new BufferedReader(dataSource); @@ -131,7 +133,8 @@ continue; } - final String mdkey = FixedWidthParserUtils.getCMDKey(getColumnMD(), line); + final String mdkey = FixedWidthParserUtils.getCMDKey(getPzMetaData(), line); + // final String mdkey = FixedWidthParserUtils.getCMDKey(getColumnMD(), line); final int recordLength = ((Integer) recordLengths.get(mdkey)).intValue(); if (line.length() > recordLength) { @@ -156,8 +159,7 @@ 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); + addError(ds, "LINE TOO SHORT. LINE IS " + line.length() + " LONG. SHOULD BE " + recordLength, lineCount, 2); continue; } } @@ -166,7 +168,8 @@ final Row row = new Row(); row.setMdkey(mdkey.equals(PZConstants.DETAIL_ID) ? null : mdkey); // try - final List cmds = ParserUtils.getColumnMetaData(mdkey, getColumnMD()); + // final List cmds = ParserUtils.getColumnMetaData(mdkey, getColumnMD()); + final List cmds = ParserUtils.getColumnMetaData(mdkey, getPzMetaData()); row.addColumn(FixedWidthParserUtils.splitFixedText(cmds, line)); // to limit the memory use // Build the columns for the row Modified: trunk/PZFileReader/src/main/java/net/sf/pzfilereader/AbstractPZParser.java =================================================================== --- trunk/PZFileReader/src/main/java/net/sf/pzfilereader/AbstractPZParser.java 2007-05-01 09:13:20 UTC (rev 309) +++ trunk/PZFileReader/src/main/java/net/sf/pzfilereader/AbstractPZParser.java 2007-05-24 22:51:48 UTC (rev 310) @@ -36,10 +36,11 @@ import java.io.Reader; import java.util.ArrayList; import java.util.Iterator; -import java.util.LinkedHashMap; import java.util.List; -import java.util.Map; +import net.sf.pzfilereader.util.ParserUtils; +import net.sf.pzfilereader.xml.PZMetaData; + /** * @author xhensevb * @author zepernick @@ -48,52 +49,23 @@ public abstract class AbstractPZParser implements PZParser { private boolean handlingShortLines = false; - private boolean ignoreExtraColumns = false; - private boolean columnNamesCaseSensitive = false; - private boolean initialised = false; - private boolean ignoreParseWarnings = false; - private boolean nullEmptyStrings = false; /** Map of column metadata's */ - private Map columnMD = null; - + // private Map columnMD = null; + private PZMetaData pzMetaData = null; private String dataDefinition = null; - - // private InputStream dataSourceStream = null; - - // private File dataSource = null; - private Reader dataSourceReader = null; - - private List readersToClose = null; + private List readersToClose = null; - /*protected AbstractPZParser(final File dataSource) { - this.dataSourceReader = new FileReader(dataSource); - } - - protected AbstractPZParser(final InputStream dataSourceStream) { - this.dataSourceStream = dataSourceStream; - } - - protected AbstractPZParser(final File dataSource, final String dataDefinition) { - this.dataSource = dataSource; - this.dataDefinition = dataDefinition; - } - - protected AbstractPZParser(final InputStream dataSourceStream, final String dataDefinition) { - this.dataSourceStream = dataSourceStream; - this.dataDefinition = dataDefinition; - }*/ - protected AbstractPZParser(final Reader dataSourceReader) { this.dataSourceReader = dataSourceReader; } - + protected AbstractPZParser(final Reader dataSourceReader, final String dataDefinition) { this.dataSourceReader = dataSourceReader; this.dataDefinition = dataDefinition; @@ -116,13 +88,13 @@ public void setHandlingShortLines(final boolean handleShortLines) { this.handlingShortLines = handleShortLines; } - + public boolean isIgnoreExtraColumns() { return ignoreExtraColumns; } - - public void setIgnoreExtraColumns(boolean ignoreExtraColumns) { - this.ignoreExtraColumns = ignoreExtraColumns; + + public void setIgnoreExtraColumns(final boolean ignoreExtraColumns) { + this.ignoreExtraColumns = ignoreExtraColumns; } public final DataSet parse() { @@ -136,23 +108,25 @@ protected abstract void init(); - protected void setColumnMD(final Map map) { - columnMD = map; - } - + /** + * @deprecated + */ + // protected void setColumnMD(final Map map) { + // columnMD = map; + // } //this is used for backward compatability. We are instantiating Readers from //InputStream and File from previous versions. Close out any Readers in the //readersToClose list. This can be removed after we remove the deprecated methods - protected void closeReaders() throws IOException{ + protected void closeReaders() throws IOException { if (readersToClose != null) { final Iterator readersToCloseIt = readersToClose.iterator(); while (readersToCloseIt.hasNext()) { - final Reader r = (Reader)readersToCloseIt.next(); + final Reader r = (Reader) readersToCloseIt.next(); r.close(); } } } - + //adds a reader to the close list. the list will be processed after parsing is //completed. protected void addToCloseReaderList(final Reader r) { @@ -162,13 +136,22 @@ readersToClose.add(r); } - protected void addToColumnMD(final Object key, final Object value) { - if (columnMD == null) { - columnMD = new LinkedHashMap(); + protected void addToMetaData(final List columns) { + if (pzMetaData == null) { + pzMetaData = new PZMetaData(columns, ParserUtils.buidColumnIndexMap(columns, this)); + } else { + pzMetaData.setColumnsNames(columns); + pzMetaData.setColumnIndexMap(ParserUtils.buidColumnIndexMap(columns, this)); } - columnMD.put(key, value); } + // protected void addToColumnMD(final Object key, final Object value) { + // if (columnMD == null) { + // columnMD = new LinkedHashMap(); + // } + // columnMD.put(key, value); + // } + protected boolean isInitialised() { return initialised; } @@ -185,26 +168,25 @@ this.dataDefinition = dataDefinition; } - /* protected File getDataSource() { - return dataSource; - } + /* protected File getDataSource() { + return dataSource; + } - protected void setDataSource(final File dataSource) { - this.dataSource = dataSource; - } + protected void setDataSource(final File dataSource) { + this.dataSource = dataSource; + } - protected InputStream getDataSourceStream() { - return dataSourceStream; - } + protected InputStream getDataSourceStream() { + return dataSourceStream; + } - protected void setDataSourceStream(final InputStream dataSourceStream) { - this.dataSourceStream = dataSourceStream; - }*/ + protected void setDataSourceStream(final InputStream dataSourceStream) { + this.dataSourceStream = dataSourceStream; + }*/ - protected Map getColumnMD() { - return columnMD; - } - + // protected Map getColumnMD() { + // return columnMD; + // } /** * Adds a new error to this DataSet. These can be collected, and retreived * after processing @@ -235,32 +217,40 @@ /** * @param dataSourceReader the dataSourceReader to set */ - protected void setDataSourceReader(Reader dataSourceReader) { + protected void setDataSourceReader(final Reader dataSourceReader) { this.dataSourceReader = dataSourceReader; } - + public boolean isColumnNamesCaseSensitive() { return columnNamesCaseSensitive; } - - public void setColumnNamesCaseSensitive(boolean columnNamesCaseSensitive) { - this.columnNamesCaseSensitive = columnNamesCaseSensitive; + + public void setColumnNamesCaseSensitive(final boolean columnNamesCaseSensitive) { + this.columnNamesCaseSensitive = columnNamesCaseSensitive; } - + public boolean isIgnoreParseWarnings() { return ignoreParseWarnings; } - - public void setIgnoreParseWarnings(boolean ignoreParseWarnings) { - this.ignoreParseWarnings = ignoreParseWarnings; + + public void setIgnoreParseWarnings(final boolean ignoreParseWarnings) { + this.ignoreParseWarnings = ignoreParseWarnings; } - + public boolean isNullEmptyStrings() { return nullEmptyStrings; } - - public void setNullEmptyStrings(boolean nullEmptyStrings) { - this.nullEmptyStrings = nullEmptyStrings; + + public void setNullEmptyStrings(final boolean nullEmptyStrings) { + this.nullEmptyStrings = nullEmptyStrings; } + public PZMetaData getPzMetaData() { + return pzMetaData; + } + + public void setPzMetaData(final PZMetaData pzMap) { + this.pzMetaData = pzMap; + } + } Modified: trunk/PZFileReader/src/main/java/net/sf/pzfilereader/DBDelimiterPZParser.java =================================================================== --- trunk/PZFileReader/src/main/java/net/sf/pzfilereader/DBDelimiterPZParser.java 2007-05-01 09:13:20 UTC (rev 309) +++ trunk/PZFileReader/src/main/java/net/sf/pzfilereader/DBDelimiterPZParser.java 2007-05-24 22:51:48 UTC (rev 310) @@ -40,7 +40,6 @@ import java.sql.SQLException; import java.util.List; -import net.sf.pzfilereader.util.PZConstants; import net.sf.pzfilereader.util.ParserUtils; /** @@ -50,18 +49,18 @@ */ public class DBDelimiterPZParser extends AbstractDelimiterPZParser { private Connection con; - + private InputStream dataSourceStream; - public DBDelimiterPZParser(final Connection con, final InputStream dataSourceStream, final String dataDefinition, - final char delimiter, final char qualifier, final boolean ignoreFirstRecord) { + public DBDelimiterPZParser(final Connection con, final InputStream dataSourceStream, final String dataDefinition, final char delimiter, + final char qualifier, final boolean ignoreFirstRecord) { super(null, dataDefinition, delimiter, qualifier, ignoreFirstRecord); this.con = con; this.dataSourceStream = dataSourceStream; } - - public DBDelimiterPZParser(final Connection con, final Reader dataSourceReader, final String dataDefinition, - final char delimiter, final char qualifier, final boolean ignoreFirstRecord) { + + public DBDelimiterPZParser(final Connection con, final Reader dataSourceReader, final String dataDefinition, final char delimiter, + final char qualifier, final boolean ignoreFirstRecord) { super(dataSourceReader, dataDefinition, delimiter, qualifier, ignoreFirstRecord); this.con = con; } @@ -74,11 +73,12 @@ final Reader r = new InputStreamReader(dataSourceStream); setDataSourceReader(r); addToCloseReaderList(r); - } - + } + final List cmds = ParserUtils.buildMDFromSQLTable(con, getDataDefinition()); - addToColumnMD(PZConstants.DETAIL_ID, cmds); - addToColumnMD(PZConstants.COL_IDX, ParserUtils.buidColumnIndexMap(cmds, 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()); @@ -88,7 +88,7 @@ throw new InitialisationException(e); } catch (final FileNotFoundException e) { throw new InitialisationException(e); - } + } } protected boolean shouldCreateMDFromFile() { Modified: trunk/PZFileReader/src/main/java/net/sf/pzfilereader/DBFixedLengthPZParser.java =================================================================== --- trunk/PZFileReader/src/main/java/net/sf/pzfilereader/DBFixedLengthPZParser.java 2007-05-01 09:13:20 UTC (rev 309) +++ trunk/PZFileReader/src/main/java/net/sf/pzfilereader/DBFixedLengthPZParser.java 2007-05-24 22:51:48 UTC (rev 310) @@ -42,8 +42,6 @@ import java.sql.SQLException; import java.util.List; - -import net.sf.pzfilereader.util.PZConstants; import net.sf.pzfilereader.util.ParserUtils; /** @@ -53,12 +51,12 @@ */ public class DBFixedLengthPZParser extends AbstractFixedLengthPZParser { private Connection con; - + //this InputStream and file can be removed after support for //file and inputstream is removed from the parserfactory. The //methods have been deprecated..pz private InputStream dataSourceStream = null; - + private File dataSource = null; public DBFixedLengthPZParser(final Connection con, final InputStream dataSourceStream, final String dataDefinition) { @@ -79,7 +77,7 @@ super(dataSourceReader, dataDefinition); this.con = con; } - + protected void init() { try { //check to see if the user is using a File or InputStream. This is @@ -88,16 +86,17 @@ final Reader r = new InputStreamReader(dataSourceStream); setDataSourceReader(r); addToCloseReaderList(r); - } else if (dataSource != null){ + } else if (dataSource != null) { final Reader r = new FileReader(dataSource); setDataSourceReader(r); addToCloseReaderList(r); } - + final List cmds = ParserUtils.buildMDFromSQLTable(con, getDataDefinition()); + addToMetaData(cmds); - addToColumnMD(PZConstants.DETAIL_ID, cmds); - addToColumnMD(PZConstants.COL_IDX, ParserUtils.buidColumnIndexMap(cmds, this)); + // 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()); @@ -108,7 +107,7 @@ throw new InitialisationException(e); } catch (final FileNotFoundException e) { throw new InitialisationException(e); - } + } } public DataSet doParse() { Modified: trunk/PZFileReader/src/main/java/net/sf/pzfilereader/DataSet.java =================================================================== --- trunk/PZFileReader/src/main/java/net/sf/pzfilereader/DataSet.java 2007-05-01 09:13:20 UTC (rev 309) +++ trunk/PZFileReader/src/main/java/net/sf/pzfilereader/DataSet.java 2007-05-24 22:51:48 UTC (rev 310) @@ -36,6 +36,7 @@ import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; +import java.util.NoSuchElementException; import java.util.Properties; import net.sf.pzfilereader.ordering.OrderBy; @@ -132,8 +133,7 @@ * @return Date */ Date getDate(final String column, final SimpleDateFormat sdf) throws ParseException; - - + /** * Returns the value of the column with the type of object * specified @@ -145,7 +145,7 @@ * @return Object * Value of the column in the specified object */ - Object getObject(final String column, final Class classToConvertTo); + Object getObject(final String column, final Class classToConvertTo); /** * Returns a String array of column names in the DataSet. This will assume @@ -235,7 +235,6 @@ */ void orderRows(final OrderBy ob) throws Exception; - /** * Sets data in the DataSet to lowercase */ @@ -274,7 +273,7 @@ * The strictNumericParse to set. */ void setStrictNumericParse(final boolean strictNumericParse); - + /** * Sets the properties from the pzconvert.properties file. * This file specifies the PZConverter implementation to use @@ -284,7 +283,7 @@ * Property mapping for String to Object conversion */ void setPZConvertProps(Properties props); - + /** * Changes the value of the given column only for the * given row which the pointer is currently sitting on. Modified: trunk/PZFileReader/src/main/java/net/sf/pzfilereader/DefaultDataSet.java =================================================================== --- trunk/PZFileReader/src/main/java/net/sf/pzfilereader/DefaultDataSet.java 2007-05-01 09:13:20 UTC (rev 309) +++ trunk/PZFileReader/src/main/java/net/sf/pzfilereader/DefaultDataSet.java 2007-05-24 22:51:48 UTC (rev 310) @@ -39,14 +39,15 @@ import java.util.Date; import java.util.List; import java.util.Locale; -import java.util.Map; import java.util.Properties; import net.sf.pzfilereader.ordering.OrderBy; import net.sf.pzfilereader.structure.ColumnMetaData; import net.sf.pzfilereader.structure.Row; import net.sf.pzfilereader.util.PZConstants; +import net.sf.pzfilereader.util.PZStringUtils; import net.sf.pzfilereader.util.ParserUtils; +import net.sf.pzfilereader.xml.PZMetaData; /** * @author xhensevb @@ -56,7 +57,7 @@ private final List rows = new ArrayList(); private final List errors = new ArrayList(); - + private Properties pzConvertProps = null; /** Pointer for the current row in the array we are on */ @@ -74,12 +75,22 @@ */ private boolean strictNumericParse = false; - private Map columnMD; - + // private Map columnMD; + + private PZMetaData pzMetaData; + private PZParser pzparser; - public DefaultDataSet(final Map columnMD2, final PZParser pzparser) { - this.columnMD = columnMD2; + // /** + // * @deprecated use the constructor with PZMetaData + // */ + // public DefaultDataSet(final Map columnMD2, final PZParser pzparser) { + // this.columnMD = columnMD2; + // this.pzparser = pzparser; + // } + + public DefaultDataSet(final PZMetaData pzMetaData, final PZParser pzparser) { + this.pzMetaData = pzMetaData; this.pzparser = pzparser; } @@ -100,8 +111,8 @@ ColumnMetaData column = null; String[] array = null; - if (columnMD != null) { - final List cmds = ParserUtils.getColumnMetaData(PZConstants.DETAIL_ID, columnMD); + if (/*columnMD != null || */pzMetaData != null) { + final List cmds = pzMetaData.getColumnsNames();// ParserUtils.getColumnMetaData(PZConstants.DETAIL_ID, columnMD); array = new String[cmds.size()]; for (int i = 0; i < cmds.size(); i++) { @@ -121,8 +132,8 @@ public String[] getColumns(final String recordID) { String[] array = null; - if (columnMD != null) { - final List cmds = ParserUtils.getColumnMetaData(recordID, columnMD); + if (pzMetaData != null) { + final List cmds = ParserUtils.getColumnMetaData(recordID, pzMetaData); array = new String[cmds.size()]; for (int i = 0; i < cmds.size(); i++) { final ColumnMetaData column = (ColumnMetaData) cmds.get(i); @@ -149,9 +160,8 @@ * java.text.SimpleDateFormat) */ public Date getDate(final String column, final SimpleDateFormat sdf) throws ParseException { - final Row row = (Row) rows.get(pointer); - final String s = row.getValue(ParserUtils.getColumnIndex(row.getMdkey(), columnMD, column, pzparser)); - if (s.trim().equals("")) { + final String s = getStringValue(column); + if (PZStringUtils.isBlank(s)) { //don't do the parse on empties return null; } @@ -165,10 +175,9 @@ */ public double getDouble(final String column) { final StringBuffer newString = new StringBuffer(); - final Row row = (Row) rows.get(pointer); + final String s = getStringValue(column); + // final String s = row.getValue(ParserUtils.getColumnIndex(row.getMdkey(), columnMD, column, pzparser)); - final String s = row.getValue(ParserUtils.getColumnIndex(row.getMdkey(), columnMD, column, pzparser)); - if (!strictNumericParse) { newString.append(ParserUtils.stripNonDoubleChars(s)); } else { @@ -177,11 +186,14 @@ return Double.parseDouble(newString.toString()); } - - - public Object getObject(String column, Class classToConvertTo) { + + private String getStringValue(final String column) { final Row row = (Row) rows.get(pointer); - final String s = row.getValue(ParserUtils.getColumnIndex(row.getMdkey(), columnMD, column, pzparser)); + return row.getValue(ParserUtils.getColumnIndex(row.getMdkey(), pzMetaData, column, pzparser)); + } + + public Object getObject(final String column, final Class classToConvertTo) { + final String s = getStringValue(column); return ParserUtils.runPzConverter(pzConvertProps, s, classToConvertTo); } @@ -222,17 +234,13 @@ * @see net.sf.pzfilereader.IDataSet#getInt(java.lang.String) */ public int getInt(final String column) { - final StringBuffer newString = new StringBuffer(); - final Row row = (Row) rows.get(pointer); - final String s = row.getValue(ParserUtils.getColumnIndex(row.getMdkey(), columnMD, column, pzparser)); + final String s = getStringValue(column); if (!strictNumericParse) { - newString.append(ParserUtils.stripNonLongChars(s)); - } else { - newString.append(s); + return Integer.parseInt(ParserUtils.stripNonLongChars(s)); } - return Integer.parseInt(newString.toString()); + return Integer.parseInt(s); } /* @@ -259,13 +267,12 @@ * @see net.sf.pzfilereader.IDataSet#getString(java.lang.String) */ public String getString(final String column) { - final Row row = (Row) rows.get(pointer); - final String s = row.getValue(ParserUtils.getColumnIndex(row.getMdkey(), columnMD, column, pzparser)); - - if (pzparser.isNullEmptyStrings() && s.trim().equals("")) { + final String s = getStringValue(column); + + if (pzparser.isNullEmptyStrings() && PZStringUtils.isBlank(s)) { return null; - } - + } + if (upperCase) { // convert data to uppercase before returning // return row.getValue(ParserUtils.findColumn(column, @@ -283,12 +290,11 @@ // return value as how it is in the file return s; } - - - public void setValue(String column, String value) { + + public void setValue(final String column, final String value) { final Row row = (Row) rows.get(pointer); - final int colIndex = ParserUtils.getColumnIndex(row.getMdkey(), columnMD, column, pzparser); - + final int colIndex = ParserUtils.getColumnIndex(row.getMdkey(), pzMetaData, column, pzparser); + row.setValue(colIndex, value); } @@ -350,7 +356,8 @@ // with <RECORD> mappings"); // } if (ob != null && rows != null) { - final List cmds = ParserUtils.getColumnMetaData(PZConstants.DETAIL_ID, columnMD); + final List cmds = pzMetaData.getColumnsNames(); + // final List cmds = ParserUtils.getColumnMetaData(PZConstants.DETAIL_ID, columnMD); ob.setColumnMD(cmds); Collections.sort(rows, ob); goTop(); @@ -439,32 +446,18 @@ rows.remove(pointer); pointer--; } - - public void setPZConvertProps(Properties props) { + + public void setPZConvertProps(final Properties props) { this.pzConvertProps = props; } - protected void setColumnMD(final Map columnMD) { - this.columnMD = columnMD; - } - /** - * Returns the column meta data assoicated with - * this DataSet - * - * @return Map - */ - protected Map getColumnMD() { - return this.columnMD; - } - - /** * @param pointer the pointer to set */ - protected void setPointer(int pointer) { + protected void setPointer(final int pointer) { this.pointer = pointer; } - + /** * Clears all of the in memory rows of the DataSet * @@ -473,4 +466,12 @@ rows.clear(); } + public PZMetaData getPzMetaData() { + return pzMetaData; + } + + public void setPzMetaData(final PZMetaData pzMetaData) { + this.pzMetaData = pzMetaData; + } + } Modified: trunk/PZFileReader/src/main/java/net/sf/pzfilereader/DefaultPZParserFactory.java =================================================================== --- trunk/PZFileReader/src/main/java/net/sf/pzfilereader/DefaultPZParserFactory.java 2007-05-01 09:13:20 UTC (rev 309) +++ trunk/PZFileReader/src/main/java/net/sf/pzfilereader/DefaultPZParserFactory.java 2007-05-24 22:51:48 UTC (rev 310) @@ -87,11 +87,11 @@ public PZParser newFixedLengthParser(final InputStream pzmapXMLStream, final InputStream dataSourceStream) { return new FixedLengthPZParser(pzmapXMLStream, dataSourceStream); } - + public PZParser newFixedLengthParser(final Connection con, final Reader dataSource, final String dataDefinition) { return new DBFixedLengthPZParser(con, dataSource, dataDefinition); } - + public PZParser newFixedLengthParser(final Reader pzmapXMLStream, final Reader dataSource) { return new FixedLengthPZParser(pzmapXMLStream, dataSource); } @@ -102,8 +102,8 @@ * @see net.sf.pzfilereader.PZParserFactory#newParser(java.sql.Connection, * java.io.InputStream, java.lang.String, char, char, boolean) */ - public PZParser newDelimitedParser(final Connection con, final InputStream dataSourceStream, final String dataDefinition, - final char delimiter, final char qualifier, final boolean ignoreFirstRecord) { + public PZParser newDelimitedParser(final Connection con, final InputStream dataSourceStream, final String dataDefinition, final char delimiter, + final char qualifier, final boolean ignoreFirstRecord) { return new DBDelimiterPZParser(con, dataSourceStream, dataDefinition, delimiter, qualifier, ignoreFirstRecord); } @@ -124,8 +124,8 @@ * @see net.sf.pzfilereader.PZParserFactory#newParser(java.io.InputStream, * java.io.InputStream, char, char, boolean) */ - public PZParser newDelimitedParser(final InputStream pzmapXMLStream, final InputStream dataSourceStream, - final char delimiter, final char qualifier, final boolean ignoreFirstRecord) { + public PZParser newDelimitedParser(final InputStream pzmapXMLStream, final InputStream dataSourceStream, final char delimiter, + final char qualifier, final boolean ignoreFirstRecord) { return new DelimiterPZParser(pzmapXMLStream, dataSourceStream, delimiter, qualifier, ignoreFirstRecord); } @@ -148,16 +148,18 @@ public PZParser newDelimitedParser(final InputStream dataSourceStream, final char delimiter, final char qualifier) { return new DelimiterPZParser(dataSourceStream, delimiter, qualifier, false); } - - public PZParser newDelimitedParser(Connection con, Reader dataSource, String dataDefinition, char delimiter, char qualifier, boolean ignoreFirstRecord) { + + public PZParser newDelimitedParser(final Connection con, final Reader dataSource, final String dataDefinition, final char delimiter, + final char qualifier, final boolean ignoreFirstRecord) { return new DBDelimiterPZParser(con, dataSource, dataDefinition, delimiter, qualifier, ignoreFirstRecord); } - - public PZParser newDelimitedParser(Reader dataSource, char delimiter, char qualifier) { + + public PZParser newDelimitedParser(final Reader dataSource, final char delimiter, final char qualifier) { return new DelimiterPZParser(dataSource, delimiter, qualifier, false); } - - public PZParser newDelimitedParser(Reader pzmapXML, Reader dataSource, char delimiter, char qualifier, boolean ignoreFirstRecord) { + + public PZParser newDelimitedParser(final Reader pzmapXML, final Reader dataSource, final char delimiter, final char qualifier, + final boolean ignoreFirstRecord) { return new DelimiterPZParser(pzmapXML, dataSource, delimiter, qualifier, ignoreFirstRecord); } Modified: trunk/PZFileReader/src/main/java/net/sf/pzfilereader/DelimiterPZParser.java =================================================================== --- trunk/PZFileReader/src/main/java/net/sf/pzfilereader/DelimiterPZParser.java 2007-05-01 09:13:20 UTC (rev 309) +++ trunk/PZFileReader/src/main/java/net/sf/pzfilereader/DelimiterPZParser.java 2007-05-24 22:51:48 UTC (rev 310) @@ -49,28 +49,23 @@ */ public class DelimiterPZParser extends AbstractDelimiterPZParser { private InputStream pzmapXMLStream = null; - private File pzmapXML = null; - private Reader pzmapReader; - + //this InputStream and file can be removed after support for //file and inputstream is removed from the parserfactory. The //methods have been deprecated..pz private InputStream dataSourceStream = null; - private File dataSource = null; - - public DelimiterPZParser(final File pzmapXML, final File dataSource, final char delimiter, final char qualifier, - final boolean ignoreFirstRecord) { + public DelimiterPZParser(final File pzmapXML, final File dataSource, final char delimiter, final char qualifier, final boolean ignoreFirstRecord) { super(null, delimiter, qualifier, ignoreFirstRecord); this.pzmapXML = pzmapXML; this.dataSource = dataSource; } - public DelimiterPZParser(final InputStream pzmapXMLStream, final InputStream dataSourceStream, final char delimiter, - final char qualifier, final boolean ignoreFirstRecord) { + public DelimiterPZParser(final InputStream pzmapXMLStream, final InputStream dataSourceStream, final char delimiter, final char qualifier, + final boolean ignoreFirstRecord) { super(null, delimiter, qualifier, ignoreFirstRecord); this.pzmapXMLStream = pzmapXMLStream; this.dataSourceStream = dataSourceStream; @@ -81,17 +76,15 @@ this.dataSource = dataSource; } - public DelimiterPZParser(final InputStream dataSourceStream, final char delimiter, final char qualifier, - final boolean ignoreFirstRecord) { + public DelimiterPZParser(final InputStream dataSourceStream, final char delimiter, final char qualifier, final boolean ignoreFirstRecord) { super(null, delimiter, qualifier, ignoreFirstRecord); this.dataSourceStream = dataSourceStream; } - - public DelimiterPZParser(final Reader dataSourceReader, final char delimiter, final char qualifier, - final boolean ignoreFirstRecord) { + + public DelimiterPZParser(final Reader dataSourceReader, final char delimiter, final char qualifier, final boolean ignoreFirstRecord) { super(dataSourceReader, delimiter, qualifier, ignoreFirstRecord); } - + public DelimiterPZParser(final Reader dataSourceReader, final Reader pzmapReader, final char delimiter, final char qualifier, final boolean ignoreFirstRecord) { super(dataSourceReader, delimiter, qualifier, ignoreFirstRecord); @@ -106,13 +99,12 @@ final Reader r = new InputStreamReader(dataSourceStream); setDataSourceReader(r); addToCloseReaderList(r); - } else if (dataSource != null){ + } else if (dataSource != null) { final Reader r = new FileReader(dataSource); setDataSourceReader(r); addToCloseReaderList(r); } - - + boolean closeMapReader = false; if (pzmapXML != null) { this.pzmapReader = new FileReader(pzmapXML); @@ -121,10 +113,11 @@ this.pzmapReader = new InputStreamReader(pzmapXMLStream); closeMapReader = true; } - + if (this.pzmapReader != null) { try { - setColumnMD(PZMapParser.parse(this.pzmapReader, this)); + // setColumnMD(PZMapParser.parse(this.pzmapReader, this)); + setPzMetaData(PZMapParser.parseMap(this.pzmapReader, this)); } finally { if (closeMapReader) { //only close the reader if it is one we created @@ -133,7 +126,7 @@ } } } - + setInitialised(true); } catch (final JDOMException e) { throw new InitialisationException(e); Modified: trunk/PZFileReader/src/main/java/net/sf/pzfilereader/FixedLengthPZParser.java =================================================================== --- trunk/PZFileReader/src/main/java/net/sf/pzfilereader/FixedLengthPZParser.java 2007-05-01 09:13:20 UTC (rev 309) +++ trunk/PZFileReader/src/main/java/net/sf/pzfilereader/FixedLengthPZParser.java 2007-05-24 22:51:48 UTC (rev 310) @@ -52,14 +52,14 @@ private InputStream pzmapXMLStream; private File pzmapXML; - + private Reader pzmapReader; - + //this InputStream and file can be removed after support for //file and inputstream is removed from the parserfactory. The //methods have been deprecated..pz private InputStream dataSourceStream = null; - + private File dataSource = null; public FixedLengthPZParser(final InputStream pzmapXMLStream, final InputStream dataSourceStream) { @@ -73,9 +73,9 @@ this.pzmapXML = pzmapXML; this.dataSource = dataSource; } - + public FixedLengthPZParser(final Reader pzmapReader, final Reader dataSourceReader) { - super (dataSourceReader); + super(dataSourceReader); this.pzmapReader = pzmapReader; } @@ -87,13 +87,12 @@ final Reader r = new InputStreamReader(dataSourceStream); setDataSourceReader(r); addToCloseReaderList(r); - } else if (dataSource != null){ + } else if (dataSource != null) { final Reader r = new FileReader(dataSource); setDataSourceReader(r); addToCloseReaderList(r); } - - + boolean closeMapReader = false; if (pzmapXML != null) { this.pzmapReader = new FileReader(pzmapXML); @@ -102,9 +101,10 @@ this.pzmapReader = new InputStreamReader(pzmapXMLStream); closeMapReader = true; } - + try { - setColumnMD(PZMapParser.parse(this.pzmapReader, this)); + // setColumnMD(PZMapParser.parse(this.pzmapReader, this)); + setPzMetaData(PZMapParser.parseMap(this.pzmapReader, this)); } finally { if (closeMapReader) { //only close the reader if it is one we created @@ -112,8 +112,8 @@ this.pzmapReader.close(); } } - - // setInitialised(true); + + // setInitialised(true); } catch (final JDOMException e) { throw new InitialisationException(e); } catch (final IOException e) { Modified: trunk/PZFileReader/src/main/java/net/sf/pzfilereader/PZParser.java =================================================================== --- trunk/PZFileReader/src/main/java/net/sf/pzfilereader/PZParser.java 2007-05-01 09:13:20 UTC (rev 309) +++ trunk/PZFileReader/src/main/java/net/sf/pzfilereader/PZParser.java 2007-05-24 22:51:48 UTC (rev 310) @@ -62,7 +62,7 @@ * producing an error */ void setHandlingShortLines(final boolean handleShortLines); - + /** * * @return true, detail lines with a length or column count > the mapping @@ -70,7 +70,7 @@ * lines as erros in the DataError collection. */ boolean isIgnoreExtraColumns(); - + /** * * @param ignoreExtraColumns when true, detail lines with a length or column @@ -78,7 +78,7 @@ * will NOT register these lines as erros in the DataError collection. */ void setIgnoreExtraColumns(final boolean ignoreExtraColumns); - + /** * Default is false * @@ -87,7 +87,7 @@ * Example when false: Column name = AColumnName ; getString("acolumnname") would pass */ boolean isColumnNamesCaseSensitive(); - + /** * Default is false * @@ -97,28 +97,27 @@ * Example when false: Column name = AColumnName ; getString("acolumnname") would pass */ void setColumnNamesCaseSensitive(final boolean columnNamesCaseSensitive); - + /** * Default is false * * @return true, warnings encountered durring parsing will not be included in the DataSet errors */ boolean isIgnoreParseWarnings(); - - + /** * * @param ignoreParseWarnings when true, warnings encountered durring parsing will not be included * in the DataSet errors */ void setIgnoreParseWarnings(final boolean ignoreParseWarnings); - + /** * * @return true, empty Strings will get returned as NULL when calling DataSet.getString() */ boolean isNullEmptyStrings(); - + /** * * @param nullEmptyStrings when true, empty Strings will get returned as NULL when calling DataSet.getString() Modified: trunk/PZFileReader/src/main/java/net/sf/pzfilereader/PZParserFactory.java =================================================================== --- trunk/PZFileReader/src/main/java/net/sf/pzfilereader/PZParserFactory.java 2007-05-01 09:13:20 UTC (rev 309) +++ trunk/PZFileReader/src/main/java/net/sf/pzfilereader/PZParserFactory.java 2007-05-24 22:51:48 UTC (rev 310) @@ -116,7 +116,7 @@ * @return PZParser */ PZParser newFixedLengthParser(final InputStream pzmapXMLStream, final InputStream dataSourceStream); - + /** * Constructs a new DataSet using the database table file layout method. * This is used for a FIXED LENGTH text file. @@ -134,7 +134,7 @@ * @return PZParser */ PZParser newFixedLengthParser(final Connection con, final Reader dataSource, final String dataDefinition); - + /** * New constructor based on Reader. Constructs a new DataSet using the * PZMAP XML file layout method. This is used for a FIXED LENGTH text file. @@ -187,9 +187,9 @@ * The InputStream can be wrapped in a "new InputStreamReader(InputStream)" * @return PZParser */ - PZParser newDelimitedParser(final Connection con, final InputStream dataSource, final String dataDefinition, - final char delimiter, final char qualifier, final boolean ignoreFirstRecord); - + PZParser newDelimitedParser(final Connection con, final InputStream dataSource, final String dataDefinition, final char delimiter, + final char qualifier, final boolean ignoreFirstRecord); + /** * New constructor based on Reader. Constructs a new DataSet using the * database table file layout method. This is used for a DELIMITED text @@ -218,8 +218,8 @@ * skips the first line that contains data in the file * @return PZParser */ - PZParser newDelimitedParser(final Connection con, final Reader dataSource, final String dataDefinition, - final char delimiter, final char qualifier, final boolean ignoreFirstRecord); + PZParser newDelimitedParser(final Connection con, final Reader dataSource, final String dataDefinition, final char delimiter, + final char qualifier, final boolean ignoreFirstRecord); /** * Constructs a new DataSet using the PZMAP XML file layout method. This is @@ -270,8 +270,7 @@ */ PZParser newDelimitedParser(final Reader pzmapXML, final Reader dataSource, final char delimiter, final char qualifier, final boolean ignoreFirstRecord); - - + /** * New constructor based on InputStream. Constructs a new DataSet using the * PZMAP XML file layout method. This is used for a DELIMITED text file. @@ -299,8 +298,8 @@ * The InputStream can be wrapped in a "new InputStreamReader(InputStream)" * @return PZParser */ - PZParser newDelimitedParser(final InputStream pzmapXMLStream, final InputStream dataSourceStream, final char delimiter, - final char qualifier, final boolean ignoreFirstRecord); + PZParser newDelimitedParser(final InputStream pzmapXMLStream, final InputStream dataSourceStream, final char delimiter, final char qualifier, + final boolean ignoreFirstRecord); /** * Constructs a new DataSet using the first line of data found in the text @@ -322,7 +321,7 @@ * @return PZParser */ PZParser newDelimitedParser(final File dataSource, final char delimiter, final char qualifier); - + /** * Constructs a new DataSet using the first line of data found in the text * file as the column names. This is used for a DELIMITED text file. esacpe Modified: trunk/PZFileReader/src/main/java/net/sf/pzfilereader/brparse/BuffReaderDelimPZParser.java =================================================================== --- trunk/PZFileReader/src/main/java/net/sf/pzfilereader/brparse/BuffReaderDelimPZParser.java 2007-05-01 09:13:20 UTC (rev 309) +++ trunk/PZFileReader/src/main/java/net/sf/pzfilereader/brparse/BuffReaderDelimPZParser.java 2007-05-24 22:51:48 UTC (rev 310) @@ -38,11 +38,7 @@ import java.io.InputStream; import java.io.Reader; import java.util.List; -import java.util.Map; -import org.slf4j.Logger; -import org.slf4j.Logg... [truncated message content] |