Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
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
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
00067 _parser.setProperty("http://xml.org/sax/properties/lexical-handler", this);
00068 }
00069
00070
00071 void read(InputSource source) throws SAXException, IOException
00072 {
00073
00074 _parser.parse(source, this);
00075
00076
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;
00096
00097
00098 public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException
00099 {
00100
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()) {
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
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
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
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
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())
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
00179 _content += XMLHelper.encodeXMLString(new String(ch, start, length));
00180
00181 if (_inCDATASection)
00182 _isContentCDATA = true;
00183 }
00184
00185
00186
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 }