KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > webdav > client > methods > Lock


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

36 public class Lock extends AbstractWebDAVMethod {
37
38     /**
39      * Method name.
40      */

41     public static String JavaDoc METHOD_NAME = "LOCK";
42     
43     /**
44      * Lock type exclusive identifier.
45      */

46     public static final String JavaDoc LOCK_EXCLUSIVE = "exclusive";
47     
48     /**
49      * Lock type shared identifier.
50      */

51     public static final String JavaDoc LOCK_SHARED = "shared";
52     
53     /**
54      * Lock type.
55      */

56     private String JavaDoc m_sLockType = "exclusive";
57     
58     /**
59      * Owner of lock.
60      */

61     private String JavaDoc m_sOwner = null;
62     
63     /**
64      * Creates a new lock method.
65      *
66      * @param sURL URL for request
67      */

68     public Lock(String JavaDoc sURL) {
69         super(METHOD_NAME, sURL);
70     }
71     
72     /**
73      * Sets the type of the lock.
74      *
75      * @param sLockType Lock type
76      */

77     public void setLockType(String JavaDoc sLockType) {
78         if( sLockType.equals(LOCK_EXCLUSIVE) || sLockType.equals(LOCK_SHARED)) {
79             this.m_sLockType = sLockType;
80         }
81     }
82     
83     /**
84      * Sets the owner of the lock.
85      *
86      * @param sOwner Lock owner
87      */

88     public void setOwner(String JavaDoc sOwner) {
89         this.m_sOwner = sOwner;
90     }
91     
92     public byte[] getData() {
93         
94         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
95         factory.setNamespaceAware(true);
96         Document xmlDoc = null;
97         try {
98             xmlDoc = factory.newDocumentBuilder().newDocument();
99         } catch (ParserConfigurationException e) {
100             e.printStackTrace();
101         }
102         
103         Element elLockInfo = xmlDoc.createElementNS( PropPatch.WEBDAV_NAMESPACE ,"lockinfo");
104         xmlDoc.appendChild(elLockInfo);
105         
106         Element elLockScope = xmlDoc.createElementNS( PropPatch.WEBDAV_NAMESPACE ,"lockscope");
107         elLockInfo.appendChild(elLockScope);
108         Element elLockScopeVal = xmlDoc.createElementNS( PropPatch.WEBDAV_NAMESPACE ,this.m_sLockType);
109         elLockScope.appendChild(elLockScopeVal);
110         
111         Element elLockType = xmlDoc.createElementNS( PropPatch.WEBDAV_NAMESPACE ,"locktype");
112         elLockInfo.appendChild(elLockType);
113         Element elLockTypeVal = xmlDoc.createElementNS( PropPatch.WEBDAV_NAMESPACE ,"write");
114         elLockType.appendChild(elLockTypeVal);
115         
116         XMLPrettyPrint printer = new XMLPrettyPrint();
117         printer.setNamespaceAware(true);
118         
119         String JavaDoc sXML = null;
120         try {
121             sXML = printer.printNode(xmlDoc.getDocumentElement());
122         } catch (NamespaceClashException e1) {
123             e1.printStackTrace();
124         }
125         
126         return sXML.getBytes();
127     }
128
129 }
130
Popular Tags