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.order;
020:
021: import java.util.ArrayList;
022: import java.util.List;
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: import org.w3c.dom.Text;
033:
034: /**
035: * Order path method.
036: *
037: * @author Matthew Large
038: * @version $Revision: 1.1 $
039: *
040: */
041: public class OrderPatch extends AbstractWebDAVMethod {
042:
043: /**
044: * Method name.
045: */
046: public static String METHOD_NAME = "ORDERPATCH";
047:
048: /**
049: * List of paths in order.
050: */
051: private ArrayList m_aOrderedPaths = null;
052:
053: /**
054: * Constructs new order patch method.
055: *
056: * @param sURL URL for request
057: */
058: public OrderPatch(String sURL) {
059: super (METHOD_NAME, sURL);
060: }
061:
062: public byte[] getData() {
063:
064: DocumentBuilderFactory factory = DocumentBuilderFactory
065: .newInstance();
066: factory.setNamespaceAware(true);
067: Document xmlDoc = null;
068: try {
069: xmlDoc = factory.newDocumentBuilder().newDocument();
070: } catch (ParserConfigurationException e) {
071: e.printStackTrace();
072: }
073:
074: Element elOrderPatch = xmlDoc.createElementNS(
075: OrderPatch.WEBDAV_NAMESPACE, "orderpatch");
076: xmlDoc.appendChild(elOrderPatch);
077:
078: if (this .m_aOrderedPaths != null
079: && this .m_aOrderedPaths.size() != 0) {
080: for (int i = 0; i < this .m_aOrderedPaths.size(); i++) {
081: String sPath = (String) this .m_aOrderedPaths.get(i);
082: Element elOrderMember = xmlDoc.createElementNS(
083: OrderPatch.WEBDAV_NAMESPACE, "order-member");
084:
085: Element elSegment = xmlDoc.createElementNS(
086: OrderPatch.WEBDAV_NAMESPACE, "segment");
087: Text txtSegment = xmlDoc.createTextNode(sPath
088: .substring(sPath.lastIndexOf("/") + 1));
089:
090: Element elPosition = xmlDoc.createElementNS(
091: OrderPatch.WEBDAV_NAMESPACE, "position");
092: Element elPositionDetail = xmlDoc.createElementNS(
093: OrderPatch.WEBDAV_NAMESPACE, "last");
094:
095: elSegment.appendChild(txtSegment);
096: elOrderMember.appendChild(elSegment);
097: elPosition.appendChild(elPositionDetail);
098: elOrderMember.appendChild(elPosition);
099: elOrderPatch.appendChild(elOrderMember);
100: }
101: }
102:
103: XMLPrettyPrint printer = new XMLPrettyPrint();
104: printer.setNamespaceAware(true);
105:
106: String sXML = "";
107: try {
108: sXML = sXML
109: + printer.printNode(xmlDoc.getDocumentElement());
110: } catch (NamespaceClashException e1) {
111: e1.printStackTrace();
112: }
113:
114: return sXML.getBytes();
115: }
116:
117: /**
118: * Sets the ordered list of paths.
119: *
120: * @param aOrderedPaths Ordered list of paths
121: */
122: public void setOrderedPaths(List aOrderedPaths) {
123: m_aOrderedPaths = (ArrayList) aOrderedPaths;
124: }
125:
126: public static void main(String[] args) {
127: OrderPatch method = new OrderPatch("/ete/ete");
128:
129: ArrayList aPaths = new ArrayList();
130: aPaths.add("/webdav/Documents/orderdir/1stFile");
131: aPaths.add("/webdav/Documents/orderdir/2ndFile");
132: aPaths.add("/webdav/Documents/orderdir/3rdFile");
133: aPaths.add("/webdav/Documents/orderdir/4thFile");
134:
135: method.setOrderedPaths(aPaths);
136:
137: String sData = new String(method.getData());
138: }
139:
140: }
|