[Batchserver-cvs] batchserver/src/org/jmonks/batchserver/io ReaderRecord.java, NONE, 1.1 WriterReco
Brought to you by:
suresh_pragada
From: Suresh <sur...@us...> - 2006-06-02 21:02:13
|
Update of /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv8745 Modified Files: FileParseException.java FileReader.java FixedWidthFlatFileReader.java FixedWidthFlatFileRecordSpec.java Record.java RecordSpec.java Added Files: ReaderRecord.java WriterRecord.java Removed Files: FileIO.java Log Message: no message Index: FileParseException.java =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/FileParseException.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** FileParseException.java 30 May 2006 17:09:43 -0000 1.1 --- FileParseException.java 2 Jun 2006 21:02:06 -0000 1.2 *************** *** 19,24 **** /** Creates a new instance of FileParseException */ ! public FileParseException() { } --- 19,25 ---- /** Creates a new instance of FileParseException */ ! public FileParseException(String message) { + super(message); } Index: Record.java =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/Record.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** Record.java 26 May 2006 22:22:26 -0000 1.1 --- Record.java 2 Jun 2006 21:02:06 -0000 1.2 *************** *** 21,34 **** * @since 1.0 */ ! public class Record { private RecordType recordType=null; - private Map fieldMap=null; /** Creates a new instance of Record */ public Record(RecordType recordType) { this.recordType=recordType; - fieldMap=new HashMap(); } --- 21,32 ---- * @since 1.0 */ ! public abstract class Record { private RecordType recordType=null; /** Creates a new instance of Record */ public Record(RecordType recordType) { this.recordType=recordType; } *************** *** 38,50 **** } - public void setField(String fieldName,String fieldValue) - { - this.fieldMap.put(fieldName,fieldValue); - } - - public Object getField(String fieldName) - { - return this.fieldMap.get(fieldName); - } - } --- 36,38 ---- Index: FixedWidthFlatFileReader.java =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/FixedWidthFlatFileReader.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** FixedWidthFlatFileReader.java 2 Jun 2006 04:40:07 -0000 1.3 --- FixedWidthFlatFileReader.java 2 Jun 2006 21:02:06 -0000 1.4 *************** *** 16,19 **** --- 16,23 ---- import java.io.IOException; import java.io.InputStreamReader; + import java.util.HashMap; + import java.util.Iterator; + import java.util.List; + import java.util.Map; import org.apache.log4j.Logger; *************** *** 24,61 **** public class FixedWidthFlatFileReader extends FileReader { private BufferedReader reader=null; private static Logger logger=Logger.getLogger(FixedWidthFlatFileReader.class); ! public void initialize() { try { ! reader=new BufferedReader(new InputStreamReader(new FileInputStream(new File(super.filePath)))); } catch(IOException exception) { exception.printStackTrace(); logger.fatal("IO Exception while initializing the file reader. Message = " + exception.getMessage(),exception); ! throw FileParseException("IO Exception while initializing the file reader. Message = " + exception.getMessage()); } } ! public Record getNextRecord() ! { ! String record=reader.readLine(); ! return null; ! } ! ! public boolean hasMoreRecords() { } ! ! public boolean hasMoreRecords() { ! } ! ! public void initialize(File inputFile, FileSpec fileSpec) { } } --- 28,139 ---- public class FixedWidthFlatFileReader extends FileReader { + protected String absoluteFilePath=null; + + protected FileSpec fileSpec=null; + private BufferedReader reader=null; private static Logger logger=Logger.getLogger(FixedWidthFlatFileReader.class); ! public FixedWidthFlatFileReader(String absoluteFilePath,FileSpec fileSpec) { + this.absoluteFilePath=absoluteFilePath; + this.fileSpec=fileSpec; + try { ! reader=new BufferedReader(new InputStreamReader(new FileInputStream(new File(this.absoluteFilePath)))); } catch(IOException exception) { + reader=null; exception.printStackTrace(); logger.fatal("IO Exception while initializing the file reader. Message = " + exception.getMessage(),exception); ! throw new FileParseException("IO Exception while initializing the file reader. Message = " + exception.getMessage()); } } ! public ReaderRecord getNextRecord() { + if(this.reader==null) + return null; + else + { + try + { + String recordString=this.reader.readLine(); + if(recordString==null) + { + this.reader=null; + return null; + } + else + { + ReaderRecord record=this.parseRecord(recordString); + return record; + } + } + catch(IOException exception) + { + logger.info("IOException while retrieving the next record. Message = " + exception.getMessage(),exception); + throw new FileParseException("IO Exception while retrieving the next record. Message = " + exception.getMessage()); + } + } } ! ! private ReaderRecord parseRecord(String recordString) { ! FixedWidthFlatFileReaderRecord record=null; ! List recordSpecList=this.fileSpec.getRecordSpecs(); ! for(Iterator recordSpecIterator=recordSpecList.iterator();recordSpecIterator.hasNext();) ! { ! FixedWidthFlatFileRecordSpec recordSpec=(FixedWidthFlatFileRecordSpec)recordSpecIterator.next(); ! if(recordSpec.isMatch(recordString)) ! { ! List fieldSpecList=recordSpec.getFieldSpecs(); ! record=new FixedWidthFlatFileReaderRecord(recordSpec.getRecordType(),fieldSpecList.size()); ! for(Iterator fieldSpecIterator=fieldSpecList.iterator();fieldSpecIterator.hasNext();) ! { ! FixedWidthFlatFileFieldSpec fieldSpec=(FixedWidthFlatFileFieldSpec)fieldSpecIterator.next(); ! String fieldValue=null; ! try ! { ! fieldValue=recordString.substring(fieldSpec.getStartPosition(),fieldSpec.getEndPosition()); ! } ! catch(IndexOutOfBoundsException exception) ! { ! exception.printStackTrace(); ! logger.fatal("Could not retrieve the field " + fieldSpec.getFieldName() + " from the record string " + ! recordString + " from the positions start = " + fieldSpec.getStartPosition() + " end = " + fieldSpec.getEndPosition()); ! } ! record.writeField(fieldSpec.getFieldName(),fieldValue); ! } ! break; ! } ! if(!recordSpecIterator.hasNext()) ! throw new FileParseException("Record " + recordString + " cannot be matched with any configured record specs."); ! } ! return record; ! } ! ! public class FixedWidthFlatFileReaderRecord extends ReaderRecord { + private Map fieldMap=null; + + protected FixedWidthFlatFileReaderRecord(RecordType recordType,int fieldCount) + { + super(recordType); + fieldMap=new HashMap(fieldCount); + } + + public Object readField(String fieldName) + { + return this.fieldMap.get(fieldName); + } + + protected void writeField(String fieldName,String fieldValue) + { + this.fieldMap.put(fieldName,fieldValue); + } } } Index: RecordSpec.java =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/RecordSpec.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** RecordSpec.java 1 Jun 2006 22:11:41 -0000 1.3 --- RecordSpec.java 2 Jun 2006 21:02:06 -0000 1.4 *************** *** 47,49 **** --- 47,54 ---- return fieldSpecList; } + + public RecordType getRecordType() + { + return this.recordType; + } } --- NEW FILE: WriterRecord.java --- /* * WriterRecord.java * * Created on June 2, 2006, 11:26 AM * * To change this template, choose Tools | Options and locate the template under * the Source Creation and Management node. Right-click the template and choose * Open. You can then make changes to the template in the Source Editor. */ package org.jmonks.batchserver.io; import org.apache.xml.resolver.apps.resolver; /** * * @author Suresh Pragada */ public abstract class WriterRecord extends Record { /** Creates a new instance of WriterRecord */ public WriterRecord(RecordType recordType) { super(recordType); } public abstract void writeField(String fieldName,Object fieldValue); } --- FileIO.java DELETED --- --- NEW FILE: ReaderRecord.java --- /* * ReaderRecord.java * * Created on June 2, 2006, 11:24 AM * * To change this template, choose Tools | Options and locate the template under * the Source Creation and Management node. Right-click the template and choose * Open. You can then make changes to the template in the Source Editor. */ package org.jmonks.batchserver.io; /** * * @author Suresh Pragada */ public abstract class ReaderRecord extends Record { /** Creates a new instance of ReaderRecord */ public ReaderRecord(RecordType recordType) { super(recordType); } public abstract Object readField(String fieldName); } Index: FixedWidthFlatFileRecordSpec.java =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/FixedWidthFlatFileRecordSpec.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** FixedWidthFlatFileRecordSpec.java 1 Jun 2006 22:11:41 -0000 1.2 --- FixedWidthFlatFileRecordSpec.java 2 Jun 2006 21:02:06 -0000 1.3 *************** *** 36,44 **** } public static RecordSpec createFixedWidthFlatFileRecordSpec(final Element recordSpecElement) { RecordType recordType=RecordType.toRecordType(recordSpecElement.getAttribute(RecordSpec.RECORD_TYPE_ATTRIB_NAME)); RecordSpec recordSpec=new FixedWidthFlatFileRecordSpec(recordType); ! ((FixedWidthFlatFileRecordSpec)recordSpec).startsWith=recordSpecElement.getAttribute(FixedWidthFlatFileRecordSpec.STARTS_WITH_ATTRIB_NAME); NodeList fieldSpecNodeList=recordSpecElement.getElementsByTagName(FieldSpec.FIELD_SPEC_TAG_NAME); for(int i=0;i<fieldSpecNodeList.getLength();i++) --- 36,55 ---- } + public boolean isMatch(String recordString) + { + if(recordString==null) + throw new IllegalArgumentException("Record string cannot be null to match record spec."); + return recordString.startsWith(this.startsWith); + } + public static RecordSpec createFixedWidthFlatFileRecordSpec(final Element recordSpecElement) { RecordType recordType=RecordType.toRecordType(recordSpecElement.getAttribute(RecordSpec.RECORD_TYPE_ATTRIB_NAME)); RecordSpec recordSpec=new FixedWidthFlatFileRecordSpec(recordType); ! String startsWith=recordSpecElement.getAttribute(FixedWidthFlatFileRecordSpec.STARTS_WITH_ATTRIB_NAME); ! if(startsWith!=null && startsWith.trim().equals("")) ! ((FixedWidthFlatFileRecordSpec)recordSpec).startsWith=startsWith.trim(); ! else ! throw new FileSpecException("Record Spec in Fixed Width File Spec should have starts-with attribute."); NodeList fieldSpecNodeList=recordSpecElement.getElementsByTagName(FieldSpec.FIELD_SPEC_TAG_NAME); for(int i=0;i<fieldSpecNodeList.getLength();i++) Index: FileReader.java =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/FileReader.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** FileReader.java 2 Jun 2006 04:40:07 -0000 1.2 --- FileReader.java 2 Jun 2006 21:02:06 -0000 1.3 *************** *** 15,28 **** /** * * @author Suresh Pragada */ ! public interface FileReader { ! void initialize(File inputFile,FileSpec fileSpec); ! ! Record getNextRecord(); ! boolean hasMoreRecords(); } --- 15,53 ---- + + /** * * @author Suresh Pragada */ ! public abstract class FileReader { ! public abstract ReaderRecord getNextRecord(); ! public static FileReader getFileReader(String absoluteFilePath,String absoluteFileSpecPath) ! { ! if(absoluteFilePath==null) ! throw new IllegalArgumentException("Absolute file path to create the reader cannot be null."); ! else if(!(new File(absoluteFilePath).exists() && new File(absoluteFilePath).isFile())) ! throw new IllegalArgumentException("Absolute file path to create the reader should exist and should be a file. Given absolute file path = " + absoluteFilePath); ! ! if(absoluteFileSpecPath==null) ! throw new IllegalArgumentException("Absolute file path to read the file spec cannot be null."); ! else if(!(new File(absoluteFileSpecPath).exists() && new File(absoluteFileSpecPath).isFile())) ! throw new IllegalArgumentException("Absolute file path to read the file spec should exist and should be a file. Given absolute file spec path = " + absoluteFilePath); ! ! FileSpec fileSpec=FileSpec.createFileSpec(absoluteFileSpecPath); ! if(fileSpec.getFileType()==FileType.FIXED_WIDTH_FLAT_FILE) ! { ! return new FixedWidthFlatFileReader(absoluteFilePath,fileSpec); ! } ! else if(fileSpec.getFileType()==FileType.DELIMITED_FLAT_FILE) ! { ! return null; // Create Demlited Flat file reader. ! } ! else ! { ! throw new FileSpecException("Unsupported file type in the file spec = " + fileSpec.getFileType().toString()); ! } ! } } |