KickJava   Java API By Example, From Geeks To Geeks.

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


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

41 public class DAVStringValue extends StringValue {
42
43     /**
44      * Constructs a new WebDAV string value.
45      *
46      * @param sValue Value
47      */

48     public DAVStringValue(String JavaDoc sValue) {
49         super(sValue);
50     }
51
52     /**
53      *
54      */

55     public DAVStringValue() {
56         super();
57     }
58     
59     /**
60      * Publishes WebDAV string values to a XML element.
61      *
62      * @param xmlDoc Owning XML document
63      * @param propEl Element to append to
64      * @param aValues List of {@link DAVBooleanValue} objects
65      */

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

92     public static void fromXML(PropertyInstance propInst, Element JavaDoc propEl) {
93         if(propEl.getChildNodes().getLength()==1) {
94             Node JavaDoc node = propEl.getFirstChild();
95             if(node!=null && node.getNodeType()==Node.TEXT_NODE) {
96                 Text JavaDoc txt = (Text JavaDoc) propEl.getFirstChild();
97                 DAVStringValue val = (DAVStringValue) propInst.getNewValueInstance();
98                 val.setValue( txt.getData() );
99                 propInst.addValueWithoutFiringVirtualFileEvent(val);
100             }
101         } else if(propEl.getChildNodes().getLength()>1) {
102             NodeList JavaDoc nl = propEl.getChildNodes();
103             for (int i = 0; i < nl.getLength(); i++) {
104                 Node JavaDoc node = nl.item(i);
105                 if(node.getNodeType()==Node.ELEMENT_NODE) {
106                     Node JavaDoc node2 = node.getFirstChild();
107                     if(node2!=null && node2.getNodeType()==Node.TEXT_NODE) {
108                         Text JavaDoc txt = (Text JavaDoc) node.getFirstChild();
109                         DAVStringValue val = (DAVStringValue) propInst.getNewValueInstance();
110                         val.setValue( txt.getData() );
111                         propInst.addValueWithoutFiringVirtualFileEvent(val);
112                     }
113                 }
114             }
115         }
116     }
117
118 }
119
Popular Tags