KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.ArrayList JavaDoc;
22 import java.util.Iterator JavaDoc;
23
24 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
25 import javax.xml.parsers.ParserConfigurationException JavaDoc;
26
27 import org.openharmonise.commons.xml.*;
28 import org.openharmonise.commons.xml.namespace.*;
29 import org.openharmonise.webdav.client.*;
30 import org.w3c.dom.Document JavaDoc;
31 import org.w3c.dom.Element JavaDoc;
32
33
34 /**
35  * PropFind method.
36  *
37  * @author Matthew Large
38  * @version $Revision: 1.1 $
39  *
40  */

41 public class PropFind extends AbstractWebDAVMethod {
42
43     /**
44      * Method name.
45      */

46     public static String JavaDoc METHOD_NAME = "PROPFIND";
47     
48     /**
49      * List of properties to request.
50      */

51     private ArrayList JavaDoc m_properties = new ArrayList JavaDoc(5);
52
53     /**
54      * Creates new propfind method.
55      *
56      * @param sURL URL for request
57      */

58     public PropFind(String JavaDoc sURL) {
59         super(METHOD_NAME, sURL);
60     }
61     
62     public byte[] getData() {
63         
64         DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
65         factory.setNamespaceAware(true);
66         Document JavaDoc xmlDoc = null;
67         try {
68             xmlDoc = factory.newDocumentBuilder().newDocument();
69         } catch (ParserConfigurationException JavaDoc e) {
70             e.printStackTrace();
71         }
72         
73         Element JavaDoc elPropFind = xmlDoc.createElementNS( PropPatch.WEBDAV_NAMESPACE ,"propfind");
74         xmlDoc.appendChild(elPropFind);
75         
76         if( this.m_properties.size()==0 ) {
77             Element JavaDoc elAllProp = xmlDoc.createElementNS( PropPatch.WEBDAV_NAMESPACE ,"allprop");
78             elPropFind.appendChild(elAllProp);
79         } else {
80             Element JavaDoc elProp = xmlDoc.createElementNS( PropPatch.WEBDAV_NAMESPACE, "prop");
81             elPropFind.appendChild(elProp);
82             Iterator JavaDoc itor = this.m_properties.iterator();
83             while(itor.hasNext()) {
84                 PropFindProperty prop = (PropFindProperty)itor.next();
85                 Element JavaDoc elProperty = xmlDoc.createElementNS( prop.getNamespaceURI(), prop.getName());
86                 elProp.appendChild(elProperty);
87             }
88         }
89         
90         XMLPrettyPrint printer = new XMLPrettyPrint();
91         printer.setNamespaceAware(true);
92         
93         String JavaDoc sXML = "";
94         try {
95             sXML = sXML + printer.printNode(xmlDoc.getDocumentElement());
96         } catch (NamespaceClashException e1) {
97             e1.printStackTrace();
98         }
99         
100         return sXML.getBytes();
101     }
102     
103     /**
104      * Adds a property to the request.
105      *
106      * @param sNamespaceURI Namepace
107      * @param sName Name
108      */

109     public void addProperty(String JavaDoc sNamespaceURI, String JavaDoc sName) {
110         this.m_properties.add( new PropFindProperty(sNamespaceURI, sName) );
111     }
112     
113 }
114
Popular Tags