[Batchserver-cvs] batchserver/src/org/jmonks/batchserver/io/flat DelimitedFlatFileFieldSpec.java, N
Brought to you by:
suresh_pragada
Update of /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/flat In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv20357 Modified Files: FixedWidthFlatFileFieldSpec.java FixedWidthFlatFileFileSpec.java FixedWidthFlatFileReader.java FixedWidthFlatFileRecordSpec.java FixedWidthFlatFileWriter.java sample-fixed-width-file-spec.xml Added Files: DelimitedFlatFileFieldSpec.java DelimitedFlatFileFileSpec.java DelimitedFlatFileReader.java DelimitedFlatFileRecordSpec.java DelimitedFlatFileWriter.java FlatFileReader.java FlatFileWriter.java sample-delimited-file-spec.xml sample-delimited-file.dat Log Message: no message --- NEW FILE: DelimitedFlatFileRecordSpec.java --- /* * DelimitedFlatFileRecordSpec.java * * Created on June 10, 2006, 10:55 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.flat; import java.util.Iterator; import org.apache.log4j.Logger; import org.jmonks.batchserver.io.FieldSpec; import org.jmonks.batchserver.io.FileSpecException; import org.jmonks.batchserver.io.RecordSpec; import org.jmonks.batchserver.io.RecordType; import org.w3c.dom.Element; import org.w3c.dom.NodeList; /** * <p> * DelimitedFlatFileRecordSpec represents record-spec element in the file spec * belongs to the delimited flat file type. Along with the <code>record-type</code> attribute * it looks for the <code>delimiter</code> attribute in the record-spec element to parse * and generate the record. Here is a sample spec snippet... * </p> * <p> * <pre> * <file-spec file-type="delimited-flat"> * <record-spec record-type="detail" delimiter="|"> * <!-- field specs will follow here --> * </record-spec> * </file-spec> * </pre> * </p> * * @author Suresh Pragada * @version 1.0 * @since 1.0 */ public class DelimitedFlatFileRecordSpec extends RecordSpec { /** * Represents the delimiter for the record. */ private String delimiter=null; /** * Constant defines the delimiter attribute name which the value is "delimiter" */ public static final String DELIMITER_ATTRIB_NAME = "delimiter"; private static Logger logger=Logger.getLogger(DelimitedFlatFileRecordSpec.class); /** * Constructs the record spec by accepting the record type. */ protected DelimitedFlatFileRecordSpec(RecordType recordType) { super(recordType); } /** * Returns the delimiter which divides the fields in the record. */ public String getDelimiter() { return this.delimiter; } /** * Factory method to create the delimited flat record spec from the given * DOM Element representing the record-spec element in the file spec. * * @param recordSpecElement DOM Element representing the record spec. * * @return Returns the delimited flat file record spec. * * @throws org.jmonks.batchserver.io.FileSpecException If record-spec * doesnt have the delimiter or record-type attribute or index value is repeated on more than one field spec. */ public static DelimitedFlatFileRecordSpec createDelimitedFlatFileRecordSpec(final Element recordSpecElement) { logger.trace("Entering createDelimitedFlatFileRecordSpec"); DelimitedFlatFileRecordSpec recordSpec=null; String configuredRecordType=recordSpecElement.getAttribute(RecordSpec.RECORD_TYPE_ATTRIB_NAME); logger.debug("record specs record-type value = " + configuredRecordType); if(configuredRecordType!=null && !(configuredRecordType.trim().equals(""))) { RecordType recordType=RecordType.toRecordType(configuredRecordType); recordSpec=new DelimitedFlatFileRecordSpec(recordType); } else throw new FileSpecException("Record Spec in Delimited File Spec should have record-type attribute."); String delimiter=recordSpecElement.getAttribute(DelimitedFlatFileRecordSpec.DELIMITER_ATTRIB_NAME); logger.debug("record specs delimiter value = " + delimiter); if(delimiter!=null && !"".equals(delimiter.trim())) recordSpec.delimiter=delimiter.trim(); else throw new FileSpecException("Record Spec in Delimited File Spec should have delimiter attribute."); NodeList fieldSpecNodeList=recordSpecElement.getElementsByTagName(FieldSpec.FIELD_SPEC_TAG_NAME); for(int i=0;i<fieldSpecNodeList.getLength();i++) { DelimitedFlatFileFieldSpec fieldSpec=DelimitedFlatFileFieldSpec.createDelimitedFlatFileFieldSpec((Element)fieldSpecNodeList.item(i)); for(Iterator iterator=recordSpec.getFieldSpecs().iterator();iterator.hasNext();) { DelimitedFlatFileFieldSpec existingFieldSpec=(DelimitedFlatFileFieldSpec)iterator.next(); if(existingFieldSpec.getIndex()==fieldSpec.getIndex()) { throw new FileSpecException("Found the index value " + fieldSpec.getIndex() + " on multiple fields."); } } recordSpec.addFieldSpec(fieldSpec); } return recordSpec; } /** * @see java.lang.Object#toString() */ public String toString() { StringBuffer stringValue=new StringBuffer("{DelimitedFlatFileRecordSpec "); stringValue.append("[recordType = " + super.recordType.toString() + "]"); stringValue.append("[delimiter = " + this.delimiter + "]"); stringValue.append("[fieldSpecList = "); for(Iterator iterator=fieldSpecList.iterator();iterator.hasNext();) stringValue.append(((DelimitedFlatFileFieldSpec)iterator.next()).toString()); stringValue.append("]}"); return stringValue.toString(); } } Index: FixedWidthFlatFileWriter.java =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/flat/FixedWidthFlatFileWriter.java,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** FixedWidthFlatFileWriter.java 9 Jun 2006 21:36:34 -0000 1.5 --- FixedWidthFlatFileWriter.java 10 Jun 2006 17:51:37 -0000 1.6 *************** *** 10,18 **** package org.jmonks.batchserver.io.flat; - - import java.io.BufferedWriter; import java.io.IOException; import java.io.OutputStream; - import java.io.OutputStreamWriter; import java.util.Arrays; import java.util.HashMap; --- 10,15 ---- *************** *** 21,27 **** import java.util.Map; import org.apache.log4j.Logger; - import org.jmonks.batchserver.io.FileParseException; import org.jmonks.batchserver.io.FileSpec; - import org.jmonks.batchserver.io.FileWriter; import org.jmonks.batchserver.io.RecordType; import org.jmonks.batchserver.io.WriterRecord; --- 18,22 ---- *************** *** 40,57 **** * @since 1.0 */ ! public class FixedWidthFlatFileWriter extends FileWriter ! { ! /** ! * File spec to be used in writing the file. ! */ ! protected FixedWidthFlatFileFileSpec fileSpec=null; ! ! private BufferedWriter writer=null; ! /** ! * Flag holds whethe first record has been written or not. This is ! * to identify whether we need to write EOL character before every record. ! */ ! private boolean writtenFirstLine=false; ! private static Logger logger=Logger.getLogger(FixedWidthFlatFileWriter.class); --- 35,40 ---- * @since 1.0 */ ! public class FixedWidthFlatFileWriter extends FlatFileWriter ! { private static Logger logger=Logger.getLogger(FixedWidthFlatFileWriter.class); *************** *** 64,96 **** public FixedWidthFlatFileWriter(OutputStream outputStream,FileSpec fileSpec) { ! logger.trace("Entering FixedWidthFlatFileWriter constructor"); ! this.fileSpec=(FixedWidthFlatFileFileSpec)fileSpec; ! writer=new BufferedWriter(new OutputStreamWriter(outputStream)); ! logger.debug("Done creating and initializing the writer"); ! logger.trace("Exiting FixedWidthFlatFileWriter constructor"); ! } ! ! /** ! * Closes the writer. ! */ ! public void close() ! { ! logger.trace("Entering close"); ! if(this.writer!=null) ! { ! try ! { ! this.writer.close(); ! logger.debug("Writer has been closed"); ! } ! catch(IOException exception) ! { ! logger.debug("IOException while closing the reader. Message = " + exception.getMessage(),exception); ! } ! finally ! { ! this.writer=null; ! } ! } } --- 47,51 ---- public FixedWidthFlatFileWriter(OutputStream outputStream,FileSpec fileSpec) { ! super(outputStream,fileSpec); } *************** *** 123,168 **** * @throws org.jmonks.batchserver.io.FileParseException Problems while trying to write the record. */ ! public void writeRecord(WriterRecord writerRecord) { ! logger.trace("Entering writeRecord"); ! if(writerRecord!=null) { ! ! if(this.writer!=null) ! { ! try ! { ! FixedWidthFlatFileWriterRecord record=(FixedWidthFlatFileWriterRecord)writerRecord; ! FixedWidthFlatFileRecordSpec recordSpec=(FixedWidthFlatFileRecordSpec)this.fileSpec.getRecordSpec(record.getRecordType()); ! char[] recordBuffer=new char[recordSpec.getRecordSize()]; ! Arrays.fill(recordBuffer,' '); ! System.arraycopy(recordSpec.startsWith.toCharArray(), 0, recordBuffer, 0, recordSpec.startsWith.length()); ! List fieldSpecList=recordSpec.getFieldSpecs(); ! for(Iterator iterator=fieldSpecList.iterator();iterator.hasNext();) ! { ! FixedWidthFlatFileFieldSpec fieldSpec=(FixedWidthFlatFileFieldSpec)iterator.next(); ! char[] fieldValue=((String)record.readField(fieldSpec.getFieldName())).toCharArray(); ! System.arraycopy(fieldValue, 0, recordBuffer, fieldSpec.getStartPosition()-1, ! ((fieldValue.length>fieldSpec.getFieldWidth())?fieldSpec.getFieldWidth():fieldValue.length)); ! } ! if(writtenFirstLine) ! this.writer.newLine(); ! else ! writtenFirstLine=true; ! this.writer.write(recordBuffer); ! } ! catch(IOException exception) ! { ! exception.printStackTrace(); ! logger.fatal("IOException while writing the record into the reader. Message = " + exception.getMessage(),exception); ! throw new FileParseException("IOException while creating the reader. Message = " + exception.getMessage()); ! } ! } ! else ! throw new IllegalStateException("FileWriter is not available to write this record. Writer is either closed or not initialized properly."); } ! else ! throw new IllegalArgumentException("Writer record cannot be null to write record into the file."); ! logger.trace("Exiting writeRecord"); } --- 78,99 ---- * @throws org.jmonks.batchserver.io.FileParseException Problems while trying to write the record. */ ! protected String generateRecord(WriterRecord writerRecord) throws IOException { ! logger.trace("Entering generateRecord"); ! FixedWidthFlatFileWriterRecord record=(FixedWidthFlatFileWriterRecord)writerRecord; ! FixedWidthFlatFileRecordSpec recordSpec=(FixedWidthFlatFileRecordSpec)this.fileSpec.getRecordSpec(record.getRecordType()); ! char[] recordBuffer=new char[recordSpec.getRecordSize()]; ! Arrays.fill(recordBuffer,' '); ! System.arraycopy(recordSpec.startsWith.toCharArray(), 0, recordBuffer, 0, recordSpec.startsWith.length()); ! List fieldSpecList=recordSpec.getFieldSpecs(); ! for(Iterator iterator=fieldSpecList.iterator();iterator.hasNext();) { ! FixedWidthFlatFileFieldSpec fieldSpec=(FixedWidthFlatFileFieldSpec)iterator.next(); ! char[] fieldValue=((String)record.readField(fieldSpec.getFieldName())).toCharArray(); ! System.arraycopy(fieldValue, 0, recordBuffer, fieldSpec.getStartPosition()-1, ! ((fieldValue.length>fieldSpec.getFieldWidth())?fieldSpec.getFieldWidth():fieldValue.length)); } ! logger.trace("Exiting generateRecord"); ! return new String(recordBuffer); } --- NEW FILE: DelimitedFlatFileWriter.java --- /* * DelimitedFlatFileWriter.java * * Created on June 10, 2006, 12:38 PM * * 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.flat; import java.io.OutputStream; import org.apache.log4j.Logger; import org.jmonks.batchserver.io.FileSpec; import org.jmonks.batchserver.io.RecordType; import org.jmonks.batchserver.io.WriterRecord; /** * * @author Suresh Pragada */ public class DelimitedFlatFileWriter extends FlatFileWriter { private static Logger logger=Logger.getLogger(DelimitedFlatFileWriter.class); /** Creates a new instance of DelimitedFlatFileWriter */ public DelimitedFlatFileWriter(OutputStream outputStream,FileSpec fileSpec) { super(outputStream,fileSpec); } public WriterRecord createWriterRecord(RecordType recordType) { return null; } protected String generateRecord(WriterRecord writerRecord) throws java.io.IOException { return null; } } --- NEW FILE: sample-delimited-file-spec.xml --- <?xml version="1.0" encoding="UTF-8"?> <file-spec file-type="delimited-flat"> <record-spec record-type="detail" delimiter="|"> <field-spec field-name="field1" index="1"/> <field-spec field-name="field2" index="2"/> <field-spec field-name="field3" index="3"/> <field-spec field-name="field4" index="4"/> </record-spec> </file-spec> --- NEW FILE: DelimitedFlatFileFileSpec.java --- /* * DelimitedFlatFileFileSpec.java * * Created on June 10, 2006, 10:47 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.flat; import java.util.Iterator; import org.apache.log4j.Logger; import org.jmonks.batchserver.io.FileSpec; import org.jmonks.batchserver.io.FileSpecException; import org.jmonks.batchserver.io.FileType; import org.jmonks.batchserver.io.RecordSpec; import org.w3c.dom.Element; import org.w3c.dom.NodeList; /** * <p> * DelimitedFlatFileFileSpec represents the file spec defines the flat file * where all the fields in the records delimited with a particular character or value. * This file-spec doesn't require any special attributes other than file-type, * which should be "delimited-flat". * Here is a sample file spec... * </p> * <p> * <pre> * <file-spec file-type="delimited-flat"> * <!-- record specs will follow here --> * </file-spec> * </pre> * </p> * * @author Suresh Pragada * @version 1.0 * @since 1.0 */ public class DelimitedFlatFileFileSpec extends FileSpec { private static Logger logger=Logger.getLogger(DelimitedFlatFileFileSpec.class); /** * Constructs the DelimitedFlatFileFileSpec. */ protected DelimitedFlatFileFileSpec() { super(FileType.DELIMITED_FLAT_FILE); } /** * Factory method create the delimited flat file spec object from the given * DOM Element representing the file-spec element. * * @param fileSpecElement DOM Element representing the file-spec element. * * @return Returns the instance of DelimitedFlatFileFileSpec. * * @throws org.jmonks.batchserver.io.FileSpecException If more than one record spec is configured. */ public static FileSpec createDelimitedFlatFileFileSpec(final Element fileSpecElement) { logger.trace("Entering createDelimitedFlatFileFileSpec"); DelimitedFlatFileFileSpec fileSpec=new DelimitedFlatFileFileSpec(); NodeList recordSpecNodeList=fileSpecElement.getElementsByTagName(RecordSpec.RECORD_SPEC_TAG_NAME); if(recordSpecNodeList.getLength()>1) { logger.fatal("Delimited file spec should not have more than 1 record spec. Number of record specs found = " + recordSpecNodeList.getLength()); throw new FileSpecException("Delimited file spec should have only one record spec."); } else { DelimitedFlatFileRecordSpec recordSpec=DelimitedFlatFileRecordSpec.createDelimitedFlatFileRecordSpec((Element)recordSpecNodeList.item(0)); logger.debug("Adding the record spec = " + recordSpec.toString()); fileSpec.addRecordSpec(recordSpec); return (FileSpec)fileSpec; } } /** * @see java.lang.Object#toString() */ public String toString() { StringBuffer stringValue=new StringBuffer("{DelimitedFlatFileFileSpec "); stringValue.append("[fileType = " + super.fileType.toString() + "]"); stringValue.append("[recordSpecList = "); for(Iterator iterator=recordSpecMap.values().iterator();iterator.hasNext();) stringValue.append(((DelimitedFlatFileRecordSpec)iterator.next()).toString()); stringValue.append("]}"); return stringValue.toString(); } } Index: FixedWidthFlatFileFileSpec.java =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/flat/FixedWidthFlatFileFileSpec.java,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** FixedWidthFlatFileFileSpec.java 9 Jun 2006 21:36:34 -0000 1.4 --- FixedWidthFlatFileFileSpec.java 10 Jun 2006 17:51:37 -0000 1.5 *************** *** 26,32 **** --- 26,34 ---- * </p> * <p> + * <pre> * <file-spec file-type="fixed-width-flat"> * <!-- record specs will follow here --> * </file-spec> + * </pre> * </p> * *************** *** 48,52 **** /** ! * Factory method create the fixed with flat file spec object from the given * DOM Element representing the file-spec element. * --- 50,54 ---- /** ! * Factory method create the fixed width flat file spec object from the given * DOM Element representing the file-spec element. * Index: FixedWidthFlatFileReader.java =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/flat/FixedWidthFlatFileReader.java,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** FixedWidthFlatFileReader.java 9 Jun 2006 21:36:34 -0000 1.5 --- FixedWidthFlatFileReader.java 10 Jun 2006 17:51:37 -0000 1.6 *************** *** 10,18 **** package org.jmonks.batchserver.io.flat; - - import java.io.BufferedReader; - import java.io.IOException; import java.io.InputStream; - import java.io.InputStreamReader; import java.util.HashMap; import java.util.Iterator; --- 10,14 ---- *************** *** 35,47 **** * @since 1.0 */ ! public class FixedWidthFlatFileReader extends FileReader { - /** - * File spec to be used to read the file. - */ - protected FixedWidthFlatFileFileSpec fileSpec=null; - - private BufferedReader reader=null; - private static Logger logger=Logger.getLogger(FixedWidthFlatFileReader.class); --- 31,36 ---- * @since 1.0 */ ! public class FixedWidthFlatFileReader extends FlatFileReader { private static Logger logger=Logger.getLogger(FixedWidthFlatFileReader.class); *************** *** 51,134 **** * @param fileInputStream Input stream to the file. * @param fileSpec File spec to be used to read the file. - * - * @throws org.jmonks.batchserver.io.FileParseException If there is a problem - * initializing the reader. */ public FixedWidthFlatFileReader(InputStream fileInputStream,FileSpec fileSpec) { ! logger.trace("Entering FixedWidthFlatFileReader constructor"); ! this.fileSpec=(FixedWidthFlatFileFileSpec)fileSpec; ! reader=new BufferedReader(new InputStreamReader(fileInputStream)); ! logger.debug("Created the reader successfully"); ! } ! ! /** ! * <p> ! * Gets the next available record from the file and returns it wrapped ! * with the FixedWidthFlatFileReaderRecod object to easily access the data ! * fields from the record. If you defined multiple record specs, make sure ! * to verify the recrod type before you read the values. ! * </p> ! * ! * @return Returns the next available record. ! * ! * @throws org.jmonks.batchserver.io.FileParseException Problems while parsing the ! * next record. This includes unable to identify the recod with the availble record specs. ! */ ! public ReaderRecord getNextRecord() ! { ! logger.trace("Entering getNextRecord"); ! if(this.reader==null) ! return null; ! else ! { ! try ! { ! String recordString=this.reader.readLine(); ! if(recordString==null) ! { ! logger.info("Reader has reached EOF.. Closing the reader."); ! this.reader.close(); ! this.reader=null; ! return null; ! } ! else ! { ! ReaderRecord record=this.parseRecord(recordString); ! return record; ! } ! } ! catch(IOException exception) ! { ! logger.error("IOException while retrieving the next record. Message = " + exception.getMessage(),exception); ! throw new FileParseException("IO Exception while retrieving the next record. Message = " + exception.getMessage()); ! } ! } ! } ! ! /** ! * Closes the reader. ! */ ! public void close() ! { ! logger.trace("Entering close"); ! if(this.reader!=null) ! { ! try ! { ! this.reader.close(); ! logger.info("Reader has been closed"); ! } ! catch(IOException exception) ! { ! logger.debug("IO Exception while closing the reader. Message = " + exception.getMessage(),exception); ! } ! finally ! { ! this.reader=null; ! } ! } } ! /** * Parses the given record string, translates it into the proper ReaderRecord --- 40,49 ---- * @param fileInputStream Input stream to the file. * @param fileSpec File spec to be used to read the file. */ public FixedWidthFlatFileReader(InputStream fileInputStream,FileSpec fileSpec) { ! super(fileInputStream,fileSpec); } ! /** * Parses the given record string, translates it into the proper ReaderRecord *************** *** 140,145 **** * next record. This includes unable to identify the recod with the availble record specs. */ ! private ReaderRecord parseRecord(String recordString) { FixedWidthFlatFileReaderRecord record=null; List recordSpecList=this.fileSpec.getRecordSpecs(); --- 55,61 ---- * next record. This includes unable to identify the recod with the availble record specs. */ ! protected ReaderRecord parseRecord(String recordString) { + logger.trace("Entering parseRecord"); FixedWidthFlatFileReaderRecord record=null; List recordSpecList=this.fileSpec.getRecordSpecs(); *************** *** 172,175 **** --- 88,92 ---- throw new FileParseException("Record " + recordString + " cannot be matched with any configured record specs."); } + logger.trace("Entering parseRecord"); return record; } Index: FixedWidthFlatFileFieldSpec.java =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/flat/FixedWidthFlatFileFieldSpec.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** FixedWidthFlatFileFieldSpec.java 8 Jun 2006 22:21:15 -0000 1.3 --- FixedWidthFlatFileFieldSpec.java 10 Jun 2006 17:51:37 -0000 1.4 *************** *** 18,22 **** * <p> * FixedWidthFlatFileFieldSpec represents field-spec element in the record spec ! * belongs to the fixed with flat file type. Along with the field-name attribute * it looks for the start-pos and end-pos attributes in the field-spec element to retrieve * the field from the record. Filed name on field-spec should be unique across the record-spec. --- 18,22 ---- * <p> * FixedWidthFlatFileFieldSpec represents field-spec element in the record spec ! * belongs to the fixed width flat file type. Along with the field-name attribute * it looks for the start-pos and end-pos attributes in the field-spec element to retrieve * the field from the record. Filed name on field-spec should be unique across the record-spec. *************** *** 25,28 **** --- 25,29 ---- * </p> * <p> + * <pre> * <file-spec file-type="fixed-width-flat"> * <record-spec record-type="detail" starts-with="5"> *************** *** 31,34 **** --- 32,36 ---- * </record-spec> * </file-spec> + * </pre> * </p> * *************** *** 107,115 **** FixedWidthFlatFileFieldSpec fieldSpec=new FixedWidthFlatFileFieldSpec(fieldName); ! int startPosition=Integer.parseInt(fieldSpecElement.getAttribute(FixedWidthFlatFileFieldSpec.START_POSITION_ATTRIB_NAME)); ! int endPosition=Integer.parseInt(fieldSpecElement.getAttribute(FixedWidthFlatFileFieldSpec.END_POSITION_ATTRIB_NAME)); ! fieldSpec.startPosition=startPosition; ! fieldSpec.endPosition=endPosition; return fieldSpec; --- 109,123 ---- FixedWidthFlatFileFieldSpec fieldSpec=new FixedWidthFlatFileFieldSpec(fieldName); ! String startPosition=fieldSpecElement.getAttribute(FixedWidthFlatFileFieldSpec.START_POSITION_ATTRIB_NAME); ! if(startPosition!=null && !"".equals(startPosition.trim())) ! fieldSpec.startPosition=Integer.parseInt(startPosition); ! else ! throw new FileSpecException("start-pos attribute is required in fixed width flat file spec element."); ! String endPosition=fieldSpecElement.getAttribute(FixedWidthFlatFileFieldSpec.END_POSITION_ATTRIB_NAME); ! if(endPosition!=null && !"".equals(endPosition.trim())) ! fieldSpec.startPosition=Integer.parseInt(endPosition); ! else ! throw new FileSpecException("end-pos attribute is required in fixed width flat file spec element."); return fieldSpec; --- NEW FILE: FlatFileWriter.java --- /* * FixedWidthFlatFileWriter.java * * Created on June 6, 2006, 8:58 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.flat; import java.io.BufferedWriter; import java.io.IOException; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.util.Arrays; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import org.apache.log4j.Logger; import org.jmonks.batchserver.io.FileParseException; import org.jmonks.batchserver.io.FileSpec; import org.jmonks.batchserver.io.FileWriter; import org.jmonks.batchserver.io.RecordType; import org.jmonks.batchserver.io.WriterRecord; /** * <p> * FlatFileWriter writes/generates the file according to the given file spec * with the data submitted in the form of WriterRecord's. This provides the methods * to create the required writer records and write the writer record into the file writer. * Once finished writing of all the records file writer should be closed by calling * the "close" method. * </p> * * @author Suresh Pragada * @version 1.0 * @since 1.0 */ public abstract class FlatFileWriter extends FileWriter { /** * File spec to be used in writing the file. */ protected FileSpec fileSpec=null; /** * Buffered writer to conveniently write each record as a new line into the file. */ private BufferedWriter writer=null; /** * Flag holds whethe first record has been written or not. This is * to identify whether we need to write EOL character before every record. */ private boolean writtenFirstLine=false; private static Logger logger=Logger.getLogger(FlatFileWriter.class); /** * Constructs and initializes the writer with the given values. * * @param outputStream Output stream to write the records. * @param fileSpec File spec to be used to generate the file. */ public FlatFileWriter(OutputStream outputStream,FileSpec fileSpec) { logger.trace("Entering FlatFileWriter constructor"); this.fileSpec=fileSpec; writer=new BufferedWriter(new OutputStreamWriter(outputStream)); logger.info("Done creating the flat file writer."); } /** * Creates the writer record assocites with the given record type. * IllegalArgumentException will be thrown, if there is no record * spec is found with this record type. * * @param recordType Type fo the record to be created. * * @return Returns the requested writer record. * * @throws IllegalArgumentException No record spec is found with this record type. */ public abstract WriterRecord createWriterRecord(RecordType recordType); /** * Generates the string representation of the record from given * writer record. * * @param writerRecord Record where field values can be read. * * @return Returns the string representation of the record. */ protected abstract String generateRecord(WriterRecord writerRecord) throws IOException; /** * Writes the given record into the file/writer. * * @param writerRecord Record consist of field names and values. * * @throws IllegalStateException If writer is closed and trying to write the recrod. * @throws IllegalArgumentException If writer record is null. * @throws org.jmonks.batchserver.io.FileParseException Problems while trying to write the record. */ public void writeRecord(WriterRecord writerRecord) { logger.trace("Entering writeRecord"); if(writerRecord!=null) { if(this.writer!=null) { try { String record=generateRecord(writerRecord); if(writtenFirstLine) this.writer.newLine(); else writtenFirstLine=true; this.writer.write(record); } catch(IOException exception) { exception.printStackTrace(); logger.fatal("IOException while writing the record into the reader. Message = " + exception.getMessage(),exception); throw new FileParseException("IOException while creating the reader. Message = " + exception.getMessage()); } } else throw new IllegalStateException("FileWriter is not available to write this record. Writer is either closed or not initialized properly."); } else throw new IllegalArgumentException("Writer record cannot be null to write record into the file."); logger.trace("Exiting writeRecord"); } /** * Closes the writer and all the resource being used by this writer. * This includes the output stream passed to construct the writer. */ public void close() { logger.trace("Entering close"); if(this.writer!=null) { try { this.writer.close(); logger.debug("Writer has been closed"); } catch(IOException exception) { logger.debug("IOException while closing the reader. Message = " + exception.getMessage(),exception); } finally { this.writer=null; } } } } Index: FixedWidthFlatFileRecordSpec.java =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/flat/FixedWidthFlatFileRecordSpec.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** FixedWidthFlatFileRecordSpec.java 8 Jun 2006 22:21:15 -0000 1.3 --- FixedWidthFlatFileRecordSpec.java 10 Jun 2006 17:51:37 -0000 1.4 *************** *** 20,29 **** * <p> * FixedWidthFlatFileRecordSpec represents record-spec element in the file spec ! * belongs to the fixed with flat file type. Along with the record-type attribute ! * it looks for the starts-with attribute in the record-spec element to identify * the record. Record type and starts-with attribute values should be uniquej across * the file-spec. Here is a sample spec snippet... * </p> * <p> * <file-spec file-type="fixed-width-flat"> * <record-spec record-type="detail" starts-with="5"> --- 20,30 ---- * <p> * FixedWidthFlatFileRecordSpec represents record-spec element in the file spec ! * belongs to the fixed width flat file type. Along with the <code>record-type</code> attribute ! * it looks for the <code>starts-with</code> attribute in the record-spec element to identify * the record. Record type and starts-with attribute values should be uniquej across * the file-spec. Here is a sample spec snippet... * </p> * <p> + * <pre> * <file-spec file-type="fixed-width-flat"> * <record-spec record-type="detail" starts-with="5"> *************** *** 31,34 **** --- 32,36 ---- * </record-spec> * </file-spec> + * </pre> * </p> * Index: sample-fixed-width-file-spec.xml =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/flat/sample-fixed-width-file-spec.xml,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** sample-fixed-width-file-spec.xml 3 Jun 2006 13:14:41 -0000 1.1 --- sample-fixed-width-file-spec.xml 10 Jun 2006 17:51:37 -0000 1.2 *************** *** 14,19 **** <field-spec field-name="recordCount" start-pos="2" end-pos="9"/> </record-spec> - <!-- <record-spec record-type="detail" delimiter="|"> - <field-spec field-name="pmtId" index="1"/> - </record-spec> --> </file-spec> --- 14,16 ---- --- NEW FILE: sample-delimited-file.dat --- 1234567891|0000012.00|3434343434|7878787878 1234567892|0000013.00|3434343434|7878787878 1234567893|0000014.00|3434343434|7878787878 1234567894|0000015.00|3434343434|7878787878 1234567895|0000016.00|3434343434|7878787878 --- NEW FILE: DelimitedFlatFileFieldSpec.java --- /* * DelimitedFlatFileFieldSpec.java * * Created on June 10, 2006, 11:09 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.flat; import org.apache.log4j.Logger; import org.jmonks.batchserver.io.FieldSpec; import org.jmonks.batchserver.io.FileSpecException; import org.w3c.dom.Element; /** * <p> * DelimitedFlatFileFieldSpec represents field-spec element in the record spec * belongs to the delimited flat file type. Along with the field-name attribute * it looks for the <code>index</code> attribute in the field-spec element to retrieve * the field from the record. It specifies the field position in the record. * Field index value starts with 1. * Filed name on field-spec should be unique across the record-spec. * Here is a sample spec snippet... * </p> * <p> * <pre> * <file-spec file-type="delimited-flat"> * <record-spec record-type="detail" delimiter="|"> * <field-spec field-name="consumer-id" index="1"/> * <field-spec field-name="consumer-name" index="2"/> * </record-spec> * </file-spec> * </pre> * </p> * * @author Suresh Pragada * @version 1.0 * @since 1.0 */ public class DelimitedFlatFileFieldSpec extends FieldSpec { /** * Represents the field position in the record. */ private int index; /** * Constant defines the index attribute name which is "index" */ public static final String INDEX_ATTRIB_NAME = "index"; private static Logger logger=Logger.getLogger(DelimitedFlatFileFieldSpec.class); /** * Creates a new instance of DelimitedFlatFileFieldSpec by using field name. */ protected DelimitedFlatFileFieldSpec(String fieldName) { super(fieldName); } /** * Returns the index of field in the record. Index value starts with 1. */ public int getIndex() { return this.index; } /** * Factory method to create the DelimitedFlatFileFieldSpec instance from * the given DOM Element representing the filed-spec element in record spec. * * @param fieldSpecElement DOM Element representing the field-spec element. * * @return Returns the DelimitedFlatFileFieldSpec instance. * * @throws org.jmonks.batchserver.io.FileSpecException If field-spec * field-name values are not unique across record-spec and it is missing index attribute. */ public static DelimitedFlatFileFieldSpec createDelimitedFlatFileFieldSpec(final Element fieldSpecElement) { logger.trace("Entering createDelimitedFlatFileFieldSpec"); String fieldName=fieldSpecElement.getAttribute(FieldSpec.FIELD_NAME_ATTRIB_NAME); DelimitedFlatFileFieldSpec fieldSpec=new DelimitedFlatFileFieldSpec(fieldName); String index=fieldSpecElement.getAttribute(DelimitedFlatFileFieldSpec.INDEX_ATTRIB_NAME); if(index!=null && !"".equals(index.trim())) fieldSpec.index=Integer.parseInt(index); else throw new FileSpecException("index attribute is required in delimited flat file spec element."); return fieldSpec; } /** * @see java.lang.Object#toString() */ public String toString() { StringBuffer stringValue=new StringBuffer("{DelimitedFlatFileFieldSpec "); stringValue.append("[fieldName = " + super.fieldName + "]"); stringValue.append("[index = " + this.index + "]"); stringValue.append("}"); return stringValue.toString(); } } --- NEW FILE: DelimitedFlatFileReader.java --- /* * DelimitedFlatFileReader.java * * Created on June 10, 2006, 12:37 PM * * 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.flat; import java.io.InputStream; import org.apache.log4j.Logger; import org.jmonks.batchserver.io.FileSpec; import org.jmonks.batchserver.io.ReaderRecord; /** * * @author Suresh Pragada */ public class DelimitedFlatFileReader extends FlatFileReader { private static Logger logger=Logger.getLogger(DelimitedFlatFileReader.class); public DelimitedFlatFileReader(InputStream fileInputStream,FileSpec fileSpec) { super(fileInputStream, fileSpec); } protected ReaderRecord parseRecord(String recordString) { return null; } } --- NEW FILE: FlatFileReader.java --- /* * FlatFileReader.java * * Created on May 26, 2006, 2:15 PM * * 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.flat; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import org.apache.log4j.Logger; import org.jmonks.batchserver.io.*; /** * <p> * FlatFileReader reads the specified fixed width flat file according to the given file spec * and returns the recrods on the needed basis. Each field value from the record * should be read using readField method by passing the fieldName mentioned in * the file spec. To find out how to read each record from the file and to read * the each field from the record, refer to the FileReader javadoc. * </p> * * @author Suresh Pragada * @version 1.0 * @since 1.0 */ public abstract class FlatFileReader extends FileReader { /** * File spec to be used to read the flat file. */ protected FileSpec fileSpec=null; /** * Buffered reader to flexibly read each line as record from the file. */ private BufferedReader reader=null; private static Logger logger=Logger.getLogger(FlatFileReader.class); /** * Constructs and initializes the flat file reader with the given file path and file spec. * * @param fileInputStream Input stream to the file. * @param fileSpec File spec to be used to read the file. */ public FlatFileReader(InputStream fileInputStream,FileSpec fileSpec) { logger.trace("Entering FlatFileReader constructor"); this.fileSpec=fileSpec; reader=new BufferedReader(new InputStreamReader(fileInputStream)); logger.info("Done creating the flat file reader."); } /** * <p> * Gets the next available record from the file and returns it wrapped * with the appropriate ReaderRecod object to easily access the data * fields from the record. If you defined multiple record specs, make sure * to verify the recrod type before you read the values. * </p> * * @return Returns the next available record. * * @throws org.jmonks.batchserver.io.FileParseException Problems while parsing the * next record. This includes unable to identify the recod with the availble record specs. */ public ReaderRecord getNextRecord() { logger.trace("Entering getNextRecord"); if(this.reader==null) return null; else { try { String recordString=this.reader.readLine(); if(recordString==null) { logger.info("Reader has reached EOF.. Closing the reader."); this.reader.close(); this.reader=null; return null; } else { ReaderRecord record=this.parseRecord(recordString); return record; } } catch(IOException exception) { logger.error("IOException while retrieving the next record. Message = " + exception.getMessage(),exception); throw new FileParseException("IO Exception while retrieving the next record. Message = " + exception.getMessage()); } } } /** * Closes the reader and all the resources it is using. This includes the * input stream passed to create the reader. */ public void close() { logger.trace("Entering close"); if(this.reader!=null) { try { this.reader.close(); logger.info("Reader has been closed"); } catch(IOException exception) { logger.debug("IO Exception while closing the reader. Message = " + exception.getMessage(),exception); } finally { this.reader=null; } } } /** * Parses the given record string based on the record spec, * translates it into the appropriate ReaderRecord * and returns that reader record. * * @param recordString Next available line read from the file. * * @throws org.jmonks.batchserver.io.FileParseException Problems while parsing the * next record. This includes unable to identify the recod with the availble record specs. */ protected abstract ReaderRecord parseRecord(String recordString); } |