[Batchserver-cvs] batchserver/src/org/jmonks/batchserver/io FieldSpec.java, 1.4, 1.5 FileReader.jav
Brought to you by:
suresh_pragada
From: Suresh <sur...@us...> - 2006-06-08 03:39:17
|
Update of /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv17834 Modified Files: FieldSpec.java FileReader.java FileSpec.java FileWriter.java ReaderRecord.java Record.java RecordSpec.java RecordType.java WriterRecord.java Log Message: no message Index: RecordType.java =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/RecordType.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** RecordType.java 7 Jun 2006 22:10:41 -0000 1.3 --- RecordType.java 8 Jun 2006 03:39:13 -0000 1.4 *************** *** 17,20 **** --- 17,21 ---- * type information should be provided when defining the record spec in file spec. * </p> + * * @author Suresh Pragada * @version 1.0 Index: Record.java =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/Record.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** Record.java 7 Jun 2006 22:10:41 -0000 1.3 --- Record.java 8 Jun 2006 03:39:13 -0000 1.4 *************** *** 11,18 **** package org.jmonks.batchserver.io; - - - /** * * @author Suresh Pragada --- 11,24 ---- package org.jmonks.batchserver.io; /** + * <p> + * Record represents the map of field names and values based on the record spec + * defined in the file spec. ReaderRecord and WriterRecord classes provide + * the methods to read and write the values to this record object for the + * FileReader and FileWriter consequently. + * <br> + * TODO :: Based on the memory statistics on handling huge files introduces a method to + * remove the data from the records, once they have been done using. + * </p> * * @author Suresh Pragada *************** *** 22,37 **** public abstract class Record { private RecordType recordType=null; ! /** Creates a new instance of Record */ ! public Record(RecordType recordType) { this.recordType=recordType; } public RecordType getRecordType() { return this.recordType; } - } --- 28,52 ---- public abstract class Record { + /** + * Holds the type of the record. + */ private RecordType recordType=null; ! /** ! * Creates the record by accepting the record type. ! * ! * @param recordType Type of the record. ! */ ! protected Record(RecordType recordType) { this.recordType=recordType; } + /** + * Gets the record type. + */ public RecordType getRecordType() { return this.recordType; } } Index: RecordSpec.java =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/RecordSpec.java,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** RecordSpec.java 2 Jun 2006 21:02:06 -0000 1.4 --- RecordSpec.java 8 Jun 2006 03:39:13 -0000 1.5 *************** *** 12,34 **** import java.util.ArrayList; import java.util.List; import org.apache.log4j.Logger; /** * ! * @author Suresh Pragada */ public abstract class RecordSpec { protected RecordType recordType=null; ! protected List fieldSpecList=null; ! public static final String RECORD_SPEC_TAG_NAME = "record-spec"; ! public static final String RECORD_TYPE_ATTRIB_NAME = "record-type"; private static Logger logger=Logger.getLogger(RecordSpec.class); protected RecordSpec(RecordType recordType) { --- 12,72 ---- import java.util.ArrayList; + import java.util.Iterator; import java.util.List; import org.apache.log4j.Logger; /** + * <p> + * RecordSpec represents the <record-spec> element in file spec. + * Record spec specifies the format of the record in the form of multiple + * field specs and contains the attribute helps in identifying the type of + * record and so. Each record spec contains zero or more field specs based + * on the file type. + * </p> + * <p> + * Every record-spec element contains record-type attribute + * specifies the kind of record it is. Most of these values will be defined + * in RecordType class and additional values can be added by extending the + * RecordType. The record-type values should be unique across one file spec. + * Here is a sample record-spec configuration. + * <br> + * <pre> + * <record-spec record-type="DETAIL"> + * <field-spec field-name="consumer-id"> + * </field-spec> + * </record-spec> + * </pre> + * </p> * ! * @author Suresh Pragada ! * @version 1.0 ! * @since 1.0 */ public abstract class RecordSpec { + /** + * Holds the type of the record this spec is representing. + */ protected RecordType recordType=null; ! /** ! * Holds all the field spec objects. ! */ protected List fieldSpecList=null; ! /** ! * Constant defines the record spec tag name. ! */ public static final String RECORD_SPEC_TAG_NAME = "record-spec"; ! /** ! * Constant defines the record type attribute name. ! */ public static final String RECORD_TYPE_ATTRIB_NAME = "record-type"; private static Logger logger=Logger.getLogger(RecordSpec.class); + /** + * Constructs the RecordSpec object by accepting the RecordType. + * + * @param recordType Type of the record. + */ protected RecordSpec(RecordType recordType) { *************** *** 37,46 **** } protected boolean addFieldSpec(FieldSpec fieldSpec) { ! this.fieldSpecList.add(fieldSpec); ! return true; } public List getFieldSpecs() { --- 75,109 ---- } + /** + * Adds the given field spec object to the record spec. + * + * @param fieldSpec Field spec object to be added. + * + * @return Returns true if field spec has been added successfully, flase otherwise. + * + * @throws IllegalArgumentException If given field spec is null. + * @throws org.jmonks.batchserver.io.FileSpecException If multiple field specs configured with same field name. + */ protected boolean addFieldSpec(FieldSpec fieldSpec) { ! if(fieldSpec==null) ! throw new IllegalArgumentException("FiledSpec object to add to the record spec cannot be null."); ! else ! { ! for(Iterator iterator=this.fieldSpecList.iterator();iterator.hasNext();) ! { ! FieldSpec existingFieldSpec=(FieldSpec)iterator.next(); ! if(existingFieldSpec.getFieldName().equalsIgnoreCase(fieldSpec.getFieldName())) ! throw new FileSpecException("Multiple field specs have been defined with same field name = " + fieldSpec.getFieldName()); ! } ! return this.fieldSpecList.add(fieldSpec); ! } } + /** + * Gets the field spec objects as a list. + * + * @returns Returns the list contains of all the field specs. + */ public List getFieldSpecs() { *************** *** 48,51 **** --- 111,117 ---- } + /** + * Returns the record type of this record spec. + */ public RecordType getRecordType() { Index: WriterRecord.java =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/WriterRecord.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** WriterRecord.java 7 Jun 2006 22:10:41 -0000 1.2 --- WriterRecord.java 8 Jun 2006 03:39:13 -0000 1.3 *************** *** 11,29 **** package org.jmonks.batchserver.io; - - /** * ! * @author Suresh Pragada */ public abstract class WriterRecord extends Record { ! ! /** Creates a new instance of WriterRecord */ ! public WriterRecord(RecordType recordType) { super(recordType); } public abstract void writeField(String fieldName,Object fieldValue); } --- 11,47 ---- package org.jmonks.batchserver.io; /** + * <p> + * WriterRecord provides the methods to write the values of the fields into the record. + * This record will be created from and submitted to write into the file for the FileWriter's. + * </p> * ! * @author Suresh Pragada ! * @version 1.0 ! * @since 1.0 */ public abstract class WriterRecord extends Record { ! /** ! * Creates a new instance of WriterRecord by accepting the type of record. ! * ! * @param recordType Type of the record. ! */ ! protected WriterRecord(RecordType recordType) { super(recordType); } + /** + * <p> + * Writes the given field name and value into the record. The value will be + * depends on the type of file. Concrete implementors provide the details + * on the kind of the object should be written for different fields and what the field + * name corresponds to. + * </p> + * + * @param fieldName Name of the field. + * @param fieldValue Value corresponding the field name. + */ public abstract void writeField(String fieldName,Object fieldValue); } Index: FileSpec.java =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/FileSpec.java,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** FileSpec.java 7 Jun 2006 22:10:41 -0000 1.6 --- FileSpec.java 8 Jun 2006 03:39:13 -0000 1.7 *************** *** 17,20 **** --- 17,21 ---- import java.io.InputStream; import java.util.ArrayList; + import java.util.Iterator; import java.util.List; import javax.xml.parsers.DocumentBuilder; *************** *** 33,37 **** * file to read or write using this package. This provides the factory * method to create the FileSpec object from a file contains the spec ! * and necessary methods to access the record spec objects. * </p> * --- 34,55 ---- * file to read or write using this package. This provides the factory * method to create the FileSpec object from a file contains the spec ! * and necessary methods to access the record spec objects. ! * </p> ! * <p> ! * File spec defines the file type and possible records could exists in the file. ! * Each record will have possible fields. Every file spec will contains one or ! * more record specs. Element file-spec should contain one attribute file-type ! * defines the type of the file this spec is going to represent. Based on the file ! * type, there could be other attributes in file-spec element along with file-type ! * attribute. These attributes can be found from the javadoc of the particular file type ! * file specs. Here is a sample file-spec configuration. ! * </p> ! * <p> ! * <pre> ! * <file-spec file-type="fixed-width-flat"> ! * <record-spec record-type="DETAIL"> ! * </record-spec> ! * </file-spec> ! * </pre> * </p> * *************** *** 87,90 **** --- 105,109 ---- * * @throws IllegalArgumentException If recordSpec is null. + * @throws org.jmonks.batchserver.io.FileSpecException If multiple record specs configured with same record type. */ protected boolean addRecordSpec(RecordSpec recordSpec) *************** *** 93,97 **** --- 112,124 ---- throw new IllegalArgumentException("RecordSpec cannot be null to add to the file spec."); else + { + for(Iterator iterator=this.recordSpecList.iterator();iterator.hasNext();) + { + RecordSpec existingRecordSpec=(RecordSpec)iterator.next(); + if(existingRecordSpec.getRecordType().toString().equalsIgnoreCase(recordSpec.getRecordType().toString())) + throw new FileSpecException("Duplicate record spec with the record type " + recordSpec.getRecordType().toString()); + } return this.recordSpecList.add(recordSpec); + } } *************** *** 107,110 **** --- 134,143 ---- /** + * Gets the record spec associated with the given record type. + * + * @param recordType Record types associated with the record spec. + * + * @return Retruns the record spec associated with the record type, + * null if record spec could not be found. * * @throws IllegalArgumentException If recordType is null. *************** *** 119,124 **** { RecordSpec recordSpec=(RecordSpec)iterator.next(); ! if(recordType.toString().equ) } } } --- 152,161 ---- { RecordSpec recordSpec=(RecordSpec)iterator.next(); ! if(recordType.toString().equalsIgnoreCase(recordSpec.getRecordType().toString())) ! { ! return recordSpec; ! } } + return null; } } *************** *** 135,149 **** /** ! * Constructs the file spcification object from the file represented by ! * given the file specification path. */ public static FileSpec createFileSpec(String fileSpecPath) { try { - /** - * Read the file specification to find out the file type and then - * instantiate the appropirate file spec object. - */ InputStream fileSpecStream=new FileInputStream(new File(fileSpecPath)); DocumentBuilderFactory documentBuilderFactory=DocumentBuilderFactory.newInstance(); --- 172,193 ---- /** ! * <p> ! * Creates the file spec object from the file contains the spec. This looks for ! * the file-type attribute in file-spec element and creates the appropriate ! * file spec object. ! * </p> ! * @param fileSpecPath Absolute path to the file contains spec. ! * ! * @return Returns the file spec object. ! * ! * @throws org.jmonks.batchserver.io.FileSpecException If file-type attribute ! * is missing or doesnt consists of the type available FileType class ! * or given file cannot be found or cannot parse the file. */ public static FileSpec createFileSpec(String fileSpecPath) { + logger.trace("Entering createFileSpec = " + fileSpecPath); try { InputStream fileSpecStream=new FileInputStream(new File(fileSpecPath)); DocumentBuilderFactory documentBuilderFactory=DocumentBuilderFactory.newInstance(); *************** *** 151,158 **** Document document=documentBuilder.parse(fileSpecStream); Element fileSpecElement=document.getDocumentElement(); ! if(FileSpec.FILE_SPEC_TAG_NAME.equals(fileSpecElement.getTagName())) { ! FileType fileType=FileType.toFileType(fileSpecElement.getAttribute(FileSpec.FILE_TYPE_ATTRIB_NAME)); if(fileType==FileType.FIXED_WIDTH_FLAT_FILE) return FixedWidthFlatFileFileSpec.createFixedWidthFlatFileFileSpec(fileSpecPath,fileSpecElement); --- 195,204 ---- Document document=documentBuilder.parse(fileSpecStream); Element fileSpecElement=document.getDocumentElement(); ! if(FileSpec.FILE_SPEC_TAG_NAME.equals(fileSpecElement.getTagName())) { ! String configuredFileType=fileSpecElement.getAttribute(FileSpec.FILE_TYPE_ATTRIB_NAME); ! logger.info("Configured file-type value = " + configuredFileType); ! FileType fileType=FileType.toFileType(configuredFileType); if(fileType==FileType.FIXED_WIDTH_FLAT_FILE) return FixedWidthFlatFileFileSpec.createFixedWidthFlatFileFileSpec(fileSpecPath,fileSpecElement); Index: ReaderRecord.java =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/ReaderRecord.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** ReaderRecord.java 2 Jun 2006 21:02:06 -0000 1.1 --- ReaderRecord.java 8 Jun 2006 03:39:13 -0000 1.2 *************** *** 12,27 **** /** * ! * @author Suresh Pragada */ public abstract class ReaderRecord extends Record { ! ! /** Creates a new instance of ReaderRecord */ ! public ReaderRecord(RecordType recordType) { super(recordType); } public abstract Object readField(String fieldName); } --- 12,48 ---- /** + * <p> + * ReaderRecord provides the methods to read the values of the fields for a record. + * This record will be generated from the FileReader's. + * </p> * ! * @author Suresh Pragada ! * @version 1.0 ! * @since 1.0 */ public abstract class ReaderRecord extends Record { ! /** ! * Constructs the ReaderRecord by accepting the record type. ! * ! * @param recordType Type of the record. ! */ ! protected ReaderRecord(RecordType recordType) { super(recordType); } + /** + * <p> + * Reads the value of the given field from the record. The value will be + * depends on the type of file. Concrete implementors provide the details + * on the kind of the object they return for different fields and what the field + * name corresponds to. + * </p> + * + * @param fieldName Name of the field. + * + * @return Returns the value of the given field from the record, null if field doesnt exists. + */ public abstract Object readField(String fieldName); } Index: FieldSpec.java =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/FieldSpec.java,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** FieldSpec.java 2 Jun 2006 04:40:07 -0000 1.4 --- FieldSpec.java 8 Jun 2006 03:39:13 -0000 1.5 *************** *** 13,29 **** /** * ! * @author Suresh Pragada */ public abstract class FieldSpec { protected String fieldName=null; ! public static final String FIELD_SPEC_TAG_NAME = "field-spec"; ! public static final String FIELD_NAME_ATTRIB_NAME = "field-name"; private static Logger logger=Logger.getLogger(FieldSpec.class); public FieldSpec(String fieldName) { --- 13,57 ---- /** + * <p> + * FieldSpec represents the <field-spec> element in reocrd spec. + * Field spec specifies the name of the field and additional attributes to + * identifies the field among the record and other properties based on the file type. + * Every field-spec element contains field-name attribute identifies among the + * record spec. The field-name values should be unique across one record spec. + * </p> + * <p> + * Here is a sample record-spec configuration. + * <br> + * <pre> + * <field-spec field-name="consumer-id"> + * </pre> + * </p> * ! * @author Suresh Pragada ! * @version 1.0 ! * @since 1.0 */ public abstract class FieldSpec { + /** + * Holds the name of the field. + */ protected String fieldName=null; ! /** ! * Constant defines the field spec tag name. ! */ public static final String FIELD_SPEC_TAG_NAME = "field-spec"; ! /** ! * Constant defines the field name atttribute name. ! */ public static final String FIELD_NAME_ATTRIB_NAME = "field-name"; private static Logger logger=Logger.getLogger(FieldSpec.class); + /** + * Constructs the FieldSpec object by accepting the field name. + * + * @param fieldName Name of the field. + */ public FieldSpec(String fieldName) { *************** *** 31,38 **** } public String getFieldName() { return this.fieldName; } - } --- 59,68 ---- } + /** + * Returns the field name. + */ public String getFieldName() { return this.fieldName; } } Index: FileWriter.java =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/FileWriter.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** FileWriter.java 7 Jun 2006 03:11:20 -0000 1.2 --- FileWriter.java 8 Jun 2006 03:39:13 -0000 1.3 *************** *** 12,61 **** import java.io.File; import org.jmonks.batchserver.io.flat.FixedWidthFlatFileWriter; import org.jmonks.batchserver.io.xml.XMLFileWriter; /** * ! * @author Suresh Pragada */ public abstract class FileWriter { ! public abstract WriterRecord createWriterRecord(RecordType recordType); public abstract void writeRecord(WriterRecord record); public abstract void close(); ! public static FileWriter getFileWriter(String absoluteFilePath,String absoluteFileSpecPath) { ! if(absoluteFilePath==null) throw new IllegalArgumentException("Absolute file path to create the writer cannot be null."); ! else if((new File(absoluteFilePath)).exists()) ! throw new IllegalArgumentException("Absolute file path to create the writer should not exist. Given absolute file path = " + absoluteFilePath); ! else if ((new File(absoluteFilePath)).isDirectory()) ! throw new IllegalArgumentException("Absolute file path to create the writer should not be directory. Given absolute file path = " + absoluteFilePath); ! if(absoluteFileSpecPath==null) throw new IllegalArgumentException("Absolute file path to read the file spec cannot be null."); ! else if(!(new File(absoluteFileSpecPath).exists() && new File(absoluteFileSpecPath).isFile())) ! throw new IllegalArgumentException("Absolute file path to read the file spec should exist and should be a file. Given absolute file spec path = " + absoluteFileSpecPath); ! FileSpec fileSpec=FileSpec.createFileSpec(absoluteFileSpecPath); if(fileSpec.getFileType()==FileType.FIXED_WIDTH_FLAT_FILE) ! { ! return new FixedWidthFlatFileWriter(absoluteFilePath,fileSpec); ! } else if(fileSpec.getFileType()==FileType.DELIMITED_FLAT_FILE) - { return null; // Create Demlited Flat file reader. - } else if(fileSpec.getFileType()==FileType.XML_FILE) ! { ! return new XMLFileWriter(absoluteFilePath,fileSpec); ! } else - { throw new FileSpecException("Unsupported file type in the file spec = " + fileSpec.getFileType().toString()); - } } } --- 12,112 ---- import java.io.File; + import org.apache.log4j.Logger; import org.jmonks.batchserver.io.flat.FixedWidthFlatFileWriter; import org.jmonks.batchserver.io.xml.XMLFileWriter; /** + * <p> + * FileWriter writes/generates the specified file based on the given file spec with + * the data written in the form of WriterRecord's. This provides the methods + * to create the required records and write them into the file writer. Once finished + * writing of all the records file writer should be closed by calling + * the appropriate methods. This provides the factory method to create the file writer + * based on the file spec. Here is a sample code snippet to use the file writer. + * </p> + * <p> + * <pre + * FileWriter fileWriter=FileWriter.getFileWriter("/data/consumer.dat","/config/consumer-file-spec.xml"); + * WriterRecord record=fileWriter.createWriterRecord(RecordType.DETAIL); + * record.writeField("consumer-id","123456"); + * //Write the other field values. + * fileWriter.writeRecord(record); + * fileWriter.close(); + * </pre> + * </p> * ! * @author Suresh Pragada ! * @version 1.0 ! * @since 1.0 */ public abstract class FileWriter { ! private static Logger logger=Logger.getLogger(FileWriter.class); + /** + * Writes the given record into the file. + * + * @throws org.jmonks.batchserver.io.FileParseException Problems while writing the record into file. + */ public abstract void writeRecord(WriterRecord record); + /** + * Closes the writer and releases all the resources associated with the writer. + */ public abstract void close(); + + /** + * Creates the writer record to be used to fill all the field values + * and submit to the file writer to write the record into the file. + * + * @param recordType Type of record. + * + * @return Retruns the writer record. + * + * @throws IllegalArgumentException If given record type is not associated with any record spec. + */ + public abstract WriterRecord createWriterRecord(RecordType recordType); ! /** ! * <p> ! * Factory method to get the file writer. This method returns the appropriate file writer ! * based on the file type specified in the file spec. ! * </p> ! * ! * @param filePath Absolute path to create the data file. ! * @param fileSpecPath Absolute path to the file contains the file spec. ! * ! * @return Returns the appropriate writer. ! * ! * @throws IllegalArgumentException If the given file path or file spec path is null or ! * or if they are directoires. ! * @throws org.jmonks.batchserver.io.FileSpecException If the configured file type in file spec doesnt ! * exist in FileType class. ! */ ! public static FileWriter getFileWriter(String filePath,String fileSpecPath) { ! logger.trace("Entering getFileReader file path = " + filePath + " File spec path = " + fileSpecPath); ! if(filePath==null) throw new IllegalArgumentException("Absolute file path to create the writer cannot be null."); ! else if((new File(filePath)).exists()) ! throw new IllegalArgumentException("Absolute file path to create the writer should not exist. Given absolute file path = " + filePath); ! else if ((new File(filePath)).isDirectory()) ! throw new IllegalArgumentException("Absolute file path to create the writer should not be directory. Given absolute file path = " + filePath); ! if(fileSpecPath==null) throw new IllegalArgumentException("Absolute file path to read the file spec cannot be null."); ! else if(!(new File(fileSpecPath).exists() && new File(fileSpecPath).isFile())) ! throw new IllegalArgumentException("Absolute file path to read the file spec should exist and should be a file. Given absolute file spec path = " + fileSpecPath); ! FileSpec fileSpec=FileSpec.createFileSpec(fileSpecPath); ! logger.debug("Given file spec = " + fileSpec.toString()); if(fileSpec.getFileType()==FileType.FIXED_WIDTH_FLAT_FILE) ! return new FixedWidthFlatFileWriter(filePath,fileSpec); else if(fileSpec.getFileType()==FileType.DELIMITED_FLAT_FILE) return null; // Create Demlited Flat file reader. else if(fileSpec.getFileType()==FileType.XML_FILE) ! return new XMLFileWriter(filePath,fileSpec); else throw new FileSpecException("Unsupported file type in the file spec = " + fileSpec.getFileType().toString()); } } Index: FileReader.java =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/FileReader.java,v retrieving revision 1.7 retrieving revision 1.8 diff -C2 -d -r1.7 -r1.8 *** FileReader.java 5 Jun 2006 03:12:43 -0000 1.7 --- FileReader.java 8 Jun 2006 03:39:13 -0000 1.8 *************** *** 12,61 **** import java.io.File; import org.jmonks.batchserver.io.xml.XMLFileReader; import org.jmonks.batchserver.io.flat.FixedWidthFlatFileReader; - - - - /** * ! * @author Suresh Pragada */ public abstract class FileReader { public abstract ReaderRecord getNextRecord(); public abstract void close(); ! public static FileReader getFileReader(String absoluteFilePath,String absoluteFileSpecPath) { ! if(absoluteFilePath==null) throw new IllegalArgumentException("Absolute file path to create the reader cannot be null."); ! else if(!(new File(absoluteFilePath).exists() && new File(absoluteFilePath).isFile())) ! throw new IllegalArgumentException("Absolute file path to create the reader should exist and should be a file. Given absolute file path = " + absoluteFilePath); ! if(absoluteFileSpecPath==null) throw new IllegalArgumentException("Absolute file path to read the file spec cannot be null."); ! else if(!(new File(absoluteFileSpecPath).exists() && new File(absoluteFileSpecPath).isFile())) ! throw new IllegalArgumentException("Absolute file path to read the file spec should exist and should be a file. Given absolute file spec path = " + absoluteFileSpecPath); ! FileSpec fileSpec=FileSpec.createFileSpec(absoluteFileSpecPath); if(fileSpec.getFileType()==FileType.FIXED_WIDTH_FLAT_FILE) ! { ! return new FixedWidthFlatFileReader(absoluteFilePath,fileSpec); ! } else if(fileSpec.getFileType()==FileType.DELIMITED_FLAT_FILE) - { return null; // Create Demlited Flat file reader. - } else if(fileSpec.getFileType()==FileType.XML_FILE) ! { ! return new XMLFileReader(absoluteFilePath,fileSpec); ! } else - { throw new FileSpecException("Unsupported file type in the file spec = " + fileSpec.getFileType().toString()); - } } } --- 12,108 ---- import java.io.File; + import org.apache.log4j.Logger; import org.jmonks.batchserver.io.xml.XMLFileReader; import org.jmonks.batchserver.io.flat.FixedWidthFlatFileReader; /** + * <p> + * FileReader reads/parses the specified file based on the given file spec and + * returns the data in the form of ReaderRecord's to read the interested field values. + * This provides the methods to reads the records in an iterative manner. Once finished + * reading of all the records or required records file reader should be closed by calling + * the appropriate methods. This provides the factory method to create the file reader + * based on the file spec. Here is a sample code snippet to use the file reader. + * </p> + * <p> + * <pre + * FileReader fileReader=FileReader.getFileReader("/data/consumer.dat","/config/consumer-file-spec.xml"); + * ReaderRecord record=null; + * while((record=fileReader.getNextRecord())!=null) + * { + * if(record.getRecordType()==RecordType.DETAIL) + * { + * String consumerID=record.readField("consumer-id"); + * // Read the rest of the fields and does the processing. + * } + * } + * fileReader.close(); + * </pre> + * </p> * ! * @author Suresh Pragada ! * @version 1.0 ! * @since 1.0 */ public abstract class FileReader { + private static Logger logger=Logger.getLogger(FileReader.class); + + /** + * <p> + * Gets the next record from the file. If file doesnt have any more records + * it returns null. + * </p> + * + * @return Returns the next available record from the file, null if it doesnt have any more records. + * + * @throws org.jmonks.batchserver.io.FileParseException Problems while parsing the next record. + */ public abstract ReaderRecord getNextRecord(); + /** + * Closes the reader and releases all the resources associated with the reader. + */ public abstract void close(); ! /** ! * <p> ! * Factory method to get the file reader. This method returns the appropriate file reader ! * based on the file type specified in the file spec. ! * </p> ! * ! * @param filePath Absolute path to read the data file. ! * @param fileSpecPath Absolute path to the file contains the file spec. ! * ! * @return Returns the appropriate reader. ! * ! * @throws IllegalArgumentException If the given file path or file spec path is null or ! * doesnt exist and if they are directoires. ! * @throws org.jmonks.batchserver.io.FileSpecException If the configured file type in file spec doesnt ! * exist in FileType class. ! */ ! public static FileReader getFileReader(String filePath,String fileSpecPath) { ! logger.trace("Entering getFileReader file path = " + filePath + " File spec path = " + fileSpecPath); ! if(filePath==null) throw new IllegalArgumentException("Absolute file path to create the reader cannot be null."); ! else if(!(new File(filePath).exists() && new File(filePath).isFile())) ! throw new IllegalArgumentException("Absolute file path to create the reader should exist and should be a file. Given absolute file path = " + filePath); ! if(fileSpecPath==null) throw new IllegalArgumentException("Absolute file path to read the file spec cannot be null."); ! else if(!(new File(fileSpecPath).exists() && new File(fileSpecPath).isFile())) ! throw new IllegalArgumentException("Absolute file path to read the file spec should exist and should be a file. Given absolute file spec path = " + fileSpecPath); ! FileSpec fileSpec=FileSpec.createFileSpec(fileSpecPath); ! logger.debug("Given file spec = " + fileSpec.toString()); if(fileSpec.getFileType()==FileType.FIXED_WIDTH_FLAT_FILE) ! return new FixedWidthFlatFileReader(filePath,fileSpec); else if(fileSpec.getFileType()==FileType.DELIMITED_FLAT_FILE) return null; // Create Demlited Flat file reader. else if(fileSpec.getFileType()==FileType.XML_FILE) ! return new XMLFileReader(filePath,fileSpec); else throw new FileSpecException("Unsupported file type in the file spec = " + fileSpec.getFileType().toString()); } } |