KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > webdav > method > report > ExpandPropertyReport


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/webdav/server/org/apache/slide/webdav/method/report/ExpandPropertyReport.java,v 1.5 2004/07/28 09:32:12 ib Exp $
3  * $Revision: 1.5 $
4  * $Date: 2004/07/28 09:32:12 $
5  *
6  * ====================================================================
7  *
8  * Copyright 1999-2002 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */

23
24 package org.apache.slide.webdav.method.report;
25
26 import java.io.IOException JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Enumeration JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31
32 import org.apache.slide.common.NamespaceAccessToken;
33 import org.apache.slide.common.PropertyParseException;
34 import org.apache.slide.common.RequestedProperties;
35 import org.apache.slide.common.RequestedPropertiesImpl;
36 import org.apache.slide.common.SlideException;
37 import org.apache.slide.common.SlideToken;
38 import org.apache.slide.content.NodeRevisionDescriptor;
39 import org.apache.slide.content.NodeRevisionDescriptors;
40 import org.apache.slide.structure.ObjectNode;
41 import org.apache.slide.webdav.WebdavServletConfig;
42 import org.apache.slide.webdav.util.DeltavConstants;
43 import org.apache.slide.webdav.util.PreconditionViolationException;
44 import org.apache.slide.webdav.util.WebdavUtils;
45 import org.jdom.Element;
46 import org.jdom.Namespace;
47
48
49 /**
50  * DAV:expand-property report worker.
51  *
52  */

53 public class ExpandPropertyReport extends AbstractReport implements DeltavConstants {
54     
55     private static final String JavaDoc PROPERTY_LIST = "propertylist";
56     
57     private List JavaDoc propertyElmList;
58     
59     /**
60      * Constructor
61      *
62      * @param slideToken a SlideToken
63      * @param token a NamespaceAccessToken
64      * @param config a WebdavServletConfig
65      * @param serverUrl a String
66      * @param contextPath a String
67      */

68     public ExpandPropertyReport(SlideToken slideToken, NamespaceAccessToken token, WebdavServletConfig config, String JavaDoc slideContextPath) {
69         super(slideToken, token, config, slideContextPath);
70     }
71     
72     /**
73      * Initialize report worker with specified report element
74      *
75      * @param resourcePath a String
76      * @param expandPropertyElm an Element
77      *
78      * @throws PreconditionViolationException
79      */

80     public void init(String JavaDoc resourcePath, Element expandPropertyElm) throws PreconditionViolationException {
81         this.propertyElmList = expandPropertyElm.getChildren(E_PROPERTY, DNSP);
82     }
83     
84     /**
85      * Execute report and add results to given multistatus element
86      *
87      * @param resourcePath a String
88      * @param multistatusElm an Element
89      * @param depth an int
90      *
91      * @throws SlideException
92      * @throws IOException
93      */

94     public void execute(String JavaDoc resourcePath, Element multistatusElm, int depth) throws SlideException, IOException JavaDoc {
95         if (depth < 0) {
96             return;
97         }
98         writeReport( resourcePath, multistatusElm, propertyElmList );
99         ObjectNode onode = structure.retrieve( slideToken, resourcePath );
100         Enumeration JavaDoc childrenEnum = structure.getChildren( slideToken, onode );
101         while( childrenEnum.hasMoreElements() ) {
102             ObjectNode cnode = (ObjectNode)childrenEnum.nextElement();
103             execute(cnode.getUri(), multistatusElm, depth-1);
104         }
105     }
106     
107     private void writeReport(String JavaDoc resourcePath, Element multistatusElm, List JavaDoc propertyElmList) {
108         
109         try {
110             NodeRevisionDescriptors nrds = content.retrieve(slideToken, resourcePath);
111             NodeRevisionDescriptor nrd = content.retrieve(slideToken, nrds);
112             
113             Element response = getResponseElement(slideToken,
114                                                   resourcePath,
115                                                   nrd.getRevisionNumber(),
116                                                   createRequestedProperties(propertyElmList));
117             
118             multistatusElm.addContent(response);
119             
120             // check for nested <property> elements
121
Iterator JavaDoc iterator = propertyElmList.iterator();
122             while (iterator.hasNext()) {
123                 Element propertyElement = (Element)iterator.next();
124                 List JavaDoc childPropertyList = propertyElement.getChildren();
125                 if (childPropertyList.size() > 0) {
126                     try {
127                         Namespace propertyNamespace = DNSP;
128                         String JavaDoc propertyNamespaceStr = propertyElement.getAttributeValue("namespace");
129                         if( propertyNamespaceStr != null )
130                             propertyNamespace = Namespace.getNamespace(propertyNamespaceStr);
131                         List JavaDoc hrefElements = response.
132                             getChild(E_PROPSTAT, DNSP).
133                             getChild(E_PROP, DNSP).
134                             getChild(propertyElement.getAttribute("name").getValue(), propertyNamespace).
135                             getChildren(E_HREF, DNSP);
136                         Iterator JavaDoc hrefs = new ArrayList JavaDoc(hrefElements).iterator();
137                         while( hrefs.hasNext() ) {
138                             Element hrefElement = (Element)hrefs.next();
139                             if (hrefElement.getText() != null) {
140                                 // replace the <href> with <response> of the corresponding resource
141
writeReport(
142                                     WebdavUtils.getSlidePath(hrefElement.getTextTrim(),
143                                                              slideContextPath),
144                                                (Element)hrefElement.getParent(),
145                                     childPropertyList);
146                                 hrefElement.getParent().removeContent(hrefElement);
147                             }
148                         }
149                     }
150                     catch (NullPointerException JavaDoc e) {
151                         // there is no such <href> element, so what..?
152
}
153                 }
154             }
155         }
156         catch (Exception JavaDoc e) {
157             multistatusElm.addContent(getErrorResponse(resourcePath, WebdavUtils.getErrorCode(e), null));
158         }
159     }
160     
161     private RequestedProperties createRequestedProperties(List JavaDoc propertyElements) throws PropertyParseException {
162         
163         Element propertyListElement = new Element(PROPERTY_LIST);
164         Iterator JavaDoc iterator = propertyElements.iterator();
165         while (iterator.hasNext()) {
166             propertyListElement.addContent((Element)((Element)iterator.next()).clone());
167         }
168         return new RequestedPropertiesImpl(propertyListElement);
169     }
170 }
171
172
Popular Tags