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