KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > webdav > client > methods > PropPatch


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.methods;
20
21 import java.io.*;
22 import java.util.*;
23
24 import javax.xml.parsers.*;
25
26 import org.openharmonise.commons.xml.*;
27 import org.openharmonise.commons.xml.namespace.*;
28 import org.openharmonise.vfs.*;
29 import org.openharmonise.vfs.metadata.*;
30 import org.openharmonise.vfs.metadata.range.*;
31 import org.openharmonise.webdav.client.*;
32 import org.openharmonise.webdav.client.value.*;
33 import org.w3c.dom.*;
34
35
36 /**
37  * PropPatch method.
38  *
39  * @author Matthew Large
40  * @version $Revision: 1.1 $
41  *
42  */

43 public class PropPatch extends AbstractWebDAVMethod {
44
45     /**
46      * Method name.
47      */

48     public static String JavaDoc METHOD_NAME = "PROPPATCH";
49     
50     /**
51      * Map of properties to be set.
52      */

53     private Map m_setProperties = new HashMap(5);
54     
55     /**
56      * Map of properties to be removed.
57      */

58     private Map m_removeProperties = new HashMap(5);
59     
60     /**
61      * Virtual file of properties to set.
62      */

63     private VirtualFile m_vfFile = null;
64
65     /**
66      * Constructs a new proppatch method.
67      *
68      * @param sURL URL for request
69      */

70     public PropPatch(String JavaDoc sURL) {
71         super(METHOD_NAME, sURL);
72     }
73
74     /**
75      * Constructs a new proppatch method.
76      *
77      * @param sURL URL for request
78      * @param vfFile Virtual file to proppatch
79      */

80     public PropPatch(String JavaDoc sURL, VirtualFile vfFile) {
81         super(METHOD_NAME, sURL);
82         this.m_vfFile = vfFile;
83     }
84     
85     /**
86      * Adds a property instance to set.
87      *
88      * @param propInst Property instance to add
89      */

90     public void addSetProperty(PropertyInstance propInst) {
91         this.m_setProperties.put( propInst.getNamespaceURI() + "#" + propInst.getName(), propInst );
92     }
93     
94     /**
95      * Removes a property instance to set.
96      *
97      * @param propInst Property instance to remove
98      */

99     public void removeSetProperty(PropertyInstance propInst) {
100         this.m_setProperties.remove( propInst.getNamespaceURI() + "#" + propInst.getName() );
101     }
102     
103     /**
104      * Returna a list of values for a property.
105      *
106      * @param sNamespace Namespace
107      * @param sName Name
108      * @return List of {@link ValueInstance} objects.
109      */

110     public List getSetPropertyValues(String JavaDoc sNamespace, String JavaDoc sName) {
111         List aReturn = null;
112         
113         PropertyInstance prop = (PropertyInstance)this.m_setProperties.get( sNamespace + "#" + sName );
114         if( prop != null ) {
115             aReturn = prop.getValues();
116         }
117         return aReturn;
118     }
119     
120     /**
121      * Adds a property instance to remove.
122      *
123      * @param propInst Property instance to add
124      */

125     public void addRemoveProperty(PropertyInstance propInst) {
126         this.m_removeProperties.put( propInst.getNamespaceURI() + "#" + propInst.getName(), propInst );
127     }
128     
129     /**
130      * Removes a property instance to remove.
131      *
132      * @param propInst Property instance to remove
133      */

134     public void removeRemoveProperty(PropertyInstance propInst) {
135         this.m_setProperties.remove( propInst.getNamespaceURI() + "#" + propInst.getName() );
136     }
137     
138     public byte[] getData() {
139         
140         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
141         factory.setNamespaceAware(true);
142         Document xmlDoc = null;
143         try {
144             xmlDoc = factory.newDocumentBuilder().newDocument();
145         } catch (ParserConfigurationException e) {
146             e.printStackTrace();
147         }
148         
149         Element elUpdate = xmlDoc.createElementNS( PropPatch.WEBDAV_NAMESPACE ,"propertyupdate");
150         xmlDoc.appendChild(elUpdate);
151         
152         if( !this.m_setProperties.isEmpty() ) {
153             
154             Iterator itor = this.m_setProperties.values().iterator();
155             while( itor.hasNext() ) {
156                 PropertyInstance prop = (PropertyInstance)itor.next();
157                 Element elSet = xmlDoc.createElementNS( PropPatch.WEBDAV_NAMESPACE, "set");
158                 elUpdate.appendChild(elSet);
159                 Element elProp = xmlDoc.createElementNS( PropPatch.WEBDAV_NAMESPACE, "prop");
160                 elSet.appendChild(elProp);
161                 
162                 PropPatch.publishPropertyInstanceToXML(xmlDoc, prop, elProp);
163             }
164         } else if(this.m_vfFile!=null) {
165             List props = this.m_vfFile.getProperties();
166             Iterator itor = props.iterator();
167             while( itor.hasNext() ) {
168                 PropertyInstance prop = (PropertyInstance)itor.next();
169
170                 Element elSet = xmlDoc.createElementNS( PropPatch.WEBDAV_NAMESPACE, "set");
171                 elUpdate.appendChild(elSet);
172                 Element elProp = xmlDoc.createElementNS( PropPatch.WEBDAV_NAMESPACE, "prop");
173                 elSet.appendChild(elProp);
174                 
175                 PropPatch.publishPropertyInstanceToXML(xmlDoc, prop, elProp);
176             }
177         }
178         
179         if( !this.m_removeProperties.isEmpty() ) {
180             
181             Iterator itor = this.m_removeProperties.values().iterator();
182             while( itor.hasNext() ) {
183                 PropertyInstance prop = (PropertyInstance)itor.next();
184                 Element elRemove = xmlDoc.createElementNS( PropPatch.WEBDAV_NAMESPACE, "remove");
185                 elUpdate.appendChild(elRemove);
186                 Element elProp = xmlDoc.createElementNS( PropPatch.WEBDAV_NAMESPACE, "prop");
187                 elRemove.appendChild(elProp);
188                 
189                 Element elThisProp = xmlDoc.createElementNS( prop.getNamespaceURI(), prop.getName() );
190                 elProp.appendChild(elThisProp);
191             }
192         }
193         
194         XMLPrettyPrint printer = new XMLPrettyPrint();
195         printer.setNamespaceAware(true);
196         
197         String JavaDoc sXML = null;
198         try {
199             sXML = printer.printNode(xmlDoc.getDocumentElement());
200         } catch (NamespaceClashException e1) {
201             e1.printStackTrace();
202         }
203         
204         ByteArrayOutputStream baos = null;
205         OutputStreamWriter osw = null;
206         
207         try {
208             return sXML.getBytes("UTF-8");
209         } catch(Exception JavaDoc e) {
210             e.printStackTrace();
211         }
212         return sXML.getBytes();
213     }
214     
215     /**
216      * Publishes a property instance to XML.
217      *
218      * @param xmlDoc XML owning document
219      * @param propInst Property instance to publish
220      * @param parentEl Element to append to
221      */

222     public static void publishPropertyInstanceToXML(Document xmlDoc, PropertyInstance propInst, Element parentEl) {
223         
224         Element elThisProp = xmlDoc.createElementNS( propInst.getNamespaceURI(), propInst.getName() );
225         parentEl.appendChild(elThisProp);
226         
227         Range range = propInst.getDefinition().getRange();
228         
229         if(range instanceof BooleanRange) {
230             DAVBooleanValue.toXML(xmlDoc, elThisProp, propInst.getValues());
231         } else if(range instanceof DateTimeRange) {
232             DAVDateTimeValue.toXML(xmlDoc, elThisProp, propInst.getValues());
233         } else if(range instanceof DateRange) {
234             DAVDateValue.toXML(xmlDoc, elThisProp, propInst.getValues());
235         } else if(range instanceof DomainRange) {
236             DAVDomainValue.toXML(xmlDoc, elThisProp, propInst.getValues());
237         } else if(range instanceof FloatRange) {
238             DAVFloatValue.toXML(xmlDoc, elThisProp, propInst.getValues());
239         } else if(range instanceof IntegerRange) {
240             DAVIntegerValue.toXML(xmlDoc, elThisProp, propInst.getValues());
241         } else if(range instanceof PropertyRange) {
242             DAVPropertyValue.toXML(xmlDoc, elThisProp, propInst.getValues());
243         } else if(range instanceof RangeRange) {
244             DAVRangeValue.toXML(xmlDoc, elThisProp, propInst.getValues());
245         } else if(range instanceof ResourceRange || range instanceof CollectionRange ) {
246             DAVResourceValue.toXML(xmlDoc, elThisProp, propInst.getValues());
247         } else if(range instanceof URIRange) {
248             // NO-OP
249
} else if(range instanceof ValueRange) {
250             DAVValueValue.toXML(xmlDoc, elThisProp, propInst.getValues());
251         } else {
252             try {
253                 DAVStringValue.toXML(xmlDoc, elThisProp, propInst.getValues());
254             } catch(ClassCastException JavaDoc cce) {
255
256             }
257         }
258     }
259
260 }
261
Popular Tags