KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > webdav > lib > BaseProperty


1 /*
2  * $Header: /home/cvs/jakarta-slide/webdavclient/clientlib/src/java/org/apache/webdav/lib/BaseProperty.java,v 1.5 2004/08/02 15:45:49 unico Exp $
3  * $Revision: 1.5 $
4  * $Date: 2004/08/02 15:45:49 $
5  *
6  * ====================================================================
7  *
8  * Copyright 1999-2002 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */

23
24 package org.apache.webdav.lib;
25
26 import java.io.StringWriter JavaDoc;
27 import java.util.List JavaDoc;
28
29 import org.apache.webdav.lib.util.DOMUtils;
30 import org.apache.webdav.lib.util.PropertyWriter;
31 import org.jdom.input.DOMBuilder;
32 import org.jdom.output.Format;
33 import org.jdom.output.XMLOutputter;
34 import org.w3c.dom.Element JavaDoc;
35
36 /**
37  * This interface models a DAV property.
38  *
39  * @version $Revision: 1.5 $
40  */

41 public class BaseProperty implements Property {
42
43
44     // ----------------------------------------------------------- Constructors
45

46
47     /**
48      * Default constructor for the property.
49      */

50     public BaseProperty(ResponseEntity response, Element JavaDoc element) {
51         this.element = element;
52         this.response = response;
53     }
54
55
56     // ----------------------------------------------------- Instance Variables
57

58
59     /**
60      * Associated response entity.
61      */

62     protected ResponseEntity response;
63
64
65     /**
66      * Associated node element.
67      */

68     protected Element JavaDoc element;
69
70
71     // --------------------------------------------------------- Public Methods
72

73
74     /**
75      * This method returns the full name of the property. Thus, for example,
76      * calling this method on a property such as
77      * <code>&lt;D:getlastmodified&gt;Tue, 05 Dec 2000
78      * 05:25:02&lt;/D:getlastmodified&gt;</code> returns
79      * <code>D:getlastmodified</code>.
80      */

81     public String JavaDoc getName() {
82         return element.getTagName();
83     }
84
85
86     /**
87      * This method returns the local name of the property. Thus, for example,
88      * calling this method on a property such as
89      * <code>&lt;D:getlastmodified&gt;Tue, 05 Dec 2000
90      * 05:25:02&lt;/D:getlastmodified&gt;</code> returns
91      * <code>getlastmodified</code>.
92      */

93     public String JavaDoc getLocalName() {
94         return DOMUtils.getElementLocalName(element);
95     }
96
97
98     /**
99      * This method returns the namespace of the property. Thus, for example,
100      * calling this method on a property such as
101      * <code>&lt;D:getlastmodified&gt;Tue, 05 Dec 2000
102      * 05:25:02&lt;/D:getlastmodified&gt;</code> returns
103      * <code>DAV:</code>.
104      */

105     public String JavaDoc getNamespaceURI() {
106         return DOMUtils.getElementNamespaceURI(element);
107     }
108
109
110     /**
111      * This method returns the property as a DOM Element.
112      */

113     public Element JavaDoc getElement() {
114         return element;
115     }
116
117
118     /**
119      * This method returns the value of the property. Thus, for example,
120      * calling this method on a property such as
121      * <code>&lt;D:getlastmodified&gt;Tue, 05 Dec 2000
122      * 05:25:02&lt;/D:getlastmodified&gt;</code> returns
123      * <code>Tue, 05 Dec 2000 05:25:02</code>.<br/>
124      * Note: Mixed content (text and xml together) will not be returned
125      * accurately.
126      */

127     public String JavaDoc getPropertyAsString() {
128         StringBuffer JavaDoc text = new StringBuffer JavaDoc();
129         DOMBuilder builder = new DOMBuilder();
130         XMLOutputter outputter = new XMLOutputter( Format.getPrettyFormat() );
131         org.jdom.Element e = builder.build( element );
132         List JavaDoc children = e.getChildren();
133         if ( children.size() > 0 ) {
134             text.append( outputter.outputString( children ) );
135         }
136         text.append( e.getTextTrim() );
137         return text.toString();
138     }
139
140
141     /**
142      * This method returns the status code associated with the property.
143      */

144     public int getStatusCode() {
145         // A response can have multiple propstat elements each with
146
// their own status, return the (mandatory) status before asking
147
// the resoponse for its status
148

149         // <multistatus xmlns=\DAV:\>
150
// <response>
151
// <href>/slide/files/</href>
152
// <propstat>
153
// <prop><displayname>files</displayname></prop>
154
// <status>HTTP/1.1 200 OK</status>
155
// </propstat>
156
// <propstat>
157
// <prop><displayname>files</displayname></prop>
158
// <status>HTTP/1.1 200 OK</status>
159
// </propstat>
160
// </response>
161
// </multistatus>
162

163         Element JavaDoc status = DOMUtils.getFirstElement(element.getParentNode().getParentNode(),"DAV:", "status");
164         if (status != null) {
165             return DOMUtils.parseStatus(DOMUtils.getTextValue(status));
166         }
167
168         return response.getStatusCode();
169     }
170
171
172     /**
173      * This method returns URL file path of the resource to which this
174      * property belongs.
175      */

176     public String JavaDoc getOwningURL() {
177         return response.getHref();
178     }
179
180
181     /**
182      * Get a String representation of the property.
183      */

184     public String JavaDoc toString () {
185         StringWriter JavaDoc tmp = new StringWriter JavaDoc();
186         PropertyWriter propertyWriter = new PropertyWriter(tmp, true);
187         propertyWriter.print(element);
188         return tmp.getBuffer().toString();
189     }
190
191
192 }
193
Popular Tags