KickJava   Java API By Example, From Geeks To Geeks.

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


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.ArrayList JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24
25 import org.openharmonise.commons.xml.namespace.*;
26 import org.openharmonise.vfs.*;
27 import org.openharmonise.vfs.metadata.*;
28 import org.openharmonise.vfs.metadata.value.*;
29 import org.openharmonise.webdav.client.*;
30 import org.openharmonise.webdav.client.methods.*;
31 import org.w3c.dom.Document JavaDoc;
32 import org.w3c.dom.Element JavaDoc;
33 import org.w3c.dom.Node JavaDoc;
34 import org.w3c.dom.NodeList JavaDoc;
35
36
37 /**
38  * WebDAV compound value implementation.
39  *
40  * @author Matthew Large
41  * @version $Revision: 1.1 $
42  *
43  */

44 public class DAVPropertyValue extends PropertyValue {
45
46     /**
47      *
48      */

49     public DAVPropertyValue() {
50         super();
51     }
52
53     /**
54      * Constructs a new WebDAV compound value.
55      *
56      * @param aPropertyInstances List of {@link PropertyInstance} objects
57      */

58     public DAVPropertyValue(ArrayList JavaDoc aPropertyInstances) {
59         super(aPropertyInstances);
60     }
61     
62     /**
63      * Publishes WebDAV compound values to a XML element.
64      *
65      * @param xmlDoc Owning XML document
66      * @param propEl Element to append to
67      * @param aValues List of {@link DAVBooleanValue} objects
68      */

69     public static void toXML(Document JavaDoc xmlDoc, Element JavaDoc propEl, List JavaDoc aValues) {
70         if(aValues.size()==1) {
71
72             Element JavaDoc innerPropEl = xmlDoc.createElementNS(NamespaceType.DAV.getURI(), "prop");
73             propEl.appendChild(innerPropEl);
74         
75             Iterator JavaDoc itor = ((DAVPropertyValue)aValues.get(0)).getValue().iterator();
76             while (itor.hasNext()) {
77                 PropertyInstance propInst = (PropertyInstance) itor.next();
78                 PropPatch.publishPropertyInstanceToXML(xmlDoc, propInst, innerPropEl);
79             }
80         } else if(aValues.size()>1) {
81             propEl.setAttributeNS(NamespaceType.SOAP_ENCODING.getURI(), "type", "Array" );
82             propEl.setAttributeNS(NamespaceType.SOAP_ENCODING.getURI(), "arraySize", Integer.toString(aValues.size()) );
83             propEl.setAttributeNS(NamespaceType.SOAP_ENCODING.getURI(), "itemType", NamespaceType.DAV.getPrefix()+":prop");
84             Iterator JavaDoc itor = aValues.iterator();
85             while (itor.hasNext()) {
86                 Element JavaDoc innerPropEl = xmlDoc.createElementNS(NamespaceType.DAV.getURI(), "prop");
87                 propEl.appendChild(innerPropEl);
88         
89                 Iterator JavaDoc itor2 = ((DAVPropertyValue)itor.next()).getValue().iterator();
90                 while (itor2.hasNext()) {
91                     PropertyInstance propInst = (PropertyInstance) itor2.next();
92                     PropPatch.publishPropertyInstanceToXML(xmlDoc, propInst, innerPropEl);
93                 }
94                 propEl.appendChild(innerPropEl);
95             }
96         }
97     }
98     
99     /**
100      * Populates a property instance with WebDAV compound values from
101      * XML.
102      *
103      * @param vfFile Virtual file which owns property instances
104      * @param propInst Property instance to populate
105      * @param propEl Root element of property instance
106      */

107     public static void fromXML(VirtualFile vfFile, PropertyInstance propInst, Element JavaDoc propEl) {
108         if(propEl.getChildNodes().getLength()==1) {
109             Element JavaDoc containerEl = (Element JavaDoc) propEl.getFirstChild();
110             if(containerEl!=null) {
111                 DAVPropertyValue valInst = (DAVPropertyValue) propInst.getNewValueInstance();
112                 ArrayList JavaDoc aPropertyInstances = new ArrayList JavaDoc();
113                 NodeList JavaDoc nl = containerEl.getChildNodes();
114                 for(int i=0; i<nl.getLength(); i++) {
115                     Node JavaDoc node = nl.item(i);
116                     if(node.getNodeType()==Node.ELEMENT_NODE) {
117                         Element JavaDoc subPropEl = (Element JavaDoc)node;
118                         PropertyInstance subProp = new PropertyInstance();
119                         aPropertyInstances.add(subProp);
120                         ((WebDAVFileSystem)vfFile.getVFS()).populatePropertyInstance(vfFile, subPropEl, subProp, false);
121                         //prop.addPropertyInstance(subProp);
122
}
123                 }
124                 valInst.setValue(aPropertyInstances);
125                 propInst.addValueWithoutFiringVirtualFileEvent(valInst);
126             }
127         } else if(propEl.getChildNodes().getLength()>1) {
128             NodeList JavaDoc nl = propEl.getChildNodes();
129             for (int i = 0; i < nl.getLength(); i++) {
130                 Node JavaDoc node = nl.item(i);
131                 if(node.getNodeType()==Node.ELEMENT_NODE) {
132                     Element JavaDoc containerEl = (Element JavaDoc) node;
133                     if(containerEl!=null) {
134                         DAVPropertyValue valInst = (DAVPropertyValue) propInst.getNewValueInstance();
135                         ArrayList JavaDoc aPropertyInstances = new ArrayList JavaDoc();
136                         NodeList JavaDoc nl2 = containerEl.getChildNodes();
137                         for(int j=0; j<nl2.getLength(); j++) {
138                             Node JavaDoc node2 = nl2.item(j);
139                             if(node.getNodeType()==Node.ELEMENT_NODE) {
140                                 Element JavaDoc subPropEl = (Element JavaDoc)node2;
141                                 PropertyInstance subProp = new PropertyInstance();
142                                 aPropertyInstances.add(subProp);
143                                 ((WebDAVFileSystem)vfFile.getVFS()).populatePropertyInstance(vfFile, subPropEl, subProp, false);
144                             }
145                         }
146                         valInst.setValue(aPropertyInstances);
147                         propInst.addValueWithoutFiringVirtualFileEvent(valInst);
148                     }
149                 }
150             }
151         }
152     }
153
154 }
155
Popular Tags