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 string value implementation.
035: *
036: * @author Matthew Large
037: * @version $Revision: 1.1 $
038: *
039: */
040: public class DAVStringValue extends StringValue {
041:
042: /**
043: * Constructs a new WebDAV string value.
044: *
045: * @param sValue Value
046: */
047: public DAVStringValue(String sValue) {
048: super (sValue);
049: }
050:
051: /**
052: *
053: */
054: public DAVStringValue() {
055: super ();
056: }
057:
058: /**
059: * Publishes WebDAV string values to a XML element.
060: *
061: * @param xmlDoc Owning XML document
062: * @param propEl Element to append to
063: * @param aValues List of {@link DAVBooleanValue} objects
064: */
065: public static void toXML(Document xmlDoc, Element propEl,
066: List aValues) {
067: if (aValues.size() == 1) {
068: Text txt = xmlDoc.createTextNode(((DAVStringValue) aValues
069: .get(0)).getValue());
070: propEl.appendChild(txt);
071: } else if (aValues.size() > 1) {
072: propEl.setAttributeNS(NamespaceType.SOAP_ENCODING.getURI(),
073: "type", "Array");
074: propEl.setAttributeNS(NamespaceType.SOAP_ENCODING.getURI(),
075: "arraySize", Integer.toString(aValues.size()));
076: propEl.setAttributeNS(NamespaceType.SOAP_ENCODING.getURI(),
077: "itemType", NamespaceType.XML_SCHEMA.getPrefix()
078: + ":string");
079: Iterator itor = aValues.iterator();
080: while (itor.hasNext()) {
081: DAVStringValue val = (DAVStringValue) itor.next();
082: Element arrayEl = xmlDoc.createElementNS(
083: NamespaceType.XML_SCHEMA.getURI(), "string");
084: Text txt = xmlDoc.createTextNode(val.getValue());
085: arrayEl.appendChild(txt);
086: propEl.appendChild(arrayEl);
087: }
088: }
089: }
090:
091: /**
092: * Populates a property instance with WebDAV string values from
093: * XML.
094: *
095: * @param propInst Property instance to populate
096: * @param propEl Root element of property instance
097: */
098: public static void fromXML(PropertyInstance propInst, Element propEl) {
099: if (propEl.getChildNodes().getLength() == 1) {
100: Node node = propEl.getFirstChild();
101: if (node != null && node.getNodeType() == Node.TEXT_NODE) {
102: Text txt = (Text) propEl.getFirstChild();
103: DAVStringValue val = (DAVStringValue) propInst
104: .getNewValueInstance();
105: val.setValue(txt.getData());
106: propInst.addValueWithoutFiringVirtualFileEvent(val);
107: }
108: } else if (propEl.getChildNodes().getLength() > 1) {
109: NodeList nl = propEl.getChildNodes();
110: for (int i = 0; i < nl.getLength(); i++) {
111: Node node = nl.item(i);
112: if (node.getNodeType() == Node.ELEMENT_NODE) {
113: Node node2 = node.getFirstChild();
114: if (node2 != null
115: && node2.getNodeType() == Node.TEXT_NODE) {
116: Text txt = (Text) node.getFirstChild();
117: DAVStringValue val = (DAVStringValue) propInst
118: .getNewValueInstance();
119: val.setValue(txt.getData());
120: propInst
121: .addValueWithoutFiringVirtualFileEvent(val);
122: }
123: }
124: }
125: }
126: }
127:
128: }
|