001: package com.ibm.webdav;
002:
003: /*
004: * (C) Copyright IBM Corp. 2000 All rights reserved.
005: *
006: * The program is provided "AS IS" without any warranty express or
007: * implied, including the warranty of non-infringement and the implied
008: * warranties of merchantibility and fitness for a particular purpose.
009: * IBM will not be liable for any damages suffered by you as a result
010: * of using the Program. In no event will IBM be liable for any
011: * special, indirect or consequential damages or lost profits even if
012: * IBM has been advised of the possibility of their occurrence. IBM
013: * will not be liable for any third party claims against you.
014: *
015: * Portions Copyright (C) Simulacra Media Ltd, 2004.
016: */
017: import org.w3c.dom.*;
018: import java.io.*;
019: import javax.xml.parsers.*;
020:
021: /** A PropertyValue represents the value of a property of a resource and
022: * it's status.
023: *
024: * @author Jim Amsden <jamsden@us.ibm.com>
025: */
026: public class PropertyValue extends Object implements
027: java.io.Serializable {
028: /** the value of the property as an XML element
029: */
030: public Element value = null;
031:
032: /** the status of the property as returned from a Resource.getProperty()
033: * method.
034: */
035: public int status = WebDAVStatus.SC_OK;
036:
037: /** Create a PropertyValue
038: *
039: * @param value an XML DOM Element representing the value of the property
040: * @param status WebDAV the status code of the property as returned from one of the many
041: * Resource.getProperty() methods.
042: */
043: public PropertyValue(Element value, int status) {
044: this .value = value;
045: this .status = status;
046: }
047:
048: /** The status of the property as returned from a Resource.getProperty()
049: * method.
050: * @return the HTTP or WebDAV status code for a property
051: */
052: public int getStatus() {
053: return status;
054: }
055:
056: /** The value of the property as an XML element
057: * @return the value of a property
058: */
059: public Element getValue() {
060: return value;
061: }
062:
063: /** Format a PropertyValue as a string of the form: value(status)
064: * @return a string representation of a PropertyValue
065: */
066: static public String nodeToString(Element el) {
067:
068: return XMLUtility.printNode(el);
069: }
070:
071: /** De-serialize a PropertyValue from a stream.
072: * @param in the stream to read from
073: * @exception IOException
074: * @exception ClassNotFoundException
075: */
076: private void readObject(java.io.ObjectInputStream in)
077: throws IOException, ClassNotFoundException {
078: status = in.readInt();
079: int size = in.readInt();
080: byte[] buffer = new byte[size];
081: in.readFully(buffer);
082: ByteArrayInputStream is = new ByteArrayInputStream(buffer);
083: Document contents = null;
084:
085: try {
086: DocumentBuilderFactory factory = DocumentBuilderFactory
087: .newInstance();
088: factory.setNamespaceAware(true);
089: DocumentBuilder docbuilder = factory.newDocumentBuilder();
090: contents = docbuilder
091: .parse(new org.xml.sax.InputSource(is));
092: } catch (Exception e) {
093: throw new IOException(e.getMessage());
094: }
095:
096: value = contents.getDocumentElement();
097: }
098:
099: /** Format a PropertyValue as a string. This
100: * usually returns the content of the protocol tag.
101: * @return a string representation of a PropertyValue
102: */
103: public String toContentString() {
104: Element el = (Element) value;
105: NodeList children = el.getChildNodes();
106: //java.util.Enumeration children = el.elements();
107: String retval = "";
108:
109: for (int i = 0; i < children.getLength(); i++) {
110: Node child = (Node) children.item(i);
111: retval += XMLUtility.printNode(child);
112: }
113: return retval;
114: }
115:
116: /** Format a PropertyValue as a string of the form: value(status)
117: * @return a string representation of a PropertyValue
118: */
119: public String toString() {
120: StringWriter s = new StringWriter();
121: PrintWriter pout = new PrintWriter(s);
122: try {
123: pout.print(XMLUtility.printNode(value));
124: //((Element) value).print(pout);
125: pout.flush();
126: s.write(" (" + status + ")");
127: s.close();
128: } catch (IOException exc) {
129: }
130: return s.toString();
131: }
132:
133: /** Serialize a PropertyValue to a stream.
134: * @param in the stream to write to
135: * @exception IOException
136: */
137: private void writeObject(java.io.ObjectOutputStream out)
138: throws IOException {
139: Document document = null;
140:
141: try {
142: document = DocumentBuilderFactory.newInstance()
143: .newDocumentBuilder().newDocument();
144: } catch (Exception e) {
145: throw new IOException(e.getMessage());
146: }
147: //document.setVersion(Resource.XMLVersion);
148: //document.setEncoding(Resource.defaultXMLEncoding);
149: document.appendChild(value);
150: ByteArrayOutputStream os = new ByteArrayOutputStream();
151: PrintWriter pw = new PrintWriter(os, false);
152: //document.print(pw);
153: pw.print(XMLUtility.printNode(document.getDocumentElement()));
154: out.writeInt(status);
155: out.writeInt(os.size());
156: out.write(os.toByteArray());
157: }
158: }
|