001: /*
002: * The contents of this file are subject to the
003: * Mozilla Public License Version 1.1 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
009: * See the License for the specific language governing rights and
010: * limitations under the License.
011: *
012: * The Initial Developer of the Original Code is Simulacra Media Ltd.
013: * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
014: *
015: * All Rights Reserved.
016: *
017: * Contributor(s):
018: */
019: package org.openharmonise.webdav.client.value;
020:
021: import java.util.Iterator;
022: import java.util.List;
023:
024: import org.openharmonise.commons.xml.namespace.*;
025: import org.openharmonise.vfs.metadata.*;
026: import org.openharmonise.vfs.metadata.value.*;
027: import org.w3c.dom.Document;
028: import org.w3c.dom.Element;
029: import org.w3c.dom.Node;
030: import org.w3c.dom.NodeList;
031: import org.w3c.dom.Text;
032:
033: /**
034: * WebDAV resource relationship value implementation.
035: *
036: * @author Matthew Large
037: * @version $Revision: 1.1 $
038: *
039: */
040: public class DAVResourceValue extends ResourceValue {
041:
042: /**
043: *
044: */
045: public DAVResourceValue() {
046: super ();
047: }
048:
049: /**
050: * Publishes WebDAV resource relationship values to a XML element.
051: *
052: * @param xmlDoc Owning XML document
053: * @param propEl Element to append to
054: * @param aValues List of {@link DAVBooleanValue} objects
055: */
056: public static void toXML(Document xmlDoc, Element propEl,
057: List aValues) {
058: if (aValues.size() == 1) {
059: Element hrefEl = xmlDoc.createElementNS(NamespaceType.DAV
060: .getURI(), "href");
061: propEl.appendChild(hrefEl);
062: Text txt = xmlDoc
063: .createTextNode(((DAVResourceValue) aValues.get(0))
064: .getValue());
065: hrefEl.appendChild(txt);
066: } else if (aValues.size() > 1) {
067: propEl.setAttributeNS(NamespaceType.SOAP_ENCODING.getURI(),
068: "type", "Array");
069: propEl.setAttributeNS(NamespaceType.SOAP_ENCODING.getURI(),
070: "arraySize", Integer.toString(aValues.size()));
071: propEl
072: .setAttributeNS(NamespaceType.SOAP_ENCODING
073: .getURI(), "itemType", NamespaceType.DAV
074: .getPrefix()
075: + ":href");
076: Iterator itor = aValues.iterator();
077: while (itor.hasNext()) {
078: DAVResourceValue val = (DAVResourceValue) itor.next();
079: Element arrayEl = xmlDoc.createElementNS(
080: NamespaceType.DAV.getURI(), "href");
081: Text txt = xmlDoc.createTextNode(val.getValue());
082: arrayEl.appendChild(txt);
083: propEl.appendChild(arrayEl);
084: }
085: }
086: }
087:
088: /**
089: * Populates a property instance with WebDAV resource relationship values from
090: * XML.
091: *
092: * @param propInst Property instance to populate
093: * @param propEl Root element of property instance
094: */
095: public static void fromXML(PropertyInstance propInst, Element propEl) {
096: if (propEl.getChildNodes().getLength() == 1) {
097: Element elHREF = (Element) propEl.getFirstChild();
098: Text txt = (Text) elHREF.getFirstChild();
099: if (txt != null) {
100: DAVResourceValue val = (DAVResourceValue) propInst
101: .getNewValueInstance();
102: val.setValue(txt.getData());
103: propInst.addValueWithoutFiringVirtualFileEvent(val);
104: }
105: } else if (propEl.getChildNodes().getLength() > 1) {
106: NodeList nl = propEl.getChildNodes();
107: for (int i = 0; i < nl.getLength(); i++) {
108: Node node = nl.item(i);
109: if (node.getNodeType() == Node.ELEMENT_NODE) {
110: Text txt = (Text) node.getFirstChild();
111: if (txt != null) {
112: DAVResourceValue val = (DAVResourceValue) propInst
113: .getNewValueInstance();
114: val.setValue(txt.getData());
115: propInst
116: .addValueWithoutFiringVirtualFileEvent(val);
117: }
118: }
119: }
120: }
121: }
122:
123: }
|