KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > webdav > client > methods > bind > Bind


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

39 public class Bind extends AbstractWebDAVMethod {
40
41     /**
42      * Method name.
43      */

44     public static String JavaDoc METHOD_NAME = "BIND";
45     
46     /**
47      * Source URL.
48      */

49     private String JavaDoc m_sBindSourceURI = "";
50     
51     /**
52      * Destination collection URL.
53      */

54     private String JavaDoc m_sBindDestinationCollection = "";
55     
56     /**
57      * Desination segment.
58      */

59     private String JavaDoc m_sBindDestinationSegment = "";
60     
61     /**
62      * Root element name.
63      */

64     private String JavaDoc m_sRootElementName = "bind";
65     
66     /**
67      * Constructs a new bind method.
68      *
69      * @param sURL URL for request
70      */

71     public Bind(String JavaDoc sURL) {
72         this(METHOD_NAME, sURL);
73     }
74     
75     /**
76      * Constructs a new bind method.
77      *
78      * @param sMethodName Method name
79      * @param sURL URL for request
80      */

81     protected Bind(String JavaDoc sMethod, String JavaDoc sURL) {
82         super(sMethod, null);
83         this.m_sBindSourceURI=sURL;
84     }
85     
86     /**
87      * Sets the root element name.
88      *
89      * @param sRootElementName Root element name
90      */

91     protected void setRootElementName(String JavaDoc sRootElementName) {
92         this.m_sRootElementName = sRootElementName;
93     }
94
95     public void setDestination(String JavaDoc sDestination) {
96         m_sBindDestinationCollection = sDestination.substring(0, sDestination.lastIndexOf("/"));
97         super.setURL( m_sBindDestinationCollection );
98         m_sBindDestinationSegment = sDestination.substring(sDestination.lastIndexOf("/")+1);
99     }
100
101     /**
102      * Returns the destination URL.
103      *
104      * @return Destination URL
105      */

106     public String JavaDoc getBindDestination() {
107         return m_sBindDestinationCollection + "/" + m_sBindDestinationSegment;
108     }
109
110     public byte[] getData() {
111         
112         DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
113         factory.setNamespaceAware(true);
114         Document JavaDoc xmlDoc = null;
115         try {
116             xmlDoc = factory.newDocumentBuilder().newDocument();
117         } catch (ParserConfigurationException JavaDoc e) {
118             e.printStackTrace();
119         }
120         
121         Element JavaDoc elBind = xmlDoc.createElementNS( Bind.WEBDAV_NAMESPACE , m_sRootElementName);
122         xmlDoc.appendChild(elBind);
123         
124         Element JavaDoc elHREF = xmlDoc.createElementNS( Bind.WEBDAV_NAMESPACE, "href");
125         Text JavaDoc txt = xmlDoc.createTextNode(this.m_sBindSourceURI);
126         elHREF.appendChild(txt);
127         elBind.appendChild(elHREF);
128         
129         Element JavaDoc elSEG = xmlDoc.createElementNS( Bind.WEBDAV_NAMESPACE, "segment");
130         txt = xmlDoc.createTextNode(this.m_sBindDestinationSegment);
131         elSEG.appendChild(txt);
132         elBind.appendChild(elSEG);
133         
134         
135         
136         XMLPrettyPrint printer = new XMLPrettyPrint();
137         printer.setNamespaceAware(true);
138         
139         String JavaDoc sXML = null;
140         try {
141             sXML = printer.printNode(xmlDoc.getDocumentElement());
142         } catch (NamespaceClashException e1) {
143             e1.printStackTrace();
144         }
145         
146         return sXML.getBytes();
147     }
148
149 }
150
Popular Tags