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 date value implementation.
035: *
036: * @author Matthew Large
037: * @version $Revision: 1.1 $
038: *
039: */
040: public class DAVDateValue extends DateValue {
041:
042: /**
043: * Constructs a new WebDAV date value.
044: *
045: * @param sValue Value
046: */
047: public DAVDateValue(String sValue) {
048: super (sValue);
049: }
050:
051: /**
052: *
053: */
054: public DAVDateValue() {
055: super ();
056: }
057:
058: /**
059: * Publishes WebDAV date 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 DAVDateValue} objects
064: */
065: public static void toXML(Document xmlDoc, Element propEl,
066: List aValues) {
067: if (aValues.size() == 1) {
068: Text txt = xmlDoc.createTextNode(((DAVDateValue) 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: + ":date");
079: Iterator itor = aValues.iterator();
080: while (itor.hasNext()) {
081: DAVDateValue val = (DAVDateValue) itor.next();
082: Element arrayEl = xmlDoc.createElementNS(
083: NamespaceType.XML_SCHEMA.getURI(), "date");
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 date 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: Text txt = (Text) propEl.getFirstChild();
101: if (txt != null) {
102: DAVDateValue val = (DAVDateValue) propInst
103: .getNewValueInstance();
104: val.setValue(txt.getData());
105: propInst.addValueWithoutFiringVirtualFileEvent(val);
106: }
107: } else if (propEl.getChildNodes().getLength() > 1) {
108: NodeList nl = propEl.getChildNodes();
109: for (int i = 0; i < nl.getLength(); i++) {
110: Node node = nl.item(i);
111: if (node.getNodeType() == Node.ELEMENT_NODE) {
112: Text txt = (Text) node.getFirstChild();
113: if (txt != null) {
114: DAVDateValue val = (DAVDateValue) propInst
115: .getNewValueInstance();
116: val.setValue(txt.getData());
117: propInst
118: .addValueWithoutFiringVirtualFileEvent(val);
119: }
120: }
121: }
122: }
123: }
124:
125: }
|