[Batchserver-cvs] batchserver/src/org/jmonks/batchserver/io/xml package.html, NONE, 1.1 PrettyXMLIn
Brought to you by:
suresh_pragada
From: Suresh <sur...@us...> - 2006-06-13 04:02:36
|
Update of /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/xml In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv29953 Modified Files: XMLFileWriter.java Added Files: package.html PrettyXMLIndentationEngine.java XMLIndentationEngine.java Log Message: no message --- NEW FILE: XMLIndentationEngine.java --- /* * XMLIndentationEngine.java * * Created on June 12, 2006, 10:01 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; /** * <p> * XMLIndentationEngine returns the indentation information to be written to the stream * based on events received. It receives the events before start and end tags being written * to the stream. * * @author Suresh Pragada * @version 1.0 * @since 1.0 * </p> */ public interface XMLIndentationEngine { /** * Returns the indentation string needs to be written to tbe stream before * the start element being written. */ public String startElement(); /** * Returns the indentation string needs to be written to tbe stream before * the element element being written. */ public String endElement(); } Index: XMLFileWriter.java =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/xml/XMLFileWriter.java,v retrieving revision 1.7 retrieving revision 1.8 diff -C2 -d -r1.7 -r1.8 *** XMLFileWriter.java 12 Jun 2006 20:59:02 -0000 1.7 --- XMLFileWriter.java 13 Jun 2006 04:02:33 -0000 1.8 *************** *** 11,14 **** --- 11,15 ---- package org.jmonks.batchserver.io.xml; import java.io.OutputStream; + import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; *************** *** 51,55 **** * Egnine to be used for the indentation. */ ! private XMLIndentationEngine indentationEngine=new XMLIndentationEngine(); private static Logger logger=Logger.getLogger(XMLFileWriter.class); --- 52,56 ---- * Egnine to be used for the indentation. */ ! private XMLIndentationEngine indentationEngine=new PrettyXMLIndentationEngine(); private static Logger logger=Logger.getLogger(XMLFileWriter.class); *************** *** 71,75 **** logger.debug("Writer has been created."); writer.writeStartDocument("ISO-8859-1", "1.0"); ! indentationEngine.startElement(); writer.writeStartElement(this.fileSpec.rootElement); } --- 72,76 ---- logger.debug("Writer has been created."); writer.writeStartDocument("ISO-8859-1", "1.0"); ! writer.writeCharacters(indentationEngine.startElement()); writer.writeStartElement(this.fileSpec.rootElement); } *************** *** 101,104 **** --- 102,107 ---- { XMLWriterRecord record=(XMLWriterRecord)writerRecord; + if(record.isNestedElementRecord()) + throw new IllegalArgumentException("Writer record should be obtained from the file writer to write into the file."); XMLRecordSpec recordSpec=(XMLRecordSpec)this.fileSpec.getRecordSpec(record.getRecordType()); String recordXpath=recordSpec.getRecordXPath(); *************** *** 128,135 **** protected void writeSimpleElement(String fieldName,Object fieldValue) throws XMLStreamException { ! indentationEngine.startElement(); writer.writeStartElement(fieldName); writer.writeCharacters((fieldValue!=null)?fieldValue.toString():""); ! indentationEngine.endElement(); writer.writeEndElement(); } --- 131,138 ---- protected void writeSimpleElement(String fieldName,Object fieldValue) throws XMLStreamException { ! writer.writeCharacters(indentationEngine.startElement()); writer.writeStartElement(fieldName); writer.writeCharacters((fieldValue!=null)?fieldValue.toString():""); ! writer.writeCharacters(indentationEngine.endElement()); writer.writeEndElement(); } *************** *** 143,147 **** protected void writeComplexElement(String recordElementName,XMLWriterRecord fieldValueAsRecord) throws XMLStreamException { ! indentationEngine.startElement(); writer.writeStartElement(recordElementName); for(Iterator iterator=fieldValueAsRecord.getFieldNameIterator();iterator.hasNext();) --- 146,150 ---- protected void writeComplexElement(String recordElementName,XMLWriterRecord fieldValueAsRecord) throws XMLStreamException { ! writer.writeCharacters(indentationEngine.startElement()); writer.writeStartElement(recordElementName); for(Iterator iterator=fieldValueAsRecord.getFieldNameIterator();iterator.hasNext();) *************** *** 162,166 **** } } ! indentationEngine.endElement(); writer.writeEndElement(); } --- 165,169 ---- } } ! writer.writeCharacters(indentationEngine.endElement()); writer.writeEndElement(); } *************** *** 287,290 **** --- 290,295 ---- public void writeField(String fieldName, Object fieldValue) { + if(fieldName==null) + throw new IllegalArgumentException("Field name cannot be null to write the field."); this.fieldMap.put(fieldName, fieldValue); } *************** *** 295,326 **** public void writeSimpleElement(String fieldName, String fieldValue) { this.fieldMap.put(fieldName, fieldValue); } /** ! * Write nested/composite element into the record. */ ! public void writeComplexElement(String fieldName, WriterRecord fieldValue) { ! this.fieldMap.put(fieldName, fieldValue); } /** ! * 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 writeRepeatElement(String fieldName, List fieldValue) ! { ! this.fieldMap.put(fieldName, fieldValue); ! } ! ! /** ! * Returns */ ! public WriterRecord createComplexElementRecord() { ! return new XMLWriterRecord(super.getRecordType(), true); } --- 300,336 ---- public void writeSimpleElement(String fieldName, String fieldValue) { + if(fieldName==null) + throw new IllegalArgumentException("Field name cannot be null to write simple element."); this.fieldMap.put(fieldName, fieldValue); } /** ! * Create complex element and add it to the record and returns the writer ! * record instance to which all the elements can be adde. */ ! public WriterRecord createComplexElement(String fieldName) { ! if(fieldName==null) ! throw new IllegalArgumentException("Field name cannot be null to create the complex element."); ! WriterRecord complexElement=new XMLWriterRecord(super.getRecordType(), true); ! this.fieldMap.put(fieldName, complexElement); ! return complexElement; } /** ! * Creates the list and it to the record with the given field name ! * and returns the list to which values can be added. * * @param fieldName Name of the field repated more than once. ! * ! * @return Returns the list. */ ! public List writeRepeatElement(String fieldName) { ! if(fieldName==null) ! throw new IllegalArgumentException("Field name cannot be null to create the repeat element."); ! List repeatElement=new ArrayList(); ! this.fieldMap.put(fieldName, repeatElement); ! return repeatElement; } *************** *** 342,398 **** } - /** - * <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 - { - private boolean isPrevStartElement=false; - private boolean isPrevEndElement=false; - - private String indentationString="\n"; - - private XMLIndentationEngine() - { - } - - /** - * Receives before element is being writting to the stream. - */ - private void startElement() throws XMLStreamException - { - isPrevEndElement=false; - if(isPrevStartElement) - { - this.indentationString+=" "; - writer.writeCharacters(this.indentationString); - } - else - { - isPrevStartElement=true; - writer.writeCharacters(indentationString); - } - } - /** - * Receives before end element is being writting to the stream. - */ - 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; - } - } - } } --- 352,354 ---- --- NEW FILE: package.html --- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>XML File Implementation Overview</title> </head> <body> Explain the usage of XML File IO API. </body> </html> --- NEW FILE: PrettyXMLIndentationEngine.java --- /* * PrettyXMLIndentationEngine.java * * Created on June 12, 2006, 10:06 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; /** * <p> * PrettyXMLIndentationEngine writes the the elements in XML in the folloing manner. * <br> * <pre> * <root> * <element1>element-data1</element1> * <element2> * <element3>element-data3</element> * <element4>element-data4</element> * </element2> * </root> * </pre> * </p> * * @author Suresh Pragada * @version 1.0 * @since 1.0 */ public class PrettyXMLIndentationEngine implements XMLIndentationEngine { /** * Remembers previous element is start element. */ private boolean isPrevStartElement=false; /** * Remembers previous element is end element. */ private boolean isPrevEndElement=false; private String indentationString="\n"; public PrettyXMLIndentationEngine() { } /** * @see org.jmonks.batchserver.io.xml.XMLIndentationEngine#startElement */ public String startElement() { isPrevEndElement=false; if(isPrevStartElement) { this.indentationString+=" "; return this.indentationString; } else { isPrevStartElement=true; return this.indentationString; } } /** * @see org.jmonks.batchserver.io.xml.XMLIndentationEngine#endElement */ public String endElement() { isPrevStartElement=false; if(isPrevEndElement) { this.indentationString=this.indentationString.substring(0, this.indentationString.length()-4); return this.indentationString; } else { isPrevEndElement=true; return ""; } } } |