[Batchserver-cvs] batchserver/src/org/jmonks/batchserver/io/flat FixedWidthFlatFileFieldSpec.java,
Brought to you by:
suresh_pragada
From: Suresh <sur...@us...> - 2006-06-08 22:21:22
|
Update of /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/flat In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv22592/flat Modified Files: FixedWidthFlatFileFieldSpec.java FixedWidthFlatFileFileSpec.java FixedWidthFlatFileReader.java FixedWidthFlatFileRecordSpec.java 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.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** FixedWidthFlatFileFileSpec.java 6 Jun 2006 21:19:58 -0000 1.2 --- FixedWidthFlatFileFileSpec.java 8 Jun 2006 22:21:15 -0000 1.3 *************** *** 18,23 **** /** * ! * @author Suresh Pragada */ public class FixedWidthFlatFileFileSpec extends FileSpec --- 18,37 ---- /** + * <p> + * FixedWidthFlatFileFileSpec represents the file spec defines the flat file + * where each different kind of record starts with a particular value and the + * fields in each record will have start and end position. This file-spec doesn't + * require any special attributes other than file-type, which should be "fixed-width-flat". + * Here is a sample file spec... + * </p> + * <p> + * <file-spec file-type="fixed-width-flat"> + * <!-- record specs will follow here --> + * </file-spec> + * </p> * ! * @author Suresh Pragada ! * @version 1.0 ! * @since 1.0 */ public class FixedWidthFlatFileFileSpec extends FileSpec *************** *** 25,40 **** 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++) { FixedWidthFlatFileRecordSpec recordSpec=FixedWidthFlatFileRecordSpec.createFixedWidthFlatFileRecordSpec((Element)recordSpecNodeList.item(i)); fileSpec.addRecordSpec(recordSpec); } --- 39,86 ---- private static Logger logger=Logger.getLogger(FixedWidthFlatFileFileSpec.class); ! /** ! * Constructs the FixedWidthFlatFileFileSpec by accepting the file spec path and ! * file type. ! * ! * @param fileSpecPath Absolute path to the file spec. ! */ ! protected FixedWidthFlatFileFileSpec(String fileSpecPath) { ! super(fileSpecPath,FileType.FIXED_WIDTH_FLAT_FILE); } + /** + * Factory method create the fixed with flat file spec object from the given + * DOM Element representing the file-spec element. + * + * @param fileSpecPath Absolute path to the file spec. + * @param fileSpecElement DOM Element representing the file-spec element. + * + * @return Returns the instance of FixedWidthFlatFileFileSpec. + * + * @throws org.jmonks.batchserver.io.FileSpecException If two record specs + * has the same value for the starts-with and record-type attributes. + */ public static FileSpec createFixedWidthFlatFileFileSpec(final String fileSpecPath,final Element fileSpecElement) { ! logger.trace("Entering createFixedWidthFlatFileFileSpec " + fileSpecPath); ! FixedWidthFlatFileFileSpec fileSpec=new FixedWidthFlatFileFileSpec(fileSpecPath); NodeList recordSpecNodeList=fileSpecElement.getElementsByTagName(RecordSpec.RECORD_SPEC_TAG_NAME); + logger.debug("Number of record specs found = " + recordSpecNodeList.getLength()); for(int i=0;i<recordSpecNodeList.getLength();i++) { FixedWidthFlatFileRecordSpec recordSpec=FixedWidthFlatFileRecordSpec.createFixedWidthFlatFileRecordSpec((Element)recordSpecNodeList.item(i)); + /** + * Check for the duplicat start-with value on the record specs. + */ + for(Iterator iterator=fileSpec.getRecordSpecs().iterator();iterator.hasNext();) + { + FixedWidthFlatFileRecordSpec existingRecordSpec=(FixedWidthFlatFileRecordSpec)iterator.next(); + if(existingRecordSpec.getStartsWith().equalsIgnoreCase(recordSpec.getStartsWith())) + { + throw new FileSpecException("Two record specs in the same file spec cannot have same values for starts-with attribute."); + } + } + logger.debug("Adding the record spec = " + recordSpec.toString()); fileSpec.addRecordSpec(recordSpec); } *************** *** 42,45 **** --- 88,94 ---- } + /** + * @see java.lang.Object#toString() + */ public String toString() { *************** *** 51,55 **** stringValue.append("]}"); return stringValue.toString(); ! } ! } --- 100,103 ---- 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.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** FixedWidthFlatFileWriter.java 8 Jun 2006 03:39:13 -0000 1.3 --- FixedWidthFlatFileWriter.java 8 Jun 2006 22:21:15 -0000 1.4 *************** *** 26,51 **** /** * ! * @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) --- 26,76 ---- /** + * <p> + * FixedWidthFlatFileWriter 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 class FixedWidthFlatFileWriter extends FileWriter { ! /** ! * Path of the file to be written. ! */ ! protected String filePath=null; ! /** ! * 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); ! /** ! * Constructs and initializes the writer with the given values. ! * ! * @param filePath Absolute path to the file to be generated. ! * @param fileSpec File spec to be used to generate the file. ! */ ! public FixedWidthFlatFileWriter(String filePath,FileSpec fileSpec) { + logger.trace("Entering FixedWidthFlatFileWriter constructor"); try { ! this.filePath=filePath; this.fileSpec=(FixedWidthFlatFileFileSpec)fileSpec; ! writer=new BufferedWriter(new java.io.FileWriter(this.filePath)); ! logger.debug("Done creating and initializing the writer"); } catch(IOException exception) *************** *** 56,63 **** --- 81,93 ---- throw new FileParseException("IOException while creating the reader. Message = " + exception.getMessage()); } + logger.trace("Exiting FixedWidthFlatFileWriter constructor"); } + /** + * Closes the writer. + */ public void close() { + logger.trace("Entering close"); if(this.writer!=null) { *************** *** 65,68 **** --- 95,99 ---- { this.writer.close(); + logger.debug("Writer has been closed"); } catch(IOException exception) *************** *** 78,141 **** /** ! * TODO ::: Check whether this record type associates to any of the record specs. */ public WriterRecord createWriterRecord(RecordType recordType) { ! ! return new FixedWidthFlatFileWriterRecord(recordType); } public void writeRecord(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) { --- 109,200 ---- /** ! * 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 WriterRecord createWriterRecord(RecordType recordType) { ! if(this.fileSpec.isValidRecordType(recordType)) ! return new FixedWidthFlatFileWriterRecord(recordType); ! else ! throw new IllegalArgumentException("No record spec has been found with the record type = " + recordType); } + /** + * 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 { ! 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"); } ! /** ! * FixedWidthFlatFileWriterRecord implements WriterRecord by maintaing the ! * field names and values as a map and provides the methods with proper ! * access privileges to read into and write from the record. ! * ! * @author Suresh Pragada ! */ public class FixedWidthFlatFileWriterRecord extends WriterRecord { + /** + * Map to hold the field names and values. + */ private Map fieldMap=null; + /** + * Constructs and initializes the writer record. + */ private FixedWidthFlatFileWriterRecord(RecordType recordType) { *************** *** 144,153 **** } public void writeField(String fieldName, Object fieldValue) { this.fieldMap.put(fieldName,fieldValue); } ! ! protected Object readField(String fieldName) { if(this.fieldMap.containsKey(fieldName)) --- 203,225 ---- } + /** + * Writes the field data into the record. + * + * @param fieldName Name of the field defined in field spec. + * @param fieldValue Value for the field name. + */ public void writeField(String fieldName, Object fieldValue) { this.fieldMap.put(fieldName,fieldValue); } ! ! /** ! * Reads and returns the value associated with requested field name. ! * ! * @param fieldName Name of the field. ! * ! * @return Returns the value associated with the field name. ! */ ! private Object readField(String fieldName) { if(this.fieldMap.containsKey(fieldName)) Index: FixedWidthFlatFileFieldSpec.java =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/flat/FixedWidthFlatFileFieldSpec.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** FixedWidthFlatFileFieldSpec.java 6 Jun 2006 21:19:58 -0000 1.2 --- FixedWidthFlatFileFieldSpec.java 8 Jun 2006 22:21:15 -0000 1.3 *************** *** 16,32 **** /** * ! * @author Suresh Pragada */ public class FixedWidthFlatFileFieldSpec extends FieldSpec { private int startPosition=0; ! private int endPosition=0; ! ! private int fieldWidth=0; ! public static final String START_POSITION_ATTRIB_NAME = "start-pos"; ! public static final String END_POSITION_ATTRIB_NAME = "end-pos"; --- 16,57 ---- /** + * <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. + * Position value starts with "1" and the start-pos and end-pos values are inclusive. + * Here is a sample spec snippet... + * </p> + * <p> + * <file-spec file-type="fixed-width-flat"> + * <record-spec record-type="detail" starts-with="5"> + * <field-spec field-name="consumer-id" start-pos="2" end-pos="11"/> + * <field-spec field-name="consumer-name" start-pos="12" end-pos="31"/> + * </record-spec> + * </file-spec> + * </p> * ! * @author Suresh Pragada ! * @version 1.0 ! * @since 1.0 */ public class FixedWidthFlatFileFieldSpec extends FieldSpec { + /** + * Holds the start position of the field. + */ private int startPosition=0; ! /** ! * Holds the end position of the field. ! */ private int endPosition=0; ! /** ! * Constant defines the start position attribute name. ! */ public static final String START_POSITION_ATTRIB_NAME = "start-pos"; ! /** ! * Constant defines the end position attribute name. ! */ public static final String END_POSITION_ATTRIB_NAME = "end-pos"; *************** *** 34,38 **** /** ! * Creates a new instance of FixedWidthFlatFileFieldSpec */ protected FixedWidthFlatFileFieldSpec(String fieldName) --- 59,63 ---- /** ! * Creates a new instance of FixedWidthFlatFileFieldSpec by using field name. */ protected FixedWidthFlatFileFieldSpec(String fieldName) *************** *** 41,44 **** --- 66,72 ---- } + /** + * Gets the starting position of the field. + */ public int getStartPosition() { *************** *** 46,49 **** --- 74,80 ---- } + /** + * Gets the ending position of the field. + */ public int getEndPosition() { *************** *** 51,61 **** } 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); --- 82,107 ---- } + /** + * Gets the field width. + */ public int getFieldWidth() { ! return (endPosition-startPosition)+1; } + /** + * Factory method to create the FixedWidthFlatFileFieldSpec 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 FixedWidthFlatFileFieldSpec instance. + * + * @throws org.jmonks.batchserver.io.FileSpecException If field-spec + * field-name values are not unique across record-spec. + */ public static FixedWidthFlatFileFieldSpec createFixedWidthFlatFileFieldSpec(final Element fieldSpecElement) { + logger.trace("Entering createFixedWidthFlatFileFieldSpec"); String fieldName=fieldSpecElement.getAttribute(FieldSpec.FIELD_NAME_ATTRIB_NAME); FixedWidthFlatFileFieldSpec fieldSpec=new FixedWidthFlatFileFieldSpec(fieldName); *************** *** 66,74 **** fieldSpec.startPosition=startPosition; fieldSpec.endPosition=endPosition; - fieldSpec.fieldWidth=(endPosition-startPosition)+1; return fieldSpec; } ! public String toString() { --- 112,122 ---- fieldSpec.startPosition=startPosition; fieldSpec.endPosition=endPosition; return fieldSpec; } ! ! /** ! * @see java.lang.Object#toString() ! */ public String toString() { Index: FixedWidthFlatFileReader.java =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/flat/FixedWidthFlatFileReader.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** FixedWidthFlatFileReader.java 6 Jun 2006 21:19:58 -0000 1.3 --- FixedWidthFlatFileReader.java 8 Jun 2006 22:21:15 -0000 1.4 *************** *** 22,46 **** /** * ! * @author Suresh Pragada */ public class FixedWidthFlatFileReader extends FileReader { ! protected String absoluteFilePath=null; ! protected FixedWidthFlatFileFileSpec 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=(FixedWidthFlatFileFileSpec)fileSpec; - try { ! reader=new BufferedReader(new java.io.FileReader(this.absoluteFilePath)); } catch(FileNotFoundException exception) --- 22,70 ---- /** + * <p> + * FixedWidthFlatFileReader 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 class FixedWidthFlatFileReader extends FileReader { ! /** ! * Holds the absolute path to the file to read. ! */ ! protected String filePath=null; ! /** ! * 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); ! /** ! * Constructs and initializes the reader with the given file path and file spec. ! * ! * @param filePath Absolute path to the file to read. ! * @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(String filePath,FileSpec fileSpec) { ! logger.trace("Entering FixedWidthFlatFileReader constructor"); ! this.filePath=filePath; this.fileSpec=(FixedWidthFlatFileFileSpec)fileSpec; try { ! reader=new BufferedReader(new java.io.FileReader(this.filePath)); ! logger.debug("Created the reader successfully = " + this.filePath); } catch(FileNotFoundException exception) *************** *** 58,65 **** --- 82,104 ---- throw new FileParseException("IO Exception while initializing the file reader. Message = " + exception.getMessage()); } + logger.trace("Exiting FixedWidthFlatFileReader constructor"); } + /** + * <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; *************** *** 71,74 **** --- 110,114 ---- if(recordString==null) { + logger.info("Reader has reached EOF.. Closing the reader."); this.reader.close(); this.reader=null; *************** *** 83,87 **** 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()); } --- 123,127 ---- 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()); } *************** *** 89,94 **** --- 129,138 ---- } + /** + * Closes the reader. + */ public void close() { + logger.trace("Entering close"); if(this.reader!=null) { *************** *** 96,99 **** --- 140,144 ---- { this.reader.close(); + logger.info("Reader has been closed"); } catch(IOException exception) *************** *** 108,111 **** --- 153,165 ---- } + /** + * Parses the given record string, translates it into the proper ReaderRecord + * and returns the reader record. + * + * @param recordString Record 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. + */ private ReaderRecord parseRecord(String recordString) { *************** *** 142,150 **** return record; } ! public class FixedWidthFlatFileReaderRecord extends ReaderRecord { private Map fieldMap=null; private FixedWidthFlatFileReaderRecord(RecordType recordType,int fieldCount) { --- 196,220 ---- return record; } ! ! /** ! * FixedWidthFlatFileReaderRecord implements ReaderRecord by maintaing the ! * field names and values as a map and provides the methods with proper ! * access privileges to read into and write from the record. ! * ! * @author Suresh Pragada ! */ public class FixedWidthFlatFileReaderRecord extends ReaderRecord { + /** + * Holds the field data. + */ private Map fieldMap=null; + /** + * Private constructor to make sure only reader can create the record. + * + * @param recordType Type of the record. + * @param fieldCount Number of fields expected in this record to initializes the map properly. + */ private FixedWidthFlatFileReaderRecord(RecordType recordType,int fieldCount) { *************** *** 153,156 **** --- 223,233 ---- } + /** + * Field values can be read by using the names provided in the field spec. + * + * @param fieldName Name of the field. + * + * @return Returns the field value. + */ public Object readField(String fieldName) { *************** *** 158,161 **** --- 235,244 ---- } + /** + * This is for the Reader to write the data into the record. + * + * @param fieldName Name of the field. + * @param fieldValue Value of the field. + */ private void writeField(String fieldName,String fieldValue) { Index: FixedWidthFlatFileRecordSpec.java =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/flat/FixedWidthFlatFileRecordSpec.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** FixedWidthFlatFileRecordSpec.java 6 Jun 2006 21:19:58 -0000 1.2 --- FixedWidthFlatFileRecordSpec.java 8 Jun 2006 22:21:15 -0000 1.3 *************** *** 12,15 **** --- 12,16 ---- import java.util.Iterator; + import org.apache.log4j.Logger; import org.w3c.dom.Element; import org.w3c.dom.NodeList; *************** *** 17,31 **** /** * ! * @author Suresh Pragada */ public class FixedWidthFlatFileRecordSpec extends RecordSpec { ! public static final String STARTS_WITH_ATTRIB_NAME = "starts-with"; ! protected String startsWith=null; ! protected int recordSize=0; protected FixedWidthFlatFileRecordSpec(RecordType recordType) { --- 18,60 ---- /** + * <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"> + * <!-- field specs will follow here --> + * </record-spec> + * </file-spec> + * </p> * ! * @author Suresh Pragada ! * @version 1.0 ! * @since 1.0 */ public class FixedWidthFlatFileRecordSpec extends RecordSpec { ! /** ! * Holds the record specs starts with attribute value. ! */ protected String startsWith=null; ! /** ! * Holds the length of the record. ! */ protected int recordSize=0; + /** + * Constant defines the starts with attribute name. + */ + public static final String STARTS_WITH_ATTRIB_NAME = "starts-with"; + + private static Logger logger=Logger.getLogger(FixedWidthFlatFileRecordSpec.class); + /** + * Constructs the record spec by accepting the record type. + */ protected FixedWidthFlatFileRecordSpec(RecordType recordType) { *************** *** 33,36 **** --- 62,68 ---- } + /** + * Gets the starts-with value of this record. + */ public String getStartsWith() { *************** *** 38,41 **** --- 70,79 ---- } + /** + * Gets the length of the record. This will be calculated by adding the maximum + * end position of field specs available in the record spec and the lenght of + * starts-with value. This will be used while writing/generating the record using + * file wrier. + */ public int getRecordSize() { *************** *** 43,46 **** --- 81,92 ---- } + /** + * Tells whether the given record matches with the record spec. It checks whether + * this string starts with the starts-with value. + * + * @param recordString Record read from the file. + * + * @return Returns true if the recordString matches this recrd spec, false otherwise. + */ public boolean isMatch(String recordString) { *************** *** 49,62 **** return recordString.startsWith(this.startsWith); } ! public static FixedWidthFlatFileRecordSpec 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."); int recordSize=startsWith.length(); NodeList fieldSpecNodeList=recordSpecElement.getElementsByTagName(FieldSpec.FIELD_SPEC_TAG_NAME); --- 95,132 ---- return recordString.startsWith(this.startsWith); } ! ! /** ! * Factory method to create the fixed width 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 fixed width flat file record spec. ! * ! * @throws org.jmonks.batchserver.io.FileSpecException If record-spec ! * doesnt have the starts-with or record-type attribute. ! */ public static FixedWidthFlatFileRecordSpec createFixedWidthFlatFileRecordSpec(final Element recordSpecElement) { ! logger.trace("Entering createFixedWidthFlatFileRecordSpec"); ! FixedWidthFlatFileRecordSpec 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 FixedWidthFlatFileRecordSpec(recordType); ! } ! else ! throw new FileSpecException("Record Spec in Fixed Width File Spec should have record-type attribute."); ! String startsWith=recordSpecElement.getAttribute(FixedWidthFlatFileRecordSpec.STARTS_WITH_ATTRIB_NAME); ! logger.debug("record specs starts-with value = " + startsWith); ! 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."); + int recordSize=startsWith.length(); NodeList fieldSpecNodeList=recordSpecElement.getElementsByTagName(FieldSpec.FIELD_SPEC_TAG_NAME); *************** *** 73,76 **** --- 143,149 ---- } + /** + * @see java.lang.Object#toString() + */ public String toString() { |