[Batchserver-cvs] batchserver/src/org/jmonks/batchserver/io/flat FixedWidthFlatFileFieldSpec.java,
Brought to you by:
suresh_pragada
From: Suresh <sur...@us...> - 2006-06-03 13:14:47
|
Update of /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/flat In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv14982/flat Added Files: FixedWidthFlatFileFieldSpec.java FixedWidthFlatFileFileSpec.java FixedWidthFlatFileReader.java FixedWidthFlatFileRecordSpec.java sample-fixed-width-file-spec.xml sample-fixed-width-file.dat Log Message: no message --- NEW FILE: FixedWidthFlatFileReader.java --- /* * FixedWidthFlatFileReader.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.FileNotFoundException; import java.io.IOException; 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.*; /** * * @author Suresh Pragada */ 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 java.io.FileReader(this.absoluteFilePath)); } catch(FileNotFoundException exception) { reader=null; exception.printStackTrace(); logger.fatal("File could not be found to create the reader. Message = " + exception.getMessage(),exception); throw new FileParseException("File could not be found to create the reader. Message = " + exception.getMessage()); } 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.close(); 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; private FixedWidthFlatFileReaderRecord(RecordType recordType,int fieldCount) { super(recordType); fieldMap=new HashMap(fieldCount); } public Object readField(String fieldName) { return this.fieldMap.get(fieldName); } private void writeField(String fieldName,String fieldValue) { this.fieldMap.put(fieldName,fieldValue); } } } --- NEW FILE: FixedWidthFlatFileFieldSpec.java --- /* * FixedWidthFlatFileFieldSpec.java * * Created on May 26, 2006, 2:43 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 org.apache.log4j.Logger; import org.w3c.dom.Element; import org.jmonks.batchserver.io.*; /** * * @author Suresh Pragada */ public class FixedWidthFlatFileFieldSpec extends FieldSpec { private int startPosition=0; private int endPosition=0; public static final String START_POSITION_ATTRIB_NAME = "start-pos"; public static final String END_POSITION_ATTRIB_NAME = "end-pos"; private static Logger logger=Logger.getLogger(FixedWidthFlatFileFieldSpec.class); /** * Creates a new instance of FixedWidthFlatFileFieldSpec */ protected FixedWidthFlatFileFieldSpec(String fieldName) { super(fieldName); } public int getStartPosition() { return this.startPosition; } public int getEndPosition() { return this.endPosition; } public static FieldSpec createFixedWidthFlatFileFieldSpec(final Element fieldSpecElement) { String fieldName=fieldSpecElement.getAttribute(FieldSpec.FIELD_NAME_ATTRIB_NAME); FieldSpec 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)); ((FixedWidthFlatFileFieldSpec)fieldSpec).startPosition=startPosition; ((FixedWidthFlatFileFieldSpec)fieldSpec).endPosition=endPosition; return fieldSpec; } public String toString() { StringBuffer stringValue=new StringBuffer("{FixedWidthFlatFileFieldSpec "); stringValue.append("[fieldName = " + super.fieldName + "]"); stringValue.append("[startPosition = " + this.startPosition + "]"); stringValue.append("[endPosition = " + this.endPosition+ "]"); stringValue.append("}"); return stringValue.toString(); } } --- NEW FILE: FixedWidthFlatFileFileSpec.java --- /* * FixedWidthFlatFileFileSpec.java * * Created on June 1, 2006, 2:06 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.util.Iterator; import org.apache.log4j.Logger; import org.w3c.dom.Element; import org.w3c.dom.NodeList; import org.jmonks.batchserver.io.*; /** * * @author Suresh Pragada */ public class FixedWidthFlatFileFileSpec extends FileSpec { private static Logger logger=Logger.getLogger(FixedWidthFlatFileFileSpec.class); protected FixedWidthFlatFileFileSpec(String fileSpecPath,FileType fileType) { super(fileSpecPath,fileType); } public static FileSpec createFixedWidthFlatFileFileSpec(final String fileSpecPath,final Element fileSpecElement) { FixedWidthFlatFileFileSpec fileSpec=new FixedWidthFlatFileFileSpec(fileSpecPath,FileType.FIXED_WIDTH_FLAT_FILE); NodeList recordSpecNodeList=fileSpecElement.getElementsByTagName(RecordSpec.RECORD_SPEC_TAG_NAME); for(int i=0;i<recordSpecNodeList.getLength();i++) { RecordSpec recordSpec=FixedWidthFlatFileRecordSpec.createFixedWidthFlatFileRecordSpec((Element)recordSpecNodeList.item(i)); fileSpec.addRecordSpec(recordSpec); } return (FileSpec)fileSpec; } public String toString() { StringBuffer stringValue=new StringBuffer("{FixedWidthFlatFileFileSpec "); stringValue.append("[fileType = " + super.fileType.toString() + "]"); stringValue.append("[recordSpecList = "); for(Iterator iterator=recordSpecList.iterator();iterator.hasNext();) stringValue.append(((FixedWidthFlatFileRecordSpec)iterator.next()).toString()); stringValue.append("]}"); return stringValue.toString(); } } --- NEW FILE: FixedWidthFlatFileRecordSpec.java --- /* * FixedWidthFlatFileRecordSpec.java * * Created on May 26, 2006, 11:33 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.util.Iterator; import org.w3c.dom.Element; import org.w3c.dom.NodeList; import org.jmonks.batchserver.io.*; /** * * @author Suresh Pragada */ public class FixedWidthFlatFileRecordSpec extends RecordSpec { public static final String STARTS_WITH_ATTRIB_NAME = "starts-with"; protected String startsWith=null; /** Creates a new instance of FixedWidthFlatFileRecordSpec */ protected FixedWidthFlatFileRecordSpec(RecordType recordType) { super(recordType); } public String getStartsWith() { return this.startsWith; } 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)); FixedWidthFlatFileRecordSpec recordSpec=new FixedWidthFlatFileRecordSpec(recordType); String startsWith=recordSpecElement.getAttribute(FixedWidthFlatFileRecordSpec.STARTS_WITH_ATTRIB_NAME); if(startsWith!=null && !"".equals(startsWith.trim())) 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++) { FieldSpec fieldSpec=FixedWidthFlatFileFieldSpec.createFixedWidthFlatFileFieldSpec((Element)fieldSpecNodeList.item(i)); recordSpec.addFieldSpec(fieldSpec); } return (RecordSpec)recordSpec; } public String toString() { StringBuffer stringValue=new StringBuffer("{FixedWidthFlatFileRecordSpec "); stringValue.append("[recordType = " + super.recordType.toString() + "]"); stringValue.append("[startsWith = " + this.startsWith + "]"); stringValue.append("[fieldSpecList = "); for(Iterator iterator=fieldSpecList.iterator();iterator.hasNext();) stringValue.append(((FixedWidthFlatFileFieldSpec)iterator.next()).toString()); stringValue.append("]}"); return stringValue.toString(); } } --- NEW FILE: sample-fixed-width-file-spec.xml --- <?xml version="1.0" encoding="UTF-8"?> <file-spec file-type="fixed-width-flat"> <record-spec record-type="header" starts-with="1"> <field-spec field-name="timestamp" start-pos="2" end-pos="9"/> </record-spec> <record-spec record-type="detail" starts-with="5"> <field-spec field-name="field1" start-pos="2" end-pos="11"/> <field-spec field-name="field2" start-pos="12" end-pos="21"/> <field-spec field-name="field3" start-pos="22" end-pos="31"/> <field-spec field-name="field4" start-pos="32" end-pos="41"/> </record-spec> <record-spec record-type="trailor" starts-with="6"> <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> --- NEW FILE: sample-fixed-width-file.dat --- 120060526 512345678910000012.0034343434347878787878 512345678920000013.0034343434347878787878 512345678930000014.0034343434347878787878 512345678940000015.0034343434347878787878 512345678950000016.0034343434347878787878 60000000.00 |