KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > webdav > client > value > DAVBooleanValue


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

19 package org.openharmonise.webdav.client.value;
20
21 import java.util.Iterator JavaDoc;
22 import java.util.List JavaDoc;
23
24 import org.openharmonise.commons.xml.namespace.*;
25 import org.openharmonise.vfs.metadata.*;
26 import org.openharmonise.vfs.metadata.value.*;
27 import org.w3c.dom.Document JavaDoc;
28 import org.w3c.dom.Element JavaDoc;
29 import org.w3c.dom.Node JavaDoc;
30 import org.w3c.dom.NodeList JavaDoc;
31 import org.w3c.dom.Text JavaDoc;
32
33
34 /**
35  * WebDAV boolean value implementation.
36  *
37  * @author Matthew Large
38  * @version $Revision: 1.1 $
39  *
40  */

41 public class DAVBooleanValue extends BooleanValue {
42
43     /**
44      * Constructs a new WebDAV boolean value.
45      *
46      * @param bValue Value
47      */

48     public DAVBooleanValue(boolean bValue) {
49         super(bValue);
50     }
51
52     public DAVBooleanValue() {
53         super();
54     }
55     
56     /**
57      * Publishes WebDAV boolean values to a XML element.
58      *
59      * @param xmlDoc Owning XML document
60      * @param propEl Element to append to
61      * @param aValues List of {@link DAVBooleanValue} objects
62      */

63     public static void toXML(Document JavaDoc xmlDoc, Element JavaDoc propEl, List JavaDoc aValues) {
64         if(aValues.size()==1) {
65             if( ((DAVBooleanValue)aValues.get(0)).getValue() ) {
66                 Text JavaDoc txt = xmlDoc.createTextNode("true");
67                 propEl.appendChild(txt);
68             } else {
69                 Text JavaDoc txt = xmlDoc.createTextNode("false");
70                 propEl.appendChild(txt);
71             }
72         } else if(aValues.size()>1) {
73             propEl.setAttributeNS(NamespaceType.SOAP_ENCODING.getURI(), "type", "Array" );
74             propEl.setAttributeNS(NamespaceType.SOAP_ENCODING.getURI(), "arraySize", Integer.toString(aValues.size()) );
75             propEl.setAttributeNS(NamespaceType.SOAP_ENCODING.getURI(), "itemType", NamespaceType.XML_SCHEMA.getPrefix()+":boolean");
76             Iterator JavaDoc itor = aValues.iterator();
77             while (itor.hasNext()) {
78                 DAVBooleanValue val = (DAVBooleanValue) itor.next();
79                 Element JavaDoc arrayEl = xmlDoc.createElementNS(NamespaceType.XML_SCHEMA.getURI(), "boolean");
80                 if( val.getValue() ) {
81                     Text JavaDoc txt = xmlDoc.createTextNode("true");
82                     arrayEl.appendChild(txt);
83                 } else {
84                     Text JavaDoc txt = xmlDoc.createTextNode("false");
85                     arrayEl.appendChild(txt);
86                 }
87                 propEl.appendChild(arrayEl);
88             }
89         }
90     }
91     
92     /**
93      * Populates a property instance with WebDAV boolean values from
94      * XML.
95      *
96      * @param propInst Property instance to populate
97      * @param propEl Root element of property instance
98      */

99     public static void fromXML(PropertyInstance propInst, Element JavaDoc propEl) {
100         if(propEl.getChildNodes().getLength()==1) {
101             Text JavaDoc txt = (Text JavaDoc) propEl.getFirstChild();
102             if(txt!=null) {
103                 DAVBooleanValue val = (DAVBooleanValue) propInst.getNewValueInstance();
104                 val.setValue( Boolean.valueOf(txt.getData()).booleanValue() );
105                 propInst.addValueWithoutFiringVirtualFileEvent(val);
106             }
107         } else if(propEl.getChildNodes().getLength()>1) {
108             NodeList JavaDoc nl = propEl.getChildNodes();
109             for (int i = 0; i < nl.getLength(); i++) {
110                 Node JavaDoc node = nl.item(i);
111                 if(node.getNodeType()==Node.ELEMENT_NODE) {
112                     Text JavaDoc txt = (Text JavaDoc) node.getFirstChild();
113                     if(txt!=null) {
114                         DAVBooleanValue val = (DAVBooleanValue) propInst.getNewValueInstance();
115                         val.setValue( Boolean.valueOf(txt.getData()).booleanValue() );
116                         propInst.addValueWithoutFiringVirtualFileEvent(val);
117                     }
118                 }
119             }
120         }
121     }
122
123 }
124
Popular Tags