KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > webdav > client > methods > order > OrderPatch


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.order;
20
21 import java.util.ArrayList JavaDoc;
22 import java.util.List JavaDoc;
23
24 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
25 import javax.xml.parsers.ParserConfigurationException JavaDoc;
26
27 import org.openharmonise.commons.xml.*;
28 import org.openharmonise.commons.xml.namespace.*;
29 import org.openharmonise.webdav.client.*;
30 import org.w3c.dom.Document JavaDoc;
31 import org.w3c.dom.Element JavaDoc;
32 import org.w3c.dom.Text JavaDoc;
33
34
35 /**
36  * Order path method.
37  *
38  * @author Matthew Large
39  * @version $Revision: 1.1 $
40  *
41  */

42 public class OrderPatch extends AbstractWebDAVMethod {
43
44     /**
45      * Method name.
46      */

47     public static String JavaDoc METHOD_NAME = "ORDERPATCH";
48     
49     /**
50      * List of paths in order.
51      */

52     private ArrayList JavaDoc m_aOrderedPaths = null;
53     
54     /**
55      * Constructs new order patch method.
56      *
57      * @param sURL URL for request
58      */

59     public OrderPatch(String JavaDoc sURL) {
60         super(METHOD_NAME, sURL);
61     }
62     
63     public byte[] getData() {
64         
65         DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
66         factory.setNamespaceAware(true);
67         Document JavaDoc xmlDoc = null;
68         try {
69             xmlDoc = factory.newDocumentBuilder().newDocument();
70         } catch (ParserConfigurationException JavaDoc e) {
71             e.printStackTrace();
72         }
73         
74         Element JavaDoc elOrderPatch = xmlDoc.createElementNS( OrderPatch.WEBDAV_NAMESPACE ,"orderpatch");
75         xmlDoc.appendChild(elOrderPatch);
76         
77         if( this.m_aOrderedPaths!=null && this.m_aOrderedPaths.size()!=0 ) {
78             for (int i = 0; i < this.m_aOrderedPaths.size(); i++) {
79                 String JavaDoc sPath = (String JavaDoc) this.m_aOrderedPaths.get(i);
80                 Element JavaDoc elOrderMember = xmlDoc.createElementNS( OrderPatch.WEBDAV_NAMESPACE, "order-member");
81                 
82                 Element JavaDoc elSegment = xmlDoc.createElementNS( OrderPatch.WEBDAV_NAMESPACE, "segment");
83                 Text JavaDoc txtSegment = xmlDoc.createTextNode( sPath.substring(sPath.lastIndexOf("/")+1) );
84                 
85                 Element JavaDoc elPosition = xmlDoc.createElementNS( OrderPatch.WEBDAV_NAMESPACE, "position");
86                 Element JavaDoc elPositionDetail = xmlDoc.createElementNS( OrderPatch.WEBDAV_NAMESPACE, "last");
87                 
88                 elSegment.appendChild(txtSegment);
89                 elOrderMember.appendChild(elSegment);
90                 elPosition.appendChild(elPositionDetail);
91                 elOrderMember.appendChild(elPosition);
92                 elOrderPatch.appendChild(elOrderMember);
93             }
94         }
95         
96         XMLPrettyPrint printer = new XMLPrettyPrint();
97         printer.setNamespaceAware(true);
98
99         String JavaDoc sXML = "";
100         try {
101             sXML = sXML + printer.printNode(xmlDoc.getDocumentElement());
102         } catch (NamespaceClashException e1) {
103             e1.printStackTrace();
104         }
105         
106         return sXML.getBytes();
107     }
108     
109     /**
110      * Sets the ordered list of paths.
111      *
112      * @param aOrderedPaths Ordered list of paths
113      */

114     public void setOrderedPaths(List JavaDoc aOrderedPaths) {
115         m_aOrderedPaths = (ArrayList JavaDoc) aOrderedPaths;
116     }
117     
118     public static void main(String JavaDoc[] args) {
119         OrderPatch method = new OrderPatch("/ete/ete");
120         
121         ArrayList JavaDoc aPaths = new ArrayList JavaDoc();
122         aPaths.add("/webdav/Documents/orderdir/1stFile");
123         aPaths.add("/webdav/Documents/orderdir/2ndFile");
124         aPaths.add("/webdav/Documents/orderdir/3rdFile");
125         aPaths.add("/webdav/Documents/orderdir/4thFile");
126         
127         method.setOrderedPaths(aPaths);
128         
129         String JavaDoc sData = new String JavaDoc(method.getData());
130     }
131
132 }
133
Popular Tags