View Javadoc

1   /*
2    * Copyright (c) 2006-2007 Creative Sphere Limited.
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the Eclipse Public License v1.0
5    * which accompanies this distribution, and is available at
6    * http://www.eclipse.org/legal/epl-v10.html
7    *
8    * Contributors:
9    *
10   *   Creative Sphere - initial API and implementation
11   *
12   */
13  package org.abstracthorizon.danube.webdav.xml;
14  
15  
16  import org.abstracthorizon.danube.webdav.util.NamespacesProvider;
17  
18  import java.io.IOException;
19  
20  import org.xml.sax.Attributes;
21  import org.xml.sax.InputSource;
22  import org.xml.sax.SAXException;
23  import org.xml.sax.helpers.DefaultHandler;
24  
25  /**
26   * WebDAV XML handler
27   *
28   * @author Daniel Sendula
29   */
30  public class WebDAVXMLHandler extends DefaultHandler {
31  
32      /** Current object */
33      protected Object currentObject;
34  
35      /** Namespace provider */
36      protected NamespacesProvider namespacesProvider;
37  
38      /** String buffer for tag values */
39      protected StringBuffer buffer = new StringBuffer();
40  
41      /**
42       * Constructor
43       * @param namespacesProvider namespace provider
44       */
45      public WebDAVXMLHandler(NamespacesProvider namespacesProvider) {
46          this.namespacesProvider = namespacesProvider;
47      }
48  
49      /**
50       * Constructor
51       * @param namespacesProvider namespace provider
52       * @param startObject start object
53       */
54      public WebDAVXMLHandler(NamespacesProvider namespacesProvider, Object startObject) {
55          this.currentObject = startObject;
56          this.namespacesProvider = namespacesProvider;
57      }
58  
59      /**
60       * Returns resulted object
61       * @return resulted object or <code>null</code>
62       */
63      public Object getResultObject() {
64          return currentObject;
65      }
66  
67      @Override
68      public void startPrefixMapping(String prefix, String uri) throws SAXException {
69      }
70  
71      @Override
72      public void endPrefixMapping(String prefix) throws SAXException {
73      }
74  
75      @Override
76      public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
77          XMLParserHandler handler = namespacesProvider.getParserHandler(uri);
78          if (handler != null) {
79              buffer.delete(0, buffer.length());
80              currentObject = handler.start(currentObject, localName, attributes);
81          }
82      }
83  
84      @Override
85      public void endElement(String uri, String localName, String qName) throws SAXException {
86          XMLParserHandler handler = namespacesProvider.getParserHandler(uri);
87          if (handler != null) {
88              currentObject = handler.end(currentObject, localName, buffer.toString().trim());
89              buffer.delete(0, buffer.length());
90          }
91      }
92  
93      @Override
94      public InputSource resolveEntity(String publicId, String systemId) throws IOException, SAXException {
95          return null;
96      }
97  
98      @Override
99      public void characters(char[] chars, int start, int len) throws SAXException {
100         buffer.append(chars, start, len);
101     }
102 }