Package org.apache.webdav.lib.methods

Source Code of org.apache.webdav.lib.methods.PropPatchMethod

/*
* $Header: /home/cvs/jakarta-slide/src/webdav/client/src/org/apache/webdav/lib/methods/PropPatchMethod.java,v 1.19 2001/08/26 18:27:35 dirkv Exp $
* $Revision: 1.19 $
* $Date: 2001/08/26 18:27:35 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999 The Apache Software Foundation.  All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
*    notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
*    notice, this list of conditions and the following disclaimer in
*    the documentation and/or other materials provided with the
*    distribution.
*
* 3. The end-user documentation included with the redistribution, if
*    any, must include the following acknowlegement:
*       "This product includes software developed by the
*        Apache Software Foundation (http://www.apache.org/)."
*    Alternately, this acknowlegement may appear in the software itself,
*    if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
*    Foundation" must not be used to endorse or promote products derived
*    from this software without prior written permission. For written
*    permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
*    nor may "Apache" appear in their names without prior written
*    permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation.  For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
* [Additional notices, if required by prior licensing conditions]
*
*/
package org.apache.webdav.lib.methods;

import java.io.*;
import java.util.*;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.util.XMLPrinter;

import org.apache.commons.httpclient.State;
import org.apache.commons.httpclient.Header;


/**
* PROPPATCH Method.
*
* @author <a href="mailto:remm@apache.org">Remy Maucherat</a>
* @author <a href="mailto:bcholmes@interlog.com">B.C. Holmes</a>
* @author <a href="mailto:eurrow@sas.com">Robert Owen</a>
* @author Dirk Verbeeck
*/
public class PropPatchMethod
    extends XMLResponseMethodBase {


    // ----------------------------------------------------------- Constructors


    /**
     * Method constructor.
     */
    public PropPatchMethod() {
        name = "PROPPATCH";
    }


    /**
     * Method constructor.
     */
    public PropPatchMethod(String path) {
        super(path);
        name = "PROPPATCH";
    }


    // ----------------------------------------------------- Instance Variables


    /**
     * Hashtable of the properties to set.
     */
    protected Hashtable toSet = new Hashtable();


    /**
     * Hashtable of the properties to remove.
     */
    protected Hashtable toRemove = new Hashtable();


    // --------------------------------------------------------- Public Methods


    /**
     * Add a new property to set.
     *
     * @param name Property name
     * @param value Property value
     */
    public void addPropertyToSet(String name, String value) {
        checkNotUsed();
        Property propertyToSet = new Property();
        if (name != null) {
            propertyToSet.name = name;
            if (value != null)
                propertyToSet.value = value;
            else
                propertyToSet.value = "";
            toSet.put(name, propertyToSet);
        }
    }


    /**
     * Add a new property to set.
     *
     * @param name Property name
     * @param value Property value
     * @param namespace Namespace abbreviation
     * @param namespaceInfo Namespace information
     */
    public void addPropertyToSet(String name, String value, String namespace,
                                 String namespaceInfo) {
        checkNotUsed();
        Property propertyToSet = new Property();
        if (name != null) {
            propertyToSet.name = name;
            if (value != null)
                propertyToSet.value = value;
            else
                propertyToSet.value = "";
            propertyToSet.namespace = namespace;
            propertyToSet.namespaceInfo = namespaceInfo;
            toSet.put(namespace + name, propertyToSet);
        }
    }


    /**
     * Add property to remove.
     *
     * @param name Property name
     */
    public void addPropertyToRemove(String name) {
        checkNotUsed();
        Property propertyToRemove = new Property();
        if (name != null) {
            propertyToRemove.name = name;
            toRemove.put(name, propertyToRemove);
        }
    }


    /**
     * Add property to remove.
     *
     * @param name Property name
     * @param namespace Namespace abbreviation
     * @param namespaceInfo Namespace information
     */
    public void addPropertyToRemove(String name, String namespace,
                                    String namespaceInfo) {
        checkNotUsed();
        Property propertyToRemove = new Property();
        if (name != null) {
            propertyToRemove.name = name;
            propertyToRemove.namespace = namespace;
            propertyToRemove.namespaceInfo = namespaceInfo;
            toRemove.put(name, propertyToRemove);
        }
    }


    // --------------------------------------------------- WebdavMethod Methods


    /**
     * Generate additional headers needed by the request.
     *
     * @param host the host
     * @param state State token
     */
    public void generateHeaders(String host, State state) {

        super.generateHeaders(host, state);

        super.setHeader("Content-Type", "text/xml; charset=utf-8");

    }


    /**
     * Generate the query body.
     *
     * @return String query
     */
    public String generateQuery() {

        if (query != null)
            return query;

        XMLPrinter printer = new XMLPrinter();
        printer.writeXMLHeader();
        printer.writeElement("D", "DAV:", "propertyupdate",
                             XMLPrinter.OPENING);

        if (toSet.size() > 0) {

            printer.writeElement("D", null, "set", XMLPrinter.OPENING);

            Enumeration toSetList = toSet.elements();
            printer.writeElement("D", null, "prop", XMLPrinter.OPENING);
            while (toSetList.hasMoreElements()) {
                Property current = (Property) toSetList.nextElement();
                if ("DAV:".equals(current.namespaceInfo)) {
                    printer.writeProperty("D", null, current.name, current.value);
                }
                else {
                    printer.writeProperty(current.namespace, current.namespaceInfo,
                                      current.name, current.value);
                }
            }
            printer.writeElement("D", null, "prop", XMLPrinter.CLOSING);

            printer.writeElement("D", null, "set", XMLPrinter.CLOSING);

        }

        if (toRemove.size() > 0) {

            printer.writeElement("D", null, "remove", XMLPrinter.OPENING);

            Enumeration toRemoveList = toRemove.elements();
            printer.writeElement("D", null, "prop", XMLPrinter.OPENING);
            while (toRemoveList.hasMoreElements()) {
                Property current = (Property) toRemoveList.nextElement();
                printer.writeElement(current.namespace, current.namespaceInfo,
                                     current.name, XMLPrinter.NO_CONTENT);
            }
            printer.writeElement("D", null, "prop", XMLPrinter.CLOSING);

            printer.writeElement("D", null, "remove", XMLPrinter.CLOSING);

        }

        printer.writeElement("D", "propertyupdate", XMLPrinter.CLOSING);

        query = printer.toString();

        return query;
    }


    // --------------------------------------------------- Property Inner Class


    private class Property {

        public String name = "";
        public String namespace;
        public String namespaceInfo;
        public String value;

    }


}
TOP

Related Classes of org.apache.webdav.lib.methods.PropPatchMethod

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.