From: Suresh <sur...@us...> - 2006-06-15 18:57:10
|
Update of /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/flat In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv27657 Modified Files: DelimitedFlatFileReader.java DelimitedFlatFileRecordSpec.java FixedWidthFlatFileRecordSpec.java FixedWidthFlatFileWriter.java package.html sample-delimited-file-spec.xml sample-fixed-width-file-spec.xml Log Message: no message Index: DelimitedFlatFileRecordSpec.java =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/flat/DelimitedFlatFileRecordSpec.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** DelimitedFlatFileRecordSpec.java 12 Jun 2006 20:58:51 -0000 1.2 --- DelimitedFlatFileRecordSpec.java 15 Jun 2006 18:57:02 -0000 1.3 *************** *** 24,34 **** * 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> --- 24,37 ---- * 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 two additional attributes <code>delimiter</code> and <code>field-count</code>. ! * Attribute <code>delimiter</code> tells the value that divides or delimits the fields ! * and attribute <code>field-count</code> tells the number of fields exists in the record. ! * There should be only one record spec is allwed in the file spec. ! * Here is a sample spec snippet... * </p> * <p> * <pre> * <file-spec file-type="delimited-flat"> ! * <record-spec record-type="detail" delimiter="|" field-count="4"> * <!-- field specs will follow here --> * </record-spec> *************** *** 55,58 **** --- 58,65 ---- */ public static final String DELIMITER_ATTRIB_NAME = "delimiter"; + /** + * Constant defines the field count attribute name which the value is "field-count" + */ + public static final String FIELD_COUNT_ATTRIB_NAME = "field-count"; private static Logger logger=Logger.getLogger(DelimitedFlatFileRecordSpec.class); *************** *** 115,119 **** throw new FileSpecException("Record Spec in Delimited File Spec should have delimiter attribute."); ! recordSpec.fieldCount=1; NodeList fieldSpecNodeList=recordSpecElement.getElementsByTagName(FieldSpec.FIELD_SPEC_TAG_NAME); for(int i=0;i<fieldSpecNodeList.getLength();i++) --- 122,132 ---- throw new FileSpecException("Record Spec in Delimited File Spec should have delimiter attribute."); ! String fieldCount=recordSpecElement.getAttribute(DelimitedFlatFileRecordSpec.FIELD_COUNT_ATTRIB_NAME); ! logger.debug("record specs field count value = " + delimiter); ! if(fieldCount!=null && !"".equals(fieldCount.trim())) ! recordSpec.fieldCount=Integer.parseInt(fieldCount); ! else ! throw new FileSpecException("Record Spec in Delimited File Spec should have field-count attribute."); ! NodeList fieldSpecNodeList=recordSpecElement.getElementsByTagName(FieldSpec.FIELD_SPEC_TAG_NAME); for(int i=0;i<fieldSpecNodeList.getLength();i++) *************** *** 129,134 **** } recordSpec.addFieldSpec(fieldSpec); - if(fieldSpec.getIndex()>recordSpec.fieldCount) - recordSpec.fieldCount=fieldSpec.getIndex(); } return recordSpec; --- 142,145 ---- *************** *** 143,146 **** --- 154,158 ---- stringValue.append("[recordType = " + super.recordType.toString() + "]"); stringValue.append("[delimiter = " + this.delimiter + "]"); + stringValue.append("[fieldCount = " + this.fieldCount + "]"); stringValue.append("[fieldSpecList = "); for(Iterator iterator=fieldSpecList.iterator();iterator.hasNext();) Index: sample-delimited-file-spec.xml =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/flat/sample-delimited-file-spec.xml,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** sample-delimited-file-spec.xml 10 Jun 2006 17:51:37 -0000 1.1 --- sample-delimited-file-spec.xml 15 Jun 2006 18:57:02 -0000 1.2 *************** *** 2,6 **** <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"/> --- 2,6 ---- <file-spec file-type="delimited-flat"> ! <record-spec record-type="detail" delimiter="|" field-count="4"> <field-spec field-name="field1" index="1"/> <field-spec field-name="field2" index="2"/> Index: DelimitedFlatFileReader.java =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/flat/DelimitedFlatFileReader.java,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** DelimitedFlatFileReader.java 13 Jun 2006 22:10:08 -0000 1.4 --- DelimitedFlatFileReader.java 15 Jun 2006 18:57:02 -0000 1.5 *************** *** 78,84 **** logger.trace("Entering parseRecord"); FlatFileReaderRecord record=null; List fieldSpecList=recordSpec.getFieldSpecs(); record=new FlatFileReaderRecord(recordSpec.getRecordType(),fieldSpecList.size()); - String[] fieldValuesArray=recordString.split(recordSpec.getDelimiter()); for(Iterator fieldSpecIterator=fieldSpecList.iterator();fieldSpecIterator.hasNext();) { --- 78,87 ---- logger.trace("Entering parseRecord"); FlatFileReaderRecord record=null; + String[] fieldValuesArray=recordString.split(recordSpec.getDelimiter()); + if(fieldValuesArray.length!=recordSpec.getFieldCount()) + throw new FileParseException("Record " + recordString + " doesnt have " + recordSpec.getFieldCount() + " of fields."); + List fieldSpecList=recordSpec.getFieldSpecs(); record=new FlatFileReaderRecord(recordSpec.getRecordType(),fieldSpecList.size()); for(Iterator fieldSpecIterator=fieldSpecList.iterator();fieldSpecIterator.hasNext();) { Index: package.html =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/flat/package.html,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** package.html 15 Jun 2006 03:35:52 -0000 1.3 --- package.html 15 Jun 2006 18:57:02 -0000 1.4 *************** *** 19,53 **** is starts with some value to identify the record and each record consists of set of of fields where each field starts at one position and ends at one position. ! <h5>Defining the file spec for fixed width flat files</h5> ! File spec which descibes the fixed width flat file expects the <code>file-type</code> ! attribute value should be "fixed-width-flat". It doesnt require any additional ! attributes along with the <code>file-type</code> attribute. ! <pre> ! <file-spec file-type="fixed-width-flat"> ! </file-spec> ! </pre> ! There could be multiple record specs exists in a file spec. Along with the ! record-type attribute, it requires an additional attribute <code>starts-with</code> ! which tells the value that the record starts with. ! <pre> ! <file-spec file-type="fixed-width-flat"> ! <record-spec record-type="DETAIL" starts-with="5"> ! </record-spec> ! </file-spec> ! </pre> There could be multiple field specs exists in a single record spec. Field spec ! requires few additional attributes along with the field-name attribute to identify ! or extract the field from the record. These attributes are <code>start-pos</code> which ! tells the starting position of the field in the record and <code>end-pos</code> which ! tells the ending position of the field in the record. Starting index in a record starts with "1". ! <pre> ! <file-spec file-type="fixed-width-flat"> ! <record-spec record-type="DETAIL" starts-with="5"> ! <field-spec field-name="field-name1" start-pos="12" end-pos="23"/> ! </record-spec> ! </file-spec> ! </pre> ! <h5>Reading the records from fixed width flat files</h5> ! Reading the records from the fixed width flat files is fairly simple. <pre style="color:green"> <i> --- 19,54 ---- is starts with some value to identify the record and each record consists of set of of fields where each field starts at one position and ends at one position. ! <h5>Defining the file spec for fixed width flat files</h5> ! File spec which descibes the fixed width flat file expects the <code>file-type</code> ! attribute value should be "fixed-width-flat". It doesnt require any additional ! attributes along with the <code>file-type</code> attribute. ! <pre> ! <file-spec file-type="fixed-width-flat"> ! </file-spec> ! </pre> ! There could be multiple record specs exists in a file spec. Along with the ! <code>record-type</code> attribute, it requires two additional attributes ! <code>starts-with</code> which tells the value that the record should starts with ! and <code>record-length</code> which tells the lenght of the record. ! <pre> ! <file-spec file-type="fixed-width-flat"> ! <record-spec record-type="DETAIL" starts-with="5" record-length="42"> ! </record-spec> ! </file-spec> ! </pre> There could be multiple field specs exists in a single record spec. Field spec ! requires few additional attributes along with the field-name attribute to identify ! or extract the field from the record. These attributes are <code>start-pos</code> which ! tells the starting position of the field in the record and <code>end-pos</code> which ! tells the ending position of the field in the record. Starting index in a record starts with "1". ! <pre> ! <file-spec file-type="fixed-width-flat"> ! <record-spec record-type="DETAIL" starts-with="5" record-length="42"> ! <field-spec field-name="field-name1" start-pos="12" end-pos="23"/> ! </record-spec> ! </file-spec> ! </pre> ! <h5>Reading the records from fixed width flat files</h5> ! Reading the records from the fixed width flat files is fairly simple. <pre style="color:green"> <i> *************** *** 89,123 **** consists of same number of fields and each field is are delimited by a special value in the record. ! <h5>Defining the file spec for delimited flat files</h5> ! File spec which descibes the delimited flat file expects the <code>file-type</code> ! attribute value should be "delimited-flat". It doesnt require any additional ! attributes along with the <code>file-type</code> attribute. ! <pre> ! <file-spec file-type="delimited-flat"> ! </file-spec> ! </pre> ! Since all the records in delimited flat file of the same type, it allows only ! one record spec should exists in the file spec. Along with the ! record-type attribute, it requires an additional attribute <code>delimiter</code> ! which tells the value that delimits the fields in the record. ! <pre> ! <file-spec file-type="delimited-flat"> ! <record-spec record-type="DETAIL" delimiter="|"> ! </record-spec> ! </file-spec> ! </pre> There could be multiple field specs exists in a record spec. Field spec ! requires one additional attributes along with the field-name attribute to identify ! or extract the field from the record. This attribute is <code>index</code> which ! tells the position of the field in the record. Starting index in a record starts with "1". ! <pre> ! <file-spec file-type="delimited-flat"> ! <record-spec record-type="DETAIL" delimiter="|"> ! <field-spec field-name="field-name1" index="3"/> ! </record-spec> ! </file-spec> ! </pre> ! <h5>Reading the records from delimited flat files</h5> ! Reading the records from the delimited flat files is fairly simple. <pre style="color:green"> <i> --- 90,126 ---- consists of same number of fields and each field is are delimited by a special value in the record. ! <h5>Defining the file spec for delimited flat files</h5> ! File spec which descibes the delimited flat file expects the <code>file-type</code> ! attribute value should be "delimited-flat". It doesnt require any additional ! attributes along with the <code>file-type</code> attribute. ! <pre> ! <file-spec file-type="delimited-flat"> ! </file-spec> ! </pre> ! Since all the records in delimited flat file of the same type, it allows only ! one record spec should exists in the file spec. Along with the ! record-type attribute, it requires two additional attributes <code>delimiter</code> ! and <code>field-count</code>. Attributes <code>delimiter</code> tells the value ! that delimites fields among the record and attribute <code>field-count</code> ! tells the number of fields can exists in the record. ! <pre> ! <file-spec file-type="delimited-flat"> ! <record-spec record-type="DETAIL" delimiter="|" field-count="4"> ! </record-spec> ! </file-spec> ! </pre> There could be multiple field specs exists in a record spec. Field spec ! requires one additional attributes along with the field-name attribute to identify ! or extract the field from the record. This attribute is <code>index</code> which ! tells the position of the field in the record. Starting index in a record starts with "1". ! <pre> ! <file-spec file-type="delimited-flat"> ! <record-spec record-type="DETAIL" delimiter="|" field-count="4"> ! <field-spec field-name="field-name1" index="3"/> ! </record-spec> ! </file-spec> ! </pre> ! <h5>Reading the records from delimited flat files</h5> ! Reading the records from the delimited flat files is fairly simple. <pre style="color:green"> <i> Index: FixedWidthFlatFileRecordSpec.java =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/flat/FixedWidthFlatFileRecordSpec.java,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** FixedWidthFlatFileRecordSpec.java 12 Jun 2006 13:15:37 -0000 1.5 --- FixedWidthFlatFileRecordSpec.java 15 Jun 2006 18:57:02 -0000 1.6 *************** *** 21,32 **** * 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"> * <!-- field specs will follow here --> * </record-spec> --- 21,34 ---- * 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> and <code>record-length</code> attributes ! * in the record-spec element to identify the record. <code>starts-with</code> tells ! * the value that the record starts with and <code>record-length</code> tells the ! * length of the record. Record type and starts-with attribute values ! * should be unique 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" record-length="42"> * <!-- field specs will follow here --> * </record-spec> *************** *** 48,56 **** * 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); --- 50,62 ---- * Holds the length of the record. */ ! protected int recordLength=0; /** * Constant defines the starts with attribute name. */ public static final String STARTS_WITH_ATTRIB_NAME = "starts-with"; + /** + * Constant defines the record length attribute name. + */ + public static final String RECORD_LENGTH_ATTRIB_NAME = "record-length"; private static Logger logger=Logger.getLogger(FixedWidthFlatFileRecordSpec.class); *************** *** 73,89 **** /** ! * 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() { ! return recordSize; } /** * 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. --- 79,95 ---- /** ! * Gets the length of the record. This will be obtained from the record-legnth ! * attribute in the record-spec. This will be used while writing/generating the record using ! * file writer and while reading the record to identify the record type. */ ! public int getRecordLength() { ! return recordLength; } /** * Tells whether the given record matches with the record spec. It checks whether ! * this record string starts with the starts-with value and the length is equals ! * to the record length. * * @param recordString Record read from the file. *************** *** 95,99 **** if(recordString==null) throw new IllegalArgumentException("Record string cannot be null to match record spec."); ! return recordString.startsWith(this.startsWith); } --- 101,105 ---- if(recordString==null) throw new IllegalArgumentException("Record string cannot be null to match record spec."); ! return recordString.startsWith(this.startsWith) && recordString.length()==recordLength; } *************** *** 131,135 **** 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++) --- 137,147 ---- throw new FileSpecException("Record Spec in Fixed Width File Spec should have starts-with attribute."); ! String recordLength=recordSpecElement.getAttribute(FixedWidthFlatFileRecordSpec.RECORD_LENGTH_ATTRIB_NAME); ! logger.debug("record specs length value = " + startsWith); ! if(recordLength!=null && !"".equals(startsWith.trim())) ! recordSpec.recordLength=Integer.parseInt(recordLength); ! else ! throw new FileSpecException("Record Spec in Fixed Width File Spec should have record-length attribute."); ! NodeList fieldSpecNodeList=recordSpecElement.getElementsByTagName(FieldSpec.FIELD_SPEC_TAG_NAME); for(int i=0;i<fieldSpecNodeList.getLength();i++) *************** *** 137,144 **** FixedWidthFlatFileFieldSpec fieldSpec=FixedWidthFlatFileFieldSpec.createFixedWidthFlatFileFieldSpec((Element)fieldSpecNodeList.item(i)); recordSpec.addFieldSpec(fieldSpec); - if(fieldSpec.getEndPosition()>recordSize) - recordSize=fieldSpec.getEndPosition(); } - recordSpec.recordSize=recordSize; return recordSpec; } --- 149,153 ---- *************** *** 152,155 **** --- 161,165 ---- stringValue.append("[recordType = " + super.recordType.toString() + "]"); stringValue.append("[startsWith = " + this.startsWith + "]"); + stringValue.append("[recordLength = " + this.recordLength + "]"); stringValue.append("[fieldSpecList = "); for(Iterator iterator=fieldSpecList.iterator();iterator.hasNext();) 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.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** sample-fixed-width-file-spec.xml 10 Jun 2006 17:51:37 -0000 1.2 --- sample-fixed-width-file-spec.xml 15 Jun 2006 18:57:02 -0000 1.3 *************** *** 2,9 **** <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"/> --- 2,9 ---- <file-spec file-type="fixed-width-flat"> ! <record-spec record-type="header" starts-with="1" record-length="9"> <field-spec field-name="timestamp" start-pos="2" end-pos="9"/> </record-spec> ! <record-spec record-type="detail" starts-with="5" record-length="41"> <field-spec field-name="field1" start-pos="2" end-pos="11"/> <field-spec field-name="field2" start-pos="12" end-pos="21"/> *************** *** 11,15 **** <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> --- 11,15 ---- <field-spec field-name="field4" start-pos="32" end-pos="41"/> </record-spec> ! <record-spec record-type="trailor" starts-with="6" record-length="9"> <field-spec field-name="recordCount" start-pos="2" end-pos="9"/> </record-spec> Index: FixedWidthFlatFileWriter.java =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/flat/FixedWidthFlatFileWriter.java,v retrieving revision 1.8 retrieving revision 1.9 diff -C2 -d -r1.8 -r1.9 *** FixedWidthFlatFileWriter.java 13 Jun 2006 22:10:08 -0000 1.8 --- FixedWidthFlatFileWriter.java 15 Jun 2006 18:57:02 -0000 1.9 *************** *** 74,78 **** FlatFileWriterRecord record=(FlatFileWriterRecord)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()); --- 74,78 ---- FlatFileWriterRecord record=(FlatFileWriterRecord)writerRecord; FixedWidthFlatFileRecordSpec recordSpec=(FixedWidthFlatFileRecordSpec)this.fileSpec.getRecordSpec(record.getRecordType()); ! char[] recordBuffer=new char[recordSpec.getRecordLength()]; Arrays.fill(recordBuffer,' '); System.arraycopy(recordSpec.startsWith.toCharArray(), 0, recordBuffer, 0, recordSpec.startsWith.length()); |