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;
020:
021: import java.util.ArrayList;
022: import java.util.Iterator;
023:
024: import javax.xml.parsers.DocumentBuilderFactory;
025: import javax.xml.parsers.ParserConfigurationException;
026:
027: import org.openharmonise.commons.xml.*;
028: import org.openharmonise.commons.xml.namespace.*;
029: import org.openharmonise.webdav.client.*;
030: import org.w3c.dom.Document;
031: import org.w3c.dom.Element;
032:
033: /**
034: * PropFind method.
035: *
036: * @author Matthew Large
037: * @version $Revision: 1.1 $
038: *
039: */
040: public class PropFind extends AbstractWebDAVMethod {
041:
042: /**
043: * Method name.
044: */
045: public static String METHOD_NAME = "PROPFIND";
046:
047: /**
048: * List of properties to request.
049: */
050: private ArrayList m_properties = new ArrayList(5);
051:
052: /**
053: * Creates new propfind method.
054: *
055: * @param sURL URL for request
056: */
057: public PropFind(String sURL) {
058: super (METHOD_NAME, sURL);
059: }
060:
061: public byte[] getData() {
062:
063: DocumentBuilderFactory factory = DocumentBuilderFactory
064: .newInstance();
065: factory.setNamespaceAware(true);
066: Document xmlDoc = null;
067: try {
068: xmlDoc = factory.newDocumentBuilder().newDocument();
069: } catch (ParserConfigurationException e) {
070: e.printStackTrace();
071: }
072:
073: Element elPropFind = xmlDoc.createElementNS(
074: PropPatch.WEBDAV_NAMESPACE, "propfind");
075: xmlDoc.appendChild(elPropFind);
076:
077: if (this .m_properties.size() == 0) {
078: Element elAllProp = xmlDoc.createElementNS(
079: PropPatch.WEBDAV_NAMESPACE, "allprop");
080: elPropFind.appendChild(elAllProp);
081: } else {
082: Element elProp = xmlDoc.createElementNS(
083: PropPatch.WEBDAV_NAMESPACE, "prop");
084: elPropFind.appendChild(elProp);
085: Iterator itor = this .m_properties.iterator();
086: while (itor.hasNext()) {
087: PropFindProperty prop = (PropFindProperty) itor.next();
088: Element elProperty = xmlDoc.createElementNS(prop
089: .getNamespaceURI(), prop.getName());
090: elProp.appendChild(elProperty);
091: }
092: }
093:
094: XMLPrettyPrint printer = new XMLPrettyPrint();
095: printer.setNamespaceAware(true);
096:
097: String sXML = "";
098: try {
099: sXML = sXML
100: + printer.printNode(xmlDoc.getDocumentElement());
101: } catch (NamespaceClashException e1) {
102: e1.printStackTrace();
103: }
104:
105: return sXML.getBytes();
106: }
107:
108: /**
109: * Adds a property to the request.
110: *
111: * @param sNamespaceURI Namepace
112: * @param sName Name
113: */
114: public void addProperty(String sNamespaceURI, String sName) {
115: this .m_properties
116: .add(new PropFindProperty(sNamespaceURI, sName));
117: }
118:
119: }
|