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