001: /*
002: * The contents of this file are subject to the
003: * Mozilla Public License Version 1.1 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
009: * See the License for the specific language governing rights and
010: * limitations under the License.
011: *
012: * The Initial Developer of the Original Code is Simulacra Media Ltd.
013: * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
014: *
015: * All Rights Reserved.
016: *
017: * Contributor(s):
018: */
019: package org.openharmonise.webdav.client.value;
020:
021: import java.util.Iterator;
022: import java.util.List;
023:
024: import org.openharmonise.commons.xml.namespace.*;
025: import org.openharmonise.vfs.metadata.*;
026: import org.openharmonise.vfs.metadata.value.*;
027: import org.w3c.dom.Document;
028: import org.w3c.dom.Element;
029: import org.w3c.dom.Node;
030: import org.w3c.dom.NodeList;
031: import org.w3c.dom.Text;
032:
033: /**
034: * WebDAV float value implementation.
035: *
036: * @author Matthew Large
037: * @version $Revision: 1.1 $
038: *
039: */
040: public class DAVFloatValue extends FloatValue {
041:
042: /**
043: * Constructs a new WebDAV float value.
044: *
045: * @param fValue Value
046: */
047: public DAVFloatValue(float fValue) {
048: super (fValue);
049: }
050:
051: /**
052: *
053: */
054: public DAVFloatValue() {
055: super ();
056: }
057:
058: /**
059: * Publishes WebDAV float values to a XML element.
060: *
061: * @param xmlDoc Owning XML document
062: * @param propEl Element to append to
063: * @param aValues List of {@link DAVFloatValue} objects
064: */
065: public static void toXML(Document xmlDoc, Element propEl,
066: List aValues) {
067: if (aValues.size() == 1) {
068: Text txt = xmlDoc.createTextNode(Float
069: .toString(((DAVFloatValue) aValues.get(0))
070: .getValue()));
071: propEl.appendChild(txt);
072: } else if (aValues.size() > 1) {
073: propEl.setAttributeNS(NamespaceType.SOAP_ENCODING.getURI(),
074: "type", "Array");
075: propEl.setAttributeNS(NamespaceType.SOAP_ENCODING.getURI(),
076: "arraySize", Integer.toString(aValues.size()));
077: propEl.setAttributeNS(NamespaceType.SOAP_ENCODING.getURI(),
078: "itemType", NamespaceType.XML_SCHEMA.getPrefix()
079: + ":float");
080: Iterator itor = aValues.iterator();
081: while (itor.hasNext()) {
082: DAVFloatValue val = (DAVFloatValue) itor.next();
083: Element arrayEl = xmlDoc.createElementNS(
084: NamespaceType.XML_SCHEMA.getURI(), "float");
085: Text txt = xmlDoc.createTextNode(Float.toString(val
086: .getValue()));
087: arrayEl.appendChild(txt);
088: propEl.appendChild(arrayEl);
089: }
090: }
091: }
092:
093: /**
094: * Populates a property instance with WebDAV float values from
095: * XML.
096: *
097: * @param propInst Property instance to populate
098: * @param propEl Root element of property instance
099: */
100: public static void fromXML(PropertyInstance propInst, Element propEl) {
101: if (propEl.getChildNodes().getLength() == 1) {
102: Text txt = (Text) propEl.getFirstChild();
103: if (txt != null) {
104: DAVFloatValue val = (DAVFloatValue) propInst
105: .getNewValueInstance();
106: val.setValue(Float.parseFloat(txt.getData()));
107: propInst.addValueWithoutFiringVirtualFileEvent(val);
108: }
109: } else if (propEl.getChildNodes().getLength() > 1) {
110: NodeList nl = propEl.getChildNodes();
111: for (int i = 0; i < nl.getLength(); i++) {
112: Node node = nl.item(i);
113: if (node.getNodeType() == Node.ELEMENT_NODE) {
114: Text txt = (Text) node.getFirstChild();
115: if (txt != null) {
116: DAVFloatValue val = (DAVFloatValue) propInst
117: .getNewValueInstance();
118: val.setValue(Float.parseFloat(txt.getData()));
119: propInst
120: .addValueWithoutFiringVirtualFileEvent(val);
121: }
122: }
123: }
124: }
125: }
126:
127: }
|