3 * SAXDAVHandler.java created 19th October 2008
4 * Copyright 2008-2010 Mathew McBride
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
;
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.
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
;
61 public SAXDAVHandler(IDAVHandler parent
) {
64 contents
= new Stack();
69 /** Creates a handler and handles the parsing of XML from the provided source */
70 public SAXDAVHandler(IDAVHandler parent
, InputSource source
) throws Exception
{
72 XMLReader p
= XMLReaderFactory
.createXMLReader();
73 p
.setContentHandler(this);
78 public void startDocument() throws SAXException
{
79 super.startDocument();
80 callback
.setReady(false);
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();
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");
104 if (localName
.contains("collection") && uri
.contains("groupdav.org")) {
105 types
.push(localName
);
106 } else if (localName
.contains("getcontenttype")) {
108 } else if (localName
.contains("response")) {
109 if (hrefs
.size() != etags
.size()) {
112 if (types
.size() != etags
.size()) {
116 super.endElement(uri
, localName
, qName
);
120 public void characters(char[] ch
, int start
, int length
) throws SAXException
{
121 chars
.append(ch
, start
, length
);
125 public void endDocument() throws SAXException
{
127 callback
.setReady(true);
131 public void error(SAXParseException e
) throws SAXException
{
132 callback
.setReady(true);
133 callback
.setRetException(e
);
136 public Stack
<String
> getContents() {
140 public Stack
<String
> getEtags() {
144 public Stack
<String
> getHrefs() {
148 public Stack
<String
> getTypes() {
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
;