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.him.harmonise.*;
026: import org.openharmonise.vfs.metadata.*;
027: import org.openharmonise.vfs.metadata.value.*;
028: import org.w3c.dom.Document;
029: import org.w3c.dom.Element;
030: import org.w3c.dom.Node;
031: import org.w3c.dom.NodeList;
032: import org.w3c.dom.Text;
033:
034: /**
035: * WebDAV domain value implementation.
036: *
037: * @author Matthew Large
038: * @version $Revision: 1.1 $
039: *
040: */
041: public class DAVDomainValue extends DomainValue {
042:
043: /**
044: *
045: */
046: public DAVDomainValue() {
047: super ();
048: }
049:
050: /**
051: * Constructs a new WebDAV domain value.
052: *
053: * @param sPath Full path of domain
054: */
055: public DAVDomainValue(String sPath) {
056: super (sPath);
057: }
058:
059: /**
060: * Constructs a new WebDAV domain value.
061: *
062: * @param sPath Full path of domain
063: * @param nMinOccurs Mimumum occurrence
064: * @param nMaxOccurs Maximum occurrence
065: * @param aContentTypes List of valid content types
066: * @param sResourceType Valid resource type
067: */
068: public DAVDomainValue(String sPath, int nMinOccurs, int nMaxOccurs,
069: List aContentTypes, String sResourceType) {
070: super (sPath, nMinOccurs, nMaxOccurs, aContentTypes,
071: sResourceType);
072: }
073:
074: /**
075: * Publishes WebDAV domain values to a XML element.
076: *
077: * @param xmlDoc Owning XML document
078: * @param propEl Element to append to
079: * @param aValues List of {@link DAVDomainValue} objects
080: */
081: public static void toXML(Document xmlDoc, Element propEl,
082: List aValues) {
083: if (aValues.size() == 1) {
084: DAVDomainValue.toXMLDomainData(xmlDoc, propEl,
085: (DAVDomainValue) aValues.get(0));
086: } else if (aValues.size() > 1) {
087: propEl.setAttributeNS(NamespaceType.SOAP_ENCODING.getURI(),
088: "type", "Array");
089: propEl.setAttributeNS(NamespaceType.SOAP_ENCODING.getURI(),
090: "arraySize", Integer.toString(aValues.size()));
091: propEl.setAttributeNS(NamespaceType.SOAP_ENCODING.getURI(),
092: "itemType", NamespaceType.DAV.getPrefix()
093: + ":domainData");
094: Iterator itor = aValues.iterator();
095: while (itor.hasNext()) {
096: DAVDomainValue value = (DAVDomainValue) itor.next();
097: Element elDomainData = xmlDoc.createElementNS(
098: NamespaceType.DAV.getURI(), "domainData");
099: propEl.appendChild(elDomainData);
100: DAVDomainValue.toXMLDomainData(xmlDoc, elDomainData,
101: value);
102: }
103: }
104: }
105:
106: /**
107: * Publishes WebDAV a domain value to a XML element.
108: *
109: * @param xmlDoc Owning XML document
110: * @param propEl Element to append to
111: * @param aValues Value
112: */
113: private static void toXMLDomainData(Document xmlDoc,
114: Element parentEl, DAVDomainValue value) {
115: Element elResourceType = null;
116:
117: elResourceType = xmlDoc.createElementNS(NamespaceType.DAV
118: .getURI(), "resourcetype");
119: parentEl.appendChild(elResourceType);
120:
121: if (value.getResourceType() == DomainValue.RESOURCE) {
122: if (value.getPath().startsWith(
123: HarmonisePaths.PATH_PROPERTIES)) {
124: Element elType = xmlDoc.createElementNS(
125: NamespaceType.DAV.getURI(),
126: DAVDomainValue.PROPERTY);
127: elResourceType.appendChild(elType);
128: } else if (value.getPath().startsWith(
129: HarmonisePaths.PATH_VALUES)) {
130: Element elType = xmlDoc.createElementNS(
131: NamespaceType.DAV.getURI(),
132: DAVDomainValue.VALUE);
133: elResourceType.appendChild(elType);
134: } else {
135: Element elType = xmlDoc.createElementNS(
136: NamespaceType.DAV.getURI(), value
137: .getResourceType());
138: elResourceType.appendChild(elType);
139: }
140: } else {
141: Element elType = xmlDoc.createElementNS(NamespaceType.DAV
142: .getURI(), value.getResourceType());
143: elResourceType.appendChild(elType);
144: }
145:
146: if (value.getContentTypes().size() > 0) {
147: Iterator itorCT = value.getContentTypes().iterator();
148: while (itorCT.hasNext()) {
149: String sContentType = (String) itorCT.next();
150: Element elContentType = xmlDoc.createElementNS(
151: NamespaceType.DAV.getURI(), "contenttype");
152: parentEl.appendChild(elContentType);
153:
154: Text txt = xmlDoc.createTextNode(sContentType);
155: elContentType.appendChild(txt);
156: }
157: }
158:
159: if (value.getPath() != null && !value.getPath().equals("")) {
160: Element elHREF = xmlDoc.createElementNS(NamespaceType.DAV
161: .getURI(), "href");
162: parentEl.appendChild(elHREF);
163: Text txtHREF = xmlDoc.createTextNode(value.getPath());
164: elHREF.appendChild(txtHREF);
165: }
166:
167: Element elMaxOccurs = xmlDoc.createElementNS(NamespaceType.DAV
168: .getURI(), "maxOccurs");
169: parentEl.appendChild(elMaxOccurs);
170: Text txtMax = xmlDoc.createTextNode(Integer.toString(value
171: .getMaxOccurs()));
172: elMaxOccurs.appendChild(txtMax);
173:
174: Element elMinOccurs = xmlDoc.createElementNS(NamespaceType.DAV
175: .getURI(), "minOccurs");
176: parentEl.appendChild(elMinOccurs);
177: Text txtMin = xmlDoc.createTextNode(Integer.toString(value
178: .getMinOccurs()));
179: elMinOccurs.appendChild(txtMin);
180: }
181:
182: /**
183: * Populates a property instance with WebDAV domain values from
184: * XML.
185: *
186: * @param propInst Property instance to populate
187: * @param propEl Root element of property instance
188: */
189: public static void fromXML(PropertyInstance propInst, Element propEl) {
190: boolean bMultiple = false;
191: if (propEl.getChildNodes().getLength() > 1) {
192: NodeList nl = propEl.getChildNodes();
193: for (int i = 0; i < nl.getLength(); i++) {
194: Node node = nl.item(i);
195: if (node.getNodeType() == Node.ELEMENT_NODE) {
196: if (((Element) node).getLocalName()
197: .equalsIgnoreCase("domainData")) {
198: bMultiple = true;
199: }
200: }
201: }
202: }
203:
204: if (!bMultiple) {
205: DAVDomainValue val = (DAVDomainValue) propInst
206: .getNewValueInstance();
207: DAVDomainValue.fromXMLDomainData(propInst, propEl, val);
208: if (val.getPath() != null && !val.getPath().equals("")) {
209: propInst.addValueWithoutFiringVirtualFileEvent(val);
210: }
211: } else if (propEl.getChildNodes().getLength() > 1) {
212: NodeList nl = propEl.getChildNodes();
213: for (int i = 0; i < nl.getLength(); i++) {
214: Node node = nl.item(i);
215: if (node.getNodeType() == Node.ELEMENT_NODE) {
216: DAVDomainValue val = (DAVDomainValue) propInst
217: .getNewValueInstance();
218: DAVDomainValue.fromXMLDomainData(propInst,
219: (Element) node, val);
220: if (val.getPath() != null
221: && !val.getPath().equals("")) {
222: propInst
223: .addValueWithoutFiringVirtualFileEvent(val);
224: }
225: }
226: }
227: }
228: }
229:
230: /**
231: * Populates a property instance with a WebDAV domain value from
232: * XML.
233: *
234: * @param propInst Property instance to populate
235: * @param parentEl Root element of property instance
236: * @param value Value to populate
237: */
238: public static void fromXMLDomainData(PropertyInstance propInst,
239: Element parentEl, DAVDomainValue value) {
240: NodeList nl = parentEl.getChildNodes();
241: for (int i = 0; i < nl.getLength(); i++) {
242: Node node = nl.item(i);
243: if (node.getNodeType() == Node.ELEMENT_NODE) {
244: Element elTemp = (Element) node;
245: if (elTemp.getLocalName().equals("resourcetype")) {
246: if (elTemp.getChildNodes().getLength() > 0
247: && elTemp.getFirstChild().getNodeType() == Node.ELEMENT_NODE) {
248: Element elType = (Element) elTemp
249: .getFirstChild();
250: if (elType.getLocalName().equals("collection")) {
251: value
252: .setResourceType(DAVDomainValue.COLLECTION);
253: } else if (elType.getLocalName().equals(
254: "property-resource")) {
255: value
256: .setResourceType(DAVDomainValue.RESOURCE);
257: } else if (elType.getLocalName()
258: .equals("value")) {
259: value
260: .setResourceType(DAVDomainValue.RESOURCE);
261: } else {
262: value
263: .setResourceType(DAVDomainValue.RESOURCE);
264: }
265: } else {
266: value.setResourceType(DAVDomainValue.RESOURCE);
267: }
268: } else if (elTemp.getLocalName().equals("contentype")) {
269: if (elTemp.getChildNodes().getLength() > 0
270: && elTemp.getFirstChild().getNodeType() == Node.TEXT_NODE) {
271: String sContentType = elTemp.getFirstChild()
272: .getNodeValue();
273: value.addContentType(sContentType);
274: }
275: } else if (elTemp.getLocalName().equals("href")) {
276: if (elTemp.getChildNodes().getLength() > 0
277: && elTemp.getFirstChild().getNodeType() == Node.TEXT_NODE) {
278: String sHREF = elTemp.getFirstChild()
279: .getNodeValue();
280: value.setPath(sHREF);
281: }
282: } else if (elTemp.getLocalName().equals("maxOccurs")) {
283: if (elTemp.getChildNodes().getLength() > 0
284: && elTemp.getFirstChild().getNodeType() == Node.TEXT_NODE) {
285: String sMaxOccurs = elTemp.getFirstChild()
286: .getNodeValue();
287: value
288: .setMaxOccurs(Integer
289: .parseInt(sMaxOccurs));
290: }
291: } else if (elTemp.getLocalName().equals("minOccurs")) {
292: if (elTemp.getChildNodes().getLength() > 0
293: && elTemp.getFirstChild().getNodeType() == Node.TEXT_NODE) {
294: String sMinOccurs = elTemp.getFirstChild()
295: .getNodeValue();
296: value
297: .setMinOccurs(Integer
298: .parseInt(sMinOccurs));
299: }
300: }
301: }
302:
303: }
304: }
305:
306: }
|