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.response;
14  
15  import org.abstracthorizon.danube.webdav.util.NamespacesProvider;
16  import org.abstracthorizon.danube.webdav.xml.XMLRenderer;
17  import org.abstracthorizon.danube.webdav.xml.common.XMLUtils;
18  import org.abstracthorizon.danube.webdav.xml.dav.DAVNamespace;
19  import org.abstracthorizon.danube.webdav.xml.dav.response.properties.ResponseProperty;
20  
21  import java.io.PrintWriter;
22  import java.util.ArrayList;
23  import java.util.List;
24  
25  /**
26   * This class models WebDAV's response prop tag.
27   *
28   * @author Daniel Sendula
29   */
30  public class ResponseProp implements XMLRenderer {
31  
32      /** Tag name */
33      public static final String TAG_NAME = "prop";
34  
35      /** List of properties */
36      protected List<ResponseProperty> properties;
37  
38      /**
39       * Constructor
40       */
41      public ResponseProp() {
42      }
43  
44      /**
45       * Returns a list of properties. It will never be null.
46       * @return a list of properties
47       */
48      public List<ResponseProperty> getProperties() {
49          if (properties == null) {
50              properties = new ArrayList<ResponseProperty>();
51          }
52          return properties;
53      }
54  
55      @Override
56      public String toString() {
57          StringBuffer result = new StringBuffer("Prop[");
58          if (properties != null) {
59              boolean first = true;
60              for (ResponseProperty property : properties) {
61                  if (first) {
62                      first = false;
63                  } else {
64                      result.append(',');
65                  }
66                  result.append(property.toString());
67              }
68          }
69          result.append(']');
70          return result.toString();
71      }
72  
73      /**
74       * Renders the tag
75       * @param writer writer
76       * @param provider namespace provider
77       */
78      public void render(PrintWriter writer, NamespacesProvider provider) {
79          writer.println(XMLUtils.createStartTag(provider, DAVNamespace.DAV_NAMESPACE_URL, TAG_NAME));
80          if (properties != null) {
81              for (ResponseProperty property : properties) {
82                  property.render(writer, provider);
83              }
84          }
85          writer.println(XMLUtils.createEndTag(provider, DAVNamespace.DAV_NAMESPACE_URL, TAG_NAME));
86      }
87  
88      /**
89       * Renders the tag with all namespaces. It is used as partial response xml.
90       *
91       * @param writer writer
92       * @param provider namespace provider
93       */
94      public void renderWithNamespaces(PrintWriter writer, NamespacesProvider provider) {
95          writer.print(XMLUtils.createStartOpenTag(provider, DAVNamespace.DAV_NAMESPACE_URL, TAG_NAME));
96          writer.print(' ');
97          MultiStatus.renderNamespaces(writer, provider);
98          writer.println('>');
99          if (properties != null) {
100             for (ResponseProperty property : properties) {
101                 property.render(writer, provider);
102             }
103         }
104         writer.println(XMLUtils.createEndTag(provider, DAVNamespace.DAV_NAMESPACE_URL, TAG_NAME));
105     }
106 }