KickJava   Java API By Example, From Geeks To Geeks.

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


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 vocabulary value implementation.
36  *
37  * @author Matthew Large
38  * @version $Revision: 1.1 $
39  *
40  */

41 public class DAVValueValue extends ValueValue {
42
43     /**
44      *
45      */

46     public DAVValueValue() {
47         super();
48     }
49     
50     /**
51      * Publishes WebDAV vocabulary values to a XML element.
52      *
53      * @param xmlDoc Owning XML document
54      * @param propEl Element to append to
55      * @param aValues List of {@link DAVBooleanValue} objects
56      */

57     public static void toXML(Document JavaDoc xmlDoc, Element JavaDoc propEl, List JavaDoc aValues) {
58         if(aValues.size()==1) {
59             Element JavaDoc hrefEl = xmlDoc.createElementNS(NamespaceType.DAV.getURI(), "href");
60             propEl.appendChild(hrefEl);
61             Text JavaDoc txt = xmlDoc.createTextNode( ((DAVValueValue)aValues.get(0)).getValue() );
62             hrefEl.appendChild(txt);
63         } else if(aValues.size()>1) {
64             propEl.setAttributeNS(NamespaceType.SOAP_ENCODING.getURI(), "type", "Array" );
65             propEl.setAttributeNS(NamespaceType.SOAP_ENCODING.getURI(), "arraySize", Integer.toString(aValues.size()) );
66             propEl.setAttributeNS(NamespaceType.SOAP_ENCODING.getURI(), "itemType", NamespaceType.DAV.getPrefix()+":href");
67             Iterator JavaDoc itor = aValues.iterator();
68             while (itor.hasNext()) {
69                 DAVValueValue val = (DAVValueValue) itor.next();
70                 Element JavaDoc arrayEl = xmlDoc.createElementNS(NamespaceType.DAV.getURI(), "href");
71                 Text JavaDoc txt = xmlDoc.createTextNode( val.getValue() );
72                 arrayEl.appendChild(txt);
73                 propEl.appendChild(arrayEl);
74             }
75         }
76     }
77     
78     /**
79      * Populates a property instance with WebDAV vocabulary values from
80      * XML.
81      *
82      * @param propInst Property instance to populate
83      * @param propEl Root element of property instance
84      */

85     public static void fromXML(PropertyInstance propInst, Element JavaDoc propEl) {
86         try {
87             if(propEl.getChildNodes().getLength()==1) {
88                 Element JavaDoc elHREF = (Element JavaDoc) propEl.getFirstChild();
89                 Text JavaDoc txt = (Text JavaDoc) elHREF.getFirstChild();
90                 if(txt!=null) {
91                     DAVValueValue val = (DAVValueValue) propInst.getNewValueInstance();
92                     val.setValue( txt.getData() );
93                     propInst.addValueWithoutFiringVirtualFileEvent(val);
94                 }
95             } else if(propEl.getChildNodes().getLength()>1) {
96                 NodeList JavaDoc nl = propEl.getChildNodes();
97                 for (int i = 0; i < nl.getLength(); i++) {
98                     Node JavaDoc node = nl.item(i);
99                     if(node.getNodeType()==Node.ELEMENT_NODE) {
100                         Text JavaDoc txt = (Text JavaDoc) node.getFirstChild();
101                         if(txt!=null) {
102                             DAVValueValue val = (DAVValueValue) propInst.getNewValueInstance();
103                             val.setValue( txt.getData() );
104                             propInst.addValueWithoutFiringVirtualFileEvent(val);
105                         }
106                     }
107                 }
108             }
109         } catch(Exception JavaDoc e) {
110             e.printStackTrace(System.err);
111         }
112     }
113
114 }
115
Popular Tags