/*
* The contents of this file are subject to the
* Mozilla Public License Version 1.1 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
* See the License for the specific language governing rights and
* limitations under the License.
*
* The Initial Developer of the Original Code is Simulacra Media Ltd.
* Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
*
* All Rights Reserved.
*
* Contributor(s):
*/
package org.openharmonise.webdav.client.methods.bind;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.openharmonise.commons.xml.*;
import org.openharmonise.commons.xml.namespace.*;
import org.openharmonise.webdav.client.*;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;
/**
* Bind method.
*
* @author Matthew Large
* @version $Revision: 1.1 $
*
*/
public class Bind extends AbstractWebDAVMethod {
/**
* Method name.
*/
public static String METHOD_NAME = "BIND";
/**
* Source URL.
*/
private String m_sBindSourceURI = "";
/**
* Destination collection URL.
*/
private String m_sBindDestinationCollection = "";
/**
* Desination segment.
*/
private String m_sBindDestinationSegment = "";
/**
* Root element name.
*/
private String m_sRootElementName = "bind";
/**
* Constructs a new bind method.
*
* @param sURL URL for request
*/
public Bind(String sURL) {
this(METHOD_NAME, sURL);
}
/**
* Constructs a new bind method.
*
* @param sMethodName Method name
* @param sURL URL for request
*/
protected Bind(String sMethod, String sURL) {
super(sMethod, null);
this.m_sBindSourceURI=sURL;
}
/**
* Sets the root element name.
*
* @param sRootElementName Root element name
*/
protected void setRootElementName(String sRootElementName) {
this.m_sRootElementName = sRootElementName;
}
public void setDestination(String sDestination) {
m_sBindDestinationCollection = sDestination.substring(0, sDestination.lastIndexOf("/"));
super.setURL( m_sBindDestinationCollection );
m_sBindDestinationSegment = sDestination.substring(sDestination.lastIndexOf("/")+1);
}
/**
* Returns the destination URL.
*
* @return Destination URL
*/
public String getBindDestination() {
return m_sBindDestinationCollection + "/" + m_sBindDestinationSegment;
}
public byte[] getData() {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
Document xmlDoc = null;
try {
xmlDoc = factory.newDocumentBuilder().newDocument();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
Element elBind = xmlDoc.createElementNS( Bind.WEBDAV_NAMESPACE , m_sRootElementName);
xmlDoc.appendChild(elBind);
Element elHREF = xmlDoc.createElementNS( Bind.WEBDAV_NAMESPACE, "href");
Text txt = xmlDoc.createTextNode(this.m_sBindSourceURI);
elHREF.appendChild(txt);
elBind.appendChild(elHREF);
Element elSEG = xmlDoc.createElementNS( Bind.WEBDAV_NAMESPACE, "segment");
txt = xmlDoc.createTextNode(this.m_sBindDestinationSegment);
elSEG.appendChild(txt);
elBind.appendChild(elSEG);
XMLPrettyPrint printer = new XMLPrettyPrint();
printer.setNamespaceAware(true);
String sXML = null;
try {
sXML = printer.printNode(xmlDoc.getDocumentElement());
} catch (NamespaceClashException e1) {
e1.printStackTrace();
}
return sXML.getBytes();
}
}
|