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.ArrayList;
022: import java.util.Iterator;
023: import java.util.List;
024:
025: import org.openharmonise.commons.xml.namespace.*;
026: import org.openharmonise.vfs.*;
027: import org.openharmonise.vfs.metadata.*;
028: import org.openharmonise.vfs.metadata.value.*;
029: import org.openharmonise.webdav.client.*;
030: import org.openharmonise.webdav.client.methods.*;
031: import org.w3c.dom.Document;
032: import org.w3c.dom.Element;
033: import org.w3c.dom.Node;
034: import org.w3c.dom.NodeList;
035:
036: /**
037: * WebDAV compound value implementation.
038: *
039: * @author Matthew Large
040: * @version $Revision: 1.1 $
041: *
042: */
043: public class DAVPropertyValue extends PropertyValue {
044:
045: /**
046: *
047: */
048: public DAVPropertyValue() {
049: super ();
050: }
051:
052: /**
053: * Constructs a new WebDAV compound value.
054: *
055: * @param aPropertyInstances List of {@link PropertyInstance} objects
056: */
057: public DAVPropertyValue(ArrayList aPropertyInstances) {
058: super (aPropertyInstances);
059: }
060:
061: /**
062: * Publishes WebDAV compound values to a XML element.
063: *
064: * @param xmlDoc Owning XML document
065: * @param propEl Element to append to
066: * @param aValues List of {@link DAVBooleanValue} objects
067: */
068: public static void toXML(Document xmlDoc, Element propEl,
069: List aValues) {
070: if (aValues.size() == 1) {
071:
072: Element innerPropEl = xmlDoc.createElementNS(
073: NamespaceType.DAV.getURI(), "prop");
074: propEl.appendChild(innerPropEl);
075:
076: Iterator itor = ((DAVPropertyValue) aValues.get(0))
077: .getValue().iterator();
078: while (itor.hasNext()) {
079: PropertyInstance propInst = (PropertyInstance) itor
080: .next();
081: PropPatch.publishPropertyInstanceToXML(xmlDoc,
082: propInst, innerPropEl);
083: }
084: } else if (aValues.size() > 1) {
085: propEl.setAttributeNS(NamespaceType.SOAP_ENCODING.getURI(),
086: "type", "Array");
087: propEl.setAttributeNS(NamespaceType.SOAP_ENCODING.getURI(),
088: "arraySize", Integer.toString(aValues.size()));
089: propEl
090: .setAttributeNS(NamespaceType.SOAP_ENCODING
091: .getURI(), "itemType", NamespaceType.DAV
092: .getPrefix()
093: + ":prop");
094: Iterator itor = aValues.iterator();
095: while (itor.hasNext()) {
096: Element innerPropEl = xmlDoc.createElementNS(
097: NamespaceType.DAV.getURI(), "prop");
098: propEl.appendChild(innerPropEl);
099:
100: Iterator itor2 = ((DAVPropertyValue) itor.next())
101: .getValue().iterator();
102: while (itor2.hasNext()) {
103: PropertyInstance propInst = (PropertyInstance) itor2
104: .next();
105: PropPatch.publishPropertyInstanceToXML(xmlDoc,
106: propInst, innerPropEl);
107: }
108: propEl.appendChild(innerPropEl);
109: }
110: }
111: }
112:
113: /**
114: * Populates a property instance with WebDAV compound values from
115: * XML.
116: *
117: * @param vfFile Virtual file which owns property instances
118: * @param propInst Property instance to populate
119: * @param propEl Root element of property instance
120: */
121: public static void fromXML(VirtualFile vfFile,
122: PropertyInstance propInst, Element propEl) {
123: if (propEl.getChildNodes().getLength() == 1) {
124: Element containerEl = (Element) propEl.getFirstChild();
125: if (containerEl != null) {
126: DAVPropertyValue valInst = (DAVPropertyValue) propInst
127: .getNewValueInstance();
128: ArrayList aPropertyInstances = new ArrayList();
129: NodeList nl = containerEl.getChildNodes();
130: for (int i = 0; i < nl.getLength(); i++) {
131: Node node = nl.item(i);
132: if (node.getNodeType() == Node.ELEMENT_NODE) {
133: Element subPropEl = (Element) node;
134: PropertyInstance subProp = new PropertyInstance();
135: aPropertyInstances.add(subProp);
136: ((WebDAVFileSystem) vfFile.getVFS())
137: .populatePropertyInstance(vfFile,
138: subPropEl, subProp, false);
139: //prop.addPropertyInstance(subProp);
140: }
141: }
142: valInst.setValue(aPropertyInstances);
143: propInst.addValueWithoutFiringVirtualFileEvent(valInst);
144: }
145: } else if (propEl.getChildNodes().getLength() > 1) {
146: NodeList nl = propEl.getChildNodes();
147: for (int i = 0; i < nl.getLength(); i++) {
148: Node node = nl.item(i);
149: if (node.getNodeType() == Node.ELEMENT_NODE) {
150: Element containerEl = (Element) node;
151: if (containerEl != null) {
152: DAVPropertyValue valInst = (DAVPropertyValue) propInst
153: .getNewValueInstance();
154: ArrayList aPropertyInstances = new ArrayList();
155: NodeList nl2 = containerEl.getChildNodes();
156: for (int j = 0; j < nl2.getLength(); j++) {
157: Node node2 = nl2.item(j);
158: if (node.getNodeType() == Node.ELEMENT_NODE) {
159: Element subPropEl = (Element) node2;
160: PropertyInstance subProp = new PropertyInstance();
161: aPropertyInstances.add(subProp);
162: ((WebDAVFileSystem) vfFile.getVFS())
163: .populatePropertyInstance(
164: vfFile, subPropEl,
165: subProp, false);
166: }
167: }
168: valInst.setValue(aPropertyInstances);
169: propInst
170: .addValueWithoutFiringVirtualFileEvent(valInst);
171: }
172: }
173: }
174: }
175: }
176:
177: }
|