1 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 43 public class PropPatch extends AbstractWebDAVMethod { 44 45 48 public static String METHOD_NAME = "PROPPATCH"; 49 50 53 private Map m_setProperties = new HashMap(5); 54 55 58 private Map m_removeProperties = new HashMap(5); 59 60 63 private VirtualFile m_vfFile = null; 64 65 70 public PropPatch(String sURL) { 71 super(METHOD_NAME, sURL); 72 } 73 74 80 public PropPatch(String sURL, VirtualFile vfFile) { 81 super(METHOD_NAME, sURL); 82 this.m_vfFile = vfFile; 83 } 84 85 90 public void addSetProperty(PropertyInstance propInst) { 91 this.m_setProperties.put( propInst.getNamespaceURI() + "#" + propInst.getName(), propInst ); 92 } 93 94 99 public void removeSetProperty(PropertyInstance propInst) { 100 this.m_setProperties.remove( propInst.getNamespaceURI() + "#" + propInst.getName() ); 101 } 102 103 110 public List getSetPropertyValues(String sNamespace, String 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 125 public void addRemoveProperty(PropertyInstance propInst) { 126 this.m_removeProperties.put( propInst.getNamespaceURI() + "#" + propInst.getName(), propInst ); 127 } 128 129 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 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 e) { 210 e.printStackTrace(); 211 } 212 return sXML.getBytes(); 213 } 214 215 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 } 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 cce) { 255 256 } 257 } 258 } 259 260 } 261 | Popular Tags |