[Batchserver-cvs] batchserver/src/org/jmonks/batchserver/io/xml sample-xml-file.xml, 1.2, 1.3 XMLFi
Brought to you by:
suresh_pragada
From: Suresh <sur...@us...> - 2006-06-07 22:26:32
|
Update of /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/xml In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv9364 Modified Files: sample-xml-file.xml XMLFileReader.java XMLRecordSpec.java Log Message: no message Index: sample-xml-file.xml =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/xml/sample-xml-file.xml,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** sample-xml-file.xml 5 Jun 2006 03:13:11 -0000 1.2 --- sample-xml-file.xml 5 Jun 2006 22:40:34 -0000 1.3 *************** *** 22,27 **** <sample-nested-field3>sample-nested-value3</sample-nested-field3> </sample-field3> ! <sample-field4>sample-value1</sample-field4> ! <sample-field5> </sample-field5> </sample-detail> <sample-trailor> --- 22,27 ---- <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> Index: XMLFileReader.java =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/xml/XMLFileReader.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** XMLFileReader.java 5 Jun 2006 03:13:11 -0000 1.2 --- XMLFileReader.java 5 Jun 2006 22:40:34 -0000 1.3 *************** *** 12,19 **** --- 12,28 ---- 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; import org.apache.log4j.Logger; import org.jmonks.batchserver.io.*; *************** *** 27,33 **** protected String absoluteFilePath=null; ! protected FileSpec fileSpec=null; ! protected XMLEventReader reader=null; private static Logger logger=Logger.getLogger(XMLFileReader.class); --- 36,44 ---- protected String absoluteFilePath=null; ! protected XMLFileSpec fileSpec=null; ! protected XMLEventReader eventReader=null; ! ! protected String xpath=""; private static Logger logger=Logger.getLogger(XMLFileReader.class); *************** *** 35,54 **** public XMLFileReader(String absoluteFilePath,FileSpec fileSpec) { ! this.absoluteFilePath=absoluteFilePath; ! this.fileSpec=fileSpec; try { XMLInputFactory inputFactory=XMLInputFactory.newInstance(); ! reader=inputFactory.createXMLEventReader(new java.io.FileReader(this.absoluteFilePath)); } catch(FileNotFoundException exception) { exception.printStackTrace(); 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) { exception.printStackTrace(); 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()); --- 46,74 ---- public XMLFileReader(String absoluteFilePath,FileSpec fileSpec) { ! this.absoluteFilePath=absoluteFilePath; ! this.fileSpec=(XMLFileSpec)fileSpec; try { 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."); ! } } catch(FileNotFoundException exception) { 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()); ! } catch(XMLStreamException exception) { 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()); *************** *** 58,72 **** public ReaderRecord getNextRecord() { ! return null; } ! public void close() { ! if(this.reader!=null) { ! this.reader.close(); } } public class XMLReaderRecord extends ReaderRecord { --- 78,267 ---- public 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(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; ! } } } + private boolean validateRootElement() + { + boolean valid=false; + try + { + while(eventReader.hasNext()) + { + XMLEvent event=eventReader.nextEvent(); + if(event.isStartElement()) + { + StartElement startElement=(StartElement)event; + if(this.fileSpec.getRootElement().equalsIgnoreCase(startElement.getName().getLocalPart())) + { + valid=true; + break; + } + } + } + } + catch(XMLStreamException exception) + { + exception.printStackTrace(); + logger.fatal("Exception while validating the root element. Message = " + exception.getMessage(),exception); + throw new FileParseException("Exception while validating the root element. Message = " + exception.getMessage()); + } + 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(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(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; + } + public class XMLReaderRecord extends ReaderRecord { *************** *** 76,95 **** { super(recordType); } public Object readField(String fieldName) { ! return null; } public Object readElement(String elementName) { ! return null; } ! protected void writeElement(String fieldName, String fieldValue) { ! this.fieldMap.put(fieldName,fieldValue); } ! } } --- 271,316 ---- { 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(); ! } ! } ! } Index: XMLRecordSpec.java =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/xml/XMLRecordSpec.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** XMLRecordSpec.java 3 Jun 2006 13:14:42 -0000 1.1 --- XMLRecordSpec.java 5 Jun 2006 22:40:34 -0000 1.2 *************** *** 37,40 **** --- 37,47 ---- return this.recordXPath; } + + public boolean isMatch(String xpath) + { + if(xpath==null) + throw new IllegalArgumentException("xpath cannot be null to match XML record spec."); + return xpath.equalsIgnoreCase(this.recordXPath); + } public static RecordSpec createXMLRecordSpec(final Element recordSpecElement) |