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.methods;
020:
021: import java.io.*;
022: import java.util.*;
023:
024: import javax.xml.parsers.*;
025:
026: import org.openharmonise.commons.xml.*;
027: import org.openharmonise.commons.xml.namespace.*;
028: import org.openharmonise.vfs.*;
029: import org.openharmonise.vfs.metadata.*;
030: import org.openharmonise.vfs.metadata.range.*;
031: import org.openharmonise.webdav.client.*;
032: import org.openharmonise.webdav.client.value.*;
033: import org.w3c.dom.*;
034:
035: /**
036: * PropPatch method.
037: *
038: * @author Matthew Large
039: * @version $Revision: 1.1 $
040: *
041: */
042: public class PropPatch extends AbstractWebDAVMethod {
043:
044: /**
045: * Method name.
046: */
047: public static String METHOD_NAME = "PROPPATCH";
048:
049: /**
050: * Map of properties to be set.
051: */
052: private Map m_setProperties = new HashMap(5);
053:
054: /**
055: * Map of properties to be removed.
056: */
057: private Map m_removeProperties = new HashMap(5);
058:
059: /**
060: * Virtual file of properties to set.
061: */
062: private VirtualFile m_vfFile = null;
063:
064: /**
065: * Constructs a new proppatch method.
066: *
067: * @param sURL URL for request
068: */
069: public PropPatch(String sURL) {
070: super (METHOD_NAME, sURL);
071: }
072:
073: /**
074: * Constructs a new proppatch method.
075: *
076: * @param sURL URL for request
077: * @param vfFile Virtual file to proppatch
078: */
079: public PropPatch(String sURL, VirtualFile vfFile) {
080: super (METHOD_NAME, sURL);
081: this .m_vfFile = vfFile;
082: }
083:
084: /**
085: * Adds a property instance to set.
086: *
087: * @param propInst Property instance to add
088: */
089: public void addSetProperty(PropertyInstance propInst) {
090: this .m_setProperties.put(propInst.getNamespaceURI() + "#"
091: + propInst.getName(), propInst);
092: }
093:
094: /**
095: * Removes a property instance to set.
096: *
097: * @param propInst Property instance to remove
098: */
099: public void removeSetProperty(PropertyInstance propInst) {
100: this .m_setProperties.remove(propInst.getNamespaceURI() + "#"
101: + propInst.getName());
102: }
103:
104: /**
105: * Returna a list of values for a property.
106: *
107: * @param sNamespace Namespace
108: * @param sName Name
109: * @return List of {@link ValueInstance} objects.
110: */
111: public List getSetPropertyValues(String sNamespace, String sName) {
112: List aReturn = null;
113:
114: PropertyInstance prop = (PropertyInstance) this .m_setProperties
115: .get(sNamespace + "#" + sName);
116: if (prop != null) {
117: aReturn = prop.getValues();
118: }
119: return aReturn;
120: }
121:
122: /**
123: * Adds a property instance to remove.
124: *
125: * @param propInst Property instance to add
126: */
127: public void addRemoveProperty(PropertyInstance propInst) {
128: this .m_removeProperties.put(propInst.getNamespaceURI() + "#"
129: + propInst.getName(), propInst);
130: }
131:
132: /**
133: * Removes a property instance to remove.
134: *
135: * @param propInst Property instance to remove
136: */
137: public void removeRemoveProperty(PropertyInstance propInst) {
138: this .m_setProperties.remove(propInst.getNamespaceURI() + "#"
139: + propInst.getName());
140: }
141:
142: public byte[] getData() {
143:
144: DocumentBuilderFactory factory = DocumentBuilderFactory
145: .newInstance();
146: factory.setNamespaceAware(true);
147: Document xmlDoc = null;
148: try {
149: xmlDoc = factory.newDocumentBuilder().newDocument();
150: } catch (ParserConfigurationException e) {
151: e.printStackTrace();
152: }
153:
154: Element elUpdate = xmlDoc.createElementNS(
155: PropPatch.WEBDAV_NAMESPACE, "propertyupdate");
156: xmlDoc.appendChild(elUpdate);
157:
158: if (!this .m_setProperties.isEmpty()) {
159:
160: Iterator itor = this .m_setProperties.values().iterator();
161: while (itor.hasNext()) {
162: PropertyInstance prop = (PropertyInstance) itor.next();
163: Element elSet = xmlDoc.createElementNS(
164: PropPatch.WEBDAV_NAMESPACE, "set");
165: elUpdate.appendChild(elSet);
166: Element elProp = xmlDoc.createElementNS(
167: PropPatch.WEBDAV_NAMESPACE, "prop");
168: elSet.appendChild(elProp);
169:
170: PropPatch.publishPropertyInstanceToXML(xmlDoc, prop,
171: elProp);
172: }
173: } else if (this .m_vfFile != null) {
174: List props = this .m_vfFile.getProperties();
175: Iterator itor = props.iterator();
176: while (itor.hasNext()) {
177: PropertyInstance prop = (PropertyInstance) itor.next();
178:
179: Element elSet = xmlDoc.createElementNS(
180: PropPatch.WEBDAV_NAMESPACE, "set");
181: elUpdate.appendChild(elSet);
182: Element elProp = xmlDoc.createElementNS(
183: PropPatch.WEBDAV_NAMESPACE, "prop");
184: elSet.appendChild(elProp);
185:
186: PropPatch.publishPropertyInstanceToXML(xmlDoc, prop,
187: elProp);
188: }
189: }
190:
191: if (!this .m_removeProperties.isEmpty()) {
192:
193: Iterator itor = this .m_removeProperties.values().iterator();
194: while (itor.hasNext()) {
195: PropertyInstance prop = (PropertyInstance) itor.next();
196: Element elRemove = xmlDoc.createElementNS(
197: PropPatch.WEBDAV_NAMESPACE, "remove");
198: elUpdate.appendChild(elRemove);
199: Element elProp = xmlDoc.createElementNS(
200: PropPatch.WEBDAV_NAMESPACE, "prop");
201: elRemove.appendChild(elProp);
202:
203: Element elThisProp = xmlDoc.createElementNS(prop
204: .getNamespaceURI(), prop.getName());
205: elProp.appendChild(elThisProp);
206: }
207: }
208:
209: XMLPrettyPrint printer = new XMLPrettyPrint();
210: printer.setNamespaceAware(true);
211:
212: String sXML = null;
213: try {
214: sXML = printer.printNode(xmlDoc.getDocumentElement());
215: } catch (NamespaceClashException e1) {
216: e1.printStackTrace();
217: }
218:
219: ByteArrayOutputStream baos = null;
220: OutputStreamWriter osw = null;
221:
222: try {
223: return sXML.getBytes("UTF-8");
224: } catch (Exception e) {
225: e.printStackTrace();
226: }
227: return sXML.getBytes();
228: }
229:
230: /**
231: * Publishes a property instance to XML.
232: *
233: * @param xmlDoc XML owning document
234: * @param propInst Property instance to publish
235: * @param parentEl Element to append to
236: */
237: public static void publishPropertyInstanceToXML(Document xmlDoc,
238: PropertyInstance propInst, Element parentEl) {
239:
240: Element elThisProp = xmlDoc.createElementNS(propInst
241: .getNamespaceURI(), propInst.getName());
242: parentEl.appendChild(elThisProp);
243:
244: Range range = propInst.getDefinition().getRange();
245:
246: if (range instanceof BooleanRange) {
247: DAVBooleanValue.toXML(xmlDoc, elThisProp, propInst
248: .getValues());
249: } else if (range instanceof DateTimeRange) {
250: DAVDateTimeValue.toXML(xmlDoc, elThisProp, propInst
251: .getValues());
252: } else if (range instanceof DateRange) {
253: DAVDateValue
254: .toXML(xmlDoc, elThisProp, propInst.getValues());
255: } else if (range instanceof DomainRange) {
256: DAVDomainValue.toXML(xmlDoc, elThisProp, propInst
257: .getValues());
258: } else if (range instanceof FloatRange) {
259: DAVFloatValue.toXML(xmlDoc, elThisProp, propInst
260: .getValues());
261: } else if (range instanceof IntegerRange) {
262: DAVIntegerValue.toXML(xmlDoc, elThisProp, propInst
263: .getValues());
264: } else if (range instanceof PropertyRange) {
265: DAVPropertyValue.toXML(xmlDoc, elThisProp, propInst
266: .getValues());
267: } else if (range instanceof RangeRange) {
268: DAVRangeValue.toXML(xmlDoc, elThisProp, propInst
269: .getValues());
270: } else if (range instanceof ResourceRange
271: || range instanceof CollectionRange) {
272: DAVResourceValue.toXML(xmlDoc, elThisProp, propInst
273: .getValues());
274: } else if (range instanceof URIRange) {
275: // NO-OP
276: } else if (range instanceof ValueRange) {
277: DAVValueValue.toXML(xmlDoc, elThisProp, propInst
278: .getValues());
279: } else {
280: try {
281: DAVStringValue.toXML(xmlDoc, elThisProp, propInst
282: .getValues());
283: } catch (ClassCastException cce) {
284:
285: }
286: }
287: }
288:
289: }
|