Update deps to Funambol 8.7
[jgroupdav.git] / src / main / java / net / bionicmessage / groupdav / SAXDAVHandler.java
blobd2fd34d21f278de53053a726da85443e9ac8e3db
1 /*
2 * JGroupDAV library
3 * SAXDAVHandler.java created 19th October 2008
4 * Copyright 2008-2010 Mathew McBride
5 *
6 * Redistribution and use in source and binary forms, with or without modification, are
7 * permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright notice, this list of
10 * conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
13 * of conditions and the following disclaimer in the documentation and/or other materials
14 * provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY Mathew McBride ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
18 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> OR
19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
22 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
23 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 * The views and conclusions contained in the software and documentation are those of the
27 * authors and should not be interpreted as representing official policies, either expressed
28 * or implied, of Mathew McBride.
30 package net.bionicmessage.groupdav;
32 import java.util.HashMap;
33 import java.util.Map;
34 import java.util.Stack;
35 import org.xml.sax.Attributes;
36 import org.xml.sax.InputSource;
37 import org.xml.sax.SAXException;
38 import org.xml.sax.SAXParseException;
39 import org.xml.sax.XMLReader;
40 import org.xml.sax.helpers.DefaultHandler;
41 import org.xml.sax.helpers.XMLReaderFactory;
43 /** A SAX handler class for Cal/Card/GroupDAV PROPFIND
44 * and REPORT operations.
45 * @author matt
47 public class SAXDAVHandler extends DefaultHandler {
49 protected Stack<String> hrefs;
50 protected Stack<String> etags;
51 protected Stack<String> contents;
52 protected Stack<String> types;
53 protected StringBuffer chars;
54 protected IDAVHandler callback;
55 protected Map<String, String> cachedEtagMap;
57 /**
58 * Creates a handler
59 * @param parent
61 public SAXDAVHandler(IDAVHandler parent) {
62 hrefs = new Stack();
63 etags = new Stack();
64 contents = new Stack();
65 types = new Stack();
66 callback = parent;
69 /** Creates a handler and handles the parsing of XML from the provided source */
70 public SAXDAVHandler(IDAVHandler parent, InputSource source) throws Exception {
71 this(parent);
72 XMLReader p = XMLReaderFactory.createXMLReader();
73 p.setContentHandler(this);
74 p.parse(source);
77 @Override
78 public void startDocument() throws SAXException {
79 super.startDocument();
80 callback.setReady(false);
83 @Override
84 public void startElement(String namespaceURI, String localName,
85 String qualifiedName, Attributes atts) throws SAXException {
86 super.startElement(namespaceURI, localName, localName, atts);
87 chars = new StringBuffer();
90 @Override
91 public void endElement(String uri, String localName, String qName) throws SAXException {
92 if (localName.equals("getetag")) {
93 etags.push(chars.toString());
94 } else if (localName.equals("href")) {
95 hrefs.push(chars.toString());
96 } else if (localName.equals("address-data") || localName.equals("calendar-data")) {
97 // replace '\n' with '\r\n' because Xerces wants to remove it. BAD BAD
98 String ch = chars.toString();
99 if (ch.contains("\n") && !ch.contains("\r")) {
100 ch = ch.replace("\n", "\r\n");
102 contents.push(ch);
104 if (localName.contains("collection") && uri.contains("groupdav.org")) {
105 types.push(localName);
106 } else if (localName.contains("getcontenttype")) {
107 types.push("obj");
108 } else if (localName.contains("response")) {
109 if (hrefs.size() != etags.size()) {
110 etags.push("");
112 if (types.size() != etags.size()) {
113 types.push("");
116 super.endElement(uri, localName, qName);
119 @Override
120 public void characters(char[] ch, int start, int length) throws SAXException {
121 chars.append(ch, start, length);
124 @Override
125 public void endDocument() throws SAXException {
126 super.endDocument();
127 callback.setReady(true);
130 @Override
131 public void error(SAXParseException e) throws SAXException {
132 callback.setReady(true);
133 callback.setRetException(e);
136 public Stack<String> getContents() {
137 return contents;
140 public Stack<String> getEtags() {
141 return etags;
144 public Stack<String> getHrefs() {
145 return hrefs;
148 public Stack<String> getTypes() {
149 return types;
152 public Map<String, String> getEtagMap() {
153 if (cachedEtagMap == null) {
154 cachedEtagMap = new HashMap(etags.size());
155 for (int i = 0; i < hrefs.size(); i++) {
156 String href = hrefs.get(i);
157 String etag = etags.get(i);
158 cachedEtagMap.put(href, etag);
161 return cachedEtagMap;