batchserver-cvs Mailing List for Enterprise Batch Server (Page 15)
Brought to you by:
suresh_pragada
You can subscribe to this list here.
2006 |
Jan
|
Feb
(10) |
Mar
(159) |
Apr
(5) |
May
(52) |
Jun
(70) |
Jul
|
Aug
(28) |
Sep
(256) |
Oct
(38) |
Nov
|
Dec
|
---|---|---|---|---|---|---|---|---|---|---|---|---|
2007 |
Jan
|
Feb
|
Mar
|
Apr
|
May
(3) |
Jun
|
Jul
|
Aug
|
Sep
|
Oct
|
Nov
|
Dec
|
From: Suresh <sur...@us...> - 2006-06-10 17:51:57
|
Update of /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/xml In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv20379 Modified Files: XMLFileReader.java XMLFileSpec.java XMLFileWriter.java XMLRecordSpec.java Log Message: no message Index: XMLFileReader.java =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/xml/XMLFileReader.java,v retrieving revision 1.7 retrieving revision 1.8 diff -C2 -d -r1.7 -r1.8 *** XMLFileReader.java 9 Jun 2006 21:36:34 -0000 1.7 --- XMLFileReader.java 10 Jun 2006 17:51:54 -0000 1.8 *************** *** 284,289 **** EndElement endElement=(EndElement)reader.nextEvent(); isPreviousElementStart=false; ! if(recordElement.equalsIgnoreCase(endElement.getName().getLocalPart())) break; else { --- 284,294 ---- EndElement endElement=(EndElement)reader.nextEvent(); isPreviousElementStart=false; ! if(recordElement.equalsIgnoreCase(endElement.getName().getLocalPart()) && elementStack.capacity()==0) ! { ! /** ! * End element name matches the starting element name of this record and no elements in stack. ! */ break; + } else { *************** *** 393,397 **** * @return Returns the value associated with this element. */ ! public ReaderRecord readCompositeElement(String elementName) { return (ReaderRecord)fieldMap.get(elementName); --- 398,402 ---- * @return Returns the value associated with this element. */ ! public ReaderRecord readComplexElement(String elementName) { return (ReaderRecord)fieldMap.get(elementName); *************** *** 405,409 **** * @return Returns the value associated with this element. */ ! public List readMutlipleElements(String elementName) { Object fieldValue=this.fieldMap.get(elementName); --- 410,414 ---- * @return Returns the value associated with this element. */ ! public List readRepeatElement(String elementName) { Object fieldValue=this.fieldMap.get(elementName); Index: XMLFileWriter.java =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/xml/XMLFileWriter.java,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** XMLFileWriter.java 9 Jun 2006 21:36:34 -0000 1.5 --- XMLFileWriter.java 10 Jun 2006 17:51:54 -0000 1.6 *************** *** 105,109 **** int index=recordXpath.lastIndexOf('/'); String recordElement=recordXpath.substring(((index!=-1)?(index+1):0)); ! writeCompositeElement(recordElement, record); writer.flush(); } --- 105,109 ---- int index=recordXpath.lastIndexOf('/'); String recordElement=recordXpath.substring(((index!=-1)?(index+1):0)); ! writeComplexElement(recordElement, record); writer.flush(); } *************** *** 141,145 **** * @param fieldValueAsRecord Value of the nested element. */ ! protected void writeCompositeElement(String recordElementName,XMLWriterRecord fieldValueAsRecord) throws XMLStreamException { indentationEngine.startElement(); --- 141,145 ---- * @param fieldValueAsRecord Value of the nested element. */ ! protected void writeComplexElement(String recordElementName,XMLWriterRecord fieldValueAsRecord) throws XMLStreamException { indentationEngine.startElement(); *************** *** 151,159 **** if(fieldValue instanceof XMLWriterRecord) { ! writeCompositeElement(fieldName, (XMLWriterRecord)fieldValue); } else if(fieldValue instanceof List) { ! writeMlutipleElements(fieldName, (List)fieldValue); } else --- 151,159 ---- if(fieldValue instanceof XMLWriterRecord) { ! writeComplexElement(fieldName, (XMLWriterRecord)fieldValue); } else if(fieldValue instanceof List) { ! writeRepeatElement(fieldName, (List)fieldValue); } else *************** *** 172,176 **** * @param fieldValueAsList Value of the repeated element. */ ! protected void writeMlutipleElements(String multiElementName,List fieldValueAsList) throws XMLStreamException { List multiElementList=(List)fieldValueAsList; --- 172,176 ---- * @param fieldValueAsList Value of the repeated element. */ ! protected void writeRepeatElement(String multiElementName,List fieldValueAsList) throws XMLStreamException { List multiElementList=(List)fieldValueAsList; *************** *** 180,188 **** if(fieldValue instanceof XMLWriterRecord) { ! writeCompositeElement(multiElementName, (XMLWriterRecord)fieldValue); } else if(fieldValue instanceof List) { ! writeMlutipleElements(multiElementName, (List)fieldValue); } else --- 180,188 ---- if(fieldValue instanceof XMLWriterRecord) { ! writeComplexElement(multiElementName, (XMLWriterRecord)fieldValue); } else if(fieldValue instanceof List) { ! writeRepeatElement(multiElementName, (List)fieldValue); } else *************** *** 288,292 **** * Write nested/composite element into the record. */ ! public void writeCompositeElement(String fieldName, WriterRecord fieldValue) { this.fieldMap.put(fieldName, fieldValue); --- 288,292 ---- * Write nested/composite element into the record. */ ! public void writeComplexElement(String fieldName, WriterRecord fieldValue) { this.fieldMap.put(fieldName, fieldValue); *************** *** 299,303 **** * @param fieldValue Value of the field repated more than once. */ ! public void writeMultiElement(String fieldName, List fieldValue) { this.fieldMap.put(fieldName, fieldValue); --- 299,303 ---- * @param fieldValue Value of the field repated more than once. */ ! public void writeRepeatElement(String fieldName, List fieldValue) { this.fieldMap.put(fieldName, fieldValue); Index: XMLFileSpec.java =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/xml/XMLFileSpec.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** XMLFileSpec.java 9 Jun 2006 21:36:34 -0000 1.3 --- XMLFileSpec.java 10 Jun 2006 17:51:54 -0000 1.4 *************** *** 26,32 **** --- 26,34 ---- * </p> * <p> + * <pre> * <file-spec file-type="xml" root-element="consumer-request"> * <!-- record specs will follow here --> * </file-spec> + * </pre> * </p> * Index: XMLRecordSpec.java =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/xml/XMLRecordSpec.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** XMLRecordSpec.java 8 Jun 2006 22:21:15 -0000 1.3 --- XMLRecordSpec.java 10 Jun 2006 17:51:54 -0000 1.4 *************** *** 26,32 **** --- 26,34 ---- * </p> * <p> + * <pre> * <file-spec file-type="xml" root-element="consumer-request"> * <record-spec record-type="detail" record-xpath="/consumer-request/consumer-add"/> * </file-spec> + * </pre> * </p> * |
Update of /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/flat In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv20357 Modified Files: FixedWidthFlatFileFieldSpec.java FixedWidthFlatFileFileSpec.java FixedWidthFlatFileReader.java FixedWidthFlatFileRecordSpec.java FixedWidthFlatFileWriter.java sample-fixed-width-file-spec.xml Added Files: DelimitedFlatFileFieldSpec.java DelimitedFlatFileFileSpec.java DelimitedFlatFileReader.java DelimitedFlatFileRecordSpec.java DelimitedFlatFileWriter.java FlatFileReader.java FlatFileWriter.java sample-delimited-file-spec.xml sample-delimited-file.dat Log Message: no message --- NEW FILE: DelimitedFlatFileRecordSpec.java --- /* * DelimitedFlatFileRecordSpec.java * * Created on June 10, 2006, 10:55 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.util.Iterator; import org.apache.log4j.Logger; import org.jmonks.batchserver.io.FieldSpec; import org.jmonks.batchserver.io.FileSpecException; import org.jmonks.batchserver.io.RecordSpec; import org.jmonks.batchserver.io.RecordType; import org.w3c.dom.Element; import org.w3c.dom.NodeList; /** * <p> * 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> * </file-spec> * </pre> * </p> * * @author Suresh Pragada * @version 1.0 * @since 1.0 */ public class DelimitedFlatFileRecordSpec extends RecordSpec { /** * Represents the delimiter for the record. */ private String delimiter=null; /** * Constant defines the delimiter attribute name which the value is "delimiter" */ public static final String DELIMITER_ATTRIB_NAME = "delimiter"; private static Logger logger=Logger.getLogger(DelimitedFlatFileRecordSpec.class); /** * Constructs the record spec by accepting the record type. */ protected DelimitedFlatFileRecordSpec(RecordType recordType) { super(recordType); } /** * Returns the delimiter which divides the fields in the record. */ public String getDelimiter() { return this.delimiter; } /** * Factory method to create the delimited 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 delimited flat file record spec. * * @throws org.jmonks.batchserver.io.FileSpecException If record-spec * doesnt have the delimiter or record-type attribute or index value is repeated on more than one field spec. */ public static DelimitedFlatFileRecordSpec createDelimitedFlatFileRecordSpec(final Element recordSpecElement) { logger.trace("Entering createDelimitedFlatFileRecordSpec"); DelimitedFlatFileRecordSpec 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 DelimitedFlatFileRecordSpec(recordType); } else throw new FileSpecException("Record Spec in Delimited File Spec should have record-type attribute."); String delimiter=recordSpecElement.getAttribute(DelimitedFlatFileRecordSpec.DELIMITER_ATTRIB_NAME); logger.debug("record specs delimiter value = " + delimiter); if(delimiter!=null && !"".equals(delimiter.trim())) recordSpec.delimiter=delimiter.trim(); else throw new FileSpecException("Record Spec in Delimited File Spec should have delimiter attribute."); NodeList fieldSpecNodeList=recordSpecElement.getElementsByTagName(FieldSpec.FIELD_SPEC_TAG_NAME); for(int i=0;i<fieldSpecNodeList.getLength();i++) { DelimitedFlatFileFieldSpec fieldSpec=DelimitedFlatFileFieldSpec.createDelimitedFlatFileFieldSpec((Element)fieldSpecNodeList.item(i)); for(Iterator iterator=recordSpec.getFieldSpecs().iterator();iterator.hasNext();) { DelimitedFlatFileFieldSpec existingFieldSpec=(DelimitedFlatFileFieldSpec)iterator.next(); if(existingFieldSpec.getIndex()==fieldSpec.getIndex()) { throw new FileSpecException("Found the index value " + fieldSpec.getIndex() + " on multiple fields."); } } recordSpec.addFieldSpec(fieldSpec); } return recordSpec; } /** * @see java.lang.Object#toString() */ public String toString() { StringBuffer stringValue=new StringBuffer("{DelimitedFlatFileRecordSpec "); stringValue.append("[recordType = " + super.recordType.toString() + "]"); stringValue.append("[delimiter = " + this.delimiter + "]"); stringValue.append("[fieldSpecList = "); for(Iterator iterator=fieldSpecList.iterator();iterator.hasNext();) stringValue.append(((DelimitedFlatFileFieldSpec)iterator.next()).toString()); 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.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** FixedWidthFlatFileWriter.java 9 Jun 2006 21:36:34 -0000 1.5 --- FixedWidthFlatFileWriter.java 10 Jun 2006 17:51:37 -0000 1.6 *************** *** 10,18 **** package org.jmonks.batchserver.io.flat; - - import java.io.BufferedWriter; import java.io.IOException; import java.io.OutputStream; - import java.io.OutputStreamWriter; import java.util.Arrays; import java.util.HashMap; --- 10,15 ---- *************** *** 21,27 **** 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; --- 18,22 ---- *************** *** 40,57 **** * @since 1.0 */ ! public class FixedWidthFlatFileWriter extends FileWriter ! { ! /** ! * 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); --- 35,40 ---- * @since 1.0 */ ! public class FixedWidthFlatFileWriter extends FlatFileWriter ! { private static Logger logger=Logger.getLogger(FixedWidthFlatFileWriter.class); *************** *** 64,96 **** public FixedWidthFlatFileWriter(OutputStream outputStream,FileSpec fileSpec) { ! logger.trace("Entering FixedWidthFlatFileWriter constructor"); ! this.fileSpec=(FixedWidthFlatFileFileSpec)fileSpec; ! writer=new BufferedWriter(new OutputStreamWriter(outputStream)); ! logger.debug("Done creating and initializing the writer"); ! logger.trace("Exiting FixedWidthFlatFileWriter constructor"); ! } ! ! /** ! * Closes the writer. ! */ ! public void close() ! { ! logger.trace("Entering close"); ! if(this.writer!=null) ! { ! try ! { ! this.writer.close(); ! logger.debug("Writer has been closed"); ! } ! catch(IOException exception) ! { ! logger.debug("IOException while closing the reader. Message = " + exception.getMessage(),exception); ! } ! finally ! { ! this.writer=null; ! } ! } } --- 47,51 ---- public FixedWidthFlatFileWriter(OutputStream outputStream,FileSpec fileSpec) { ! super(outputStream,fileSpec); } *************** *** 123,168 **** * @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"); } --- 78,99 ---- * @throws org.jmonks.batchserver.io.FileParseException Problems while trying to write the record. */ ! protected String generateRecord(WriterRecord writerRecord) throws IOException { ! logger.trace("Entering generateRecord"); ! 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)); } ! logger.trace("Exiting generateRecord"); ! return new String(recordBuffer); } --- NEW FILE: DelimitedFlatFileWriter.java --- /* * DelimitedFlatFileWriter.java * * Created on June 10, 2006, 12:38 PM * * To change this template, choose Tools | Options and locate the template under * the Source Creation and Management node. Right-click the template and choose * Open. You can then make changes to the template in the Source Editor. */ package org.jmonks.batchserver.io.flat; import java.io.OutputStream; import org.apache.log4j.Logger; import org.jmonks.batchserver.io.FileSpec; import org.jmonks.batchserver.io.RecordType; import org.jmonks.batchserver.io.WriterRecord; /** * * @author Suresh Pragada */ public class DelimitedFlatFileWriter extends FlatFileWriter { private static Logger logger=Logger.getLogger(DelimitedFlatFileWriter.class); /** Creates a new instance of DelimitedFlatFileWriter */ public DelimitedFlatFileWriter(OutputStream outputStream,FileSpec fileSpec) { super(outputStream,fileSpec); } public WriterRecord createWriterRecord(RecordType recordType) { return null; } protected String generateRecord(WriterRecord writerRecord) throws java.io.IOException { return null; } } --- NEW FILE: sample-delimited-file-spec.xml --- <?xml version="1.0" encoding="UTF-8"?> <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"/> <field-spec field-name="field3" index="3"/> <field-spec field-name="field4" index="4"/> </record-spec> </file-spec> --- NEW FILE: DelimitedFlatFileFileSpec.java --- /* * DelimitedFlatFileFileSpec.java * * Created on June 10, 2006, 10:47 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.util.Iterator; import org.apache.log4j.Logger; import org.jmonks.batchserver.io.FileSpec; import org.jmonks.batchserver.io.FileSpecException; import org.jmonks.batchserver.io.FileType; import org.jmonks.batchserver.io.RecordSpec; import org.w3c.dom.Element; import org.w3c.dom.NodeList; /** * <p> * DelimitedFlatFileFileSpec represents the file spec defines the flat file * where all the fields in the records delimited with a particular character or value. * This file-spec doesn't require any special attributes other than file-type, * which should be "delimited-flat". * Here is a sample file spec... * </p> * <p> * <pre> * <file-spec file-type="delimited-flat"> * <!-- record specs will follow here --> * </file-spec> * </pre> * </p> * * @author Suresh Pragada * @version 1.0 * @since 1.0 */ public class DelimitedFlatFileFileSpec extends FileSpec { private static Logger logger=Logger.getLogger(DelimitedFlatFileFileSpec.class); /** * Constructs the DelimitedFlatFileFileSpec. */ protected DelimitedFlatFileFileSpec() { super(FileType.DELIMITED_FLAT_FILE); } /** * Factory method create the delimited flat file spec object from the given * DOM Element representing the file-spec element. * * @param fileSpecElement DOM Element representing the file-spec element. * * @return Returns the instance of DelimitedFlatFileFileSpec. * * @throws org.jmonks.batchserver.io.FileSpecException If more than one record spec is configured. */ public static FileSpec createDelimitedFlatFileFileSpec(final Element fileSpecElement) { logger.trace("Entering createDelimitedFlatFileFileSpec"); DelimitedFlatFileFileSpec fileSpec=new DelimitedFlatFileFileSpec(); NodeList recordSpecNodeList=fileSpecElement.getElementsByTagName(RecordSpec.RECORD_SPEC_TAG_NAME); if(recordSpecNodeList.getLength()>1) { logger.fatal("Delimited file spec should not have more than 1 record spec. Number of record specs found = " + recordSpecNodeList.getLength()); throw new FileSpecException("Delimited file spec should have only one record spec."); } else { DelimitedFlatFileRecordSpec recordSpec=DelimitedFlatFileRecordSpec.createDelimitedFlatFileRecordSpec((Element)recordSpecNodeList.item(0)); logger.debug("Adding the record spec = " + recordSpec.toString()); fileSpec.addRecordSpec(recordSpec); return (FileSpec)fileSpec; } } /** * @see java.lang.Object#toString() */ public String toString() { StringBuffer stringValue=new StringBuffer("{DelimitedFlatFileFileSpec "); stringValue.append("[fileType = " + super.fileType.toString() + "]"); stringValue.append("[recordSpecList = "); for(Iterator iterator=recordSpecMap.values().iterator();iterator.hasNext();) stringValue.append(((DelimitedFlatFileRecordSpec)iterator.next()).toString()); stringValue.append("]}"); return stringValue.toString(); } } Index: FixedWidthFlatFileFileSpec.java =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/flat/FixedWidthFlatFileFileSpec.java,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** FixedWidthFlatFileFileSpec.java 9 Jun 2006 21:36:34 -0000 1.4 --- FixedWidthFlatFileFileSpec.java 10 Jun 2006 17:51:37 -0000 1.5 *************** *** 26,32 **** --- 26,34 ---- * </p> * <p> + * <pre> * <file-spec file-type="fixed-width-flat"> * <!-- record specs will follow here --> * </file-spec> + * </pre> * </p> * *************** *** 48,52 **** /** ! * Factory method create the fixed with flat file spec object from the given * DOM Element representing the file-spec element. * --- 50,54 ---- /** ! * Factory method create the fixed width flat file spec object from the given * DOM Element representing the file-spec element. * Index: FixedWidthFlatFileReader.java =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/flat/FixedWidthFlatFileReader.java,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** FixedWidthFlatFileReader.java 9 Jun 2006 21:36:34 -0000 1.5 --- FixedWidthFlatFileReader.java 10 Jun 2006 17:51:37 -0000 1.6 *************** *** 10,18 **** package org.jmonks.batchserver.io.flat; - - import java.io.BufferedReader; - import java.io.IOException; import java.io.InputStream; - import java.io.InputStreamReader; import java.util.HashMap; import java.util.Iterator; --- 10,14 ---- *************** *** 35,47 **** * @since 1.0 */ ! public class FixedWidthFlatFileReader extends FileReader { - /** - * 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); --- 31,36 ---- * @since 1.0 */ ! public class FixedWidthFlatFileReader extends FlatFileReader { private static Logger logger=Logger.getLogger(FixedWidthFlatFileReader.class); *************** *** 51,134 **** * @param fileInputStream Input stream to the file. * @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(InputStream fileInputStream,FileSpec fileSpec) { ! logger.trace("Entering FixedWidthFlatFileReader constructor"); ! this.fileSpec=(FixedWidthFlatFileFileSpec)fileSpec; ! reader=new BufferedReader(new InputStreamReader(fileInputStream)); ! logger.debug("Created the reader successfully"); ! } ! ! /** ! * <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; ! else ! { ! try ! { ! String recordString=this.reader.readLine(); ! if(recordString==null) ! { ! logger.info("Reader has reached EOF.. Closing the reader."); ! this.reader.close(); ! this.reader=null; ! return null; ! } ! else ! { ! ReaderRecord record=this.parseRecord(recordString); ! return record; ! } ! } ! 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()); ! } ! } ! } ! ! /** ! * Closes the reader. ! */ ! public void close() ! { ! logger.trace("Entering close"); ! if(this.reader!=null) ! { ! try ! { ! this.reader.close(); ! logger.info("Reader has been closed"); ! } ! catch(IOException exception) ! { ! logger.debug("IO Exception while closing the reader. Message = " + exception.getMessage(),exception); ! } ! finally ! { ! this.reader=null; ! } ! } } ! /** * Parses the given record string, translates it into the proper ReaderRecord --- 40,49 ---- * @param fileInputStream Input stream to the file. * @param fileSpec File spec to be used to read the file. */ public FixedWidthFlatFileReader(InputStream fileInputStream,FileSpec fileSpec) { ! super(fileInputStream,fileSpec); } ! /** * Parses the given record string, translates it into the proper ReaderRecord *************** *** 140,145 **** * next record. This includes unable to identify the recod with the availble record specs. */ ! private ReaderRecord parseRecord(String recordString) { FixedWidthFlatFileReaderRecord record=null; List recordSpecList=this.fileSpec.getRecordSpecs(); --- 55,61 ---- * next record. This includes unable to identify the recod with the availble record specs. */ ! protected ReaderRecord parseRecord(String recordString) { + logger.trace("Entering parseRecord"); FixedWidthFlatFileReaderRecord record=null; List recordSpecList=this.fileSpec.getRecordSpecs(); *************** *** 172,175 **** --- 88,92 ---- throw new FileParseException("Record " + recordString + " cannot be matched with any configured record specs."); } + logger.trace("Entering parseRecord"); return record; } Index: FixedWidthFlatFileFieldSpec.java =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/flat/FixedWidthFlatFileFieldSpec.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** FixedWidthFlatFileFieldSpec.java 8 Jun 2006 22:21:15 -0000 1.3 --- FixedWidthFlatFileFieldSpec.java 10 Jun 2006 17:51:37 -0000 1.4 *************** *** 18,22 **** * <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. --- 18,22 ---- * <p> * FixedWidthFlatFileFieldSpec represents field-spec element in the record spec ! * belongs to the fixed width 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. *************** *** 25,28 **** --- 25,29 ---- * </p> * <p> + * <pre> * <file-spec file-type="fixed-width-flat"> * <record-spec record-type="detail" starts-with="5"> *************** *** 31,34 **** --- 32,36 ---- * </record-spec> * </file-spec> + * </pre> * </p> * *************** *** 107,115 **** 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; return fieldSpec; --- 109,123 ---- FixedWidthFlatFileFieldSpec fieldSpec=new FixedWidthFlatFileFieldSpec(fieldName); ! String startPosition=fieldSpecElement.getAttribute(FixedWidthFlatFileFieldSpec.START_POSITION_ATTRIB_NAME); ! if(startPosition!=null && !"".equals(startPosition.trim())) ! fieldSpec.startPosition=Integer.parseInt(startPosition); ! else ! throw new FileSpecException("start-pos attribute is required in fixed width flat file spec element."); ! String endPosition=fieldSpecElement.getAttribute(FixedWidthFlatFileFieldSpec.END_POSITION_ATTRIB_NAME); ! if(endPosition!=null && !"".equals(endPosition.trim())) ! fieldSpec.startPosition=Integer.parseInt(endPosition); ! else ! throw new FileSpecException("end-pos attribute is required in fixed width flat file spec element."); return fieldSpec; --- NEW FILE: FlatFileWriter.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.io.OutputStream; import java.io.OutputStreamWriter; 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; /** * <p> * FlatFileWriter 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 abstract class FlatFileWriter extends FileWriter { /** * File spec to be used in writing the file. */ protected FileSpec fileSpec=null; /** * Buffered writer to conveniently write each record as a new line into the file. */ 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(FlatFileWriter.class); /** * Constructs and initializes the writer with the given values. * * @param outputStream Output stream to write the records. * @param fileSpec File spec to be used to generate the file. */ public FlatFileWriter(OutputStream outputStream,FileSpec fileSpec) { logger.trace("Entering FlatFileWriter constructor"); this.fileSpec=fileSpec; writer=new BufferedWriter(new OutputStreamWriter(outputStream)); logger.info("Done creating the flat file writer."); } /** * 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 abstract WriterRecord createWriterRecord(RecordType recordType); /** * Generates the string representation of the record from given * writer record. * * @param writerRecord Record where field values can be read. * * @return Returns the string representation of the record. */ protected abstract String generateRecord(WriterRecord writerRecord) throws IOException; /** * 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 { String record=generateRecord(writerRecord); if(writtenFirstLine) this.writer.newLine(); else writtenFirstLine=true; this.writer.write(record); } 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"); } /** * Closes the writer and all the resource being used by this writer. * This includes the output stream passed to construct the writer. */ public void close() { logger.trace("Entering close"); if(this.writer!=null) { try { this.writer.close(); logger.debug("Writer has been closed"); } catch(IOException exception) { logger.debug("IOException while closing the reader. Message = " + exception.getMessage(),exception); } finally { this.writer=null; } } } } Index: FixedWidthFlatFileRecordSpec.java =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/flat/FixedWidthFlatFileRecordSpec.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** FixedWidthFlatFileRecordSpec.java 8 Jun 2006 22:21:15 -0000 1.3 --- FixedWidthFlatFileRecordSpec.java 10 Jun 2006 17:51:37 -0000 1.4 *************** *** 20,29 **** * <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"> --- 20,30 ---- * <p> * 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"> *************** *** 31,34 **** --- 32,36 ---- * </record-spec> * </file-spec> + * </pre> * </p> * 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.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** sample-fixed-width-file-spec.xml 3 Jun 2006 13:14:41 -0000 1.1 --- sample-fixed-width-file-spec.xml 10 Jun 2006 17:51:37 -0000 1.2 *************** *** 14,19 **** <field-spec field-name="recordCount" start-pos="2" end-pos="9"/> </record-spec> - <!-- <record-spec record-type="detail" delimiter="|"> - <field-spec field-name="pmtId" index="1"/> - </record-spec> --> </file-spec> --- 14,16 ---- --- NEW FILE: sample-delimited-file.dat --- 1234567891|0000012.00|3434343434|7878787878 1234567892|0000013.00|3434343434|7878787878 1234567893|0000014.00|3434343434|7878787878 1234567894|0000015.00|3434343434|7878787878 1234567895|0000016.00|3434343434|7878787878 --- NEW FILE: DelimitedFlatFileFieldSpec.java --- /* * DelimitedFlatFileFieldSpec.java * * Created on June 10, 2006, 11:09 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 org.apache.log4j.Logger; import org.jmonks.batchserver.io.FieldSpec; import org.jmonks.batchserver.io.FileSpecException; import org.w3c.dom.Element; /** * <p> * DelimitedFlatFileFieldSpec represents field-spec element in the record spec * belongs to the delimited flat file type. Along with the field-name attribute * it looks for the <code>index</code> attribute in the field-spec element to retrieve * the field from the record. It specifies the field position in the record. * Field index value starts with 1. * Filed name on field-spec should be unique across the record-spec. * Here is a sample spec snippet... * </p> * <p> * <pre> * <file-spec file-type="delimited-flat"> * <record-spec record-type="detail" delimiter="|"> * <field-spec field-name="consumer-id" index="1"/> * <field-spec field-name="consumer-name" index="2"/> * </record-spec> * </file-spec> * </pre> * </p> * * @author Suresh Pragada * @version 1.0 * @since 1.0 */ public class DelimitedFlatFileFieldSpec extends FieldSpec { /** * Represents the field position in the record. */ private int index; /** * Constant defines the index attribute name which is "index" */ public static final String INDEX_ATTRIB_NAME = "index"; private static Logger logger=Logger.getLogger(DelimitedFlatFileFieldSpec.class); /** * Creates a new instance of DelimitedFlatFileFieldSpec by using field name. */ protected DelimitedFlatFileFieldSpec(String fieldName) { super(fieldName); } /** * Returns the index of field in the record. Index value starts with 1. */ public int getIndex() { return this.index; } /** * Factory method to create the DelimitedFlatFileFieldSpec 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 DelimitedFlatFileFieldSpec instance. * * @throws org.jmonks.batchserver.io.FileSpecException If field-spec * field-name values are not unique across record-spec and it is missing index attribute. */ public static DelimitedFlatFileFieldSpec createDelimitedFlatFileFieldSpec(final Element fieldSpecElement) { logger.trace("Entering createDelimitedFlatFileFieldSpec"); String fieldName=fieldSpecElement.getAttribute(FieldSpec.FIELD_NAME_ATTRIB_NAME); DelimitedFlatFileFieldSpec fieldSpec=new DelimitedFlatFileFieldSpec(fieldName); String index=fieldSpecElement.getAttribute(DelimitedFlatFileFieldSpec.INDEX_ATTRIB_NAME); if(index!=null && !"".equals(index.trim())) fieldSpec.index=Integer.parseInt(index); else throw new FileSpecException("index attribute is required in delimited flat file spec element."); return fieldSpec; } /** * @see java.lang.Object#toString() */ public String toString() { StringBuffer stringValue=new StringBuffer("{DelimitedFlatFileFieldSpec "); stringValue.append("[fieldName = " + super.fieldName + "]"); stringValue.append("[index = " + this.index + "]"); stringValue.append("}"); return stringValue.toString(); } } --- NEW FILE: DelimitedFlatFileReader.java --- /* * DelimitedFlatFileReader.java * * Created on June 10, 2006, 12:37 PM * * To change this template, choose Tools | Options and locate the template under * the Source Creation and Management node. Right-click the template and choose * Open. You can then make changes to the template in the Source Editor. */ package org.jmonks.batchserver.io.flat; import java.io.InputStream; import org.apache.log4j.Logger; import org.jmonks.batchserver.io.FileSpec; import org.jmonks.batchserver.io.ReaderRecord; /** * * @author Suresh Pragada */ public class DelimitedFlatFileReader extends FlatFileReader { private static Logger logger=Logger.getLogger(DelimitedFlatFileReader.class); public DelimitedFlatFileReader(InputStream fileInputStream,FileSpec fileSpec) { super(fileInputStream, fileSpec); } protected ReaderRecord parseRecord(String recordString) { return null; } } --- NEW FILE: FlatFileReader.java --- /* * FlatFileReader.java * * Created on May 26, 2006, 2:15 PM * * To change this template, choose Tools | Options and locate the template under * the Source Creation and Management node. Right-click the template and choose * Open. You can then make changes to the template in the Source Editor. */ package org.jmonks.batchserver.io.flat; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import org.apache.log4j.Logger; import org.jmonks.batchserver.io.*; /** * <p> * FlatFileReader 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 abstract class FlatFileReader extends FileReader { /** * File spec to be used to read the flat file. */ protected FileSpec fileSpec=null; /** * Buffered reader to flexibly read each line as record from the file. */ private BufferedReader reader=null; private static Logger logger=Logger.getLogger(FlatFileReader.class); /** * Constructs and initializes the flat file reader with the given file path and file spec. * * @param fileInputStream Input stream to the file. * @param fileSpec File spec to be used to read the file. */ public FlatFileReader(InputStream fileInputStream,FileSpec fileSpec) { logger.trace("Entering FlatFileReader constructor"); this.fileSpec=fileSpec; reader=new BufferedReader(new InputStreamReader(fileInputStream)); logger.info("Done creating the flat file reader."); } /** * <p> * Gets the next available record from the file and returns it wrapped * with the appropriate ReaderRecod 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; else { try { String recordString=this.reader.readLine(); if(recordString==null) { logger.info("Reader has reached EOF.. Closing the reader."); this.reader.close(); this.reader=null; return null; } else { ReaderRecord record=this.parseRecord(recordString); return record; } } 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()); } } } /** * Closes the reader and all the resources it is using. This includes the * input stream passed to create the reader. */ public void close() { logger.trace("Entering close"); if(this.reader!=null) { try { this.reader.close(); logger.info("Reader has been closed"); } catch(IOException exception) { logger.debug("IO Exception while closing the reader. Message = " + exception.getMessage(),exception); } finally { this.reader=null; } } } /** * Parses the given record string based on the record spec, * translates it into the appropriate ReaderRecord * and returns that reader record. * * @param recordString Next available line 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. */ protected abstract ReaderRecord parseRecord(String recordString); } |
From: Suresh <sur...@us...> - 2006-06-10 17:50:30
|
Update of /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv19944 Modified Files: FileReader.java FileSpec.java FileWriter.java RecordType.java Log Message: no message Index: FileWriter.java =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/FileWriter.java,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** FileWriter.java 9 Jun 2006 21:36:34 -0000 1.5 --- FileWriter.java 10 Jun 2006 17:50:24 -0000 1.6 *************** *** 13,16 **** --- 13,17 ---- import java.io.OutputStream; import org.apache.log4j.Logger; + import org.jmonks.batchserver.io.flat.DelimitedFlatFileWriter; import org.jmonks.batchserver.io.flat.FixedWidthFlatFileWriter; import org.jmonks.batchserver.io.xml.XMLFileWriter; *************** *** 99,103 **** return new FixedWidthFlatFileWriter(outputStream,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(outputStream,fileSpec); --- 100,104 ---- return new FixedWidthFlatFileWriter(outputStream,fileSpec); else if(fileSpec.getFileType()==FileType.DELIMITED_FLAT_FILE) ! return new DelimitedFlatFileWriter(outputStream, fileSpec); else if(fileSpec.getFileType()==FileType.XML_FILE) return new XMLFileWriter(outputStream,fileSpec); Index: FileReader.java =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/FileReader.java,v retrieving revision 1.10 retrieving revision 1.11 diff -C2 -d -r1.10 -r1.11 *** FileReader.java 9 Jun 2006 21:36:34 -0000 1.10 --- FileReader.java 10 Jun 2006 17:50:24 -0000 1.11 *************** *** 10,17 **** package org.jmonks.batchserver.io; - - import java.io.File; import java.io.InputStream; import org.apache.log4j.Logger; import org.jmonks.batchserver.io.xml.XMLFileReader; import org.jmonks.batchserver.io.flat.FixedWidthFlatFileReader; --- 10,16 ---- package org.jmonks.batchserver.io; import java.io.InputStream; import org.apache.log4j.Logger; + import org.jmonks.batchserver.io.flat.DelimitedFlatFileReader; import org.jmonks.batchserver.io.xml.XMLFileReader; import org.jmonks.batchserver.io.flat.FixedWidthFlatFileReader; *************** *** 98,102 **** return new FixedWidthFlatFileReader(fileInputStream,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(fileInputStream,fileSpec); --- 97,101 ---- return new FixedWidthFlatFileReader(fileInputStream,fileSpec); else if(fileSpec.getFileType()==FileType.DELIMITED_FLAT_FILE) ! return new DelimitedFlatFileReader(fileInputStream,fileSpec); else if(fileSpec.getFileType()==FileType.XML_FILE) return new XMLFileReader(fileInputStream,fileSpec); Index: FileSpec.java =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/FileSpec.java,v retrieving revision 1.9 retrieving revision 1.10 diff -C2 -d -r1.9 -r1.10 *** FileSpec.java 9 Jun 2006 21:36:34 -0000 1.9 --- FileSpec.java 10 Jun 2006 17:50:24 -0000 1.10 *************** *** 10,17 **** package org.jmonks.batchserver.io; - - import java.io.File; - import java.io.FileInputStream; - import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; --- 10,13 ---- *************** *** 23,26 **** --- 19,23 ---- import javax.xml.parsers.ParserConfigurationException; import org.apache.log4j.Logger; + import org.jmonks.batchserver.io.flat.DelimitedFlatFileFileSpec; import org.w3c.dom.Document; import org.w3c.dom.Element; *************** *** 197,201 **** return FixedWidthFlatFileFileSpec.createFixedWidthFlatFileFileSpec(fileSpecElement); else if(fileType==FileType.DELIMITED_FLAT_FILE) ! return null; //DelimitedFlatFileFileSpec.createDelimitedFlatFileFileSpec(fileSpecPath,fileSpecElement); else if(fileType==FileType.XML_FILE) return XMLFileSpec.createXMLFileSpec(fileSpecElement); --- 194,198 ---- return FixedWidthFlatFileFileSpec.createFixedWidthFlatFileFileSpec(fileSpecElement); else if(fileType==FileType.DELIMITED_FLAT_FILE) ! return DelimitedFlatFileFileSpec.createDelimitedFlatFileFileSpec(fileSpecElement); else if(fileType==FileType.XML_FILE) return XMLFileSpec.createXMLFileSpec(fileSpecElement); Index: RecordType.java =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/RecordType.java,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** RecordType.java 8 Jun 2006 22:21:15 -0000 1.5 --- RecordType.java 10 Jun 2006 17:50:24 -0000 1.6 *************** *** 44,48 **** if(type!=null) { ! if("HEADER".equalsIgnoreCase(type) || "DETAIL".equalsIgnoreCase(type) || "TRAILOR".equalsIgnoreCase(type)) throw new IllegalArgumentException("Record type value " + type + " is in use."); else --- 44,49 ---- if(type!=null) { ! if("HEADER".equalsIgnoreCase(type) || "DETAIL".equalsIgnoreCase(type) || "TRAILOR".equalsIgnoreCase(type) ! || "BLOCK-START".equalsIgnoreCase(type) || "BLOCK-END".equalsIgnoreCase(type)) throw new IllegalArgumentException("Record type value " + type + " is in use."); else *************** *** 72,75 **** --- 73,80 ---- else if(RecordType.TRAILOR.equals(recordType)) return RecordType.TRAILOR; + else if(RecordType.BLOCK_START.equals(recordType)) + return RecordType.BLOCK_START; + else if(RecordType.BLOCK_END.equals(recordType)) + return RecordType.BLOCK_END; else return new RecordType(recordType); *************** *** 111,114 **** --- 116,124 ---- public static final RecordType HEADER = new RecordType("HEADER"); /** + * Represents the header record and the value to be used in the record + * spec is "TRAILOR". + */ + public static final RecordType TRAILOR = new RecordType("TRAILOR"); + /** * Represents the detailed record and the value to be used in the record * spec is "DETAIL". *************** *** 116,122 **** public static final RecordType DETAIL = new RecordType("DETAIL"); /** ! * Represents the header record and the value to be used in the record ! * spec is "TRAILOR". */ ! public static final RecordType TRAILOR = new RecordType("TRAILOR"); } --- 126,138 ---- public static final RecordType DETAIL = new RecordType("DETAIL"); /** ! * Represents the starting record of the block and the value to be used in the record ! * spec is "BLOCK-START". */ ! public static final RecordType BLOCK_START = new RecordType("BLOCK-START"); ! /** ! * Represents the ending record of the block and the value to be used in the record ! * spec is "BLOCK-END". ! */ ! public static final RecordType BLOCK_END = new RecordType("BLOCK-END"); ! } |
From: Suresh <sur...@us...> - 2006-06-09 21:36:44
|
Update of /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/flat In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv3769/flat Modified Files: FixedWidthFlatFileFileSpec.java FixedWidthFlatFileReader.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.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** FixedWidthFlatFileFileSpec.java 8 Jun 2006 22:21:15 -0000 1.3 --- FixedWidthFlatFileFileSpec.java 9 Jun 2006 21:36:34 -0000 1.4 *************** *** 40,51 **** /** ! * 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); } --- 40,48 ---- /** ! * Constructs the FixedWidthFlatFileFileSpec. */ ! protected FixedWidthFlatFileFileSpec() { ! super(FileType.FIXED_WIDTH_FLAT_FILE); } *************** *** 54,58 **** * DOM Element representing the file-spec element. * - * @param fileSpecPath Absolute path to the file spec. * @param fileSpecElement DOM Element representing the file-spec element. * --- 51,54 ---- *************** *** 62,69 **** * 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()); --- 58,65 ---- * has the same value for the starts-with and record-type attributes. */ ! public static FileSpec createFixedWidthFlatFileFileSpec(final Element fileSpecElement) { ! logger.trace("Entering createFixedWidthFlatFileFileSpec "); ! FixedWidthFlatFileFileSpec fileSpec=new FixedWidthFlatFileFileSpec(); NodeList recordSpecNodeList=fileSpecElement.getElementsByTagName(RecordSpec.RECORD_SPEC_TAG_NAME); logger.debug("Number of record specs found = " + recordSpecNodeList.getLength()); *************** *** 96,100 **** stringValue.append("[fileType = " + super.fileType.toString() + "]"); stringValue.append("[recordSpecList = "); ! for(Iterator iterator=recordSpecList.iterator();iterator.hasNext();) stringValue.append(((FixedWidthFlatFileRecordSpec)iterator.next()).toString()); stringValue.append("]}"); --- 92,96 ---- stringValue.append("[fileType = " + super.fileType.toString() + "]"); stringValue.append("[recordSpecList = "); ! for(Iterator iterator=recordSpecMap.values().iterator();iterator.hasNext();) stringValue.append(((FixedWidthFlatFileRecordSpec)iterator.next()).toString()); stringValue.append("]}"); Index: FixedWidthFlatFileWriter.java =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/flat/FixedWidthFlatFileWriter.java,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** FixedWidthFlatFileWriter.java 8 Jun 2006 22:21:15 -0000 1.4 --- FixedWidthFlatFileWriter.java 9 Jun 2006 21:36:34 -0000 1.5 *************** *** 13,16 **** --- 13,18 ---- import java.io.BufferedWriter; import java.io.IOException; + import java.io.OutputStream; + import java.io.OutputStreamWriter; import java.util.Arrays; import java.util.HashMap; *************** *** 41,48 **** { /** - * Path of the file to be written. - */ - protected String filePath=null; - /** * File spec to be used in writing the file. */ --- 43,46 ---- *************** *** 61,84 **** * 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) ! { ! 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()); ! } logger.trace("Exiting FixedWidthFlatFileWriter constructor"); } --- 59,71 ---- * Constructs and initializes the writer with the given values. * ! * @param outputStream Output stream to write the records. * @param fileSpec File spec to be used to generate the file. */ ! public FixedWidthFlatFileWriter(OutputStream outputStream,FileSpec fileSpec) { logger.trace("Entering FixedWidthFlatFileWriter constructor"); ! this.fileSpec=(FixedWidthFlatFileFileSpec)fileSpec; ! writer=new BufferedWriter(new OutputStreamWriter(outputStream)); ! logger.debug("Done creating and initializing the writer"); logger.trace("Exiting FixedWidthFlatFileWriter constructor"); } Index: FixedWidthFlatFileReader.java =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/flat/FixedWidthFlatFileReader.java,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** FixedWidthFlatFileReader.java 8 Jun 2006 22:21:15 -0000 1.4 --- FixedWidthFlatFileReader.java 9 Jun 2006 21:36:34 -0000 1.5 *************** *** 12,17 **** import java.io.BufferedReader; - import java.io.FileNotFoundException; import java.io.IOException; import java.util.HashMap; import java.util.Iterator; --- 12,18 ---- import java.io.BufferedReader; import java.io.IOException; + import java.io.InputStream; + import java.io.InputStreamReader; import java.util.HashMap; import java.util.Iterator; *************** *** 37,44 **** { /** - * Holds the absolute path to the file to read. - */ - protected String filePath=null; - /** * File spec to be used to read the file. */ --- 38,41 ---- *************** *** 52,56 **** * 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. * --- 49,53 ---- * Constructs and initializes the reader with the given file path and file spec. * ! * @param fileInputStream Input stream to the file. * @param fileSpec File spec to be used to read the file. * *************** *** 58,86 **** * 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) ! { ! reader=null; ! exception.printStackTrace(); ! logger.fatal("File could not be found to create the reader. Message = " + exception.getMessage(),exception); ! throw new FileParseException("File could not be found to create the reader. Message = " + exception.getMessage()); ! } ! catch(IOException exception) ! { ! reader=null; ! exception.printStackTrace(); ! logger.fatal("IO Exception while initializing the file reader. Message = " + exception.getMessage(),exception); ! throw new FileParseException("IO Exception while initializing the file reader. Message = " + exception.getMessage()); ! } ! logger.trace("Exiting FixedWidthFlatFileReader constructor"); } --- 55,64 ---- * initializing the reader. */ ! public FixedWidthFlatFileReader(InputStream fileInputStream,FileSpec fileSpec) { logger.trace("Entering FixedWidthFlatFileReader constructor"); this.fileSpec=(FixedWidthFlatFileFileSpec)fileSpec; ! reader=new BufferedReader(new InputStreamReader(fileInputStream)); ! logger.debug("Created the reader successfully"); } |
From: Suresh <sur...@us...> - 2006-06-09 21:36:44
|
Update of /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv3769 Modified Files: FileReader.java FileSpec.java FileWriter.java Log Message: no message Index: FileWriter.java =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/FileWriter.java,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** FileWriter.java 8 Jun 2006 22:21:15 -0000 1.4 --- FileWriter.java 9 Jun 2006 21:36:34 -0000 1.5 *************** *** 10,15 **** package org.jmonks.batchserver.io; ! ! import java.io.File; import org.apache.log4j.Logger; import org.jmonks.batchserver.io.flat.FixedWidthFlatFileWriter; --- 10,15 ---- package org.jmonks.batchserver.io; ! import java.io.InputStream; ! import java.io.OutputStream; import org.apache.log4j.Logger; import org.jmonks.batchserver.io.flat.FixedWidthFlatFileWriter; *************** *** 74,79 **** * </p> * ! * @param filePath Absolute path to create the data file. Path should not be directory and should not exist on the file system. ! * @param fileSpecPath Absolute path to the file contains the file spec. * * @return Returns the appropriate writer. --- 74,79 ---- * </p> * ! * @param outputStream Output stream to write the records. ! * @param fileSpecInputStream Input stream to read the file spec. * * @return Returns the appropriate writer. *************** *** 84,110 **** * 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()); --- 84,105 ---- * exist in FileType class. */ ! public static FileWriter getFileWriter(OutputStream outputStream,InputStream fileSpecInputStream) { ! logger.trace("Entering getFileReader"); ! if(outputStream==null) ! throw new IllegalArgumentException("Output stream to write the records cannot be null."); ! if(fileSpecInputStream==null) ! throw new IllegalArgumentException("Input stream to read the file spec cannot be null."); ! ! FileSpec fileSpec=FileSpec.createFileSpec(fileSpecInputStream); logger.debug("Given file spec = " + fileSpec.toString()); if(fileSpec.getFileType()==FileType.FIXED_WIDTH_FLAT_FILE) ! return new FixedWidthFlatFileWriter(outputStream,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(outputStream,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.9 retrieving revision 1.10 diff -C2 -d -r1.9 -r1.10 *** FileReader.java 8 Jun 2006 22:21:15 -0000 1.9 --- FileReader.java 9 Jun 2006 21:36:34 -0000 1.10 *************** *** 12,15 **** --- 12,16 ---- import java.io.File; + import java.io.InputStream; import org.apache.log4j.Logger; import org.jmonks.batchserver.io.xml.XMLFileReader; *************** *** 72,77 **** * </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. --- 73,78 ---- * </p> * ! * @param fileInputStream Input stream to read the data file. ! * @param fileSpecInputStream Input stream to read the file spec. * * @return Returns the appropriate reader. *************** *** 82,106 **** * 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()); --- 83,104 ---- * exist in FileType class. */ ! public static FileReader getFileReader(InputStream fileInputStream,InputStream fileSpecInputStream) { ! logger.trace("Entering getFileReader"); ! ! if(fileInputStream==null) ! throw new IllegalArgumentException("Input stream to read the data file cannot be null."); ! if(fileSpecInputStream==null) ! throw new IllegalArgumentException("Input stream to read the file spec cannot be null."); ! FileSpec fileSpec=FileSpec.createFileSpec(fileSpecInputStream); logger.debug("Given file spec = " + fileSpec.toString()); if(fileSpec.getFileType()==FileType.FIXED_WIDTH_FLAT_FILE) ! return new FixedWidthFlatFileReader(fileInputStream,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(fileInputStream,fileSpec); else throw new FileSpecException("Unsupported file type in the file spec = " + fileSpec.getFileType().toString()); Index: FileSpec.java =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/FileSpec.java,v retrieving revision 1.8 retrieving revision 1.9 diff -C2 -d -r1.8 -r1.9 *** FileSpec.java 8 Jun 2006 22:21:15 -0000 1.8 --- FileSpec.java 9 Jun 2006 21:36:34 -0000 1.9 *************** *** 61,68 **** { /** - * Holds the path of the file contains the spec of any file to read/write. - */ - protected String fileSpecPath=null; - /** * Holds the type of the file this spec is representing. */ --- 61,64 ---- *************** *** 90,96 **** * @param fileType Type of the file this spec is representing. */ ! protected FileSpec(String fileSpecPath,FileType fileType) { - this.fileSpecPath=fileSpecPath; this.fileType=fileType; recordSpecMap=new HashMap(); --- 86,91 ---- * @param fileType Type of the file this spec is representing. */ ! protected FileSpec(FileType fileType) { this.fileType=fileType; recordSpecMap=new HashMap(); *************** *** 174,178 **** * file spec object. * </p> ! * @param fileSpecPath Absolute path to the file contains spec. * * @return Returns the file spec object. --- 169,173 ---- * file spec object. * </p> ! * @param fileSpecInputStream Input stream represents the file spec. * * @return Returns the file spec object. *************** *** 182,194 **** * 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(); DocumentBuilder documentBuilder=documentBuilderFactory.newDocumentBuilder(); ! Document document=documentBuilder.parse(fileSpecStream); Element fileSpecElement=document.getDocumentElement(); --- 177,190 ---- * or given file cannot be found or cannot parse the file. */ ! public static FileSpec createFileSpec(InputStream fileSpecInputStream) { ! logger.trace("Entering createFileSpec"); try { ! if(fileSpecInputStream==null) ! throw new IllegalArgumentException("File spec input stream to read the file spec cannot be null."); DocumentBuilderFactory documentBuilderFactory=DocumentBuilderFactory.newInstance(); DocumentBuilder documentBuilder=documentBuilderFactory.newDocumentBuilder(); ! Document document=documentBuilder.parse(fileSpecInputStream); Element fileSpecElement=document.getDocumentElement(); *************** *** 199,218 **** FileType fileType=FileType.toFileType(configuredFileType); if(fileType==FileType.FIXED_WIDTH_FLAT_FILE) ! return FixedWidthFlatFileFileSpec.createFixedWidthFlatFileFileSpec(fileSpecPath,fileSpecElement); else if(fileType==FileType.DELIMITED_FLAT_FILE) return null; //DelimitedFlatFileFileSpec.createDelimitedFlatFileFileSpec(fileSpecPath,fileSpecElement); else if(fileType==FileType.XML_FILE) ! return XMLFileSpec.createXMLFileSpec(fileSpecPath,fileSpecElement); else ! throw new FileSpecException("Configured file type " + fileType + " in the file spec is not recognizable. File spec location = " + fileSpecPath); } else ! throw new FileSpecException("File spec root element doesnt match the expected " + FileSpec.FILE_SPEC_TAG_NAME + " value in the file spec located at = " + fileSpecPath); ! } ! catch(FileNotFoundException exception) ! { ! exception.printStackTrace(); ! logger.fatal("Cannot find the file in the given path = " + fileSpecPath,exception); ! throw new FileSpecException("Cannot find the file contains the spec in given location = ." + fileSpecPath + " Message = " + exception.getMessage()); } catch(ParserConfigurationException exception) --- 195,208 ---- FileType fileType=FileType.toFileType(configuredFileType); if(fileType==FileType.FIXED_WIDTH_FLAT_FILE) ! return FixedWidthFlatFileFileSpec.createFixedWidthFlatFileFileSpec(fileSpecElement); else if(fileType==FileType.DELIMITED_FLAT_FILE) return null; //DelimitedFlatFileFileSpec.createDelimitedFlatFileFileSpec(fileSpecPath,fileSpecElement); else if(fileType==FileType.XML_FILE) ! return XMLFileSpec.createXMLFileSpec(fileSpecElement); else ! throw new FileSpecException("Configured file type " + fileType + " in the file spec is not recognizable."); } else ! throw new FileSpecException("File spec root element doesnt match the expected " + FileSpec.FILE_SPEC_TAG_NAME + " value in the file spec obtained from the given stream."); } catch(ParserConfigurationException exception) *************** *** 225,236 **** { exception.printStackTrace(); ! logger.fatal("Parsing exception while parsing the file spec = " + fileSpecPath,exception); ! throw new FileSpecException("Parsing exception while parsing the file spec in given location = " + fileSpecPath + " Message = " + exception.getMessage()); } catch(IOException exception) { exception.printStackTrace(); ! logger.fatal("IO exception while parsing the file spec = " + fileSpecPath,exception); ! throw new FileSpecException("IO exception while parsing the file spec in given location=" + fileSpecPath + " Message = " + exception.getMessage()); } } --- 215,226 ---- { exception.printStackTrace(); ! logger.fatal("Parsing exception while parsing the given inputstream.",exception); ! throw new FileSpecException("Parsing exception while parsing the file spec in given input stream. Message = " + exception.getMessage()); } catch(IOException exception) { exception.printStackTrace(); ! logger.fatal("IO exception while parsing the given input stream to create the file spec.",exception); ! throw new FileSpecException("IO exception while parsing the given input stream to create the file spec. Message = " + exception.getMessage()); } } |
From: Suresh <sur...@us...> - 2006-06-09 21:36:42
|
Update of /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/xml In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv3769/xml Modified Files: XMLFileReader.java XMLFileSpec.java XMLFileWriter.java Log Message: no message Index: XMLFileReader.java =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/xml/XMLFileReader.java,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** XMLFileReader.java 8 Jun 2006 22:21:15 -0000 1.6 --- XMLFileReader.java 9 Jun 2006 21:36:34 -0000 1.7 *************** *** 12,15 **** --- 12,17 ---- import java.io.FileNotFoundException; + import java.io.InputStream; + import java.io.InputStreamReader; import java.util.ArrayList; import java.util.HashMap; *************** *** 49,56 **** { /** - * Path to the file to be read. - */ - protected String filePath=null; - /** * File spec to read the file. */ --- 51,54 ---- *************** *** 70,88 **** * Constructs and initializes the XML File reader. * ! * @param filePath Absolute path to the file to be read. ! * @param fileSpec File spec for the file to be read. * * @throws org.jmonks.batchserver.io.FileParseException If root element doesnt match * with the element specified in file spec and problems to initializes the reader. */ ! public XMLFileReader(String filePath,FileSpec fileSpec) { logger.trace("Entering XMLFileReader constructor"); - this.filePath=filePath; this.fileSpec=(XMLFileSpec)fileSpec; try { XMLInputFactory inputFactory=XMLInputFactory.newInstance(); ! reader=inputFactory.createXMLEventReader(new java.io.FileReader(this.filePath)); logger.debug("Created the XML Event reader"); if(this.validateRootElement()) --- 68,85 ---- * Constructs and initializes the XML File reader. * ! * @param fileInputStream Input stream to the file. ! * @param fileSpec File spec to be used to read the file. * * @throws org.jmonks.batchserver.io.FileParseException If root element doesnt match * with the element specified in file spec and problems to initializes the reader. */ ! public XMLFileReader(InputStream filInputStream,FileSpec fileSpec) { logger.trace("Entering XMLFileReader constructor"); this.fileSpec=(XMLFileSpec)fileSpec; try { XMLInputFactory inputFactory=XMLInputFactory.newInstance(); ! reader=inputFactory.createXMLEventReader(new InputStreamReader(filInputStream)); logger.debug("Created the XML Event reader"); if(this.validateRootElement()) *************** *** 95,105 **** logger.debug("Validate the root tag of the file"); } - catch(FileNotFoundException exception) - { - exception.printStackTrace(); - this.reader=null; - logger.fatal("File to create the reader cannot be found. Exception Message = " + exception.getMessage(),exception); - throw new FileParseException("File to create the reader cannot be found. Exception Message = " + exception.getMessage()); - } catch(XMLStreamException exception) { --- 92,95 ---- Index: XMLFileWriter.java =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/xml/XMLFileWriter.java,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** XMLFileWriter.java 8 Jun 2006 22:21:15 -0000 1.4 --- XMLFileWriter.java 9 Jun 2006 21:36:34 -0000 1.5 *************** *** 10,15 **** package org.jmonks.batchserver.io.xml; ! ! import java.io.IOException; import java.util.HashMap; import java.util.Iterator; --- 10,14 ---- package org.jmonks.batchserver.io.xml; ! import java.io.OutputStream; import java.util.HashMap; import java.util.Iterator; *************** *** 44,51 **** { /** - * Path to the file to be generated. - */ - protected String filePath=null; - /** * File spec to be adhered with. */ --- 43,46 ---- *************** *** 63,78 **** * 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 XMLFileWriter(String absoluteFilePath,FileSpec fileSpec) { logger.trace("Entering XMLFileWriter constructor"); try { - this.filePath=absoluteFilePath; this.fileSpec=(XMLFileSpec)fileSpec; XMLOutputFactory outputFactory=XMLOutputFactory.newInstance(); ! writer=outputFactory.createXMLStreamWriter(new java.io.FileWriter(this.filePath)); logger.debug("Writer has been created."); writer.writeStartDocument("ISO-8859-1", "1.0"); --- 58,72 ---- * Constructs and initializes the writer with the given values. * ! * @param outputStream Output stream to write the records. * @param fileSpec File spec to be used to generate the file. */ ! public XMLFileWriter(OutputStream outputStream,FileSpec fileSpec) { logger.trace("Entering XMLFileWriter constructor"); try { this.fileSpec=(XMLFileSpec)fileSpec; XMLOutputFactory outputFactory=XMLOutputFactory.newInstance(); ! writer=outputFactory.createXMLStreamWriter(outputStream); logger.debug("Writer has been created."); writer.writeStartDocument("ISO-8859-1", "1.0"); *************** *** 80,89 **** writer.writeStartElement(this.fileSpec.rootElement); } - catch(IOException exception) - { - exception.printStackTrace(); - logger.fatal("IOException while creating the writer. Message = " + exception.getMessage(), exception); - throw new FileParseException("IOException while creating the writer. Message = " + exception.getMessage()); - } catch(XMLStreamException exception) { --- 74,77 ---- Index: XMLFileSpec.java =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/xml/XMLFileSpec.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** XMLFileSpec.java 8 Jun 2006 22:21:15 -0000 1.2 --- XMLFileSpec.java 9 Jun 2006 21:36:34 -0000 1.3 *************** *** 50,59 **** /** * Constructs the XMLFileSpec instance. - * - * @fileSpecPath Absolute path to the file spec. */ ! protected XMLFileSpec(String fileSpecPath) { ! super(fileSpecPath,FileType.XML_FILE); } --- 50,57 ---- /** * Constructs the XMLFileSpec instance. */ ! protected XMLFileSpec() { ! super(FileType.XML_FILE); } *************** *** 67,74 **** /** ! * Factory method create the xml 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. * --- 65,71 ---- /** ! * Factory method to create the xml file spec object from the given * DOM Element representing the file-spec element. * * @param fileSpecElement DOM Element representing the file-spec element. * *************** *** 78,84 **** * has the same value for the record-xpath and record-type attributes. */ ! public static FileSpec createXMLFileSpec(final String fileSpecPath,final Element fileSpecElement) { ! XMLFileSpec fileSpec=new XMLFileSpec(fileSpecPath); String rootElement=fileSpecElement.getAttribute(XMLFileSpec.ROOT_ELEMENT_ATTRIB_NAME); --- 75,81 ---- * has the same value for the record-xpath and record-type attributes. */ ! public static FileSpec createXMLFileSpec(final Element fileSpecElement) { ! XMLFileSpec fileSpec=new XMLFileSpec(); String rootElement=fileSpecElement.getAttribute(XMLFileSpec.ROOT_ELEMENT_ATTRIB_NAME); *************** *** 116,120 **** stringValue.append("[rootElement = " + this.rootElement + "]"); stringValue.append("[recordSpecList = "); ! for(Iterator iterator=recordSpecList.iterator();iterator.hasNext();) stringValue.append(((XMLRecordSpec)iterator.next()).toString()); stringValue.append("]}"); --- 113,117 ---- stringValue.append("[rootElement = " + this.rootElement + "]"); stringValue.append("[recordSpecList = "); ! for(Iterator iterator=recordSpecMap.values().iterator();iterator.hasNext();) stringValue.append(((XMLRecordSpec)iterator.next()).toString()); stringValue.append("]}"); |
From: Suresh <sur...@us...> - 2006-06-09 03:57:53
|
Update of /cvsroot/batchserver/batchserver In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv29447 Added Files: build_io.xml Log Message: no message --- NEW FILE: build_io.xml --- <project name="io" default="dist" basedir="."> <!-- ===================== Property Definitions =========================== --> <property name="app.version" value="1.0"/> <property name="app.name" value="io"/> <!-- ==================== File and Directory Names ======================== --> <property name="build.home" value="${basedir}/build"/> <property name="dist.home" value="${basedir}/dist"/> <property name="docs.home" value="${basedir}/docs"/> <property name="src.home" value="${basedir}/src"/> <property name="lib.home" value="${basedir}/lib"/> <!-- ==================== Compilation Control Options ==================== --> <property name="compile.debug" value="true"/> <property name="compile.deprecation" value="false"/> <property name="compile.optimize" value="true"/> <!-- ======= Pattern Sets, File Sets, Application Classpaths ======= --> <patternset id="app.resources"> <include name="**/*.xml"/> <include name="**/*.dat"/> </patternset> <path id="compile.classpath"> <!-- Include all JAR files that will be required to compile application --> <pathelement location="${lib.home}/xml/xml_apis.jar"/> <pathelement location="${lib.home}/xml/apache/resolver_2.7.1.jar"/> <pathelement location="${lib.home}/xml/apache/xerces_impl_2.7.1.jar"/> <pathelement location="${lib.home}/stax/some.jar"/> <pathelement location="${lib.home}/stax/stax_api_1.0.zip"/> <pathelement location="${lib.home}/stax/ri/stax_impl_1.2.zip"/> <pathelement location="${lib.home}/log4j/log4j_1.2.13.jar"/> </path> <property name="compile.classpath" refid="compile.classpath"/> <!-- ==================== Source-Clean Target ==================================== --> <target name="source-clean" description="Delete old source build directories"> <delete dir="${build.home}"/> </target> <!-- ==================== Source-Compile Target ================================== --> <target name="source-compile" description="Compile Java sources"> <!-- Compile Java classes as necessary --> <mkdir dir="${build.home}"/> <echo message="Classpath for compilation : ${compile.classpath}"/> <javac srcdir="${src.home}/org/jmonks/batchserver/io" destdir="${build.home}" debug="${compile.debug}" deprecation="${compile.deprecation}" optimize="${compile.optimize}"> <classpath refid="compile.classpath"/> </javac> <!-- Copy application resources --> <copy todir="${build.home}"> <fileset dir="${src.home}/org/jmonks/batchserver/io"> <patternset refid="app.resources"/> </fileset> </copy> </target> <!-- ==================== Dist Target ===================================== --> <target name="dist" depends="source-clean,source-compile,javadoc" description="Create binary distribution"> <!-- Copy documentation subdirectories --> <delete dir="${dist.home}"/> <mkdir dir="${dist.home}"/> <mkdir dir="${dist.home}/docs"/> <copy todir="${dist.home}/docs"> <fileset dir="${docs.home}"/> </copy> <!-- Create application JAR file --> <jar jarfile="${dist.home}/${app.name}-${app.version}.jar" basedir="${build.home}"/> </target> <!-- ==================== Javadoc Target ================================== --> <target name="javadoc" description="Create Javadoc API documentation"> <delete dir="${docs.home}"/> <mkdir dir="${docs.home}"/> <javadoc sourcepath="${src.home}" destdir="${docs.home}" packagenames="org/jmonks/batchserver/io,org/jmonks/batchserver/io/flat,org/jmonks/batchserver/io/xml" author="true" version="true" use="true"> <classpath refid="compile.classpath"/> </javadoc> </target> </project> |
From: Suresh <sur...@us...> - 2006-06-09 03:57:26
|
Update of /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv29415 Added Files: package.html Log Message: no message --- NEW FILE: package.html --- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>IO API Overview</title> </head> <body> Here explain the usage of IO API. </body> </html> |
From: Suresh <sur...@us...> - 2006-06-08 22:21:24
|
Update of /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/xml In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv22592/xml Modified Files: XMLFileReader.java XMLFileSpec.java XMLFileWriter.java XMLRecordSpec.java Log Message: no message Index: XMLFileReader.java =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/xml/XMLFileReader.java,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** XMLFileReader.java 7 Jun 2006 03:12:08 -0000 1.5 --- XMLFileReader.java 8 Jun 2006 22:21:15 -0000 1.6 *************** *** 29,55 **** /** * ! * @author Suresh Pragada */ public class XMLFileReader extends FileReader { ! protected String absoluteFilePath=null; ! protected XMLFileSpec fileSpec=null; ! ! protected XMLEventReader reader=null; ! ! protected String xpath=""; private static Logger logger=Logger.getLogger(XMLFileReader.class); ! ! public XMLFileReader(String absoluteFilePath,FileSpec fileSpec) { ! this.absoluteFilePath=absoluteFilePath; this.fileSpec=(XMLFileSpec)fileSpec; try { XMLInputFactory inputFactory=XMLInputFactory.newInstance(); ! reader=inputFactory.createXMLEventReader(new java.io.FileReader(absoluteFilePath)); if(this.validateRootElement()) xpath="/"+this.fileSpec.getRootElement(); --- 29,89 ---- /** + * <p> + * XMLFileReader reads the specified xml file according to the given file spec + * and returns the recrods on the needed basis. It finds the record location + * using the xpath given in each xml record spec and loads all the child elements + * of that xpath as a fields. Field values should be accessed using the element names. + * If element has child elements, that returned value will be a reader record and + * it again follow the same convention. If element is repeated more than once, + * the value will be loaded as a list. ReaderRecord returned by this reader will + * have additional methods used to read the data accurately. 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 XMLFileReader extends FileReader { ! /** ! * Path to the file to be read. ! */ ! protected String filePath=null; ! /** ! * File spec to read the file. ! */ protected XMLFileSpec fileSpec=null; ! /** ! * XML Event reader reads and returns everything as events. ! */ ! private XMLEventReader reader=null; ! /** ! * Tracks where are we in xml file. ! */ ! private String xpath=""; private static Logger logger=Logger.getLogger(XMLFileReader.class); ! ! /** ! * Constructs and initializes the XML File reader. ! * ! * @param filePath Absolute path to the file to be read. ! * @param fileSpec File spec for the file to be read. ! * ! * @throws org.jmonks.batchserver.io.FileParseException If root element doesnt match ! * with the element specified in file spec and problems to initializes the reader. ! */ ! public XMLFileReader(String filePath,FileSpec fileSpec) { ! logger.trace("Entering XMLFileReader constructor"); ! this.filePath=filePath; this.fileSpec=(XMLFileSpec)fileSpec; try { XMLInputFactory inputFactory=XMLInputFactory.newInstance(); ! reader=inputFactory.createXMLEventReader(new java.io.FileReader(this.filePath)); ! logger.debug("Created the XML Event reader"); if(this.validateRootElement()) xpath="/"+this.fileSpec.getRootElement(); *************** *** 59,62 **** --- 93,97 ---- throw new FileParseException("Unexpected root element found. Expecting the root element " + this.fileSpec.getRootElement()); } + logger.debug("Validate the root tag of the file"); } catch(FileNotFoundException exception) *************** *** 74,81 **** --- 109,127 ---- throw new FileParseException("Exception while initializing the xml stream reader. Message = " + exception.getMessage()); } + logger.trace("Exiting XMLFileReader constructor"); } + /** + * Gets the next available record from the file. If file doesnt have any more + * records, it returns null. + * + * @return Returns the next available record. + * + * @throws org.jmonks.batchserver.io.FileParseException Problems getting the next record. + * + */ public ReaderRecord getNextRecord() { + logger.trace("Entering getNextRecord"); XMLReaderRecord readerRecord=null; if(this.reader==null) *************** *** 85,88 **** --- 131,137 ---- try { + /** + * Read events until the xpath matches with any of the record specs xpath. + */ while(reader.hasNext()) { *************** *** 96,100 **** if(recordSpec!=null) { ! //logger.debug("Found configured " + xpath); readerRecord=retrieveRecord(recordSpec,startElementName); int index=xpath.lastIndexOf("/"+startElementName); --- 145,149 ---- if(recordSpec!=null) { ! logger.trace("Found configured " + xpath); readerRecord=retrieveRecord(recordSpec,startElementName); int index=xpath.lastIndexOf("/"+startElementName); *************** *** 121,129 **** --- 170,183 ---- } } + logger.trace("Exiting getNextRecord"); return readerRecord; } + /** + * Closes the reader. + */ public void close() { + logger.trace("Entering close"); if(this.reader!=null) { *************** *** 131,134 **** --- 185,189 ---- { this.reader.close(); + logger.debug("Closed the reader"); } catch(XMLStreamException exception) *************** *** 143,146 **** --- 198,208 ---- } + /** + * Validates the root element. Read all the events until it finds first start + * element and validate its name against the configured element name. + * + * @return Returns true if the first start element name matches with configured element name, + * flase otherwise. + */ private boolean validateRootElement() { *************** *** 171,174 **** --- 233,241 ---- } + /** + * Looks for a record spec whose xpath matches with the current xpath. + * + * @return Returns the record spec matches the current xpath. + */ private XMLRecordSpec getRecordSpec() { *************** *** 183,186 **** --- 250,263 ---- } + /** + * <p> + * Loads all the elements available in the stream from now onwards as a fields + * into the record until we reach an end element matches the recordElement name. + * + * <i>TODO :: Eliminate Possible limitation :: Two elements with the same name cannot follow. </i> + * </p> + * + * @param recordSpec + */ private XMLReaderRecord retrieveRecord(XMLRecordSpec recordSpec,String recordElement) { *************** *** 265,273 **** return readerRecord; } ! public class XMLReaderRecord extends ReaderRecord { private Map fieldMap=null; private XMLReaderRecord(RecordType recordType) { --- 342,364 ---- return readerRecord; } ! ! /** ! * XMLReaderRecord 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. It provides ! * the additional method to conveniently read the data from the record. ! * ! * @author Suresh Pragada ! */ public class XMLReaderRecord extends ReaderRecord { + /** + * Map holds the field names and values. + */ private Map fieldMap=null; + /** + * Constructs XML Reader record. + */ private XMLReaderRecord(RecordType recordType) { *************** *** 275,279 **** fieldMap=new HashMap(); } ! public Object readField(String fieldName) { --- 366,382 ---- fieldMap=new HashMap(); } ! ! /** ! * Reads the values associated with the given field name and returns. ! * The return value will vary based on the element type in file. If it is ! * a simple element it would be string, if it is a nested element it would ! * reader record, it it is an element repeats multiple times it will be ! * list. Use the other methods available on XMLReaderRecord for ! * convenience. ! * ! * @param fieldName Name of the element. ! * ! * @return Returns the field value as object. ! */ public Object readField(String fieldName) { *************** *** 281,284 **** --- 384,394 ---- } + /** + * Expectes the element as a simple element and returns it as a string. + * + * @param elementName Name of the simple element. + * + * @return Returns the value associated with this element. + */ public String readSimpleElement(String elementName) { *************** *** 286,294 **** } public ReaderRecord readCompositeElement(String elementName) { return (ReaderRecord)fieldMap.get(elementName); } ! public List readMutlipleElements(String elementName) { --- 396,418 ---- } + /** + * Expectes the element as a nested/composite element and returns it as a reader record. + * + * @param elementName Name of the nested/composite element. + * + * @return Returns the value associated with this element. + */ public ReaderRecord readCompositeElement(String elementName) { return (ReaderRecord)fieldMap.get(elementName); } ! ! /** ! * Expectes the element as repeated element and returns it as a list. ! * ! * @param elementName Name of the repeated element. ! * ! * @return Returns the value associated with this element. ! */ public List readMutlipleElements(String elementName) { *************** *** 307,311 **** } ! protected void writeElement(String fieldName, Object fieldValue) { if(fieldMap.containsKey(fieldName)) --- 431,439 ---- } ! /** ! * Writes the field name and values to the map. It will take care ! * of converting the values into a list, if they are repeated. ! */ ! private void writeElement(String fieldName, Object fieldValue) { if(fieldMap.containsKey(fieldName)) *************** *** 330,333 **** --- 458,464 ---- } + /** + * @see java.lang.Object#toString() + */ public String toString() { Index: XMLFileWriter.java =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/xml/XMLFileWriter.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** XMLFileWriter.java 8 Jun 2006 03:39:13 -0000 1.3 --- XMLFileWriter.java 8 Jun 2006 22:21:15 -0000 1.4 *************** *** 27,53 **** /** * ! * @author Suresh Pragada */ public class XMLFileWriter extends FileWriter { ! protected String absoluteFilePath=null; ! protected XMLFileSpec fileSpec=null; ! protected XMLStreamWriter writer=null; ! private XMLIndentationEngine indentationEngine=new XMLIndentationEngine(); private static Logger logger=Logger.getLogger(XMLFileWriter.class); public XMLFileWriter(String absoluteFilePath,FileSpec fileSpec) { try { ! this.absoluteFilePath=absoluteFilePath; this.fileSpec=(XMLFileSpec)fileSpec; XMLOutputFactory outputFactory=XMLOutputFactory.newInstance(); ! writer=outputFactory.createXMLStreamWriter(new java.io.FileWriter(absoluteFilePath)); writer.writeStartDocument("ISO-8859-1", "1.0"); indentationEngine.startElement(); --- 27,79 ---- /** + * <p> + * XMLFileWriter writes/generates the specified xml file according to the given file spec + * with the the recrods submitted to write into the file. It generates the root tag + * from the attribute given in file-spec. It uses the last part in record xpath to + * write the record element. If nested element needs to be written, get another + * record from the writer and writes it to the main record and submit the main + * record to the writer. If repeated element needs to be written submit the values + * as a list. + * </p> * ! * @author Suresh Pragada ! * @version 1.0 ! * @since 1.0 */ public class XMLFileWriter extends FileWriter { ! /** ! * Path to the file to be generated. ! */ ! protected String filePath=null; ! /** ! * File spec to be adhered with. ! */ protected XMLFileSpec fileSpec=null; ! private XMLStreamWriter writer=null; ! /** ! * Egnine to be used for the indentation. ! */ private XMLIndentationEngine indentationEngine=new XMLIndentationEngine(); private static Logger logger=Logger.getLogger(XMLFileWriter.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 XMLFileWriter(String absoluteFilePath,FileSpec fileSpec) { + logger.trace("Entering XMLFileWriter constructor"); try { ! this.filePath=absoluteFilePath; this.fileSpec=(XMLFileSpec)fileSpec; XMLOutputFactory outputFactory=XMLOutputFactory.newInstance(); ! writer=outputFactory.createXMLStreamWriter(new java.io.FileWriter(this.filePath)); ! logger.debug("Writer has been created."); writer.writeStartDocument("ISO-8859-1", "1.0"); indentationEngine.startElement(); *************** *** 66,91 **** throw new FileParseException("XMLStream exception while creating the writer. Message = " + exception.getMessage()); } } public void writeRecord(WriterRecord writerRecord) { ! try ! { ! XMLWriterRecord record=(XMLWriterRecord)writerRecord; ! XMLRecordSpec recordSpec=getRecordSpec(record.getRecordType()); ! String recordXpath=recordSpec.getRecordXPath(); ! int index=recordXpath.lastIndexOf('/'); ! String recordElement=recordXpath.substring(((index!=-1)?(index+1):0)); ! writeCompositeElement(recordElement, record); ! writer.flush(); ! } ! catch(XMLStreamException exception) { ! exception.printStackTrace(); ! logger.fatal("XMLStream exception while writing the record into the writer. Message = " + exception.getMessage(), exception); ! throw new FileParseException("XMLStream exception while writing the record into the writer. Message = " + exception.getMessage()); } } protected void writeSimpleElement(String fieldName,Object fieldValue) throws XMLStreamException { --- 92,141 ---- throw new FileParseException("XMLStream exception while creating the writer. Message = " + exception.getMessage()); } + logger.trace("Exiting XMLFileWriter constructor"); } + /** + * Writes the record into the file. This record should be obtained from + * this file writer only by using the createWriterRecord method. + * + * @param writerRecord Record to be written into the file. + * + * @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) { ! try ! { ! XMLWriterRecord record=(XMLWriterRecord)writerRecord; ! XMLRecordSpec recordSpec=(XMLRecordSpec)this.fileSpec.getRecordSpec(record.getRecordType()); ! String recordXpath=recordSpec.getRecordXPath(); ! int index=recordXpath.lastIndexOf('/'); ! String recordElement=recordXpath.substring(((index!=-1)?(index+1):0)); ! writeCompositeElement(recordElement, record); ! writer.flush(); ! } ! catch(XMLStreamException exception) ! { ! exception.printStackTrace(); ! logger.fatal("XMLStream exception while writing the record into the writer. Message = " + exception.getMessage(), exception); ! throw new FileParseException("XMLStream exception while writing the record into the writer. Message = " + exception.getMessage()); ! } } + else + throw new IllegalArgumentException("Writer record cannot be null to write record into the file."); + logger.trace("Exiting writeRecord"); } + /** + * Writes the simple element into the file. + * + * @param fieldName Name of the element. + * @param fieldValue Value of the element. + */ protected void writeSimpleElement(String fieldName,Object fieldValue) throws XMLStreamException { *************** *** 96,100 **** writer.writeEndElement(); } ! protected void writeCompositeElement(String recordElementName,XMLWriterRecord fieldValueAsRecord) throws XMLStreamException { --- 146,156 ---- writer.writeEndElement(); } ! ! /** ! * Writes the nested/composite element into the file. ! * ! * @param recordElementName Name of the nested element. ! * @param fieldValueAsRecord Value of the nested element. ! */ protected void writeCompositeElement(String recordElementName,XMLWriterRecord fieldValueAsRecord) throws XMLStreamException { *************** *** 121,125 **** writer.writeEndElement(); } ! protected void writeMlutipleElements(String multiElementName,List fieldValueAsList) throws XMLStreamException { --- 177,187 ---- writer.writeEndElement(); } ! ! /** ! * Writes the repeated element into the file. ! * ! * @param multiElementName Name of the repeated element. ! * @param fieldValueAsList Value of the repeated element. ! */ protected void writeMlutipleElements(String multiElementName,List fieldValueAsList) throws XMLStreamException { *************** *** 142,168 **** } } ! ! private XMLRecordSpec getRecordSpec(RecordType recordType) ! { ! List recordSpecList=this.fileSpec.getRecordSpecs(); ! for(Iterator iterator=recordSpecList.iterator();iterator.hasNext();) ! { ! XMLRecordSpec recordSpec=(XMLRecordSpec)iterator.next(); ! if(recordSpec.getRecordType()==recordType) ! return recordSpec; ! } ! return null; ! } ! /** ! * TODO ::: Check whether this record type associates to any of the record specs. */ public WriterRecord createWriterRecord(RecordType recordType) { ! return new XMLWriterRecord(recordType); } public void close() { if(this.writer!=null) { --- 204,233 ---- } } ! /** ! * 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 XMLWriterRecord(recordType); ! else ! throw new IllegalArgumentException("Record type " + recordType + " doesnt match with any record specs."); } + /** + * Closes the writer. + */ public void close() { + logger.trace("Entering close"); if(this.writer!=null) { *************** *** 174,177 **** --- 239,243 ---- this.writer.flush(); this.writer.close(); + logger.debug("Writer has been closed"); } catch(XMLStreamException exception) *************** *** 184,194 **** --- 250,275 ---- } } + logger.trace("Exiting close"); } + /** + * XMLWriterRecord 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. It provides + * the additional method to conveniently write the data into the record. + * + * @author Suresh Pragada + */ public class XMLWriterRecord extends WriterRecord { + /** + * Map holds the field names and values. + */ private Map fieldMap=null; + /** + * Constructs the XML writer record. + */ private XMLWriterRecord(RecordType recordType) { *************** *** 196,200 **** fieldMap=new HashMap(); } ! public void writeField(String fieldName, Object fieldValue) { --- 277,287 ---- fieldMap=new HashMap(); } ! ! /** ! * Writes the given field name and value into the record. ! * ! * @param fieldName Name of the field. ! * @param fieldValue Value of the field. ! */ public void writeField(String fieldName, Object fieldValue) { *************** *** 202,210 **** } public void writeSimpleElement(String fieldName, String fieldValue) { this.fieldMap.put(fieldName, fieldValue); } ! public void writeCompositeElement(String fieldName, WriterRecord fieldValue) { --- 289,303 ---- } + /** + * Write simple element into the record. + */ public void writeSimpleElement(String fieldName, String fieldValue) { this.fieldMap.put(fieldName, fieldValue); } ! ! /** ! * Write nested/composite element into the record. ! */ public void writeCompositeElement(String fieldName, WriterRecord fieldValue) { *************** *** 212,215 **** --- 305,314 ---- } + /** + * Writes the repeated element name and value into the record. + * + * @param fieldName Name of the field repated more than once. + * @param fieldValue Value of the field repated more than once. + */ public void writeMultiElement(String fieldName, List fieldValue) { *************** *** 217,226 **** } ! protected Object readField(String fieldName) { return this.fieldMap.get(fieldName); } ! protected Iterator getFieldNameIterator() { return this.fieldMap.keySet().iterator(); --- 316,331 ---- } ! /** ! * Reads the field from the record. ! */ ! private Object readField(String fieldName) { return this.fieldMap.get(fieldName); } ! /** ! * Gets the iterator for the field names key set. ! */ ! private Iterator getFieldNameIterator() { return this.fieldMap.keySet().iterator(); *************** *** 228,231 **** --- 333,345 ---- } + /** + * <p> + * XMLIndentationEngine writes the indentation information to the streams + * based on events received. It receives the events before tags being written + * to the stream. + * <br> + * TODO ::: Take this class outside and allow others to write their indentation engines. + * </p> + */ private class XMLIndentationEngine { *************** *** 239,242 **** --- 353,359 ---- } + /** + * Receives before element is being writting to the stream. + */ private void startElement() throws XMLStreamException { *************** *** 253,257 **** } } ! private void endElement() throws XMLStreamException { --- 370,376 ---- } } ! /** ! * Receives before end element is being writting to the stream. ! */ private void endElement() throws XMLStreamException { Index: XMLFileSpec.java =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/xml/XMLFileSpec.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** XMLFileSpec.java 3 Jun 2006 13:14:42 -0000 1.1 --- XMLFileSpec.java 8 Jun 2006 22:21:15 -0000 1.2 *************** *** 18,37 **** /** * ! * @author Suresh Pragada */ public class XMLFileSpec extends FileSpec { protected String rootElement=null; ! public static final String ROOT_ELEMENT_ATTRIB_NAME = "root-element"; private static Logger logger=Logger.getLogger(XMLFileSpec.class); ! public XMLFileSpec(String fileSpecPath,FileType fileType) { ! super(fileSpecPath,fileType); } public String getRootElement() { --- 18,64 ---- /** + * <p> + * XMLFileSpec represents the file spec defines the xml file + * where each different kind of record identified by the xpath. + * This file-spec requires additional attribute root-element which specified + * root element of the xml file along with the file-type attribute. + * Here is a sample file spec... + * </p> + * <p> + * <file-spec file-type="xml" root-element="consumer-request"> + * <!-- record specs will follow here --> + * </file-spec> + * </p> * ! * @author Suresh Pragada ! * @version 1.0 ! * @since 1.0 */ public class XMLFileSpec extends FileSpec { + /** + * Holds the root element. + */ protected String rootElement=null; ! /** ! * Constant defines the root element attribute name. ! */ public static final String ROOT_ELEMENT_ATTRIB_NAME = "root-element"; private static Logger logger=Logger.getLogger(XMLFileSpec.class); ! /** ! * Constructs the XMLFileSpec instance. ! * ! * @fileSpecPath Absolute path to the file spec. ! */ ! protected XMLFileSpec(String fileSpecPath) { ! super(fileSpecPath,FileType.XML_FILE); } + /** + * Gets the root element. + */ public String getRootElement() { *************** *** 39,45 **** } public static FileSpec createXMLFileSpec(final String fileSpecPath,final Element fileSpecElement) { ! XMLFileSpec fileSpec=new XMLFileSpec(fileSpecPath,FileType.XML_FILE); String rootElement=fileSpecElement.getAttribute(XMLFileSpec.ROOT_ELEMENT_ATTRIB_NAME); --- 66,84 ---- } + /** + * Factory method create the xml 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 XMLFileSpec. + * + * @throws org.jmonks.batchserver.io.FileSpecException If two record specs + * has the same value for the record-xpath and record-type attributes. + */ public static FileSpec createXMLFileSpec(final String fileSpecPath,final Element fileSpecElement) { ! XMLFileSpec fileSpec=new XMLFileSpec(fileSpecPath); String rootElement=fileSpecElement.getAttribute(XMLFileSpec.ROOT_ELEMENT_ATTRIB_NAME); *************** *** 52,56 **** for(int i=0;i<recordSpecNodeList.getLength();i++) { ! RecordSpec recordSpec=XMLRecordSpec.createXMLRecordSpec((Element)recordSpecNodeList.item(i)); fileSpec.addRecordSpec(recordSpec); } --- 91,107 ---- for(int i=0;i<recordSpecNodeList.getLength();i++) { ! XMLRecordSpec recordSpec=(XMLRecordSpec)XMLRecordSpec.createXMLRecordSpec((Element)recordSpecNodeList.item(i)); ! /** ! * Check for the duplicate record-xpath value on the record specs. ! */ ! for(Iterator iterator=fileSpec.getRecordSpecs().iterator();iterator.hasNext();) ! { ! XMLRecordSpec existingRecordSpec=(XMLRecordSpec)iterator.next(); ! if(existingRecordSpec.getRecordXPath().equalsIgnoreCase(recordSpec.getRecordXPath())) ! { ! throw new FileSpecException("Two record specs in the same file spec cannot have same values for record-xpath attribute."); ! } ! } ! logger.debug("Adding the record spec = " + recordSpec.toString()); fileSpec.addRecordSpec(recordSpec); } Index: XMLRecordSpec.java =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/xml/XMLRecordSpec.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** XMLRecordSpec.java 5 Jun 2006 22:40:34 -0000 1.2 --- XMLRecordSpec.java 8 Jun 2006 22:21:15 -0000 1.3 *************** *** 17,31 **** /** * ! * @author Suresh Pragada */ public class XMLRecordSpec extends RecordSpec { protected String recordXPath=null; ! public static final String RECORD_XPATH_ATTRIB_NAME = "record-xpath"; private static Logger logger=Logger.getLogger(XMLRecordSpec.class); public XMLRecordSpec(RecordType recordType) { --- 17,56 ---- /** + * <p> + * XMLRecordSpec represents record-spec element in the file spec + * belongs to the xml file type. Along with the record-type attribute + * it looks for the record-xpath attribute in the record-spec element to identify + * the record. Record type and record-xpath attribute values should be unique across + * the file-spec. XML Record spec doesnt require any field specs to be defined. treats + * all the elements underneath the given xpath as a fields. Here is a sample spec snippet... + * </p> + * <p> + * <file-spec file-type="xml" root-element="consumer-request"> + * <record-spec record-type="detail" record-xpath="/consumer-request/consumer-add"/> + * </file-spec> + * </p> * ! * @author Suresh Pragada ! * @version 1.0 ! * @since 1.0 */ public class XMLRecordSpec extends RecordSpec { + /** + * Holds the record spec xpath. + */ protected String recordXPath=null; ! /** ! * Constant defines the record xpath attribute name. ! */ public static final String RECORD_XPATH_ATTRIB_NAME = "record-xpath"; private static Logger logger=Logger.getLogger(XMLRecordSpec.class); + /** + * Constructs the XMLRecordSpec by using record type. + * + * @param recordType Type of the record. + */ public XMLRecordSpec(RecordType recordType) { *************** *** 33,36 **** --- 58,64 ---- } + /** + * Gets the xpath to identify the record. + */ public String getRecordXPath() { *************** *** 38,41 **** --- 66,78 ---- } + /** + * Tells whether given xpath matches the xpath of this record spec. + * + * @param xpath Value to be compared with the record specs. + * + * @return Returns true if this record spec matches give xpath, false otherwise. + * + * @throws IllegalArgumentException If xpath is null. + */ public boolean isMatch(String xpath) { *************** *** 45,52 **** } public static RecordSpec createXMLRecordSpec(final Element recordSpecElement) { ! RecordType recordType=RecordType.toRecordType(recordSpecElement.getAttribute(RecordSpec.RECORD_TYPE_ATTRIB_NAME)); ! XMLRecordSpec recordSpec=new XMLRecordSpec(recordType); String recordXPath=recordSpecElement.getAttribute(XMLRecordSpec.RECORD_XPATH_ATTRIB_NAME); --- 82,109 ---- } + /** + * Factory method to create the xml 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 xml file record spec. + * + * @throws org.jmonks.batchserver.io.FileSpecException If record-spec + * doesnt have the record-xpath or record-type attribute. + */ public static RecordSpec createXMLRecordSpec(final Element recordSpecElement) { ! XMLRecordSpec 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 XMLRecordSpec(recordType); ! } ! else ! throw new FileSpecException("Record Spec in Fixed Width File Spec should have record-type attribute."); String recordXPath=recordSpecElement.getAttribute(XMLRecordSpec.RECORD_XPATH_ATTRIB_NAME); |
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() { |
From: Suresh <sur...@us...> - 2006-06-08 22:21:22
|
Update of /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv22592 Modified Files: FileReader.java FileSpec.java FileWriter.java RecordType.java Log Message: no message Index: FileWriter.java =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/FileWriter.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** FileWriter.java 8 Jun 2006 03:39:13 -0000 1.3 --- FileWriter.java 8 Jun 2006 22:21:15 -0000 1.4 *************** *** 26,30 **** * </p> * <p> ! * <pre * FileWriter fileWriter=FileWriter.getFileWriter("/data/consumer.dat","/config/consumer-file-spec.xml"); * WriterRecord record=fileWriter.createWriterRecord(RecordType.DETAIL); --- 26,30 ---- * </p> * <p> ! * <pre> * FileWriter fileWriter=FileWriter.getFileWriter("/data/consumer.dat","/config/consumer-file-spec.xml"); * WriterRecord record=fileWriter.createWriterRecord(RecordType.DETAIL); *************** *** 74,78 **** * </p> * ! * @param filePath Absolute path to create the data file. * @param fileSpecPath Absolute path to the file contains the file spec. * --- 74,78 ---- * </p> * ! * @param filePath Absolute path to create the data file. Path should not be directory and should not exist on the file system. * @param fileSpecPath Absolute path to the file contains the file spec. * Index: FileReader.java =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/FileReader.java,v retrieving revision 1.8 retrieving revision 1.9 diff -C2 -d -r1.8 -r1.9 *** FileReader.java 8 Jun 2006 03:39:13 -0000 1.8 --- FileReader.java 8 Jun 2006 22:21:15 -0000 1.9 *************** *** 21,30 **** * 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; --- 21,30 ---- * 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 the 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; Index: FileSpec.java =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/FileSpec.java,v retrieving revision 1.7 retrieving revision 1.8 diff -C2 -d -r1.7 -r1.8 *** FileSpec.java 8 Jun 2006 03:39:13 -0000 1.7 --- FileSpec.java 8 Jun 2006 22:21:15 -0000 1.8 *************** *** 16,22 **** import java.io.IOException; import java.io.InputStream; ! import java.util.ArrayList; ! import java.util.Iterator; import java.util.List; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; --- 16,22 ---- import java.io.IOException; import java.io.InputStream; ! import java.util.HashMap; import java.util.List; + import java.util.Map; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; *************** *** 69,75 **** protected FileType fileType=null; /** ! * Holds the list of record specs. */ ! protected List recordSpecList=null; /** * Constant defines the file spec tage name. --- 69,75 ---- protected FileType fileType=null; /** ! * Map holds the record specs with their record types as keys. */ ! protected Map recordSpecMap=null; /** * Constant defines the file spec tage name. *************** *** 94,98 **** this.fileSpecPath=fileSpecPath; this.fileType=fileType; ! recordSpecList=new ArrayList(); } --- 94,98 ---- this.fileSpecPath=fileSpecPath; this.fileType=fileType; ! recordSpecMap=new HashMap(); } *************** *** 113,123 **** 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); } } --- 113,125 ---- else { ! if(this.recordSpecMap.containsKey(recordSpec.getRecordType())) { ! throw new FileSpecException("Duplicate record spec with the same record type " + recordSpec.getRecordType().toString()); ! } ! else ! { ! this.recordSpecMap.put(recordSpec.getRecordType(),recordSpec); ! return true; } } } *************** *** 130,134 **** public List getRecordSpecs() { ! return this.recordSpecList; } --- 132,136 ---- public List getRecordSpecs() { ! return (List)this.recordSpecMap.keySet(); } *************** *** 148,164 **** throw new IllegalArgumentException("RecordType cannot be null to get the record spec from file spec."); else ! { ! for(Iterator iterator=this.recordSpecList.iterator();iterator.hasNext();) ! { ! RecordSpec recordSpec=(RecordSpec)iterator.next(); ! if(recordType.toString().equalsIgnoreCase(recordSpec.getRecordType().toString())) ! { ! return recordSpec; ! } ! } ! return null; ! } } /** * Gets the file type. --- 150,161 ---- throw new IllegalArgumentException("RecordType cannot be null to get the record spec from file spec."); else ! return (RecordSpec)this.recordSpecMap.get(recordType); } + public boolean isValidRecordType(RecordType recordType) + { + return this.recordSpecMap.containsKey(recordType); + } + /** * Gets the file type. Index: RecordType.java =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/RecordType.java,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** RecordType.java 8 Jun 2006 03:39:13 -0000 1.4 --- RecordType.java 8 Jun 2006 22:21:15 -0000 1.5 *************** *** 16,19 **** --- 16,22 ---- * This defines the constants to identify all the records as enums. This record * type information should be provided when defining the record spec in file spec. + * <br> + * TODO ::: Override the java.lang.Object#equals() and java.lang.Object#hashCode() + * to use this record type in maps. * </p> * *************** *** 22,26 **** * @since 1.0 */ ! public class RecordType { /** --- 25,29 ---- * @since 1.0 */ ! public final class RecordType { /** *************** *** 37,41 **** * @throws IllegalArgumentException Throws if the type is null or the value is in use. */ ! protected RecordType(String type) { if(type!=null) --- 40,44 ---- * @throws IllegalArgumentException Throws if the type is null or the value is in use. */ ! private RecordType(String type) { if(type!=null) *************** *** 51,71 **** /** ! * Utility method returns the RecordType object matches to the given value. * * @param type String representing the type of record. * ! * @return Returns the RecordType references matches to the given value, null ! * if it cannot match the given value to any of the available record types. */ public static RecordType toRecordType(String recordType) { ! if(RecordType.HEADER.toString().equalsIgnoreCase(recordType)) return RecordType.HEADER; ! else if(RecordType.DETAIL.toString().equalsIgnoreCase(recordType)) return RecordType.DETAIL; ! else if(RecordType.TRAILOR.toString().equalsIgnoreCase(recordType)) return RecordType.TRAILOR; else ! return null; } --- 54,97 ---- /** ! * Utility method returns the RecordType object matches to the given value. If ! * it doesnt match with the any pre defined record types, it creates and returns ! * a new record type. * * @param type String representing the type of record. * ! * @return Returns the RecordType. ! * ! * @throws IllegalArgumentException Throws if the recordType is null or the value is in use. */ public static RecordType toRecordType(String recordType) { ! if(RecordType.HEADER.equals(recordType)) return RecordType.HEADER; ! else if(RecordType.DETAIL.equals(recordType)) return RecordType.DETAIL; ! else if(RecordType.TRAILOR.equals(recordType)) return RecordType.TRAILOR; else ! return new RecordType(recordType); ! } ! /** ! * Equality will be based on the type. ! * ! * @see java.lang.Object#equals(java.lang.Object) ! */ ! public boolean equals(Object recordType) ! { ! return (recordType!=null) && (this.getClass()==recordType.getClass()) && ! (this.type.equalsIgnoreCase(recordType.toString())); ! } ! ! /** ! * Hashcode of the type will be returned as a hash code. ! * ! * @see java.lang.Object#hashCode() ! */ ! public int hashCode() ! { ! return this.type.hashCode(); } |
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()); } } |
From: Suresh <sur...@us...> - 2006-06-08 03:39:16
|
Update of /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/flat In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv17834/flat Modified Files: FixedWidthFlatFileWriter.java Log Message: no message Index: FixedWidthFlatFileWriter.java =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/flat/FixedWidthFlatFileWriter.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** FixedWidthFlatFileWriter.java 7 Jun 2006 03:11:35 -0000 1.2 --- FixedWidthFlatFileWriter.java 8 Jun 2006 03:39:13 -0000 1.3 *************** *** 77,82 **** --- 77,86 ---- } + /** + * TODO ::: Check whether this record type associates to any of the record specs. + */ public WriterRecord createWriterRecord(RecordType recordType) { + return new FixedWidthFlatFileWriterRecord(recordType); } |
From: Suresh <sur...@us...> - 2006-06-08 03:39:16
|
Update of /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/xml In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv17834/xml Modified Files: XMLFileWriter.java Log Message: no message Index: XMLFileWriter.java =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/xml/XMLFileWriter.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** XMLFileWriter.java 7 Jun 2006 22:10:42 -0000 1.2 --- XMLFileWriter.java 8 Jun 2006 03:39:13 -0000 1.3 *************** *** 155,158 **** --- 155,161 ---- } + /** + * TODO ::: Check whether this record type associates to any of the record specs. + */ public WriterRecord createWriterRecord(RecordType recordType) { |
From: Suresh <sur...@us...> - 2006-06-08 02:53:27
|
Update of /cvsroot/batchserver/batchserver/test/org/jmonks/batchserver/io/xml In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv2596/org/jmonks/batchserver/io/xml Modified Files: XMLFileWriterTest.java Log Message: no message Index: XMLFileWriterTest.java =================================================================== RCS file: /cvsroot/batchserver/batchserver/test/org/jmonks/batchserver/io/xml/XMLFileWriterTest.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** XMLFileWriterTest.java 7 Jun 2006 03:12:35 -0000 1.1 --- XMLFileWriterTest.java 7 Jun 2006 22:11:13 -0000 1.2 *************** *** 8,21 **** package org.jmonks.batchserver.io.xml; import junit.framework.*; - import java.io.IOException; - import java.util.HashMap; - import java.util.Map; - import javax.xml.stream.XMLOutputFactory; - import javax.xml.stream.XMLStreamException; - import javax.xml.stream.XMLStreamWriter; - 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; --- 8,14 ---- package org.jmonks.batchserver.io.xml; + import java.util.ArrayList; + import java.util.List; import junit.framework.*; import org.jmonks.batchserver.io.FileWriter; import org.jmonks.batchserver.io.RecordType; *************** *** 55,59 **** { System.out.println("testWriterRecord"); ! FileWriter fileWriter=FileWriter.getFileWriter("C:\\sample-xml-file_2.xml", "C:\\workspace\\workarea\\openapp\\batchserver\\src\\org\\jmonks\\batchserver\\io\\xml\\sample-xml-file-spec.xml"); fileWriter.close(); } --- 48,74 ---- { System.out.println("testWriterRecord"); ! FileWriter fileWriter=FileWriter.getFileWriter("D:\\sample-xml-file_2.xml", "D:\\workarea\\personal\\latestcvs\\batchserver\\src\\org\\jmonks\\batchserver\\io\\xml\\sample-xml-file-spec.xml"); ! WriterRecord rec1=fileWriter.createWriterRecord(RecordType.DETAIL); ! rec1.writeField("sample-element1", "sample-value1"); ! rec1.writeField("sample-element2", "sample-value2"); ! rec1.writeField("sample-element3", "sample-value3"); ! ! ! List element4List=new ArrayList(); ! element4List.add("element-value4-value1"); ! element4List.add("element-value4-value2"); ! element4List.add("element-value4-value3"); ! rec1.writeField("sample-element4",element4List); ! ! WriterRecord nestedRec1=fileWriter.createWriterRecord(RecordType.DETAIL); ! nestedRec1.writeField("sample-element1", "sample-value1"); ! nestedRec1.writeField("sample-element2", "sample-value2"); ! nestedRec1.writeField("sample-element3", "sample-value3"); ! ! rec1.writeField("sample-element5", nestedRec1); ! ! rec1.writeField("sample-element6", ""); ! ! fileWriter.writeRecord(rec1); fileWriter.close(); } |
From: Suresh <sur...@us...> - 2006-06-08 02:51:16
|
Update of /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv2188/org/jmonks/batchserver/io Modified Files: FileParseException.java FileSpec.java FileSpecException.java FileType.java Record.java RecordType.java WriterRecord.java Log Message: no message Index: FileSpecException.java =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/FileSpecException.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** FileSpecException.java 1 Jun 2006 22:11:41 -0000 1.2 --- FileSpecException.java 7 Jun 2006 22:10:41 -0000 1.3 *************** *** 12,15 **** --- 12,19 ---- /** + * <p> + * FileSpecException is a runtime exception thrown when there is a problem creating + * the FileSpec object from the given file contains the file specification. + * </p> * * @author Suresh Pragada *************** *** 19,24 **** public class FileSpecException extends RuntimeException { ! ! /** Creates a new instance of FileSpecException */ public FileSpecException(String message) { --- 23,32 ---- public class FileSpecException extends RuntimeException { ! /** ! * Constructs the FileSpecException by accepting the message describes the ! * problem. ! * ! * @param message Message identifies the problem. ! */ public FileSpecException(String message) { Index: RecordType.java =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/RecordType.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** RecordType.java 1 Jun 2006 22:11:41 -0000 1.2 --- RecordType.java 7 Jun 2006 22:10:41 -0000 1.3 *************** *** 12,28 **** /** ! * ! * @author Suresh Pragada */ ! public final class RecordType { private String type=null; ! /** Creates a new instance of RecordType */ ! private RecordType(String type) { ! this.type=type; } ! public static RecordType toRecordType(String recordType) { --- 12,60 ---- /** ! * <p> ! * RecordType defines the different kind of records can be available in a file. ! * This defines the constants to identify all the records as enums. This record ! * type information should be provided when defining the record spec in file spec. ! * </p> ! * @author Suresh Pragada ! * @version 1.0 ! * @since 1.0 */ ! public class RecordType { + /** + * Holds the string representing the record type. + */ private String type=null; ! /** ! * Private constructor to make sure that RecordType cannot be instantiated from ! * outside of this class. ! * ! * @param type String representing the type of record. ! * ! * @throws IllegalArgumentException Throws if the type is null or the value is in use. ! */ ! protected RecordType(String type) { ! if(type!=null) ! { ! if("HEADER".equalsIgnoreCase(type) || "DETAIL".equalsIgnoreCase(type) || "TRAILOR".equalsIgnoreCase(type)) ! throw new IllegalArgumentException("Record type value " + type + " is in use."); ! else ! this.type=type; ! } ! else ! throw new IllegalArgumentException("Record type cannot be null."); } ! ! /** ! * Utility method returns the RecordType object matches to the given value. ! * ! * @param type String representing the type of record. ! * ! * @return Returns the RecordType references matches to the given value, null ! * if it cannot match the given value to any of the available record types. ! */ public static RecordType toRecordType(String recordType) { *************** *** 37,47 **** } ! public String toString() { return type; } ! public static final RecordType HEADER = new RecordType("HEADER"); public static final RecordType DETAIL = new RecordType("DETAIL"); public static final RecordType TRAILOR = new RecordType("TRAILOR"); } --- 69,95 ---- } ! /** ! * Returns the string representation of the RecordType which can be used to ! * define the record spec in file spec. ! */ ! public final String toString() { return type; } ! ! /** ! * Represents the header record and the value to be used in the record ! * spec is "HEADER". ! */ public static final RecordType HEADER = new RecordType("HEADER"); + /** + * Represents the detailed record and the value to be used in the record + * spec is "DETAIL". + */ public static final RecordType DETAIL = new RecordType("DETAIL"); + /** + * Represents the header record and the value to be used in the record + * spec is "TRAILOR". + */ public static final RecordType TRAILOR = new RecordType("TRAILOR"); } Index: Record.java =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/Record.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** Record.java 2 Jun 2006 21:02:06 -0000 1.2 --- Record.java 7 Jun 2006 22:10:41 -0000 1.3 *************** *** 11,16 **** package org.jmonks.batchserver.io; ! import java.util.HashMap; ! import java.util.Map; --- 11,15 ---- package org.jmonks.batchserver.io; ! Index: FileParseException.java =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/FileParseException.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** FileParseException.java 2 Jun 2006 21:02:06 -0000 1.2 --- FileParseException.java 7 Jun 2006 22:10:41 -0000 1.3 *************** *** 12,26 **** /** * ! * @author Suresh Pragada */ public class FileParseException extends RuntimeException { ! ! /** Creates a new instance of FileParseException */ public FileParseException(String message) { super(message); ! } ! } --- 12,35 ---- /** + * <p> + * FileParseException is a runtime exception thrown when there is a problem reading + * the records from the file or writing the records into the file. + * </p> * ! * @author Suresh Pragada ! * @version 1.0 ! * @since 1.0 */ public class FileParseException extends RuntimeException { ! /** ! * Constructs the FileParseException by accepting the message describes ! * the problem. ! * ! * @param message Messages describes the problem. ! */ public FileParseException(String message) { super(message); ! } } Index: WriterRecord.java =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/WriterRecord.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** WriterRecord.java 2 Jun 2006 21:02:06 -0000 1.1 --- WriterRecord.java 7 Jun 2006 22:10:41 -0000 1.2 *************** *** 11,15 **** package org.jmonks.batchserver.io; ! import org.apache.xml.resolver.apps.resolver; /** --- 11,15 ---- package org.jmonks.batchserver.io; ! /** Index: FileSpec.java =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/FileSpec.java,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** FileSpec.java 3 Jun 2006 13:14:41 -0000 1.5 --- FileSpec.java 7 Jun 2006 22:10:41 -0000 1.6 *************** *** 29,32 **** --- 29,38 ---- /** + * <p> + * FileSpec represents the file contains specification (spec for short) of any + * 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> * * @author Suresh Pragada *************** *** 36,47 **** public abstract class FileSpec { protected String fileSpecPath=null; ! protected FileType fileType=null; ! protected List recordSpecList=null; ! public static final String FILE_SPEC_TAG_NAME = "file-spec"; ! public static final String FILE_TYPE_ATTRIB_NAME = "file-type"; --- 42,64 ---- public abstract class FileSpec { + /** + * Holds the path of the file contains the spec of any file to read/write. + */ protected String fileSpecPath=null; ! /** ! * Holds the type of the file this spec is representing. ! */ protected FileType fileType=null; ! /** ! * Holds the list of record specs. ! */ protected List recordSpecList=null; ! /** ! * Constant defines the file spec tage name. ! */ public static final String FILE_SPEC_TAG_NAME = "file-spec"; ! /** ! * Constant defines the attribute name file type should contain. ! */ public static final String FILE_TYPE_ATTRIB_NAME = "file-type"; *************** *** 49,53 **** /** ! * Creates a new instance of FileSpec */ protected FileSpec(String fileSpecPath,FileType fileType) --- 66,74 ---- /** ! * Constructs the FileSpec object by accepting the path of the file contains ! * the file specification and file type. ! * ! * @param fileSpecPath Path of a file contains the spec. ! * @param fileType Type of the file this spec is representing. */ protected FileSpec(String fileSpecPath,FileType fileType) *************** *** 59,76 **** /** ! * Adds the record spec object to the record spec list. * ! * @param recordSpec Record spec. * * @return Returns true, if record spec is added, false otherwise. */ protected boolean addRecordSpec(RecordSpec recordSpec) { ! this.recordSpecList.add(recordSpec); ! return true; } /** ! * Gets the record spec's as a list. * * @return Returns record spec's as a list. --- 80,101 ---- /** ! * Adds the record spec to the file spec. * ! * @param recordSpec Record spec to be added to the file spec. * * @return Returns true, if record spec is added, false otherwise. + * + * @throws IllegalArgumentException If recordSpec is null. */ protected boolean addRecordSpec(RecordSpec recordSpec) { ! if(recordSpec==null) ! throw new IllegalArgumentException("RecordSpec cannot be null to add to the file spec."); ! else ! return this.recordSpecList.add(recordSpec); } /** ! * Gets all the record spec's in this file spec as a list. * * @return Returns record spec's as a list. *************** *** 80,83 **** --- 105,126 ---- return this.recordSpecList; } + + /** + * + * @throws IllegalArgumentException If recordType is null. + */ + public RecordSpec getRecordSpec(RecordType recordType) + { + if(recordType==null) + throw new IllegalArgumentException("RecordType cannot be null to get the record spec from file spec."); + else + { + for(Iterator iterator=this.recordSpecList.iterator();iterator.hasNext();) + { + RecordSpec recordSpec=(RecordSpec)iterator.next(); + if(recordType.toString().equ) + } + } + } /** *************** *** 119,132 **** return XMLFileSpec.createXMLFileSpec(fileSpecPath,fileSpecElement); else ! throw new FileSpecException("Configured file type in the file spec is not recognizable."); } else ! throw new FileSpecException("File spec root element doenst match the expected " + FileSpec.FILE_SPEC_TAG_NAME + " value."); } catch(FileNotFoundException exception) { exception.printStackTrace(); ! logger.fatal("Couldnt find the file spec in the given file path = " + fileSpecPath,exception); ! throw new FileSpecException("Cannot find the file = " + fileSpecPath + " Message = " + exception.getMessage()); } catch(ParserConfigurationException exception) --- 162,175 ---- return XMLFileSpec.createXMLFileSpec(fileSpecPath,fileSpecElement); else ! throw new FileSpecException("Configured file type " + fileType + " in the file spec is not recognizable. File spec location = " + fileSpecPath); } else ! throw new FileSpecException("File spec root element doesnt match the expected " + FileSpec.FILE_SPEC_TAG_NAME + " value in the file spec located at = " + fileSpecPath); } catch(FileNotFoundException exception) { exception.printStackTrace(); ! logger.fatal("Cannot find the file in the given path = " + fileSpecPath,exception); ! throw new FileSpecException("Cannot find the file contains the spec in given location = ." + fileSpecPath + " Message = " + exception.getMessage()); } catch(ParserConfigurationException exception) *************** *** 140,144 **** exception.printStackTrace(); logger.fatal("Parsing exception while parsing the file spec = " + fileSpecPath,exception); ! throw new FileSpecException("Parsing exception while parsing the file spec = " + fileSpecPath + " Message = " + exception.getMessage()); } catch(IOException exception) --- 183,187 ---- exception.printStackTrace(); logger.fatal("Parsing exception while parsing the file spec = " + fileSpecPath,exception); ! throw new FileSpecException("Parsing exception while parsing the file spec in given location = " + fileSpecPath + " Message = " + exception.getMessage()); } catch(IOException exception) *************** *** 146,150 **** exception.printStackTrace(); logger.fatal("IO exception while parsing the file spec = " + fileSpecPath,exception); ! throw new FileSpecException("IO exception while parsing the file spec = " + fileSpecPath + " Message = " + exception.getMessage()); } } --- 189,193 ---- exception.printStackTrace(); logger.fatal("IO exception while parsing the file spec = " + fileSpecPath,exception); ! throw new FileSpecException("IO exception while parsing the file spec in given location=" + fileSpecPath + " Message = " + exception.getMessage()); } } Index: FileType.java =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/FileType.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** FileType.java 3 Jun 2006 04:20:15 -0000 1.3 --- FileType.java 7 Jun 2006 22:10:41 -0000 1.4 *************** *** 12,34 **** /** * ! * @author Suresh Pragada */ public final class FileType { ! private String fileType=null; ! /** Creates a new instance of FileType */ ! private FileType(String fileType) { ! this.fileType=fileType; } ! public static FileType toFileType(String fileType) { ! if(FileType.FIXED_WIDTH_FLAT_FILE.toString().equalsIgnoreCase(fileType)) return FileType.FIXED_WIDTH_FLAT_FILE; ! else if(FileType.DELIMITED_FLAT_FILE.toString().equalsIgnoreCase(fileType)) return FileType.DELIMITED_FLAT_FILE; ! else if(FileType.XML_FILE.toString().equalsIgnoreCase(fileType)) return FileType.XML_FILE; else --- 12,59 ---- /** + * <p> + * FileType defines different kinds of file formats this package supports for + * reading and writing. This defines the constants to identify those file formats + * that it supports as enums. The file type information should be provided when + * defining the file spec to identify the kind of file this spec is going to represent. + * </p> * ! * @author Suresh Pragada ! * @version 1.0 ! * @since 1.0 */ public final class FileType { ! /** ! * Holds the string representing the file type. ! */ ! private String type=null; ! ! /** ! * Private constructor to make sure that FileType cannot be instantiated from ! * outside of this class. ! * ! * @param type String representing the type of file. ! */ ! private FileType(String type) { ! this.type=type; } ! /** ! * Utility method returns the FileType object matches to the given value. ! * ! * @param type String representing the type of file. ! * ! * @return Returns the FileType references matches to the given values, null ! * if it cannot match to the available file types. ! */ ! public static FileType toFileType(String type) { ! if(FileType.FIXED_WIDTH_FLAT_FILE.toString().equalsIgnoreCase(type)) return FileType.FIXED_WIDTH_FLAT_FILE; ! else if(FileType.DELIMITED_FLAT_FILE.toString().equalsIgnoreCase(type)) return FileType.DELIMITED_FLAT_FILE; ! else if(FileType.XML_FILE.toString().equalsIgnoreCase(type)) return FileType.XML_FILE; else *************** *** 36,46 **** } public String toString() { ! return fileType; } ! public static final FileType FIXED_WIDTH_FLAT_FILE = new FileType("fixed-width-flat"); public static final FileType DELIMITED_FLAT_FILE = new FileType("delimited-flat"); ! public static final FileType XML_FILE = new FileType("xml"); } --- 61,87 ---- } + /** + * Returns the string representation of the FileType which can be used to + * defines the file spec. + */ public String toString() { ! return type; } ! ! /** ! * Represents the fixed width flat files. The value to be used in the file ! * spec for this kind of file types is "fixed-width-flat". ! */ public static final FileType FIXED_WIDTH_FLAT_FILE = new FileType("fixed-width-flat"); + /** + * Represents the delimited flat files. The value to be used in the file + * spec for this kind of file types is "delimited-flat". + */ public static final FileType DELIMITED_FLAT_FILE = new FileType("delimited-flat"); ! /** ! * Represents the xml files. The value to be used in the file ! * spec for this kind of file types is "xml". ! */ ! public static final FileType XML_FILE = new FileType("xml"); } |
From: Suresh <sur...@us...> - 2006-06-08 02:50:26
|
Update of /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/xml In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv2188/org/jmonks/batchserver/io/xml Modified Files: XMLFileWriter.java Log Message: no message Index: XMLFileWriter.java =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/xml/XMLFileWriter.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** XMLFileWriter.java 7 Jun 2006 03:12:08 -0000 1.1 --- XMLFileWriter.java 7 Jun 2006 22:10:42 -0000 1.2 *************** *** 38,41 **** --- 38,43 ---- protected XMLStreamWriter writer=null; + private XMLIndentationEngine indentationEngine=new XMLIndentationEngine(); + private static Logger logger=Logger.getLogger(XMLFileWriter.class); *************** *** 49,52 **** --- 51,55 ---- writer=outputFactory.createXMLStreamWriter(new java.io.FileWriter(absoluteFilePath)); writer.writeStartDocument("ISO-8859-1", "1.0"); + indentationEngine.startElement(); writer.writeStartElement(this.fileSpec.rootElement); } *************** *** 72,77 **** XMLRecordSpec recordSpec=getRecordSpec(record.getRecordType()); String recordXpath=recordSpec.getRecordXPath(); ! String recordElement=recordXpath.substring(recordXpath.lastIndexOf('/')); writeCompositeElement(recordElement, record); } catch(XMLStreamException exception) --- 75,82 ---- XMLRecordSpec recordSpec=getRecordSpec(record.getRecordType()); String recordXpath=recordSpec.getRecordXPath(); ! int index=recordXpath.lastIndexOf('/'); ! String recordElement=recordXpath.substring(((index!=-1)?(index+1):0)); writeCompositeElement(recordElement, record); + writer.flush(); } catch(XMLStreamException exception) *************** *** 85,90 **** --- 90,97 ---- protected void writeSimpleElement(String fieldName,Object fieldValue) throws XMLStreamException { + indentationEngine.startElement(); writer.writeStartElement(fieldName); writer.writeCharacters((fieldValue!=null)?fieldValue.toString():""); + indentationEngine.endElement(); writer.writeEndElement(); } *************** *** 92,95 **** --- 99,103 ---- protected void writeCompositeElement(String recordElementName,XMLWriterRecord fieldValueAsRecord) throws XMLStreamException { + indentationEngine.startElement(); writer.writeStartElement(recordElementName); for(Iterator iterator=fieldValueAsRecord.getFieldNameIterator();iterator.hasNext();) *************** *** 110,114 **** } } ! writer.writeEndElement(); } --- 118,123 ---- } } ! indentationEngine.endElement(); ! writer.writeEndElement(); } *************** *** 157,160 **** --- 166,170 ---- try { + indentationEngine.endElement(); this.writer.writeEndElement(); this.writer.writeEndDocument(); *************** *** 214,216 **** --- 224,267 ---- } } + + private class XMLIndentationEngine + { + private boolean isPrevStartElement=false; + private boolean isPrevEndElement=false; + + private String indentationString="\n"; + + private XMLIndentationEngine() + { + } + + private void startElement() throws XMLStreamException + { + isPrevEndElement=false; + if(isPrevStartElement) + { + this.indentationString+=" "; + writer.writeCharacters(this.indentationString); + } + else + { + isPrevStartElement=true; + writer.writeCharacters(indentationString); + } + } + + private void endElement() throws XMLStreamException + { + isPrevStartElement=false; + if(isPrevEndElement) + { + this.indentationString=this.indentationString.substring(0, this.indentationString.length()-4); + writer.writeCharacters(this.indentationString); + } + else + { + isPrevEndElement=true; + } + } + } } |
From: Suresh <sur...@us...> - 2006-06-07 22:27:28
|
Update of /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/flat In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv16424 Modified Files: FixedWidthFlatFileReader.java Log Message: no message Index: FixedWidthFlatFileReader.java =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/flat/FixedWidthFlatFileReader.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** FixedWidthFlatFileReader.java 3 Jun 2006 13:14:41 -0000 1.1 --- FixedWidthFlatFileReader.java 5 Jun 2006 03:12:57 -0000 1.2 *************** *** 89,92 **** --- 89,111 ---- } + public void close() + { + if(this.reader!=null) + { + try + { + this.reader.close(); + } + catch(IOException exception) + { + logger.debug("IO Exception while closing the reader. Message = " + exception.getMessage(),exception); + } + finally + { + this.reader=null; + } + } + } + private ReaderRecord parseRecord(String recordString) { |
From: Suresh <sur...@us...> - 2006-06-07 22:27:19
|
Update of /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv29948 Modified Files: FileWriter.java Log Message: no message Index: FileWriter.java =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/FileWriter.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** FileWriter.java 6 Jun 2006 21:20:20 -0000 1.1 --- FileWriter.java 7 Jun 2006 03:11:20 -0000 1.2 *************** *** 13,16 **** --- 13,17 ---- import java.io.File; import org.jmonks.batchserver.io.flat.FixedWidthFlatFileWriter; + import org.jmonks.batchserver.io.xml.XMLFileWriter; /** *************** *** 22,26 **** public abstract WriterRecord createWriterRecord(RecordType recordType); ! public abstract void writerRecord(WriterRecord record); public abstract void close(); --- 23,27 ---- public abstract WriterRecord createWriterRecord(RecordType recordType); ! public abstract void writeRecord(WriterRecord record); public abstract void close(); *************** *** 51,55 **** else if(fileSpec.getFileType()==FileType.XML_FILE) { ! return null;//new XMLFileWriter(absoluteFilePath,fileSpec); } else --- 52,56 ---- else if(fileSpec.getFileType()==FileType.XML_FILE) { ! return new XMLFileWriter(absoluteFilePath,fileSpec); } else |
From: Suresh <sur...@us...> - 2006-06-07 22:27:19
|
Update of /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/xml In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv30448 Modified Files: XMLFileReader.java Added Files: XMLFileWriter.java Log Message: no message Index: XMLFileReader.java =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/xml/XMLFileReader.java,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** XMLFileReader.java 6 Jun 2006 21:19:33 -0000 1.4 --- XMLFileReader.java 7 Jun 2006 03:12:08 -0000 1.5 *************** *** 38,42 **** protected XMLFileSpec fileSpec=null; ! protected XMLEventReader eventReader=null; protected String xpath=""; --- 38,42 ---- protected XMLFileSpec fileSpec=null; ! protected XMLEventReader reader=null; protected String xpath=""; *************** *** 51,61 **** { XMLInputFactory inputFactory=XMLInputFactory.newInstance(); ! eventReader=inputFactory.createXMLEventReader(new java.io.FileReader(absoluteFilePath)); if(this.validateRootElement()) xpath="/"+this.fileSpec.getRootElement(); else { ! eventReader.close(); ! throw new RuntimeException("Root tag doesnt match."); } } --- 51,61 ---- { XMLInputFactory inputFactory=XMLInputFactory.newInstance(); ! reader=inputFactory.createXMLEventReader(new java.io.FileReader(absoluteFilePath)); if(this.validateRootElement()) xpath="/"+this.fileSpec.getRootElement(); else { ! reader.close(); ! throw new FileParseException("Unexpected root element found. Expecting the root element " + this.fileSpec.getRootElement()); } } *************** *** 63,67 **** { exception.printStackTrace(); ! this.eventReader=null; logger.fatal("File to create the reader cannot be found. Exception Message = " + exception.getMessage(),exception); throw new FileParseException("File to create the reader cannot be found. Exception Message = " + exception.getMessage()); --- 63,67 ---- { exception.printStackTrace(); ! this.reader=null; logger.fatal("File to create the reader cannot be found. Exception Message = " + exception.getMessage(),exception); throw new FileParseException("File to create the reader cannot be found. Exception Message = " + exception.getMessage()); *************** *** 70,74 **** { exception.printStackTrace(); ! this.eventReader=null; logger.fatal("Exception while initializing the xml stream reader. Message = " + exception.getMessage(),exception); throw new FileParseException("Exception while initializing the xml stream reader. Message = " + exception.getMessage()); --- 70,74 ---- { exception.printStackTrace(); ! this.reader=null; logger.fatal("Exception while initializing the xml stream reader. Message = " + exception.getMessage(),exception); throw new FileParseException("Exception while initializing the xml stream reader. Message = " + exception.getMessage()); *************** *** 79,83 **** { XMLReaderRecord readerRecord=null; ! if(this.eventReader==null) return readerRecord; else --- 79,83 ---- { XMLReaderRecord readerRecord=null; ! if(this.reader==null) return readerRecord; else *************** *** 85,91 **** try { ! while(eventReader.hasNext()) { ! XMLEvent event=eventReader.nextEvent(); if(event.isStartElement()) { --- 85,91 ---- try { ! while(reader.hasNext()) { ! XMLEvent event=reader.nextEvent(); if(event.isStartElement()) { *************** *** 126,134 **** public void close() { ! if(this.eventReader!=null) { try { ! this.eventReader.close(); } catch(XMLStreamException exception) --- 126,134 ---- public void close() { ! if(this.reader!=null) { try { ! this.reader.close(); } catch(XMLStreamException exception) *************** *** 138,142 **** finally { ! this.eventReader=null; } } --- 138,142 ---- finally { ! this.reader=null; } } *************** *** 148,154 **** try { ! while(eventReader.hasNext()) { ! XMLEvent event=eventReader.nextEvent(); if(event.isStartElement()) { --- 148,154 ---- try { ! while(reader.hasNext()) { ! XMLEvent event=reader.nextEvent(); if(event.isStartElement()) { *************** *** 190,196 **** try { ! while(eventReader.hasNext()) { ! XMLEvent nextEvent=eventReader.peek(); if(nextEvent.isStartElement()) { --- 190,196 ---- try { ! while(reader.hasNext()) { ! XMLEvent nextEvent=reader.peek(); if(nextEvent.isStartElement()) { *************** *** 208,212 **** else { ! StartElement startElement=(StartElement)eventReader.nextEvent(); isPreviousElementStart=true; elementStack.push(startElement); --- 208,212 ---- else { ! StartElement startElement=(StartElement)reader.nextEvent(); isPreviousElementStart=true; elementStack.push(startElement); *************** *** 215,219 **** else if(nextEvent.isEndElement()) { ! EndElement endElement=(EndElement)eventReader.nextEvent(); isPreviousElementStart=false; if(recordElement.equalsIgnoreCase(endElement.getName().getLocalPart())) --- 215,219 ---- else if(nextEvent.isEndElement()) { ! EndElement endElement=(EndElement)reader.nextEvent(); isPreviousElementStart=false; if(recordElement.equalsIgnoreCase(endElement.getName().getLocalPart())) *************** *** 241,245 **** else if(nextEvent.isCharacters()) { ! Characters chars=(Characters)eventReader.nextEvent(); if(!chars.isWhiteSpace()) { --- 241,245 ---- else if(nextEvent.isCharacters()) { ! Characters chars=(Characters)reader.nextEvent(); if(!chars.isWhiteSpace()) { *************** *** 253,257 **** * Ignore the other events for now. */ ! eventReader.nextEvent(); } } --- 253,257 ---- * Ignore the other events for now. */ ! reader.nextEvent(); } } --- NEW FILE: XMLFileWriter.java --- /* * XMLFileWriter.java * * Created on June 5, 2006, 10:36 PM * * To change this template, choose Tools | Options and locate the template under * the Source Creation and Management node. Right-click the template and choose * Open. You can then make changes to the template in the Source Editor. */ package org.jmonks.batchserver.io.xml; import java.io.IOException; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import javax.xml.stream.XMLOutputFactory; import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamWriter; 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 XMLFileWriter extends FileWriter { protected String absoluteFilePath=null; protected XMLFileSpec fileSpec=null; protected XMLStreamWriter writer=null; private static Logger logger=Logger.getLogger(XMLFileWriter.class); public XMLFileWriter(String absoluteFilePath,FileSpec fileSpec) { try { this.absoluteFilePath=absoluteFilePath; this.fileSpec=(XMLFileSpec)fileSpec; XMLOutputFactory outputFactory=XMLOutputFactory.newInstance(); writer=outputFactory.createXMLStreamWriter(new java.io.FileWriter(absoluteFilePath)); writer.writeStartDocument("ISO-8859-1", "1.0"); writer.writeStartElement(this.fileSpec.rootElement); } catch(IOException exception) { exception.printStackTrace(); logger.fatal("IOException while creating the writer. Message = " + exception.getMessage(), exception); throw new FileParseException("IOException while creating the writer. Message = " + exception.getMessage()); } catch(XMLStreamException exception) { exception.printStackTrace(); logger.fatal("XMLStream exception while creating the writer. Message = " + exception.getMessage(), exception); throw new FileParseException("XMLStream exception while creating the writer. Message = " + exception.getMessage()); } } public void writeRecord(WriterRecord writerRecord) { try { XMLWriterRecord record=(XMLWriterRecord)writerRecord; XMLRecordSpec recordSpec=getRecordSpec(record.getRecordType()); String recordXpath=recordSpec.getRecordXPath(); String recordElement=recordXpath.substring(recordXpath.lastIndexOf('/')); writeCompositeElement(recordElement, record); } catch(XMLStreamException exception) { exception.printStackTrace(); logger.fatal("XMLStream exception while writing the record into the writer. Message = " + exception.getMessage(), exception); throw new FileParseException("XMLStream exception while writing the record into the writer. Message = " + exception.getMessage()); } } protected void writeSimpleElement(String fieldName,Object fieldValue) throws XMLStreamException { writer.writeStartElement(fieldName); writer.writeCharacters((fieldValue!=null)?fieldValue.toString():""); writer.writeEndElement(); } protected void writeCompositeElement(String recordElementName,XMLWriterRecord fieldValueAsRecord) throws XMLStreamException { writer.writeStartElement(recordElementName); for(Iterator iterator=fieldValueAsRecord.getFieldNameIterator();iterator.hasNext();) { String fieldName=(String)iterator.next(); Object fieldValue=fieldValueAsRecord.readField(fieldName); if(fieldValue instanceof XMLWriterRecord) { writeCompositeElement(fieldName, (XMLWriterRecord)fieldValue); } else if(fieldValue instanceof List) { writeMlutipleElements(fieldName, (List)fieldValue); } else { writeSimpleElement(fieldName, fieldValue); } } writer.writeEndElement(); } protected void writeMlutipleElements(String multiElementName,List fieldValueAsList) throws XMLStreamException { List multiElementList=(List)fieldValueAsList; for(Iterator iterator=multiElementList.iterator();iterator.hasNext();) { Object fieldValue=iterator.next(); if(fieldValue instanceof XMLWriterRecord) { writeCompositeElement(multiElementName, (XMLWriterRecord)fieldValue); } else if(fieldValue instanceof List) { writeMlutipleElements(multiElementName, (List)fieldValue); } else { writeSimpleElement(multiElementName, fieldValue); } } } private XMLRecordSpec getRecordSpec(RecordType recordType) { List recordSpecList=this.fileSpec.getRecordSpecs(); for(Iterator iterator=recordSpecList.iterator();iterator.hasNext();) { XMLRecordSpec recordSpec=(XMLRecordSpec)iterator.next(); if(recordSpec.getRecordType()==recordType) return recordSpec; } return null; } public WriterRecord createWriterRecord(RecordType recordType) { return new XMLWriterRecord(recordType); } public void close() { if(this.writer!=null) { try { this.writer.writeEndElement(); this.writer.writeEndDocument(); this.writer.flush(); this.writer.close(); } catch(XMLStreamException exception) { logger.debug("XML Stream Exception while closing the writer. Message = " + exception.getMessage(), exception); } finally { this.writer=null; } } } public class XMLWriterRecord extends WriterRecord { private Map fieldMap=null; private XMLWriterRecord(RecordType recordType) { super(recordType); fieldMap=new HashMap(); } public void writeField(String fieldName, Object fieldValue) { this.fieldMap.put(fieldName, fieldValue); } public void writeSimpleElement(String fieldName, String fieldValue) { this.fieldMap.put(fieldName, fieldValue); } public void writeCompositeElement(String fieldName, WriterRecord fieldValue) { this.fieldMap.put(fieldName, fieldValue); } public void writeMultiElement(String fieldName, List fieldValue) { this.fieldMap.put(fieldName, fieldValue); } protected Object readField(String fieldName) { return this.fieldMap.get(fieldName); } protected Iterator getFieldNameIterator() { return this.fieldMap.keySet().iterator(); } } } |
From: Suresh <sur...@us...> - 2006-06-07 22:27:19
|
Update of /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv20669 Added Files: FileWriter.java Log Message: no message --- NEW FILE: FileWriter.java --- /* * FileWriter.java * * Created on June 6, 2006, 8:59 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; import java.io.File; import org.jmonks.batchserver.io.flat.FixedWidthFlatFileWriter; /** * * @author Suresh Pragada */ public abstract class FileWriter { public abstract WriterRecord createWriterRecord(RecordType recordType); public abstract void writerRecord(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 null;//new XMLFileWriter(absoluteFilePath,fileSpec); } else { throw new FileSpecException("Unsupported file type in the file spec = " + fileSpec.getFileType().toString()); } } } |
From: Suresh <sur...@us...> - 2006-06-07 22:27:16
|
Update of /cvsroot/batchserver/batchserver/test/org/jmonks/batchserver/io/flat In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv21197 Added Files: FixedWidthFlatFileReaderTest.java FixedWidthFlatFileWriterTest.java Log Message: no message --- NEW FILE: FixedWidthFlatFileReaderTest.java --- /* * FixedWidthFlatFileReaderTest.java * JUnit based test * * Created on June 2, 2006, 4:02 PM */ package org.jmonks.batchserver.io.flat; import junit.framework.*; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import org.apache.log4j.Logger; import org.jmonks.batchserver.io.*; /** * * @author w951h8m */ public class FixedWidthFlatFileReaderTest extends TestCase { public FixedWidthFlatFileReaderTest(String testName) { super(testName); } protected void setUp() throws Exception { } protected void tearDown() throws Exception { } public static Test suite() { TestSuite suite = new TestSuite(FixedWidthFlatFileReaderTest.class); return suite; } /** * Test of getNextRecord method, of class org.jmonks.batchserver.io.FixedWidthFlatFileReader. */ public void testGetNextRecord() { System.out.println("testGetNextRecord"); FileReader fileReader=FileReader.getFileReader("D:\\workarea\\personal\\latestcvs\\batchserver\\src\\org\\jmonks\\batchserver\\io\\payment.pmt", "D:\\workarea\\personal\\latestcvs\\batchserver\\src\\org\\jmonks\\batchserver\\io\\payment-file-spec.xml"); ReaderRecord record=null; while((record=fileReader.getNextRecord())!=null) { if(record.getRecordType()==RecordType.DETAIL) { System.out.print(record.readField("pmtId") + " "); System.out.print(record.readField("pmtAmt") + " "); System.out.print(record.readField("mpaId") + " "); System.out.print(record.readField("pmId")); System.out.println(); } else System.out.println(record.getRecordType().toString()); } } } --- NEW FILE: FixedWidthFlatFileWriterTest.java --- /* * FixedWidthFlatFileWriterTest.java * JUnit based test * * Created on June 6, 2006, 11:41 AM */ package org.jmonks.batchserver.io.flat; import junit.framework.*; import java.io.BufferedWriter; import java.io.IOException; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import org.apache.log4j.Logger; import org.jmonks.batchserver.io.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 w951h8m */ public class FixedWidthFlatFileWriterTest extends TestCase { public FixedWidthFlatFileWriterTest(String testName) { super(testName); } protected void setUp() throws Exception { } protected void tearDown() throws Exception { } public static Test suite() { TestSuite suite = new TestSuite(FixedWidthFlatFileWriterTest.class); return suite; } /** * Test of writerRecord method, of class org.jmonks.batchserver.io.flat.FixedWidthFlatFileWriter. */ public void testWriterRecord() { System.out.println("testWriterRecord"); FileWriter fileWriter=FileWriter.getFileWriter("D:\\test.pmt", "D:\\workarea\\personal\\latestcvs\\batchserver\\src\\org\\jmonks\\batchserver\\io\\flat\\sample-fixed-width-file-spec.xml"); WriterRecord rec1=fileWriter.createWriterRecord(RecordType.DETAIL); rec1.writeField("field1", "VALUE1"); rec1.writeField("field2", "VALUE2"); rec1.writeField("field3", "VALUE3VALUE3VALUE3"); rec1.writeField("field4", "VALUE4"); fileWriter.writerRecord(rec1); WriterRecord rec2=fileWriter.createWriterRecord(RecordType.DETAIL); rec2.writeField("field1", "VALUE1"); rec2.writeField("field2", "VALUE2"); rec2.writeField("field3", "VALUE3"); rec2.writeField("field4", "VALUE4"); fileWriter.writerRecord(rec2); fileWriter.close(); } } |
From: Suresh <sur...@us...> - 2006-06-07 22:27:14
|
Update of /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/xml In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv20188 Modified Files: XMLFileReader.java Log Message: no message Index: XMLFileReader.java =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/xml/XMLFileReader.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** XMLFileReader.java 5 Jun 2006 22:40:34 -0000 1.3 --- XMLFileReader.java 6 Jun 2006 21:19:33 -0000 1.4 *************** *** 117,122 **** { exception.printStackTrace(); ! //logger.fatal(); ! throw new RuntimeException("Exception while reading the record. Message = " + exception.getMessage()); } } --- 117,122 ---- { exception.printStackTrace(); ! logger.fatal("Exception while reading the record. Message = " + exception.getMessage(),exception); ! throw new FileParseException("Exception while reading the record. Message = " + exception.getMessage()); } } *************** *** 134,138 **** catch(XMLStreamException exception) { ! //logger.debug("Streamexception while closing the reader. Message = " + exception.getMessage(),exception); } finally --- 134,138 ---- catch(XMLStreamException exception) { ! logger.debug("Streamexception while closing the reader. Message = " + exception.getMessage(),exception); } finally *************** *** 259,263 **** catch(XMLStreamException exception) { ! throw new RuntimeException("XMLStream exception while retrieving the record. Message = " + exception.getMessage(), exception); } return readerRecord; --- 259,265 ---- catch(XMLStreamException exception) { ! exception.printStackTrace(); ! logger.fatal("XMLStream exception while retrieving the record. Message = " + exception.getMessage(), exception); ! throw new FileParseException("XMLStream exception while retrieving the record. Message = " + exception.getMessage()); } return readerRecord; *************** *** 276,287 **** public Object readField(String fieldName) { ! return readElement(fieldName); } ! public Object readElement(String elementName) { ! return fieldMap.get(elementName); } protected void writeElement(String fieldName, Object fieldValue) --- 278,309 ---- public Object readField(String fieldName) { ! return this.fieldMap.get(fieldName); } ! public String readSimpleElement(String elementName) { ! return (String)fieldMap.get(elementName); } + public ReaderRecord readCompositeElement(String elementName) + { + return (ReaderRecord)fieldMap.get(elementName); + } + + public List readMutlipleElements(String elementName) + { + Object fieldValue=this.fieldMap.get(elementName); + + if(fieldValue==null) + return null; + else if(fieldValue instanceof List) + return (List)fieldValue; + else + { + List fieldValueList=new ArrayList(); + fieldValueList.add(fieldValue); + return fieldValueList; + } + } protected void writeElement(String fieldName, Object fieldValue) |
From: Suresh <sur...@us...> - 2006-06-07 22:26:53
|
Update of /cvsroot/batchserver/batchserver/test/org/jmonks/batchserver/io/xml In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv22209 Added Files: TestStax.java XMLFileReaderTest.java Log Message: no message --- NEW FILE: TestStax.java --- /* * TestStax.java * * Created on June 1, 2006, 9:41 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.xml; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Stack; import javax.xml.stream.XMLEventReader; import javax.xml.stream.XMLInputFactory; import javax.xml.stream.XMLStreamException; import javax.xml.stream.events.Characters; import javax.xml.stream.events.EndElement; import javax.xml.stream.events.StartElement; import javax.xml.stream.events.XMLEvent; /** * * @author Suresh Pragada */ public class TestStax { protected XMLFileSpec fileSpec=null; protected XMLEventReader eventReader=null; protected String xpath=""; public TestStax(String absoluteFilePath,org.jmonks.batchserver.io.FileSpec fileSpec) { this.fileSpec=(XMLFileSpec)fileSpec; try { XMLInputFactory inputFactory=XMLInputFactory.newInstance(); eventReader=inputFactory.createXMLEventReader(new java.io.FileReader(absoluteFilePath)); String rootElement=this.fileSpec.getRootElement(); if(this.validateRootElement(rootElement, eventReader)) xpath="/"+rootElement; else { eventReader.close(); throw new RuntimeException("Root tag doesnt match."); } } catch(FileNotFoundException exception) { exception.printStackTrace(); //logger.fatal(); this.eventReader=null; throw new RuntimeException("Exception while initializing the XML File reader = " + absoluteFilePath); } catch(XMLStreamException exception) { exception.printStackTrace(); //logger.fatal(); this.eventReader=null; throw new RuntimeException("Exception while initializing the XML File reader = " + absoluteFilePath); } } public org.jmonks.batchserver.io.ReaderRecord getNextRecord() { XMLReaderRecord readerRecord=null; if(this.eventReader==null) return readerRecord; else { try { while(eventReader.hasNext()) { XMLEvent event=eventReader.nextEvent(); if(event.isStartElement()) { StartElement startElement=(StartElement)event; String startElementName=startElement.getName().getLocalPart(); xpath=xpath+"/"+startElementName; XMLRecordSpec recordSpec=this.getRecordSpec(); if(recordSpec!=null) { //logger.debug("Found configured " + xpath); readerRecord=retrieveRecord(eventReader,recordSpec,startElementName); int index=xpath.lastIndexOf("/"+startElementName); if(index!=-1) xpath=xpath.substring(0, index); break; } } else if(event.isEndElement()) { EndElement endElement=(EndElement)event; String endElementName=endElement.getName().getLocalPart(); int index=xpath.lastIndexOf("/"+endElementName); if(index!=-1) xpath=xpath.substring(0, index); } } } catch(XMLStreamException exception) { exception.printStackTrace(); //logger.fatal(); throw new RuntimeException("Exception while reading the record. Message = " + exception.getMessage()); } } return readerRecord; } public void close() { if(this.eventReader!=null) { try { this.eventReader.close(); } catch(XMLStreamException exception) { //logger.debug("Streamexception while closing the reader. Message = " + exception.getMessage(),exception); } finally { this.eventReader=null; } } } public static void main(String args[]) throws Exception { org.jmonks.batchserver.io.FileSpec fileSpec=org.jmonks.batchserver.io.FileSpec.createFileSpec("D:\\workarea\\personal\\latestcvs\\batchserver\\src\\org\\jmonks\\batchserver\\io\\xml\\sample-xml-file-spec.xml"); TestStax stax=new TestStax("D:\\workarea\\personal\\latestcvs\\batchserver\\src\\org\\jmonks\\batchserver\\io\\xml\\sample-xml-file_1.xml",fileSpec); } private boolean validateRootElement(String rootElement,XMLEventReader reader) { boolean valid=false; try { while(reader.hasNext()) { XMLEvent event=reader.nextEvent(); if(event.isStartElement()) { StartElement startElement=(StartElement)event; if(rootElement.equalsIgnoreCase(startElement.getName().getLocalPart())) { valid=true; break; } } } } catch(XMLStreamException exception) { exception.printStackTrace(); //logger.fatal(); throw new RuntimeException("Exception while validating the root element. Message = " + exception.getMessage(),exception); } return valid; } private XMLRecordSpec getRecordSpec() { List recordSpecs=this.fileSpec.getRecordSpecs(); for(Iterator iterator=recordSpecs.iterator();iterator.hasNext();) { XMLRecordSpec recordSpec=(XMLRecordSpec)iterator.next(); if(recordSpec.isMatch(this.xpath)) return recordSpec; } return null; } private XMLReaderRecord retrieveRecord(XMLEventReader eventReader,XMLRecordSpec recordSpec,String recordElement) { XMLReaderRecord readerRecord=new XMLReaderRecord(recordSpec.getRecordType()); Stack elementStack=new Stack(); boolean isPreviousElementStart=false; try { while(eventReader.hasNext()) { XMLEvent nextEvent=eventReader.peek(); if(nextEvent.isStartElement()) { /** * If the previous element is also a start element, retrieve the * nested element and returns it as a nested record. */ if(isPreviousElementStart) { StartElement previousStartElement=(StartElement)elementStack.pop(); XMLReaderRecord nestedRecord=this.retrieveRecord(eventReader, recordSpec, previousStartElement.getName().getLocalPart()); readerRecord.writeElement(previousStartElement.getName().getLocalPart(),nestedRecord); isPreviousElementStart=false; } else { StartElement startElement=(StartElement)eventReader.nextEvent(); isPreviousElementStart=true; elementStack.push(startElement); } } else if(nextEvent.isEndElement()) { EndElement endElement=(EndElement)eventReader.nextEvent(); isPreviousElementStart=false; if(recordElement.equalsIgnoreCase(endElement.getName().getLocalPart())) break; else { /** * Check the value on top of the stack. If it is start element, no value has * been provided for this element in the file, otherwise pop the values and start element. */ Object topValue=elementStack.peek(); if(topValue instanceof StartElement) { StartElement startElement=(StartElement)elementStack.pop(); readerRecord.writeElement(startElement.getName().getLocalPart(),""); } else { Object fieldValue=elementStack.pop(); StartElement startElement=(StartElement)elementStack.pop(); readerRecord.writeElement(startElement.getName().getLocalPart(), fieldValue); } } } else if(nextEvent.isCharacters()) { Characters chars=(Characters)eventReader.nextEvent(); if(!chars.isWhiteSpace()) { isPreviousElementStart=false; elementStack.push(chars.getData()); } } else { /** * Ignore the other events for now. */ eventReader.nextEvent(); } } } catch(XMLStreamException exception) { throw new RuntimeException("XMLStream exception while retrieving the record. Message = " + exception.getMessage(), exception); } return readerRecord; } private void printRecord(XMLReaderRecord record) { System.out.println("*******************************************"); System.out.println(record); System.out.println("*******************************************"); } public class XMLReaderRecord extends org.jmonks.batchserver.io.ReaderRecord { private Map fieldMap=null; private XMLReaderRecord(org.jmonks.batchserver.io.RecordType recordType) { super(recordType); fieldMap=new HashMap(); } public Object readField(String fieldName) { return readElement(fieldName); } public Object readElement(String elementName) { return fieldMap.get(elementName); } protected void writeElement(String fieldName, Object fieldValue) { if(fieldMap.containsKey(fieldName)) { Object existingFieldValue=fieldMap.remove(fieldName); if(existingFieldValue instanceof List) { List existingFieldValueList=(List)existingFieldValue; existingFieldValueList.add(fieldValue); fieldMap.put(fieldName, existingFieldValueList); } else { List fieldValueList=new ArrayList(); fieldValueList.add(existingFieldValue); fieldValueList.add(fieldValue); fieldMap.put(fieldName,fieldValueList); } } else fieldMap.put(fieldName,fieldValue); } public String toString() { return this.fieldMap.toString(); } } } --- NEW FILE: XMLFileReaderTest.java --- /* * XMLFileReaderTest.java * JUnit based test * * Created on June 5, 2006, 4:38 PM */ package org.jmonks.batchserver.io.xml; import junit.framework.*; import org.jmonks.batchserver.io.*; /** * * @author w951h8m */ public class XMLFileReaderTest extends TestCase { public XMLFileReaderTest(String testName) { super(testName); } protected void setUp() throws Exception { } protected void tearDown() throws Exception { } public static Test suite() { TestSuite suite = new TestSuite(XMLFileReaderTest.class); return suite; } /** * Test of getNextRecord method, of class org.jmonks.batchserver.io.xml.XMLFileReader. */ public void testGetNextRecord() { System.out.println("testGetNextRecord"); FileReader fileReader=FileReader.getFileReader("D:\\workarea\\personal\\latestcvs\\batchserver\\src\\org\\jmonks\\batchserver\\io\\xml\\sample-xml-file_1.xml", "D:\\workarea\\personal\\latestcvs\\batchserver\\src\\org\\jmonks\\batchserver\\io\\xml\\sample-xml-file-spec.xml"); ReaderRecord readerRecord=null; while((readerRecord=fileReader.getNextRecord())!=null) System.out.println(readerRecord); fileReader.close(); } } |
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; } |