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 vocabulary value implementation.
035: *
036: * @author Matthew Large
037: * @version $Revision: 1.1 $
038: *
039: */
040: public class DAVValueValue extends ValueValue {
041:
042: /**
043: *
044: */
045: public DAVValueValue() {
046: super ();
047: }
048:
049: /**
050: * Publishes WebDAV vocabulary 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.createTextNode(((DAVValueValue) aValues
063: .get(0)).getValue());
064: hrefEl.appendChild(txt);
065: } else if (aValues.size() > 1) {
066: propEl.setAttributeNS(NamespaceType.SOAP_ENCODING.getURI(),
067: "type", "Array");
068: propEl.setAttributeNS(NamespaceType.SOAP_ENCODING.getURI(),
069: "arraySize", Integer.toString(aValues.size()));
070: propEl
071: .setAttributeNS(NamespaceType.SOAP_ENCODING
072: .getURI(), "itemType", NamespaceType.DAV
073: .getPrefix()
074: + ":href");
075: Iterator itor = aValues.iterator();
076: while (itor.hasNext()) {
077: DAVValueValue val = (DAVValueValue) itor.next();
078: Element arrayEl = xmlDoc.createElementNS(
079: NamespaceType.DAV.getURI(), "href");
080: Text txt = xmlDoc.createTextNode(val.getValue());
081: arrayEl.appendChild(txt);
082: propEl.appendChild(arrayEl);
083: }
084: }
085: }
086:
087: /**
088: * Populates a property instance with WebDAV vocabulary values from
089: * XML.
090: *
091: * @param propInst Property instance to populate
092: * @param propEl Root element of property instance
093: */
094: public static void fromXML(PropertyInstance propInst, Element propEl) {
095: try {
096: if (propEl.getChildNodes().getLength() == 1) {
097: Element elHREF = (Element) propEl.getFirstChild();
098: Text txt = (Text) elHREF.getFirstChild();
099: if (txt != null) {
100: DAVValueValue val = (DAVValueValue) 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: DAVValueValue val = (DAVValueValue) propInst
113: .getNewValueInstance();
114: val.setValue(txt.getData());
115: propInst
116: .addValueWithoutFiringVirtualFileEvent(val);
117: }
118: }
119: }
120: }
121: } catch (Exception e) {
122: e.printStackTrace(System.err);
123: }
124: }
125:
126: }
|