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.bind;
020:
021: import javax.xml.parsers.DocumentBuilderFactory;
022: import javax.xml.parsers.ParserConfigurationException;
023:
024: import org.openharmonise.commons.xml.*;
025: import org.openharmonise.commons.xml.namespace.*;
026: import org.openharmonise.webdav.client.*;
027: import org.w3c.dom.Document;
028: import org.w3c.dom.Element;
029: import org.w3c.dom.Text;
030:
031: /**
032: * Bind method.
033: *
034: * @author Matthew Large
035: * @version $Revision: 1.1 $
036: *
037: */
038: public class Bind extends AbstractWebDAVMethod {
039:
040: /**
041: * Method name.
042: */
043: public static String METHOD_NAME = "BIND";
044:
045: /**
046: * Source URL.
047: */
048: private String m_sBindSourceURI = "";
049:
050: /**
051: * Destination collection URL.
052: */
053: private String m_sBindDestinationCollection = "";
054:
055: /**
056: * Desination segment.
057: */
058: private String m_sBindDestinationSegment = "";
059:
060: /**
061: * Root element name.
062: */
063: private String m_sRootElementName = "bind";
064:
065: /**
066: * Constructs a new bind method.
067: *
068: * @param sURL URL for request
069: */
070: public Bind(String sURL) {
071: this (METHOD_NAME, sURL);
072: }
073:
074: /**
075: * Constructs a new bind method.
076: *
077: * @param sMethodName Method name
078: * @param sURL URL for request
079: */
080: protected Bind(String sMethod, String sURL) {
081: super (sMethod, null);
082: this .m_sBindSourceURI = sURL;
083: }
084:
085: /**
086: * Sets the root element name.
087: *
088: * @param sRootElementName Root element name
089: */
090: protected void setRootElementName(String sRootElementName) {
091: this .m_sRootElementName = sRootElementName;
092: }
093:
094: public void setDestination(String sDestination) {
095: m_sBindDestinationCollection = sDestination.substring(0,
096: sDestination.lastIndexOf("/"));
097: super .setURL(m_sBindDestinationCollection);
098: m_sBindDestinationSegment = sDestination.substring(sDestination
099: .lastIndexOf("/") + 1);
100: }
101:
102: /**
103: * Returns the destination URL.
104: *
105: * @return Destination URL
106: */
107: public String getBindDestination() {
108: return m_sBindDestinationCollection + "/"
109: + m_sBindDestinationSegment;
110: }
111:
112: public byte[] getData() {
113:
114: DocumentBuilderFactory factory = DocumentBuilderFactory
115: .newInstance();
116: factory.setNamespaceAware(true);
117: Document xmlDoc = null;
118: try {
119: xmlDoc = factory.newDocumentBuilder().newDocument();
120: } catch (ParserConfigurationException e) {
121: e.printStackTrace();
122: }
123:
124: Element elBind = xmlDoc.createElementNS(Bind.WEBDAV_NAMESPACE,
125: m_sRootElementName);
126: xmlDoc.appendChild(elBind);
127:
128: Element elHREF = xmlDoc.createElementNS(Bind.WEBDAV_NAMESPACE,
129: "href");
130: Text txt = xmlDoc.createTextNode(this .m_sBindSourceURI);
131: elHREF.appendChild(txt);
132: elBind.appendChild(elHREF);
133:
134: Element elSEG = xmlDoc.createElementNS(Bind.WEBDAV_NAMESPACE,
135: "segment");
136: txt = xmlDoc.createTextNode(this .m_sBindDestinationSegment);
137: elSEG.appendChild(txt);
138: elBind.appendChild(elSEG);
139:
140: XMLPrettyPrint printer = new XMLPrettyPrint();
141: printer.setNamespaceAware(true);
142:
143: String sXML = null;
144: try {
145: sXML = printer.printNode(xmlDoc.getDocumentElement());
146: } catch (NamespaceClashException e1) {
147: e1.printStackTrace();
148: }
149:
150: return sXML.getBytes();
151: }
152:
153: }
|