KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.w3c.dom.*;
18 import java.io.*;
19 import javax.xml.parsers.*;
20
21 /** A PropertyValue represents the value of a property of a resource and
22  * it's status.
23  *
24  * @author Jim Amsden <jamsden@us.ibm.com>
25  */

26 public class PropertyValue extends Object JavaDoc implements java.io.Serializable JavaDoc
27 {
28    /** the value of the property as an XML element
29     */

30    public Element value = null;
31
32    /** the status of the property as returned from a Resource.getProperty()
33     * method.
34     */

35    public int status = WebDAVStatus.SC_OK;
36 /** Create a PropertyValue
37  *
38  * @param value an XML DOM Element representing the value of the property
39  * @param status WebDAV the status code of the property as returned from one of the many
40  * Resource.getProperty() methods.
41  */

42 public PropertyValue(Element value, int status) {
43     this.value = value;
44     this.status = status;
45 }
46 /** The status of the property as returned from a Resource.getProperty()
47  * method.
48  * @return the HTTP or WebDAV status code for a property
49  */

50 public int getStatus()
51 {
52     return status;
53 }
54 /** The value of the property as an XML element
55  * @return the value of a property
56  */

57 public Element getValue() {
58     return value;
59 }
60 /** Format a PropertyValue as a string of the form: value(status)
61  * @return a string representation of a PropertyValue
62  */

63 static public String JavaDoc nodeToString( Element el ) {
64
65     return XMLUtility.printNode(el);
66 }
67 /** De-serialize a PropertyValue from a stream.
68  * @param in the stream to read from
69  * @exception IOException
70  * @exception ClassNotFoundException
71  */

72 private void readObject(java.io.ObjectInputStream JavaDoc in) throws IOException, ClassNotFoundException JavaDoc {
73     status = in.readInt();
74     int size = in.readInt();
75     byte[] buffer = new byte[size];
76     in.readFully(buffer);
77     ByteArrayInputStream is = new ByteArrayInputStream(buffer);
78     Document contents = null;
79
80         try {
81           DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
82           factory.setNamespaceAware(true);
83           DocumentBuilder docbuilder = factory.newDocumentBuilder();
84           contents = docbuilder.parse(new org.xml.sax.InputSource JavaDoc(is));
85     } catch(Exception JavaDoc e) {
86           throw new IOException(e.getMessage());
87         }
88
89         value = contents.getDocumentElement();
90 }
91 /** Format a PropertyValue as a string. This
92  * usually returns the content of the protocol tag.
93  * @return a string representation of a PropertyValue
94  */

95 public String JavaDoc toContentString() {
96     Element el = (Element)value;
97         NodeList children = el.getChildNodes();
98     //java.util.Enumeration children = el.elements();
99
String JavaDoc retval = "";
100
101     for(int i=0;i<children.getLength();i++) {
102         Node child = (Node)children.item(i);
103         retval += XMLUtility.printNode( child );
104     }
105     return retval;
106 }
107 /** Format a PropertyValue as a string of the form: value(status)
108  * @return a string representation of a PropertyValue
109  */

110 public String JavaDoc toString() {
111     StringWriter s = new StringWriter();
112     PrintWriter pout = new PrintWriter(s);
113     try {
114                 pout.print(XMLUtility.printNode(value));
115         //((Element) value).print(pout);
116
pout.flush();
117         s.write(" (" + status + ")");
118         s.close();
119     } catch (IOException exc) {
120     }
121     return s.toString();
122 }
123 /** Serialize a PropertyValue to a stream.
124  * @param in the stream to write to
125  * @exception IOException
126  */

127 private void writeObject(java.io.ObjectOutputStream JavaDoc out) throws IOException {
128     Document document = null;
129
130         try {
131           document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
132         } catch(Exception JavaDoc e) {
133           throw new IOException(e.getMessage());
134         }
135     //document.setVersion(Resource.XMLVersion);
136
//document.setEncoding(Resource.defaultXMLEncoding);
137
document.appendChild(value);
138     ByteArrayOutputStream os = new ByteArrayOutputStream();
139     PrintWriter pw = new PrintWriter(os, false);
140     //document.print(pw);
141
pw.print(XMLUtility.printNode(document.getDocumentElement()));
142     out.writeInt(status);
143     out.writeInt(os.size());
144     out.write(os.toByteArray());
145 }
146 }
147
Popular Tags