[Batchserver-cvs] batchserver/test/org/jmonks/batch/io/xml TestStax.java, NONE, 1.1 XMLFileReaderTe
Brought to you by:
suresh_pragada
From: Suresh <sur...@us...> - 2006-09-15 20:20:18
|
Update of /cvsroot/batchserver/batchserver/test/org/jmonks/batch/io/xml In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv26276 Added Files: TestStax.java XMLFileReaderTest.java XMLFileWriterTest.java Log Message: no message --- NEW FILE: XMLFileReaderTest.java --- /* * XMLFileReaderTest.java * JUnit based test * * Created on June 5, 2006, 4:38 PM */ package org.jmonks.batch.io.xml; import java.io.FileInputStream; import java.util.Iterator; import java.util.List; import junit.framework.*; import org.jmonks.batch.io.FileReader; import org.jmonks.batch.io.RecordType; import org.jmonks.batch.io.ReaderRecord; import org.jmonks.batch.io.xml.XMLFileReader.XMLReaderRecord; /** * * @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() throws Exception { System.out.println("testGetNextRecord"); 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(); } } --- NEW FILE: XMLFileWriterTest.java --- /* * XMLFileWriterTest.java * JUnit based test * * Created on June 6, 2006, 9:08 PM */ package org.jmonks.batch.io.xml; import java.io.FileOutputStream; import java.util.List; import junit.framework.*; import org.jmonks.batch.io.FileWriter; import org.jmonks.batch.io.RecordType; import org.jmonks.batch.io.xml.XMLFileWriter.XMLWriterRecord; /** * * @author Suresh Pragada */ public class XMLFileWriterTest extends TestCase { public XMLFileWriterTest(String testName) { super(testName); } protected void setUp() throws Exception { } protected void tearDown() throws Exception { } public static Test suite() { TestSuite suite = new TestSuite(XMLFileWriterTest.class); return suite; } /** * Test of writerRecord method, of class org.jmonks.batchserver.io.xml.XMLFileWriter. */ public void testWriterRecord() throws Exception { System.out.println("testWriterRecord"); //FileWriter fileWriter=FileWriter.getFileWriter(new FileOutputStream("D:\\sample-xml-file_2.xml"), new FileInputStream("D:\\workarea\\personal\\latestcvs\\batchserver\\src\\org\\jmonks\\batchserver\\io\\xml\\sample-xml-file-spec.xml")); // 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 & Details"); 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(); } } --- 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.batch.io.xml; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Collection; 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.batch.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.batch.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.batch.io.FileSpec fileSpec=org.jmonks.batch.io.FileSpec.createFileSpec(TestStax.class.getResourceAsStream("sample-xml-file-spec.xml")); TestStax stax=new TestStax("C:\\sample-xml-file.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() { Collection 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.batch.io.ReaderRecord { private Map fieldMap=null; private XMLReaderRecord(org.jmonks.batch.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(); } } } |