KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > webdav > client > MultiStatusResponse


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

19 package org.openharmonise.webdav.client;
20
21 import java.io.UnsupportedEncodingException JavaDoc;
22 import java.net.URLDecoder JavaDoc;
23
24 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
25 import javax.xml.parsers.FactoryConfigurationError JavaDoc;
26 import javax.xml.parsers.ParserConfigurationException JavaDoc;
27
28 import org.w3c.dom.Document JavaDoc;
29 import org.w3c.dom.Element JavaDoc;
30 import org.w3c.dom.Node JavaDoc;
31 import org.w3c.dom.NodeList JavaDoc;
32 import org.w3c.dom.Text JavaDoc;
33
34 /**
35  * This class contains all the methods for creating and dealing with
36  * a WebDAV MultiStatus Response.
37  *
38  * @author Matthew Large
39  * @version $Revision: 1.1 $
40  *
41  */

42 public class MultiStatusResponse {
43
44     private int m_nStatus = -1;
45     private String JavaDoc m_sURI = null;
46     private String JavaDoc m_sResponseDescription = null;
47
48     private Element JavaDoc m_elResponseXML = null;
49     
50     public MultiStatusResponse() {
51         super();
52     }
53
54     /**
55      * Returns the HTTP status code for this response.
56      *
57      * @return HTTP status code
58      */

59     public int getStatus() {
60         return this.m_nStatus;
61     }
62
63     /**
64      * Returns that URI for this response.
65      *
66      * @return URI
67      */

68     public String JavaDoc getURI() {
69         try {
70             return URLDecoder.decode(this.m_sURI, "UTF-8");
71         } catch (UnsupportedEncodingException JavaDoc e) {
72             e.printStackTrace();
73         }
74         return null;
75     }
76
77     /**
78      * Returns the response description or null if one was not present.
79      *
80      * @return Response description or null
81      */

82     public String JavaDoc getResponseDescription() {
83         return this.m_sResponseDescription;
84     }
85
86     /**
87      * Returns the DAV Response element that this MultiStatus was created from.
88      *
89      * @return Root DOM Element of the response
90      */

91     public Element JavaDoc getResponseXML() {
92         return this.m_elResponseXML;
93     }
94
95     /**
96      * Populates the MultiStatusResponse from some WebDAV response XML.
97      *
98      * @param elResponse
99      */

100     public void populate(Element JavaDoc elResponse) {
101         this.m_elResponseXML = elResponse;
102         NodeList JavaDoc nl = elResponse.getChildNodes();
103         for (int i = 0; i < nl.getLength(); i++) {
104             if (nl.item(i).getNodeType() == Node.ELEMENT_NODE) {
105                 Element JavaDoc elTemp = (Element JavaDoc) nl.item(i);
106
107                 if (elTemp.getLocalName().equals("href")) {
108                     this.m_sURI = elTemp.getFirstChild().getNodeValue();
109                 } else if (elTemp.getLocalName().equals("status")) {
110                     String JavaDoc sStatus = elTemp.getFirstChild().getNodeValue();
111                     sStatus = sStatus.substring(sStatus.indexOf(" ") + 1);
112                     sStatus = sStatus.substring(0, sStatus.indexOf(" "));
113                     this.m_nStatus = Integer.parseInt(sStatus);
114                 } else if (elTemp.getLocalName().equals("propstat")) {
115                     NodeList JavaDoc nl2 = elTemp.getChildNodes();
116                     for (int j = 0; j < nl2.getLength(); j++) {
117                         if (nl2.item(j).getNodeType() == Node.ELEMENT_NODE) {
118                             Element JavaDoc elTemp2 = (Element JavaDoc) nl2.item(j);
119
120                             if (elTemp2.getLocalName().equals("status")) {
121                                 String JavaDoc sStatus = elTemp2.getFirstChild().getNodeValue();
122                                 sStatus = sStatus.substring(sStatus.indexOf(" ") + 1);
123                                 sStatus = sStatus.substring(0, sStatus.indexOf(" "));
124                                 this.m_nStatus = Integer.parseInt(sStatus);
125                             } else if (elTemp2.getLocalName().equals("responsedescription")) {
126                                 this.m_sResponseDescription = elTemp2.getFirstChild().getNodeValue();
127                             }
128                         }
129                     }
130                 } else if (elTemp.getLocalName().equals("responsedescription")) {
131                     this.m_sResponseDescription = elTemp.getFirstChild().getNodeValue();
132                 }
133             }
134         }
135     }
136
137     public static void main(String JavaDoc[] args) {
138         MultiStatusResponse multi = new MultiStatusResponse();
139
140         Document JavaDoc xmlDoc = null;
141         try {
142             DocumentBuilderFactory JavaDoc factory =
143                 DocumentBuilderFactory.newInstance();
144             factory.setNamespaceAware(true);
145
146             xmlDoc = factory.newDocumentBuilder().newDocument();
147         } catch (ParserConfigurationException JavaDoc e) {
148             e.printStackTrace();
149         } catch (FactoryConfigurationError JavaDoc e) {
150             e.printStackTrace();
151         }
152         Element JavaDoc elResp =
153             xmlDoc.createElementNS(
154                 WebDAVConnection.WEBDAV_NAMESPACE,
155                 "response");
156
157         Element JavaDoc elHREF =
158             xmlDoc.createElementNS(WebDAVConnection.WEBDAV_NAMESPACE, "href");
159         Text JavaDoc txt = xmlDoc.createTextNode("http://www.sim.com");
160         elHREF.appendChild(txt);
161         elResp.appendChild(elHREF);
162
163         Element JavaDoc elPropStat =
164             xmlDoc.createElementNS(
165                 WebDAVConnection.WEBDAV_NAMESPACE,
166                 "propstat");
167         elResp.appendChild(elPropStat);
168
169         Element JavaDoc elStatus =
170             xmlDoc.createElementNS(WebDAVConnection.WEBDAV_NAMESPACE, "status");
171         txt = xmlDoc.createTextNode("HTTP/1.1 200 OK");
172         elStatus.appendChild(txt);
173         elPropStat.appendChild(elStatus);
174
175         multi.populate(elResp);
176     }
177
178 }
179
Popular Tags