KickJava   Java API By Example, From Geeks To Geeks.

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


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.him.harmonise.*;
26 import org.openharmonise.vfs.metadata.*;
27 import org.openharmonise.vfs.metadata.value.*;
28 import org.w3c.dom.Document JavaDoc;
29 import org.w3c.dom.Element JavaDoc;
30 import org.w3c.dom.Node JavaDoc;
31 import org.w3c.dom.NodeList JavaDoc;
32 import org.w3c.dom.Text JavaDoc;
33
34
35 /**
36  * WebDAV domain value implementation.
37  *
38  * @author Matthew Large
39  * @version $Revision: 1.1 $
40  *
41  */

42 public class DAVDomainValue extends DomainValue {
43
44     /**
45      *
46      */

47     public DAVDomainValue() {
48         super();
49     }
50
51     /**
52      * Constructs a new WebDAV domain value.
53      *
54      * @param sPath Full path of domain
55      */

56     public DAVDomainValue(String JavaDoc sPath) {
57         super(sPath);
58     }
59
60     /**
61      * Constructs a new WebDAV domain value.
62      *
63      * @param sPath Full path of domain
64      * @param nMinOccurs Mimumum occurrence
65      * @param nMaxOccurs Maximum occurrence
66      * @param aContentTypes List of valid content types
67      * @param sResourceType Valid resource type
68      */

69     public DAVDomainValue(
70         String JavaDoc sPath,
71         int nMinOccurs,
72         int nMaxOccurs,
73         List JavaDoc aContentTypes,
74         String JavaDoc sResourceType) {
75         super(sPath, nMinOccurs, nMaxOccurs, aContentTypes, sResourceType);
76     }
77     
78     /**
79      * Publishes WebDAV domain values to a XML element.
80      *
81      * @param xmlDoc Owning XML document
82      * @param propEl Element to append to
83      * @param aValues List of {@link DAVDomainValue} objects
84      */

85     public static void toXML(Document JavaDoc xmlDoc, Element JavaDoc propEl, List JavaDoc aValues) {
86         if(aValues.size()==1) {
87             DAVDomainValue.toXMLDomainData(xmlDoc, propEl, (DAVDomainValue) aValues.get(0));
88         } else if(aValues.size()>1) {
89             propEl.setAttributeNS(NamespaceType.SOAP_ENCODING.getURI(), "type", "Array" );
90             propEl.setAttributeNS(NamespaceType.SOAP_ENCODING.getURI(), "arraySize", Integer.toString(aValues.size()) );
91             propEl.setAttributeNS(NamespaceType.SOAP_ENCODING.getURI(), "itemType", NamespaceType.DAV.getPrefix()+":domainData");
92             Iterator JavaDoc itor = aValues.iterator();
93             while (itor.hasNext()) {
94                 DAVDomainValue value = (DAVDomainValue) itor.next();
95                 Element JavaDoc elDomainData = xmlDoc.createElementNS(NamespaceType.DAV.getURI(), "domainData");
96                 propEl.appendChild(elDomainData);
97                 DAVDomainValue.toXMLDomainData(xmlDoc, elDomainData, value);
98             }
99         }
100     }
101     
102     /**
103      * Publishes WebDAV a domain value to a XML element.
104      *
105      * @param xmlDoc Owning XML document
106      * @param propEl Element to append to
107      * @param aValues Value
108      */

109     private static void toXMLDomainData(Document JavaDoc xmlDoc, Element JavaDoc parentEl, DAVDomainValue value) {
110         Element JavaDoc elResourceType = null;
111         
112         elResourceType = xmlDoc.createElementNS(NamespaceType.DAV.getURI(), "resourcetype");
113         parentEl.appendChild(elResourceType);
114         
115         if(value.getResourceType()==DomainValue.RESOURCE) {
116             if(value.getPath().startsWith(HarmonisePaths.PATH_PROPERTIES)) {
117                 Element JavaDoc elType = xmlDoc.createElementNS(NamespaceType.DAV.getURI(), DAVDomainValue.PROPERTY);
118                 elResourceType.appendChild(elType);
119             } else if(value.getPath().startsWith(HarmonisePaths.PATH_VALUES)) {
120                 Element JavaDoc elType = xmlDoc.createElementNS(NamespaceType.DAV.getURI(), DAVDomainValue.VALUE);
121                 elResourceType.appendChild(elType);
122             } else {
123                 Element JavaDoc elType = xmlDoc.createElementNS(NamespaceType.DAV.getURI(), value.getResourceType());
124                 elResourceType.appendChild(elType);
125             }
126         } else {
127             Element JavaDoc elType = xmlDoc.createElementNS(NamespaceType.DAV.getURI(), value.getResourceType());
128             elResourceType.appendChild(elType);
129         }
130             
131             
132         if(value.getContentTypes().size()>0) {
133             Iterator JavaDoc itorCT = value.getContentTypes().iterator();
134             while (itorCT.hasNext()) {
135                 String JavaDoc sContentType = (String JavaDoc) itorCT.next();
136                 Element JavaDoc elContentType = xmlDoc.createElementNS(NamespaceType.DAV.getURI(), "contenttype");
137                 parentEl.appendChild(elContentType);
138                     
139                 Text JavaDoc txt = xmlDoc.createTextNode(sContentType);
140                 elContentType.appendChild(txt);
141             }
142         }
143             
144         if(value.getPath()!=null && !value.getPath().equals("")) {
145             Element JavaDoc elHREF = xmlDoc.createElementNS(NamespaceType.DAV.getURI(), "href");
146             parentEl.appendChild(elHREF);
147             Text JavaDoc txtHREF = xmlDoc.createTextNode(value.getPath());
148             elHREF.appendChild(txtHREF);
149         }
150         
151         Element JavaDoc elMaxOccurs = xmlDoc.createElementNS(NamespaceType.DAV.getURI(), "maxOccurs");
152         parentEl.appendChild(elMaxOccurs);
153         Text JavaDoc txtMax = xmlDoc.createTextNode(Integer.toString(value.getMaxOccurs()));
154         elMaxOccurs.appendChild(txtMax);
155         
156         Element JavaDoc elMinOccurs = xmlDoc.createElementNS(NamespaceType.DAV.getURI(), "minOccurs");
157         parentEl.appendChild(elMinOccurs);
158         Text JavaDoc txtMin = xmlDoc.createTextNode(Integer.toString(value.getMinOccurs()));
159         elMinOccurs.appendChild(txtMin);
160     }
161     
162     /**
163      * Populates a property instance with WebDAV domain values from
164      * XML.
165      *
166      * @param propInst Property instance to populate
167      * @param propEl Root element of property instance
168      */

169     public static void fromXML(PropertyInstance propInst, Element JavaDoc propEl) {
170         boolean bMultiple = false;
171         if(propEl.getChildNodes().getLength()>1) {
172             NodeList JavaDoc nl = propEl.getChildNodes();
173             for (int i = 0; i < nl.getLength(); i++) {
174                 Node JavaDoc node = nl.item(i);
175                 if(node.getNodeType()==Node.ELEMENT_NODE) {
176                     if( ((Element JavaDoc)node).getLocalName().equalsIgnoreCase("domainData")) {
177                         bMultiple=true;
178                     }
179                 }
180             }
181         }
182         
183         if(!bMultiple) {
184             DAVDomainValue val = (DAVDomainValue) propInst.getNewValueInstance();
185             DAVDomainValue.fromXMLDomainData(propInst, propEl, val);
186             if(val.getPath()!=null && !val.getPath().equals("")) {
187                 propInst.addValueWithoutFiringVirtualFileEvent(val);
188             }
189         } else if(propEl.getChildNodes().getLength()>1) {
190             NodeList JavaDoc nl = propEl.getChildNodes();
191             for (int i = 0; i < nl.getLength(); i++) {
192                 Node JavaDoc node = nl.item(i);
193                 if(node.getNodeType()==Node.ELEMENT_NODE) {
194                     DAVDomainValue val = (DAVDomainValue) propInst.getNewValueInstance();
195                     DAVDomainValue.fromXMLDomainData(propInst, (Element JavaDoc) node, val);
196                     if(val.getPath()!=null && !val.getPath().equals("")) {
197                         propInst.addValueWithoutFiringVirtualFileEvent(val);
198                     }
199                 }
200             }
201         }
202     }
203     
204     /**
205      * Populates a property instance with a WebDAV domain value from
206      * XML.
207      *
208      * @param propInst Property instance to populate
209      * @param parentEl Root element of property instance
210      * @param value Value to populate
211      */

212     public static void fromXMLDomainData(PropertyInstance propInst, Element JavaDoc parentEl, DAVDomainValue value) {
213         NodeList JavaDoc nl = parentEl.getChildNodes();
214         for (int i = 0; i < nl.getLength(); i++) {
215             Node JavaDoc node = nl.item(i);
216             if(node.getNodeType()==Node.ELEMENT_NODE) {
217                 Element JavaDoc elTemp = (Element JavaDoc) node;
218                 if(elTemp.getLocalName().equals("resourcetype")) {
219                     if(elTemp.getChildNodes().getLength()>0 && elTemp.getFirstChild().getNodeType()==Node.ELEMENT_NODE) {
220                         Element JavaDoc elType = (Element JavaDoc) elTemp.getFirstChild();
221                         if(elType.getLocalName().equals("collection")) {
222                             value.setResourceType(DAVDomainValue.COLLECTION);
223                         } else if(elType.getLocalName().equals("property-resource")) {
224                             value.setResourceType(DAVDomainValue.RESOURCE);
225                         } else if(elType.getLocalName().equals("value")) {
226                             value.setResourceType(DAVDomainValue.RESOURCE);
227                         }else {
228                             value.setResourceType(DAVDomainValue.RESOURCE);
229                         }
230                     } else {
231                         value.setResourceType(DAVDomainValue.RESOURCE);
232                     }
233                 } else if(elTemp.getLocalName().equals("contentype")) {
234                     if(elTemp.getChildNodes().getLength()>0 && elTemp.getFirstChild().getNodeType()==Node.TEXT_NODE) {
235                         String JavaDoc sContentType = elTemp.getFirstChild().getNodeValue();
236                         value.addContentType(sContentType);
237                     }
238                 } else if(elTemp.getLocalName().equals("href")) {
239                     if(elTemp.getChildNodes().getLength()>0 && elTemp.getFirstChild().getNodeType()==Node.TEXT_NODE) {
240                         String JavaDoc sHREF = elTemp.getFirstChild().getNodeValue();
241                         value.setPath(sHREF);
242                     }
243                 } else if(elTemp.getLocalName().equals("maxOccurs")) {
244                     if(elTemp.getChildNodes().getLength()>0 && elTemp.getFirstChild().getNodeType()==Node.TEXT_NODE) {
245                         String JavaDoc sMaxOccurs = elTemp.getFirstChild().getNodeValue();
246                         value.setMaxOccurs( Integer.parseInt(sMaxOccurs) );
247                     }
248                 } else if(elTemp.getLocalName().equals("minOccurs")) {
249                     if(elTemp.getChildNodes().getLength()>0 && elTemp.getFirstChild().getNodeType()==Node.TEXT_NODE) {
250                         String JavaDoc sMinOccurs = elTemp.getFirstChild().getNodeValue();
251                         value.setMinOccurs( Integer.parseInt(sMinOccurs) );
252                     }
253                 }
254             }
255             
256         }
257     }
258
259 }
260
Popular Tags