From: <st...@us...> - 2008-03-10 09:42:46
|
Revision: 6132 http://smartfrog.svn.sourceforge.net/smartfrog/?rev=6132&view=rev Author: steve_l Date: 2008-03-10 02:38:09 -0700 (Mon, 10 Mar 2008) Log Message: ----------- SFOS-725 Add component to turn CSV data into XML as part of a workflow 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/CSVFileRead.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 Added Paths: ----------- trunk/core/components/csvfiles/.svnignore Removed Paths: ------------- trunk/core/components/csvfiles/src/org/smartfrog/services/filesystem/csvfiles/CSVReaderThread.java Copied: trunk/core/components/csvfiles/.svnignore (from rev 6125, trunk/core/components/database/.svnignore) =================================================================== --- trunk/core/components/csvfiles/.svnignore (rev 0) +++ trunk/core/components/csvfiles/.svnignore 2008-03-10 09:38:09 UTC (rev 6132) @@ -0,0 +1,13 @@ +build +dist +build.properties +runtime.properties +common.properties +*.jpx +*.ipr +*.iml +*.iws +.classpath +.project +lib + 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 09:34:54 UTC (rev 6131) +++ trunk/core/components/csvfiles/src/org/smartfrog/services/filesystem/csvfiles/CSVArrayReader.java 2008-03-10 09:38:09 UTC (rev 6132) @@ -22,13 +22,15 @@ import org.smartfrog.sfcore.common.SmartFrogDeploymentException; import org.smartfrog.sfcore.common.SmartFrogException; import org.smartfrog.sfcore.prim.Prim; +import org.smartfrog.services.filesystem.TupleReaderThread; import java.rmi.Remote; import java.rmi.RemoteException; import java.util.Vector; /** - * component to test the file read operations; lets you validate headers and such like Created 20-Feb-2008 15:50:56 + * component to test the CSV file read operations; + * lets you validate headers and such like Created 20-Feb-2008 15:50:56 */ public class CSVArrayReader extends AbstractCSVProcessor implements Remote { @@ -79,7 +81,7 @@ * but spawn off any main loops! * * @throws SmartFrogException failure while starting - * @throws RemoteException In case of network/rmi error + * @throws RemoteException In case of network/rmi error */ public synchronized void sfStart() throws SmartFrogException, RemoteException { super.sfStart(); @@ -98,61 +100,74 @@ /** * do the work in a thread which triggers workflow events afterwards */ - private class ReaderThread extends CSVReaderThread { + private class ReaderThread extends TupleReaderThread { + private Vector<String> result; /** * Create a basic thread + * * @param source the data source */ private ReaderThread(CSVFileRead source) { - super(CSVArrayReader.this,source); + super(CSVArrayReader.this, source, true); } /** - * read the thread in, validate the values, then maybe terminate the component + * we've started. do any preparation * - * @throws Throwable if anything went wrong + * @throws SmartFrogException SmartFrog problems + * @throws RemoteException network problems */ - public void execute() throws Throwable { - String[] line; - Vector<String> result=new Vector<String>(); - int count = 0; - source.start(); - while ((line = source.getNextLine()) != null) { - if (isTerminationRequested()) { - //bail out completely + @Override + protected void onStarted() throws SmartFrogException, RemoteException { + result = new Vector<String>(); + } + + /** + * we've finished (successfully) + * + * @throws SmartFrogException SmartFrog problems + * @throws RemoteException network problems + */ + @Override + protected void onFinished() throws SmartFrogException, RemoteException { + //end of lines set the results + sfReplaceAttribute(ATTR_RESULT, result); + if (target != null) { + target.sfReplaceAttribute(targetAttribute, result); + } + } + + /** + * Process one line of the data source + * + * @param line line to process + * @throws SmartFrogException SmartFrog problems + * @throws RemoteException network problems + */ + @Override + protected void processOneLine(String[] line) throws SmartFrogException, RemoteException { + int w = line.length; + if (w < column) { + //too narrow for this column + if (skipNarrowLines) { return; + } else { + throw new SmartFrogDeploymentException("Too narrow, line #" + getCount() + ": " + + CSVFileReadImpl.merge(line), CSVArrayReader.this); } - int w = line.length; - if (w < column) { - //too narrow for this column - if (skipNarrowLines) { - continue; - } else { - throw new SmartFrogDeploymentException("Too narrow, line #" + count + ": " - + CSVFileReadImpl.merge(line), CSVArrayReader.this); - } - } - String columnValue=line[column-1]; - if(trimFields) { - columnValue=columnValue.trim(); - } - if(columnValue.length()==0 && skipEmptyFields) { - continue; - } - result.add(columnValue); } - //end of lines set the results - sfReplaceAttribute(ATTR_RESULT,result); - if(target!=null) { - target.sfReplaceAttribute(targetAttribute,result); + String columnValue = line[column - 1]; + if (trimFields) { + columnValue = columnValue.trim(); } - + if (columnValue.length() == 0 && skipEmptyFields) { + return; + } + result.add(columnValue); } - - } } \ No newline at end of file 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 09:34:54 UTC (rev 6131) +++ trunk/core/components/csvfiles/src/org/smartfrog/services/filesystem/csvfiles/CSVColumnReader.java 2008-03-10 09:38:09 UTC (rev 6132) @@ -22,6 +22,8 @@ import org.smartfrog.sfcore.common.SmartFrogDeploymentException; import org.smartfrog.sfcore.common.SmartFrogException; import org.smartfrog.sfcore.prim.Prim; +import org.smartfrog.services.filesystem.TupleReaderThread; +import org.smartfrog.services.filesystem.TupleDataSource; import java.rmi.Remote; import java.rmi.RemoteException; @@ -79,11 +81,11 @@ * but spawn off any main loops! * * @throws SmartFrogException failure while starting - * @throws RemoteException In case of network/rmi error + * @throws RemoteException In case of network/rmi error */ public synchronized void sfStart() throws SmartFrogException, RemoteException { super.sfStart(); - CSVFileRead source = (CSVFileRead) sfResolve(ATTR_SOURCE, (Prim) null, true); + TupleDataSource source = (TupleDataSource) sfResolve(ATTR_SOURCE, (Prim) null, true); target = sfResolve(ATTR_TARGET, (Prim) null, false); targetAttribute = sfResolve(ATTR_TARGET_ATTRIBUTE, "", target != null); trimFields = sfResolve(ATTR_TRIM_FIELDS, false, true); @@ -95,66 +97,78 @@ } - /** * do the work in a thread which triggers workflow events afterwards */ - private class ReaderThread extends CSVReaderThread { + private class ReaderThread extends TupleReaderThread { + private Vector<String> result; - /** * Create a basic thread + * * @param source the data source */ - private ReaderThread(CSVFileRead source) { - super(CSVColumnReader.this,source); + private ReaderThread(TupleDataSource source) { + super(CSVColumnReader.this, source, true); } /** - * read the thread in, validate the values, then maybe terminate the component + * we've finished (successfully) * - * @throws Throwable if anything went wrong + * @throws SmartFrogException SmartFrog problems + * @throws RemoteException network problems */ - public void execute() throws Throwable { - String[] line; - Vector<String> result=new Vector<String>(); - int count = 0; - source.start(); - while ((line = source.getNextLine()) != null) { - if (isTerminationRequested()) { - //bail out completely + @Override + protected void onFinished() throws SmartFrogException, RemoteException { + //end of lines set the results + sfReplaceAttribute(ATTR_RESULT, result); + if (target != null) { + target.sfReplaceAttribute(targetAttribute, result); + } + } + + /** + * Process one line of the data source + * + * @param line line to process + * @throws SmartFrogException SmartFrog problems + * @throws RemoteException network problems + */ + @Override + protected void processOneLine(String[] line) throws SmartFrogException, RemoteException { + int w = line.length; + if (w < column) { + //too narrow for this column + if (skipNarrowLines) { return; + } else { + throw new SmartFrogDeploymentException("Too narrow, line #" + getCount() + ": " + + CSVFileReadImpl.merge(line), CSVColumnReader.this); } - int w = line.length; - if (w < column) { - //too narrow for this column - if (skipNarrowLines) { - continue; - } else { - throw new SmartFrogDeploymentException("Too narrow, line #" + count + ": " - + CSVFileReadImpl.merge(line), CSVColumnReader.this); - } - } - String columnValue=line[column-1]; - if(trimFields) { - columnValue=columnValue.trim(); - } - if(columnValue.length()==0 && skipEmptyFields) { - continue; - } - result.add(columnValue); } - //end of lines set the results - sfReplaceAttribute(ATTR_RESULT,result); - if(target!=null) { - target.sfReplaceAttribute(targetAttribute,result); + String columnValue = line[column - 1]; + if (trimFields) { + columnValue = columnValue.trim(); } + if (columnValue.length() == 0 && skipEmptyFields) { + return; + } + result.add(columnValue); + } + /** + * we've started. do any preparation + * + * @throws SmartFrogException SmartFrog problems + * @throws RemoteException network problems + */ + @Override + protected void onStarted() throws SmartFrogException, RemoteException { + result = new Vector<String>(); } - } } \ No newline at end of file Modified: trunk/core/components/csvfiles/src/org/smartfrog/services/filesystem/csvfiles/CSVFileRead.java =================================================================== --- trunk/core/components/csvfiles/src/org/smartfrog/services/filesystem/csvfiles/CSVFileRead.java 2008-03-10 09:34:54 UTC (rev 6131) +++ trunk/core/components/csvfiles/src/org/smartfrog/services/filesystem/csvfiles/CSVFileRead.java 2008-03-10 09:38:09 UTC (rev 6132) @@ -20,6 +20,7 @@ package org.smartfrog.services.filesystem.csvfiles; import org.smartfrog.services.filesystem.FileIntf; +import org.smartfrog.services.filesystem.TupleDataSource; import org.smartfrog.sfcore.common.SmartFrogException; import java.rmi.RemoteException; @@ -31,7 +32,7 @@ */ -public interface CSVFileRead extends FileIntf { +public interface CSVFileRead extends FileIntf, TupleDataSource { /** {@value} */ String ATTR_HEADER_LINES = "headerLines"; @@ -58,25 +59,4 @@ */ public static final String ATTR_MAXWIDTH = "maxWidth"; - /** - * Get the next line - * @return the next line, all broken up, or null for no new lines. - * @throws RemoteException network problems - * @throws SmartFrogException parsing/file IO problems - */ - String[] getNextLine() throws RemoteException, SmartFrogException; - - /** - * Go back to the start of the file - * @throws RemoteException network problems - * @throws SmartFrogException parsing/file IO problems - */ - void start() throws RemoteException, SmartFrogException; - - /** - * Close the reader. harmless if we are already closed - * @throws RemoteException network problems - * @throws SmartFrogException parsing/file IO problems - */ - void close() throws RemoteException, SmartFrogException; } 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 09:34:54 UTC (rev 6131) +++ trunk/core/components/csvfiles/src/org/smartfrog/services/filesystem/csvfiles/CSVFileReadImpl.java 2008-03-10 09:38:09 UTC (rev 6132) @@ -135,7 +135,7 @@ * @throws RemoteException network problems * @throws SmartFrogDeploymentException parsing/file IO problems, or wrong dimensions of the array */ - public synchronized String[] getNextLine() throws RemoteException, SmartFrogException { + public synchronized String[] getNextTuple() throws RemoteException, SmartFrogException { if (reader == null) { throw new SmartFrogLifecycleException(ERROR_CSV_READER_IS_NOT_OPEN); } 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 09:34:54 UTC (rev 6131) +++ trunk/core/components/csvfiles/src/org/smartfrog/services/filesystem/csvfiles/CSVFileReadTester.java 2008-03-10 09:38:09 UTC (rev 6132) @@ -21,6 +21,10 @@ import org.smartfrog.sfcore.common.SmartFrogException; import org.smartfrog.sfcore.prim.Prim; +import org.smartfrog.sfcore.utils.ListUtils; +import org.smartfrog.sfcore.reference.Reference; +import org.smartfrog.services.filesystem.TupleReaderThread; +import org.smartfrog.services.filesystem.TupleDataSource; import java.rmi.Remote; import java.rmi.RemoteException; @@ -58,13 +62,13 @@ * but spawn off any main loops! * * @throws SmartFrogException failure while starting - * @throws RemoteException In case of network/rmi error + * @throws RemoteException In case of network/rmi error */ public synchronized void sfStart() throws SmartFrogException, RemoteException { super.sfStart(); Prim src = sfResolve(ATTR_SOURCE, (Prim) null, true); - CSVFileRead source = (CSVFileRead) src; - lines = (Vector<Vector<String>>) sfResolve(ATTR_LINES, lines, true); + TupleDataSource source = (TupleDataSource) src; + lines = ListUtils.resolveStringTupleList(this, new Reference(ATTR_LINES), true); minCount = sfResolve(ATTR_MINCOUNT, 0, true); maxCount = sfResolve(ATTR_MAXCOUNT, 0, true); setReader(new ReaderThread(source)); @@ -72,67 +76,89 @@ } + private class ReaderThread extends TupleReaderThread { - private class ReaderThread extends CSVReaderThread { - /** * Create a reader + * * @param source CSV source * @see Thread#Thread(ThreadGroup,Runnable,String) */ - private ReaderThread(CSVFileRead source) { - super(CSVFileReadTester.this, source); + private ReaderThread(TupleDataSource source) { + super(CSVFileReadTester.this, source, true); } + /** + * we've started. do any preparation + * + * @throws SmartFrogException SmartFrog problems + * @throws RemoteException network problems + */ + protected void onStarted() throws SmartFrogException, RemoteException { + } + + /** - * read the thread in, validate the values, then maybe terminate the component + * we've finished (successfully) * - * @throws Throwable if anything went wrong + * @throws SmartFrogException SmartFrog problems + * @throws RemoteException network problems */ - public void execute() throws Throwable { - String[] line; - int count = 0; - source.start(); - while ((line = source.getNextLine()) != null) { - if (isTerminationRequested()) { - //bail out completely - return; - } - if (sfLog().isInfoEnabled()) { - sfLog().info(CSVFileReadImpl.merge(line)); - } - if (lines.size() > count) { - Vector<String> expected = lines.elementAt(count); - compareLine(count, line, expected); - } - count++; - if (maxCount >= 0 && count > maxCount) { - throw new SmartFrogException("Too many lines", CSVFileReadTester.this); - } - } - if (count < minCount) { - throw new SmartFrogException("Too few lines -expected " + minCount + " but got " + count, + protected void onFinished() throws SmartFrogException, RemoteException { + if (getCount() < minCount) { + throw new SmartFrogException("Too few lines -expected " + + minCount + + " but got " + + getCount(), CSVFileReadTester.this); } - //end of lines } + /** + * Process one line of the data source + * + * @param line line to process + * @throws SmartFrogException SmartFrog problems + * @throws RemoteException network problems + */ + protected void processOneLine(String[] line) throws SmartFrogException, RemoteException { + if (sfLog().isInfoEnabled()) { + sfLog().info(CSVFileReadImpl.merge(line)); + } + int position = getCount(); + if (lines.size() > position) { + Vector<String> expected = lines.elementAt(position); + compareLine(position, line, expected); + } + if (maxCount >= 0 && position > maxCount) { + throw new SmartFrogException("Too many lines", CSVFileReadTester.this); + } + } + /** * compare two lines, fail if they mismatch * - * @param element element number - * @param line line read in + * @param element 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) throws SmartFrogException { + private void compareLine(int element, 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 + " is wrong width; expected " + size + " but got " - + actual + " elements\n" + merged); + throw new SmartFrogException("Line " + + element + + " is wrong width; expected " + + size + + " but got " + + + actual + + " elements\n" + + merged); } for (int i = 0; i < size; i++) { String expectedElt = expected.elementAt(i); @@ -142,7 +168,7 @@ "Line " + element + " does not match expected element " + i + " expected=\"" + expectedElt + '\"' + " actual=\"" + actualElt + '\"' - +":\n" + merged); + + ":\n" + merged); } } Deleted: trunk/core/components/csvfiles/src/org/smartfrog/services/filesystem/csvfiles/CSVReaderThread.java =================================================================== --- trunk/core/components/csvfiles/src/org/smartfrog/services/filesystem/csvfiles/CSVReaderThread.java 2008-03-10 09:34:54 UTC (rev 6131) +++ trunk/core/components/csvfiles/src/org/smartfrog/services/filesystem/csvfiles/CSVReaderThread.java 2008-03-10 09:38:09 UTC (rev 6132) @@ -1,49 +0,0 @@ -/* (C) Copyright 2008 Hewlett-Packard Development Company, LP - -This library is free software; you can redistribute it and/or -modify it under the terms of the GNU Lesser General Public -License as published by the Free Software Foundation; either -version 2.1 of the License, or (at your option) any later version. - -This library is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -Lesser General Public License for more details. - -You should have received a copy of the GNU Lesser General Public -License along with this library; if not, write to the Free Software -Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - -For more information: www.smartfrog.org - -*/ -package org.smartfrog.services.filesystem.csvfiles; - -import org.smartfrog.sfcore.prim.Prim; -import org.smartfrog.sfcore.utils.WorkflowThread; - -/** - * - * Created 25-Feb-2008 12:27:35 - * - */ - -public class CSVReaderThread extends WorkflowThread { - - - /** - * data source - */ - protected CSVFileRead source; - - - /** - * Create a basic thread - * - * @param source the data source - */ - public CSVReaderThread(Prim owner, CSVFileRead source) { - super(owner, true); - this.source = source; - } -} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |