[Batchserver-cvs] batchserver/src/org/jmonks/batchserver/io/xml package.html, 1.4, 1.5 sample-xml-f
Brought to you by:
suresh_pragada
Update of /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/xml In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv1690 Modified Files: package.html sample-xml-file-spec.xml sample-xml-file.xml XMLFileSpec.java XMLFileWriter.java Log Message: no message Index: package.html =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/xml/package.html,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** package.html 22 Aug 2006 03:19:36 -0000 1.4 --- package.html 22 Aug 2006 23:42:09 -0000 1.5 *************** *** 25,31 **** <code>root-element</code> which will be used highly while generating the xml file from the set of records. The value in the attribute specifies the ! root element of the xml document. <pre> ! <file-spec file-type="xml" root-element="sample-root> </file-spec> </pre> --- 25,36 ---- <code>root-element</code> which will be used highly while generating the xml file from the set of records. The value in the attribute specifies the ! root element of the xml document. This file spec optionally requires 2 more attributes ! "indentation-engine" and "encoding". "indentation-engine" attribute value tells ! the class to be used for the indentation purposes. This class should implement ! the XMLIndentationEngine interface. If this attribute is not specified and the given ! class is not valid, it uses default indentation engine. The value specified in ! "encoding" attribute will be used in generated xml processing instruction. <pre> ! <file-spec file-type="xml" root-element="sample-root" indentation-engine="org.jmonks.batchserver.io.xml.PrettyXMLIndentationEngine" encoding="ISO-8859-1"> </file-spec> </pre> *************** *** 34,38 **** which tells the xpath in the document to identify the record. <pre> ! <file-spec file-type="xml" root-element="sample-root> <record-spec record-type="DETAIL" record-xpath="/sample-root/detail-record"/> </file-spec> --- 39,43 ---- which tells the xpath in the document to identify the record. <pre> ! <file-spec file-type="xml" root-element="sample-root" indentation-engine="org.jmonks.batchserver.io.xml.PrettyXMLIndentationEngine" encoding="ISO-8859-1"> <record-spec record-type="DETAIL" record-xpath="/sample-root/detail-record"/> </file-spec> *************** *** 46,103 **** repeat more than once) values will be exposed as list. ! <h5>Reading the records from xml files</h5> ! Reading the records from the xml files is fairly simple. <pre style="color:green"> <i> ! InputStream fileSpecInputStream= // Get the input stream for the XML file contains the file structure. ! InputStream fileInputStream= // Get the input stream of the file to be read. ! FileReader reader=FileReader.getFileReader(fileInputStream,fileSpecInputStream); ! ReaderRecord record=null; ! while((record=reader.getNextRecord())!=null) { ! if(record.getRecordType().equals(RecordType.DETAIL)) { ! XMLFileReader.XMLReaderRecord xmlRecord=(XMLFileReader.XMLReaderRecord)record; ! String fieldValue1=xmlRecord.readSimpleElement("field-name1"); ! ! List repeatElement=xmlRecord.readRepeatElement("field-name2"); ! //Read the repeated values from the list. ! ! XMLFileReader.XMLReaderRecord complexRecord=(XMLFileReader.XMLReaderRecord)xmlRecord.readComplexElement("field-name3"); ! String fieldValue4=complexRecord.readSimpleElement("field-name4"); ! // Read the rest of the field and does the processing. } } ! reader.close(); </i> </pre> ! <h5>Writing the records into xml files</h5> ! Writing the records into xml files is fairly simple. <pre style="color:green"> <i> ! InputStream fileSpecInputStream= // Get the input stream for the XML file contains the file structure. ! OutputStream fileOutputStream= // Get the output stream of the file to be written/generated. ! FileWriter writer=FileWriter.getFileWriter(fileOutputStream,fileSpecInputStream); ! ! XMLFileWriter.XMLWriterRecord record=(XMLFileWriter.XMLWriterRecord)writer.createWriterRecord(RecordType.DETAIL); ! record.writeSimpleElement("field-name1","field-value1"); ! XMLFileWriter.XMLWriterRecord complexRecord=(XMLFileWriter.XMLWriterRecord)record.createComplexElement("field-name3"); ! complexRecord.writeSimpleElement("field-name4","field-value4"); ! // Write all the other field values into the writer record. ! ! List repeatList=record.createRepeateElement("field-name2"); ! ! repeatList.add("field-value21"); ! repeatList.add("field-value22"); ! ! // Create an orphan record and add that one to the list. ! XMLFileWriter.XMLWriterRecord complexRepeatRecord=record.createComplexElement(); ! complexRepeatRecord.writeSimpleElement("field-name211","field-value211"); ! complexRepeatRecord.writeSimpleElement("field-name212","field-value212"); ! repeatList.add(complexRepeatRecord); ! ! writer.writeRecord(record); ! writer.close(); </i> </pre> --- 51,224 ---- repeat more than once) values will be exposed as list. ! <br> ! Here onwards will try to show how to process/read and generates/write the following xml file. ! Lets assume we need to read and write and following xml file. ! <br> ! <pre style="color:red"> ! <b> ! <?xml version='1.0' encoding='ISO-8859-1'?> ! <sample-root> ! <sample-header> ! <file-type>Employee Records</file-type> ! </sample-header> ! <sample-detail> ! <first-name>Suresh</first-name> ! <last-name>Pragada</last-name> ! <dept-info> ! <dept-name>IT</dept-name> ! <dept-location>LOC1</dept-location> ! </dept-info> ! <addresses> ! <address> ! <address-type>home</address-type> ! <city>Menomonee Falls</city> ! <zip-code>53051</zip-code> ! </address> ! <address> ! <address-type>office</address-type> ! <city>Menomonee Falls</city> ! <zip-code>53051</zip-code> ! </address> ! <address>Unidentified</address> ! </addresses> ! </sample-detail> ! <sample-trailer> ! <transaction-count>1</transaction-count> ! </sample-trailer> ! </sample-root> ! </b> ! </pre> ! <br> ! Following is the file spec to read and write the above given file. ! <br> ! <pre style="color:red"> ! <b> ! <?xml version="1.0" encoding="UTF-8"?> ! ! <file-spec file-type="xml" root-element="sample-root" indentation-engine="org.jmonks.batchserver.io.xml.PrettyXMLIndentationEngine" encoding="ISO-8859-1"> ! <record-spec record-type="header" record-xpath="/sample-root/sample-header"/> ! <record-spec record-type="detail" record-xpath="/sample-root/sample-detail"/> ! <record-spec record-type="trailer" record-xpath="/sample-root/sample-trailer"/> ! </file-spec> ! </b> ! </pre> ! ! <h5>Code to read the records from xml files</h5> <pre style="color:green"> <i> ! FileReader fileReader=FileReader.getFileReader(new FileInputStream("C:\\sample-xml-file_2.xml"),this.getClass().getResourceAsStream("sample-xml-file-spec.xml")); ! ! ReaderRecord readerRecord=null; ! while((readerRecord=fileReader.getNextRecord())!=null) ! { ! if(readerRecord.getRecordType().equals(RecordType.HEADER)) { ! // Simple elements in the records can be read using either readFiled or readSimpleElement. ! String fileType=(String)readerRecord.readField("file-type"); ! System.out.println("File type in header record = " + fileType); ! } ! else if(readerRecord.getRecordType().equals(RecordType.TRAILER)) ! { ! // Trying to show that simple elements can be read using readSimpleElement. ! String transactionCount=((XMLReaderRecord)readerRecord).readSimpleElement("transaction-count"); ! System.out.println(transactionCount); ! } ! else if(readerRecord.getRecordType().equals(RecordType.DETAIL)) ! { ! XMLReaderRecord xmlDetailRecord=(XMLReaderRecord)readerRecord; ! ! // Simple elements can be read using readSimpleElement method. ! String firstName=xmlDetailRecord.readSimpleElement("first-name"); ! String lastName=xmlDetailRecord.readSimpleElement("last-name"); ! System.out.println(firstName + " " + lastName); ! ! // Nested elements can be read using readComplexElement method. ! XMLReaderRecord deptInfoComplexRecord=(XMLReaderRecord)xmlDetailRecord.readComplexElement("dept-info"); ! String deptName=deptInfoComplexRecord.readSimpleElement("dept-name"); ! String deptLocation=deptInfoComplexRecord.readSimpleElement("dept-location"); ! System.out.println(deptName + " " + deptLocation); ! ! XMLReaderRecord addressesComplexRecord=(XMLReaderRecord)xmlDetailRecord.readComplexElement("addresses"); ! List addressesRepeatList=addressesComplexRecord.readRepeatElement("address"); ! for(Iterator iterator=addressesRepeatList.iterator();iterator.hasNext();) { ! Object addressRecord=iterator.next(); ! if(addressRecord instanceof XMLReaderRecord) ! { ! XMLReaderRecord addressComplexRecord=(XMLReaderRecord)addressRecord; ! String addressType=addressComplexRecord.readSimpleElement("address-type"); ! String city=addressComplexRecord.readSimpleElement("city"); ! String zipCode=addressComplexRecord.readSimpleElement("zip-code"); ! System.out.println(addressType + " " + city + " " + zipCode); ! } ! else if(addressRecord instanceof String) ! { ! System.out.println((String)addressRecord); ! } ! else ! System.out.println("Unknown type."); ! } } ! else ! System.out.println("Unknown record type = " + readerRecord.getRecordType().toString()); ! } ! fileReader.close(); </i> </pre> ! <h5>Code to write the records into xml files</h5> <pre style="color:green"> <i> ! // Get the file writer by providing the output stream to write the xml file and input stream to file spec. ! FileWriter fileWriter=FileWriter.getFileWriter(new FileOutputStream("C:\\sample-xml-file_2.xml"), this.getClass().getResourceAsStream("sample-xml-file-spec.xml")); ! // Create and write the header record. ! XMLWriterRecord headerRecord=(XMLWriterRecord)fileWriter.createWriterRecord(RecordType.HEADER); ! headerRecord.writeSimpleElement("file-type", "Employee Records"); ! fileWriter.writeRecord(headerRecord); ! ! // Get the empty record you want to create by passing the record type you mentioned in file spec. ! XMLWriterRecord detailRecord=(XMLWriterRecord)fileWriter.createWriterRecord(RecordType.DETAIL); ! ! // Write the simple elements using either writeField or writeSimpleElement methods. ! detailRecord.writeSimpleElement("first-name", "Suresh"); ! detailRecord.writeField("last-name", "Pragada"); ! ! // Create the nested record by passing the nested element name. This automatically attached to detail record. No need to write it back to detail record. ! XMLWriterRecord deptComplexRecord=(XMLWriterRecord)detailRecord.createComplexElement("dept-info"); ! deptComplexRecord.writeSimpleElement("dept-name", "IT"); ! deptComplexRecord.writeSimpleElement("dept-location", "LOC1"); ! ! ! XMLWriterRecord addressesComplexRecord=(XMLWriterRecord)detailRecord.createComplexElement("addresses"); ! // Get the list to add all the elements needs to be written with the given name. ! List addressRepeatList=addressesComplexRecord.createRepeatElement("address"); ! ! // Empty nested element record can be created using any XMLWriterRecord instance. ! XMLWriterRecord homeAddressComplexRecord=(XMLWriterRecord)addressesComplexRecord.createComplexElement(); ! homeAddressComplexRecord.writeSimpleElement("address-type", "home"); ! homeAddressComplexRecord.writeSimpleElement("city", "Menomonee Falls"); ! homeAddressComplexRecord.writeSimpleElement("zip-code", "53051"); ! addressRepeatList.add(homeAddressComplexRecord); ! ! // Empty nested element record can be created using any XMLWriterRecord instance. ! XMLWriterRecord officeAddressComplexRecord=(XMLWriterRecord)addressesComplexRecord.createComplexElement(); ! officeAddressComplexRecord.writeSimpleElement("address-type", "office"); ! officeAddressComplexRecord.writeSimpleElement("city", "Menomonee Falls"); ! officeAddressComplexRecord.writeSimpleElement("zip-code", "53051"); ! addressRepeatList.add(officeAddressComplexRecord); ! ! // Feel free to drop simple elements value as well. ! addressRepeatList.add("Unidentified"); ! ! // Write the finished record into the file. ! fileWriter.writeRecord(detailRecord); ! ! // Create and write the trailer record. ! XMLWriterRecord trailerRecord=(XMLWriterRecord)fileWriter.createWriterRecord(RecordType.TRAILER); ! trailerRecord.writeSimpleElement("transaction-count", "1"); ! fileWriter.writeRecord(trailerRecord); ! ! fileWriter.close(); </i> </pre> Index: sample-xml-file-spec.xml =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/xml/sample-xml-file-spec.xml,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** sample-xml-file-spec.xml 22 Aug 2006 13:04:13 -0000 1.2 --- sample-xml-file-spec.xml 22 Aug 2006 23:42:09 -0000 1.3 *************** *** 1,7 **** <?xml version="1.0" encoding="UTF-8"?> ! <file-spec file-type="xml" root-element="sample-root" indentation-engine="org.jmonks.batchserver.io.xml.PrettyXMLIndentationEngine"> <record-spec record-type="header" record-xpath="/sample-root/sample-header"/> <record-spec record-type="detail" record-xpath="/sample-root/sample-detail"/> ! <record-spec record-type="trailor" record-xpath="/sample-root/sample-trailor"/> </file-spec> --- 1,7 ---- <?xml version="1.0" encoding="UTF-8"?> ! <file-spec file-type="xml" root-element="sample-root" indentation-engine="org.jmonks.batchserver.io.xml.PrettyXMLIndentationEngine" encoding="ISO-8859-1"> <record-spec record-type="header" record-xpath="/sample-root/sample-header"/> <record-spec record-type="detail" record-xpath="/sample-root/sample-detail"/> ! <record-spec record-type="trailer" record-xpath="/sample-root/sample-trailer"/> </file-spec> Index: sample-xml-file.xml =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/xml/sample-xml-file.xml,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** sample-xml-file.xml 5 Jun 2006 22:40:34 -0000 1.3 --- sample-xml-file.xml 22 Aug 2006 23:42:09 -0000 1.4 *************** *** 1,30 **** ! <?xml version="1.0" encoding="UTF-8"?> <sample-root> <sample-header> ! <time-stamp>20060602152356</time-stamp> </sample-header> <sample-detail> ! <sample-field1>sample-value1</sample-field1> ! <sample-field2>sample-value1</sample-field2> ! <sample-field3> ! <sample-nested-field1>sample-nested-value1</sample-nested-field1> ! <sample-nested-field1>sample-nested-value2</sample-nested-field1> ! <sample-nested-field3>sample-nested-value3</sample-nested-field3> ! </sample-field3> ! <sample-field4>sample-value1</sample-field4> </sample-detail> ! <sample-detail> ! <sample-field1>sample-value1</sample-field1> ! <sample-field2>sample-value1</sample-field2> ! <sample-field3> ! <sample-nested-field1>sample-nested-value1</sample-nested-field1> ! <sample-nested-field1>sample-nested-value2</sample-nested-field1> ! <sample-nested-field3>sample-nested-value3</sample-nested-field3> ! </sample-field3> ! <sample-field4>sample-value4</sample-field4> ! <sample-field5/> <!-- <sample-field5/> --> ! </sample-detail> ! <sample-trailor> ! <record-count>10000</record-count> ! </sample-trailor> ! </sample-root> --- 1,30 ---- ! <?xml version='1.0' encoding='ISO-8859-1'?> <sample-root> <sample-header> ! <file-type>Employee Records</file-type> </sample-header> <sample-detail> ! <first-name>Suresh</first-name> ! <last-name>Pragada</last-name> ! <dept-info> ! <dept-name>IT</dept-name> ! <dept-location>LOC1</dept-location> ! </dept-info> ! <addresses> ! <address> ! <address-type>home</address-type> ! <city>Menomonee Falls</city> ! <zip-code>53051</zip-code> ! </address> ! <address> ! <address-type>office</address-type> ! <city>Menomonee Falls</city> ! <zip-code>53051</zip-code> ! </address> ! <address>Unidentified</address> ! </addresses> </sample-detail> ! <sample-trailer> ! <transaction-count>1</transaction-count> ! </sample-trailer> ! </sample-root> \ No newline at end of file Index: XMLFileWriter.java =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/xml/XMLFileWriter.java,v retrieving revision 1.12 retrieving revision 1.13 diff -C2 -d -r1.12 -r1.13 *** XMLFileWriter.java 22 Aug 2006 13:04:13 -0000 1.12 --- XMLFileWriter.java 22 Aug 2006 23:42:09 -0000 1.13 *************** *** 36,39 **** --- 36,48 ---- * writer record provides the methods to write these elements. * </p> + * <p> + * XMLFileWriter looks for two attributes "indentation-engine" and "encoding" in the + * file spec to format the generated xml and use the encoding value in generated + * xml processing instruction. The value to the "indentation-engine" should be + * the class name implements XMLIndentationEngine interface. If it doesnt find this + * attribute or not a valid value in this attribute, it uses the default indentation + * engine. The value specified in "encoding" attribute will be used in the processing + * instruction. + * </p> * * @author Suresh Pragada *************** *** 83,87 **** logger.debug("Writer has been created."); this.indentationEngine=this.getXMLIndentationEngine(this.fileSpec.getXMLIndentationEngineClassName()); ! this.writer.writeStartDocument("ISO-8859-1", "1.0"); this.writer.writeCharacters(this.indentationEngine.startElement()); this.writer.writeStartElement(this.fileSpec.rootElement); --- 92,100 ---- logger.debug("Writer has been created."); this.indentationEngine=this.getXMLIndentationEngine(this.fileSpec.getXMLIndentationEngineClassName()); ! String encoding=this.fileSpec.getEncoding(); ! if(encoding!=null && !"".equals(encoding)) ! this.writer.writeStartDocument(encoding, "1.0"); ! else ! this.writer.writeStartDocument("UTF-8", "1.0"); this.writer.writeCharacters(this.indentationEngine.startElement()); this.writer.writeStartElement(this.fileSpec.rootElement); *************** *** 268,272 **** logger.trace("Entering getXMLindentationEngine = " + xmlIndentationEngineClassName); XMLIndentationEngine engine=null; ! if(xmlIndentationEngineClassName!=null) { try --- 281,285 ---- logger.trace("Entering getXMLindentationEngine = " + xmlIndentationEngineClassName); XMLIndentationEngine engine=null; ! if(xmlIndentationEngineClassName!=null && !"".equals(xmlIndentationEngineClassName)) { try Index: XMLFileSpec.java =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/xml/XMLFileSpec.java,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** XMLFileSpec.java 22 Aug 2006 13:04:13 -0000 1.6 --- XMLFileSpec.java 22 Aug 2006 23:42:09 -0000 1.7 *************** *** 48,51 **** --- 48,55 ---- protected String xmlIndentationEngineClassName=null; /** + * Holds the encoding value to be placed in processing instruction. + */ + protected String encoding=null; + /** * Constant defines the root element attribute name. */ *************** *** 55,58 **** --- 59,66 ---- */ public static final String XML_INDENTATION_ENGINE_ATTRIB_NAME = "indentation-engine"; + /** + * Constant defines the encoding attribute name. + */ + public static final String ENCODING_ATTRIB_NAME = "encoding"; private static Logger logger=Logger.getLogger(XMLFileSpec.class); *************** *** 87,90 **** --- 95,109 ---- /** + * Get the encoding value to be placed in processing instruction. This will be + * conifgured in file spec. + * + * @return Returns the file spec. + */ + public String getEncoding() + { + return this.encoding; + } + + /** * Factory method to create the xml file spec object from the given * DOM Element representing the file-spec element. *************** *** 108,111 **** --- 127,131 ---- fileSpec.xmlIndentationEngineClassName=fileSpecElement.getAttribute(XMLFileSpec.XML_INDENTATION_ENGINE_ATTRIB_NAME); + fileSpec.encoding=fileSpecElement.getAttribute(XMLFileSpec.ENCODING_ATTRIB_NAME); NodeList recordSpecNodeList=fileSpecElement.getElementsByTagName(RecordSpec.RECORD_SPEC_TAG_NAME); *************** *** 137,140 **** --- 157,161 ---- stringValue.append("[rootElement = " + this.rootElement + "]"); stringValue.append("[xmlIndentationEngine = " + this.xmlIndentationEngineClassName + "]"); + stringValue.append("[encoding = " + this.encoding + "]"); stringValue.append("[recordSpecList = "); for(Iterator iterator=recordSpecMap.values().iterator();iterator.hasNext();) |