[Batchserver-cvs] batchserver/src/org/jmonks/batchserver/io/xml XMLFileReader.java, 1.5, 1.6 XMLFil
Brought to you by:
suresh_pragada
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); |