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.dav.request;
14  
15  import org.abstracthorizon.danube.webdav.xml.XMLParserHandler;
16  import org.abstracthorizon.danube.webdav.xml.dav.HRef;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  import org.xml.sax.Attributes;
22  import org.xml.sax.SAXException;
23  
24  /**
25   * This class models WebDAV's keepalive tag
26   *
27   * @author Daniel Sendula
28   */
29  public class KeepAlive extends AbstractSimpleXMLHandler {
30  
31      /** Hrefs */
32      protected List<HRef> hrefs;
33  
34      /** Value */
35      protected String value;
36  
37      /**
38       * Constructor
39       * @param parent parent parser handler
40       */
41      public KeepAlive(XMLParserHandler parent) {
42          super(parent);
43      }
44  
45      @Override
46      public Object start(Object current, String tag, Attributes attributes) throws SAXException {
47          if ("href".equals(tag)) {
48              if (hrefs == null) {
49                  hrefs = new ArrayList<HRef>();
50              }
51              HRef href = new HRef(this);
52              hrefs.add(href);
53              return href;
54          }
55          return this;
56      }
57  
58      @Override
59      public Object end(Object current, String tag, String value) throws SAXException {
60          if (hrefs == null) {
61              this.value = value;
62          }
63          return super.end(current, tag, value);
64      }
65  
66      @Override
67      public String toString() {
68          StringBuffer result = new StringBuffer("KeepAlive[");
69          if (hrefs == null) {
70              if (value == null) {
71                  result.append(']');
72              } else {
73                  result.append(value).append(']');
74              }
75          } else {
76              boolean first = true;
77              for (HRef href : hrefs) {
78                  if (first) {
79                      first = false;
80                  } else {
81                      result.append(',');
82                  }
83                  result.append(href.toString());
84              }
85              result.append(']');
86          }
87          return result.toString();
88      }
89  }