KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibm > webdav > Response


1 package com.ibm.webdav;
2
3 /*
4  * (C) Copyright IBM Corp. 2000 All rights reserved.
5  *
6  * The program is provided "AS IS" without any warranty express or
7  * implied, including the warranty of non-infringement and the implied
8  * warranties of merchantibility and fitness for a particular purpose.
9  * IBM will not be liable for any damages suffered by you as a result
10  * of using the Program. In no event will IBM be liable for any
11  * special, indirect or consequential damages or lost profits even if
12  * IBM has been advised of the possibility of their occurrence. IBM
13  * will not be liable for any third party claims against you.
14  *
15  * Portions Copyright (C) Simulacra Media Ltd, 2004.
16  */

17
18 import org.w3c.dom.*;
19
20 /** A Response describes the effect of a method on a resource and/or
21  * its properties. See concrete subclasses <code>MethodResponse</code>
22  * and <code>PropertyResponse</code> for additional details. A <code>
23  * MultiStatus</code> contains a collection of Response instances, one
24  * for each resource effected by the method sent.
25  * @author Jim Amsden &lt;jamsden@us.ibm.com&gt;
26  * @see com.ibm.webdav.MethodResponse
27  * @see com.ibm.webdav.PropertyResponse
28  * @see com.ibm.webdav.MultiStatus
29  */

30 public abstract class Response extends Object JavaDoc implements java.io.Serializable JavaDoc
31 {
32
33    public static String JavaDoc HTTPVersion = "HTTP/1.1";
34
35
36    protected Document document = null;
37    private String JavaDoc resource = null; // the URL of the resource this is the response for
38
private String JavaDoc description = null;
39 /** A Response should be constructed with a Document Element
40  */

41 public Response() {
42 }
43 /** Construct a Response from an XML response element
44  *
45  * @param document the document that will contain the Response
46  * when output as XML.
47  * @exception com.ibm.webdav.ServerException thrown if the XML for the response is incorrect
48  */

49 public Response(Document document) throws ServerException {
50     this.document = document;
51 }
52 /** Construct an empty Response for some resource.
53  *
54  * @param url the URL of the resource this is a response for
55  *
56  */

57 public Response(String JavaDoc url) {
58     resource = url;
59 }
60 /** Translate this Response into an XML response element.
61  *
62  */

63 public abstract Element asXML();
64 /** Get the description of this response
65  *
66  * @return the description
67  */

68 public String JavaDoc getDescription() {
69     return description;
70 }
71 /** Get the URL of the resource this response describes
72  * @return the URL of the resource associated with this response
73  */

74 public String JavaDoc getResource() {
75     return resource;
76 }
77 /** Check to see if this response contains any errors.
78  *
79  * @return true if all response does not contain an error, false otherwise.
80  */

81 public abstract boolean isOK();
82 /** Set the description of this response
83  *
84  * @param value the new description
85  */

86 public void setDescription(String JavaDoc value) {
87     description = value;
88 }
89 /** Set the document the response element is placed in when translated to XML
90  *
91  * @param document usually a MultiStatus document
92  */

93 public void setDocument(Document document) {
94     this.document = document;
95 }
96 /** Set the URL of the resource this response describes
97  */

98 protected void setResource(String JavaDoc url) {
99     resource = url;
100 }
101 /** Convert this Response to a PropertyResponse.
102  * This method is used to convert MethodResponses to PropertyResponses
103  * when an error occurred accessing the properties of some member.
104  *
105  */

106 public abstract PropertyResponse toPropertyResponse();
107 /** Convert this Response to a String representation (an XML document).
108  * The String is formatted, and will therefore contain ignorable whitespace.
109  * Use response.asXML().print(pout) if ignorable whitespace is not desired.
110  * @return a string representation of the Response as an XML document
111  *
112  */

113 public String JavaDoc toString() {
114     /*ByteArrayOutputStream os = new ByteArrayOutputStream();
115     PrintWriter pout = new PrintWriter(os);
116     TXDocument document = (TXDocument) asXML();
117     try {
118         document.printWithFormat(pout);
119     } catch (Exception exc) {
120     }
121     return os.toString();*/

122         return printNode(asXML());
123 }
124
125  /** Takes XML node and prints to String.
126    *
127    * @param node Element to print to String
128    * @return XML node as String
129    */

130   public static String JavaDoc printNode( Node node ) {
131     StringBuffer JavaDoc sBuffer = new StringBuffer JavaDoc();
132
133     if( node.getNodeType() == Node.TEXT_NODE ) {
134         sBuffer.append( node.getNodeValue() ) ;
135     }
136     else if( node.getNodeType() == Node.CDATA_SECTION_NODE ) {
137         sBuffer.append("<![CDATA[");
138         sBuffer.append( node.getNodeValue() ) ;
139         sBuffer.append("]]>\n");
140     }
141     else if( node.getNodeType() == Node.ELEMENT_NODE ) {
142         Element el = (Element)node ;
143
144         sBuffer.append( "<" + el.getTagName() ) ;
145
146         NamedNodeMap attribs = el.getAttributes() ;
147         for( int i=0 ; i < attribs.getLength() ; i++ ) {
148            Attr nextAtt = (Attr)attribs.item(i) ;
149            sBuffer.append( " " + nextAtt.getName() + "=\"" + nextAtt.getValue() + "\"" ) ;
150         }
151
152         NodeList nodes = node.getChildNodes() ;
153
154         if( nodes.getLength() == 0 ) {
155             sBuffer.append("/>\n") ;
156         }
157         else {
158             sBuffer.append(">\n") ;
159
160             for( int i = 0 ; i < nodes.getLength() ; i++ ) {
161                 sBuffer.append(printNode(nodes.item(i) )) ;
162             }
163             sBuffer.append( "</" + el.getTagName() + ">\n" ) ;
164         }
165     }
166
167       return(sBuffer.toString());
168   }
169 }
170
Popular Tags