XMLDocReader.java

Go to the documentation of this file.
00001 
00002 //
00003 // XML storage classes version 1.3
00004 //
00005 // XMLDocReader.java
00006 //
00007 // Copyright (c) 2004, 2005, 2007, 2008 Martin Fuchs <martin-fuchs@gmx.net>
00008 //
00009 
00010 
00011 /*
00012 
00013  All rights reserved.
00014 
00015  Redistribution and use in source and binary forms, with or without
00016  modification, are permitted provided that the following conditions are met:
00017 
00018  * Redistributions of source code must retain the above copyright
00019     notice, this list of conditions and the following disclaimer.
00020  * Redistributions in binary form must reproduce the above copyright
00021     notice, this list of conditions and the following disclaimer in
00022     the documentation and/or other materials provided with the
00023     distribution.
00024 
00025  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00026  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00028  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00029  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00030  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00031  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00032  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00033  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00034  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00035  POSSIBILITY OF SUCH DAMAGE.
00036 
00037 */
00038 
00039 package de.mfuchs.xmlstorage;
00040 
00041 import java.io.IOException;
00042 
00043 import javax.xml.parsers.ParserConfigurationException;
00044 import javax.xml.parsers.SAXParser;
00045 import javax.xml.parsers.SAXParserFactory;
00046 
00047 import org.xml.sax.Attributes;
00048 import org.xml.sax.InputSource;
00049 import org.xml.sax.SAXException;
00050 import org.xml.sax.ext.DefaultHandler2;
00051 
00052 
00053 /**
00054  * XMLDocReader is the XML file reader of XMLStorage.
00055  */
00056 final class XMLDocReader extends DefaultHandler2
00057 {
00058     XMLDocReader(XMLNode node) throws ParserConfigurationException, SAXException
00059     {
00060         _pos = new XMLPos(node);
00061         _lastTag = TAG_NONE;
00062 
00063         SAXParserFactory factory = SAXParserFactory.newInstance();
00064         _parser = factory.newSAXParser();
00065 
00066          // activate CDATA notifications
00067         _parser.setProperty("http://xml.org/sax/properties/lexical-handler", this);
00068     }
00069 
00070      /// read XML stream into XML tree below _pos
00071     void read(InputSource source) throws SAXException, IOException
00072     {
00073          // read XML data
00074         _parser.parse(source, this);
00075 
00076          // process last data pieces
00077         if (_pos.cur()._children.isEmpty())
00078             _pos.cur()._trailing += _content;
00079         else
00080             _pos.cur()._children.getLast()._trailing += _content;
00081 
00082         _content = "";
00083         _isContentCDATA = false;
00084     }
00085 
00086     private XMLPos  _pos;
00087     private SAXParser _parser;
00088 
00089     private String  _content = "";
00090     private StringBuffer _instructions = new StringBuffer(); 
00091  
00092     private static final int TAG_NONE = 0;
00093     private static final int TAG_START = 1;
00094     private static final int TAG_END = 2;
00095     private int _lastTag;   // enum
00096 
00097 
00098     public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException
00099     {
00100          // search for content end leaving only whitespaces for _end_leading
00101         int i = _content.length();
00102         for (; i>0; i--) {
00103             char ch = _content.charAt(i-1);
00104             if (!Character.isWhitespace(ch))
00105                 break;
00106         }
00107 
00108         if (i > 0)
00109             if (_pos.cur()._children.isEmpty()) {   // no children in last node?
00110                 if (_lastTag == TAG_START)
00111                     _pos.cur()._content += _content.substring(0, i);
00112                 else if (_lastTag == TAG_END)
00113                     _pos.cur()._trailing += _content.substring(0, i);
00114                 // else TAG_NONE -> don't store white space in root node
00115             } else
00116                 _pos.cur()._children.getLast()._trailing += _content.substring(0, i);
00117 
00118         String leading = "";
00119 
00120         if (i < _content.length())
00121             leading = _content.substring(i);
00122 
00123         XMLNode node = new XMLNode(qName, leading);
00124         //XMLNode node = new XMLNode(XMLHelper.String_from_XML_Char(qName), leading);
00125 
00126         _pos.addDown(node);
00127 
00128         for(int n=0; n<attributes.getLength(); ++n) {
00129             final String attrName = attributes.getQName(n);
00130             final String attrValue = attributes.getValue(n);
00131 
00132             node._attributes.put(attrName, attrValue);
00133             //node.put(XMLHelper.String_from_XML_Char(attrName), XMLHelper.String_from_XML_Char(attr_value));
00134         }
00135 
00136         _lastTag = TAG_START;
00137         _content = "";
00138         _isContentCDATA = false;
00139     }
00140 
00141     public void endElement(String uri, String localName, String qName) throws SAXException
00142     {
00143         int i;
00144 
00145         if (_isContentCDATA)
00146             i = _content.length();
00147         else {
00148              // search for content end leaving only whitespaces for leading
00149             i = _content.length();
00150             for (; i>0; i--) {
00151                 char ch = _content.charAt(i-1);
00152                 if (!Character.isWhitespace(ch))
00153                     break;
00154             }
00155         }
00156 
00157         if (i > 0)
00158             if (_pos.cur()._children.isEmpty()) // no children in current node?
00159                 _pos.cur()._content += _content.substring(0, i);
00160             else
00161                 if (_lastTag == TAG_START)
00162                     _pos.cur()._content += _content.substring(0, i);
00163                 else
00164                     _pos.cur()._children.getLast()._trailing += _content.substring(0, i);
00165 
00166         if (i < _content.length())
00167             _pos.cur()._endLeading = _content.substring(i);
00168 
00169         _pos.back();
00170 
00171         _lastTag = TAG_END;
00172         _content = "";
00173         _isContentCDATA = false;
00174     }
00175 
00176     public void characters(char[] ch, int start, int length) throws SAXException
00177     {
00178          // encode _content compatible to XMLNode.setContent()
00179         _content += XMLHelper.encodeXMLString(new String(ch, start, length));
00180 
00181         if (_inCDATASection)
00182             _isContentCDATA = true;
00183     }
00184 
00185 
00186      // CDATA-Handling
00187 
00188     private boolean _inCDATASection = false;
00189     private boolean _isContentCDATA = false;
00190 
00191     public void startCDATA() throws SAXException
00192     {
00193         _inCDATASection = true;
00194     }
00195 
00196     public void endCDATA() throws SAXException
00197     {
00198         _inCDATASection = false;
00199     }
00200 
00201 
00202     public void processingInstruction(String target, String data)
00203         throws SAXException
00204     {
00205         _instructions.append("<?").append(target)
00206             .append(" ").append(data).append("?>");
00207     }
00208 
00209     public String getInstructions()
00210     {
00211         return _instructions.toString();
00212     }
00213 }