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;
14  
15  import org.abstracthorizon.danube.webdav.xml.XMLParserHandler;
16  import org.abstracthorizon.danube.webdav.xml.dav.request.properties.RequestProperty;
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 parses request properties
26   *
27   * @author Daniel Sendula
28   */
29  public class RequestProp extends DAVAbstractXMLParser {
30  
31      /** Undefined action */
32      public static final int UNDEFINED = 0;
33  
34      /** Set property */
35      public static final int SET = 1;
36  
37      /** Remove property */
38      public static final int REMOVE = 2;
39  
40      /** List of properties */
41      protected List<RequestProperty> properties;
42  
43      /** Temporary storage for a property type */
44      protected int type = UNDEFINED;
45  
46      /**
47       * Constructor
48       */
49      protected RequestProp() {
50      }
51  
52      /**
53       * Constructor
54       * @param parent parent parser handler
55       * @param davFactory factory
56       */
57      public RequestProp(XMLParserHandler parent, DAVFactory davFactory) {
58          super(parent, davFactory);
59      }
60  
61      /**
62       * Constructor
63       * @param parent parent parser handler
64       * @param davFactory factory
65       * @param set type of property - {@link #SET} or {@link #REMOVE}
66       */
67      public RequestProp(XMLParserHandler parent, DAVFactory davFactory, boolean set) {
68          super(parent, davFactory);
69          if (set) {
70              type = SET;
71          } else {
72              type = REMOVE;
73          }
74      }
75  
76      @Override
77      public Object start(Object current, String tag, Attributes attributes) throws SAXException {
78          RequestProperty property = null;
79          if ("prop".equals(tag)) {
80  
81          } else if ("creationdate".equals(tag)) {
82              property =  davFactory.newCreationDate(this);
83          } else if ("displayname".equals(tag)) {
84              property = davFactory.newDisplayName(this);
85          } else if ("getcontentlanguage".equals(tag)) {
86              property = davFactory.newGetContentLanguage(this);
87          } else if ("getcontentlength".equals(tag)) {
88              property = davFactory.newGetContentLength(this);
89          } else if ("getcontenttype".equals(tag)) {
90              property = davFactory.newGetContentType(this);
91          } else if ("getetag".equals(tag)) {
92              property = davFactory.newGetETag(this);
93          } else if ("getlastmodified".equals(tag)) {
94              property = davFactory.newGetLastModified(this);
95          } else if ("lockdiscovery".equals(tag)) {
96              property = davFactory.newLockDiscovery(this);
97          } else if ("resourcetype".equals(tag)) {
98              property = davFactory.newResourceType(this);
99          } else if ("source".equals(tag)) {
100             property = davFactory.newSource(this);
101         }
102         if (property != null) {
103             List<RequestProperty> properties = getProperties();
104             properties.add(property);
105             return property;
106         } else {
107             return super.start(current, tag, attributes);
108         }
109     }
110 
111     @Override
112     public Object end(Object current, String tag, String value) throws SAXException {
113         if ("prop".equals(tag)) {
114             return super.end(current, tag, value);
115         } else if ("set".equals(tag) || "remove".equals(tag)) {
116             return super.end(current, tag, value);
117         } else {
118             return current;
119         }
120     }
121 
122     /**
123      * Returns type
124      * @return type
125      */
126     public int getType() {
127         return type;
128     }
129 
130     /**
131      * Returns list of request properties
132      * @return list of request properties
133      */
134     public List<RequestProperty> getProperties() {
135         if (properties == null) {
136             properties = new ArrayList<RequestProperty>();
137         }
138         return properties;
139     }
140 
141     @Override
142     public String toString() {
143         if (type == UNDEFINED) {
144             StringBuffer result = new StringBuffer("Prop[");
145             if (properties != null) {
146                 boolean first = true;
147                 for (RequestProperty property : properties) {
148                     if (first) {
149                         first = false;
150                     } else {
151                         result.append(',');
152                     }
153                     result.append(property.toString());
154                 }
155             }
156             result.append(']');
157             return result.toString();
158         } else {
159             StringBuffer result = new StringBuffer();
160             if (type == SET) {
161                 result.append("Set[Prop[");
162             } else {
163                 result.append("Remove[Prop[");
164             }
165             if (properties != null) {
166                 boolean first = true;
167                 for (RequestProperty property : properties) {
168                     if (first) {
169                         first = false;
170                     } else {
171                         result.append(',');
172                     }
173                     result.append(property.toString());
174                 }
175             }
176             result.append("]]");
177             return result.toString();
178         }
179     }
180 
181 }