[Batchserver-cvs] batchserver/src/org/jmonks/batchserver/io/flat FixedWidthFlatFileWriter.java, NON
Brought to you by:
suresh_pragada
From: Suresh <sur...@us...> - 2006-06-07 22:26:53
|
Update of /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/flat In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv20246 Modified Files: FixedWidthFlatFileFieldSpec.java FixedWidthFlatFileFileSpec.java FixedWidthFlatFileReader.java FixedWidthFlatFileRecordSpec.java Added Files: FixedWidthFlatFileWriter.java Log Message: no message Index: FixedWidthFlatFileFileSpec.java =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/flat/FixedWidthFlatFileFileSpec.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** FixedWidthFlatFileFileSpec.java 3 Jun 2006 13:14:41 -0000 1.1 --- FixedWidthFlatFileFileSpec.java 6 Jun 2006 21:19:58 -0000 1.2 *************** *** 36,40 **** for(int i=0;i<recordSpecNodeList.getLength();i++) { ! RecordSpec recordSpec=FixedWidthFlatFileRecordSpec.createFixedWidthFlatFileRecordSpec((Element)recordSpecNodeList.item(i)); fileSpec.addRecordSpec(recordSpec); } --- 36,40 ---- for(int i=0;i<recordSpecNodeList.getLength();i++) { ! FixedWidthFlatFileRecordSpec recordSpec=FixedWidthFlatFileRecordSpec.createFixedWidthFlatFileRecordSpec((Element)recordSpecNodeList.item(i)); fileSpec.addRecordSpec(recordSpec); } --- NEW FILE: FixedWidthFlatFileWriter.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.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; /** * * @author Suresh Pragada */ public class FixedWidthFlatFileWriter extends FileWriter { protected String absoluteFilePath=null; protected FixedWidthFlatFileFileSpec fileSpec=null; protected BufferedWriter writer=null; protected boolean writtenFirstLine=false; private static Logger logger=Logger.getLogger(FixedWidthFlatFileWriter.class); public FixedWidthFlatFileWriter(String absoluteFilePath,FileSpec fileSpec) { try { this.absoluteFilePath=absoluteFilePath; this.fileSpec=(FixedWidthFlatFileFileSpec)fileSpec; writer=new BufferedWriter(new java.io.FileWriter(absoluteFilePath)); } catch(IOException exception) { exception.printStackTrace(); logger.fatal("IOException while creating the reader. Message = " + exception.getMessage(),exception); this.writer=null; throw new FileParseException("IOException while creating the reader. Message = " + exception.getMessage()); } } public void close() { if(this.writer!=null) { try { this.writer.close(); } catch(IOException exception) { logger.debug("IOException while closing the reader. Message = " + exception.getMessage(),exception); } finally { this.writer=null; } } } public WriterRecord createWriterRecord(RecordType recordType) { return new FixedWidthFlatFileWriterRecord(recordType); } public void writerRecord(WriterRecord writerRecord) { if(this.writer!=null) { try { FixedWidthFlatFileWriterRecord record=(FixedWidthFlatFileWriterRecord)writerRecord; FixedWidthFlatFileRecordSpec recordSpec=this.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."); } private FixedWidthFlatFileRecordSpec getRecordSpec(RecordType recordType) { List recordSpecList=this.fileSpec.getRecordSpecs(); for(Iterator iterator=recordSpecList.iterator();iterator.hasNext();) { FixedWidthFlatFileRecordSpec recordSpec=(FixedWidthFlatFileRecordSpec)iterator.next(); if(recordSpec.getRecordType()==recordType) return recordSpec; } return null; } public class FixedWidthFlatFileWriterRecord extends WriterRecord { private Map fieldMap=null; private FixedWidthFlatFileWriterRecord(RecordType recordType) { super(recordType); fieldMap=new HashMap(); } public void writeField(String fieldName, Object fieldValue) { this.fieldMap.put(fieldName,fieldValue); } protected Object readField(String fieldName) { if(this.fieldMap.containsKey(fieldName)) return this.fieldMap.get(fieldName); else return ""; } } } Index: FixedWidthFlatFileFieldSpec.java =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/flat/FixedWidthFlatFileFieldSpec.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** FixedWidthFlatFileFieldSpec.java 3 Jun 2006 13:14:41 -0000 1.1 --- FixedWidthFlatFileFieldSpec.java 6 Jun 2006 21:19:58 -0000 1.2 *************** *** 25,28 **** --- 25,30 ---- private int endPosition=0; + private int fieldWidth=0; + public static final String START_POSITION_ATTRIB_NAME = "start-pos"; *************** *** 49,62 **** } ! 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; --- 51,70 ---- } ! public int getFieldWidth() ! { ! return fieldWidth; ! } ! ! public static FixedWidthFlatFileFieldSpec createFixedWidthFlatFileFieldSpec(final Element fieldSpecElement) { String fieldName=fieldSpecElement.getAttribute(FieldSpec.FIELD_NAME_ATTRIB_NAME); ! 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; ! fieldSpec.fieldWidth=(endPosition-startPosition)+1; return fieldSpec; Index: FixedWidthFlatFileReader.java =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/flat/FixedWidthFlatFileReader.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** FixedWidthFlatFileReader.java 5 Jun 2006 03:12:57 -0000 1.2 --- FixedWidthFlatFileReader.java 6 Jun 2006 21:19:58 -0000 1.3 *************** *** 29,33 **** protected String absoluteFilePath=null; ! protected FileSpec fileSpec=null; private BufferedReader reader=null; --- 29,33 ---- protected String absoluteFilePath=null; ! protected FixedWidthFlatFileFileSpec fileSpec=null; private BufferedReader reader=null; *************** *** 38,42 **** { this.absoluteFilePath=absoluteFilePath; ! this.fileSpec=fileSpec; try --- 38,42 ---- { this.absoluteFilePath=absoluteFilePath; ! this.fileSpec=(FixedWidthFlatFileFileSpec)fileSpec; try Index: FixedWidthFlatFileRecordSpec.java =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/flat/FixedWidthFlatFileRecordSpec.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** FixedWidthFlatFileRecordSpec.java 3 Jun 2006 13:14:41 -0000 1.1 --- FixedWidthFlatFileRecordSpec.java 6 Jun 2006 21:19:58 -0000 1.2 *************** *** 26,30 **** protected String startsWith=null; ! /** Creates a new instance of FixedWidthFlatFileRecordSpec */ protected FixedWidthFlatFileRecordSpec(RecordType recordType) { --- 26,31 ---- protected String startsWith=null; ! protected int recordSize=0; ! protected FixedWidthFlatFileRecordSpec(RecordType recordType) { *************** *** 37,40 **** --- 38,46 ---- } + public int getRecordSize() + { + return recordSize; + } + public boolean isMatch(String recordString) { *************** *** 44,48 **** } ! public static RecordSpec createFixedWidthFlatFileRecordSpec(final Element recordSpecElement) { RecordType recordType=RecordType.toRecordType(recordSpecElement.getAttribute(RecordSpec.RECORD_TYPE_ATTRIB_NAME)); --- 50,54 ---- } ! public static FixedWidthFlatFileRecordSpec createFixedWidthFlatFileRecordSpec(final Element recordSpecElement) { RecordType recordType=RecordType.toRecordType(recordSpecElement.getAttribute(RecordSpec.RECORD_TYPE_ATTRIB_NAME)); *************** *** 53,63 **** 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; } --- 59,74 ---- else throw new FileSpecException("Record Spec in Fixed Width File Spec should have starts-with attribute."); + int recordSize=startsWith.length(); NodeList fieldSpecNodeList=recordSpecElement.getElementsByTagName(FieldSpec.FIELD_SPEC_TAG_NAME); for(int i=0;i<fieldSpecNodeList.getLength();i++) { ! FixedWidthFlatFileFieldSpec fieldSpec=FixedWidthFlatFileFieldSpec.createFixedWidthFlatFileFieldSpec((Element)fieldSpecNodeList.item(i)); recordSpec.addFieldSpec(fieldSpec); + if(fieldSpec.getEndPosition()>recordSize) + recordSize=fieldSpec.getEndPosition(); } ! recordSize+=startsWith.length(); ! recordSpec.recordSize=recordSize; ! return recordSpec; } |