From: <st...@us...> - 2008-03-10 14:27:43
|
Revision: 6135 http://smartfrog.svn.sourceforge.net/smartfrog/?rev=6135&view=rev Author: steve_l Date: 2008-03-10 07:27:39 -0700 (Mon, 10 Mar 2008) Log Message: ----------- SFOS-693 Add a way to read in CSV files Modified Paths: -------------- trunk/core/components/csvfiles/src/org/smartfrog/services/filesystem/csvfiles/CSVArrayReader.java trunk/core/components/csvfiles/src/org/smartfrog/services/filesystem/csvfiles/CSVColumnReader.java trunk/core/components/csvfiles/src/org/smartfrog/services/filesystem/csvfiles/CSVFileReadImpl.java trunk/core/components/csvfiles/src/org/smartfrog/services/filesystem/csvfiles/CSVFileReadTester.java Modified: trunk/core/components/csvfiles/src/org/smartfrog/services/filesystem/csvfiles/CSVArrayReader.java =================================================================== --- trunk/core/components/csvfiles/src/org/smartfrog/services/filesystem/csvfiles/CSVArrayReader.java 2008-03-10 14:26:37 UTC (rev 6134) +++ trunk/core/components/csvfiles/src/org/smartfrog/services/filesystem/csvfiles/CSVArrayReader.java 2008-03-10 14:27:39 UTC (rev 6135) @@ -154,7 +154,7 @@ if (skipNarrowLines) { return; } else { - throw new SmartFrogDeploymentException("Too narrow, line #" + getCount() + ": " + throw new SmartFrogDeploymentException("Too narrow, line #" + getCurrentLine() + ": " + CSVFileReadImpl.merge(line), CSVArrayReader.this); } } Modified: trunk/core/components/csvfiles/src/org/smartfrog/services/filesystem/csvfiles/CSVColumnReader.java =================================================================== --- trunk/core/components/csvfiles/src/org/smartfrog/services/filesystem/csvfiles/CSVColumnReader.java 2008-03-10 14:26:37 UTC (rev 6134) +++ trunk/core/components/csvfiles/src/org/smartfrog/services/filesystem/csvfiles/CSVColumnReader.java 2008-03-10 14:27:39 UTC (rev 6135) @@ -143,7 +143,7 @@ if (skipNarrowLines) { return; } else { - throw new SmartFrogDeploymentException("Too narrow, line #" + getCount() + ": " + throw new SmartFrogDeploymentException("Too narrow, line #" + getCurrentLine() + ": " + CSVFileReadImpl.merge(line), CSVColumnReader.this); } } Modified: trunk/core/components/csvfiles/src/org/smartfrog/services/filesystem/csvfiles/CSVFileReadImpl.java =================================================================== --- trunk/core/components/csvfiles/src/org/smartfrog/services/filesystem/csvfiles/CSVFileReadImpl.java 2008-03-10 14:26:37 UTC (rev 6134) +++ trunk/core/components/csvfiles/src/org/smartfrog/services/filesystem/csvfiles/CSVFileReadImpl.java 2008-03-10 14:27:39 UTC (rev 6135) @@ -132,7 +132,7 @@ * Get the next line * * @return the next line, all broken up, or null for no new lines. - * @throws RemoteException network problems + * @throws RemoteException network problems * @throws SmartFrogDeploymentException parsing/file IO problems, or wrong dimensions of the array */ public synchronized String[] getNextTuple() throws RemoteException, SmartFrogException { @@ -141,36 +141,37 @@ } try { String[] result = reader.readNext(); - if(result==null) { - if(count<minCount) { + if (result == null) { + if (count < minCount) { throw new SmartFrogDeploymentException(ERROR_TOO_FEW_LINES + minCount + " but got " + count, this); } } else { count++; - if(maxCount>=0 && count>maxCount) { - throw new SmartFrogDeploymentException(ERROR_TOO_MANY_LINES + count+ ":\n" + if (maxCount >= 0 && count > maxCount) { + throw new SmartFrogDeploymentException(ERROR_TOO_MANY_LINES + count + ":\n" + merge(result), this); } - int width=result.length; + int width = result.length; if (width < minWidth || (maxWidth >= 0 && width > maxWidth)) { - throw new SmartFrogDeploymentException(ERROR_LINE_WIDTH_WRONG + minWidth + ',' + maxWidth + "]: " + width - + '\n' +merge(result), + throw new SmartFrogDeploymentException( + ERROR_LINE_WIDTH_WRONG + minWidth + ',' + maxWidth + "]: " + width + + '\n' + merge(result), this); } } return result; } catch (IOException e) { - throw new SmartFrogDeploymentException("Reading from " + getFile(), e,this); + throw new SmartFrogDeploymentException("Reading from " + getFile(), e, this); } } /** * Close the reader. harmless if we are already closed * - * @throws RemoteException network problems + * @throws RemoteException network problems * @throws SmartFrogException parsing/file IO problems */ public synchronized void close() throws RemoteException, SmartFrogException { @@ -198,7 +199,7 @@ throw new SmartFrogDeploymentException(ERROR_UNBOUND); } try { - sfLog().info("Reading CSV file "+csvfile); + sfLog().info("Reading CSV file " + csvfile); reader = new CSVReader(new FileReader(csvfile), separator, quote, headerLines); count = 0; } catch (FileNotFoundException e) { Modified: trunk/core/components/csvfiles/src/org/smartfrog/services/filesystem/csvfiles/CSVFileReadTester.java =================================================================== --- trunk/core/components/csvfiles/src/org/smartfrog/services/filesystem/csvfiles/CSVFileReadTester.java 2008-03-10 14:26:37 UTC (rev 6134) +++ trunk/core/components/csvfiles/src/org/smartfrog/services/filesystem/csvfiles/CSVFileReadTester.java 2008-03-10 14:27:39 UTC (rev 6135) @@ -68,7 +68,8 @@ super.sfStart(); Prim src = sfResolve(ATTR_SOURCE, (Prim) null, true); TupleDataSource source = (TupleDataSource) src; - lines = ListUtils.resolveStringTupleList(this, new Reference(ATTR_LINES), true); + lines = ListUtils.resolveStringNTupleList(this, new Reference(ATTR_LINES), -1,true); + sfLog().info(ListUtils.tuplesToString(lines)); minCount = sfResolve(ATTR_MINCOUNT, 0, true); maxCount = sfResolve(ATTR_MAXCOUNT, 0, true); setReader(new ReaderThread(source)); @@ -105,11 +106,11 @@ * @throws RemoteException network problems */ protected void onFinished() throws SmartFrogException, RemoteException { - if (getCount() < minCount) { + if (getCurrentLine() < minCount) { throw new SmartFrogException("Too few lines -expected " + minCount + " but got " + - getCount(), + getCurrentLine(), CSVFileReadTester.this); } } @@ -125,7 +126,7 @@ if (sfLog().isInfoEnabled()) { sfLog().info(CSVFileReadImpl.merge(line)); } - int position = getCount(); + int position = getCurrentLine(); if (lines.size() > position) { Vector<String> expected = lines.elementAt(position); compareLine(position, line, expected); @@ -139,25 +140,25 @@ /** * compare two lines, fail if they mismatch * - * @param element element number + * @param number element number * @param line line read in * @param expected expected line * @throws SmartFrogException if there is a count mismatch, or a value is not as expected */ - private void compareLine(int element, String[] line, Vector<String> expected) + private void compareLine(int number, String[] line, Vector<String> expected) throws SmartFrogException { String merged = CSVFileReadImpl.merge(line); int size = expected.size(); int actual = line.length; if (actual != size) { - throw new SmartFrogException("Line " + - element + + throw new SmartFrogException("Line index " + + number + " is wrong width; expected " + size + - " but got " + " elements but got " + actual + - " elements\n" + + " \n" + merged); } for (int i = 0; i < size; i++) { @@ -165,7 +166,7 @@ String actualElt = line[i]; if (!expectedElt.equals(actualElt)) { throw new SmartFrogException( - "Line " + element + " does not match expected element " + i + "Line " + number + " does not match expected element " + i + " expected=\"" + expectedElt + '\"' + " actual=\"" + actualElt + '\"' + ":\n" + merged); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |