KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > common > RequestedPropertiesImpl


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/share/org/apache/slide/common/RequestedPropertiesImpl.java,v 1.17 2004/07/28 09:38:17 ib Exp $
3  * $Revision: 1.17 $
4  * $Date: 2004/07/28 09:38:17 $
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.common;
25
26 import java.util.ArrayList JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29
30 import org.apache.slide.content.NodeProperty;
31 import org.jdom.Element;
32
33
34 /**
35  * Holds one property as part of the SELECT element.
36  *
37  * @version $Revision: 1.17 $
38  */

39 public class RequestedPropertiesImpl implements RequestedProperties {
40     
41     protected boolean isAllProp = false;
42     protected List JavaDoc propertyList = new ArrayList JavaDoc ();
43     
44     /**
45      * Default constructor
46      */

47     public RequestedPropertiesImpl() {}
48
49     /**
50      * Constructs a List of RequestedProperty from a <code>&lt;DAV:prop&gt;</code>
51      * or <code>&lt;DAV:allprop&gt;</code>.
52      *
53      * @param propElement <code>&lt;DAV:prop&gt;</code> or
54      * <code>&lt;DAV:allprop&gt;</code>.
55      *
56      * @throws PropertyParseException if parsing the property fails for any reason.
57      */

58     public RequestedPropertiesImpl (Element propElement) throws PropertyParseException {
59         add(propElement);
60     }
61     
62     
63     /**
64      * Adds a List of RequestedProperty from a <code>&lt;DAV:prop&gt;</code>
65      * or <code>&lt;DAV:allprop&gt;</code>.
66      *
67      * @param propElement <code>&lt;DAV:prop&gt;</code> or
68      * <code>&lt;DAV:allprop&gt;</code>.
69      *
70      * @throws PropertyParseException if parsing the property fails for any reason.
71      */

72     public void add(Element propElement) throws PropertyParseException {
73         
74         String JavaDoc uri = propElement.getNamespace().getURI();
75         String JavaDoc prefix = propElement.getNamespace().getPrefix();
76         String JavaDoc name = propElement.getName ();
77         
78         if (name.equals ("allprop") && uri.equals ("DAV:")) {
79             isAllProp = true;
80         }
81         else {
82             Iterator JavaDoc it = propElement.getChildren().iterator();
83             
84             while (it.hasNext()) {
85                 Element prop = (Element)it.next();
86                 uri = prop.getNamespace().getURI();
87                 prefix = prop.getNamespace().getPrefix();
88                 name = prop.getName ();
89                 if (uri.equals ("DAV:") && name.equals ("property")) {
90                     name = prop.getAttributeValue ("name");
91                     if( prop.getAttributeValue ("namespace") != null ) {
92                         uri = prop.getAttributeValue ("namespace");
93                         prefix = ""; // will be ignored anyway
94
}
95                 }
96
97                 addProperty (createRequestedProperty(name,
98                                                      prefix,
99                                                      uri,
100                                                      prop.getContent()));
101             }
102         }
103     }
104     
105     /**
106      * Creates a RequestedProperty from the given parameters. This method
107      * may be overwritten by subclasses in order to create appropriate
108      * implementations of RequestedProperty.
109      *
110      * @param name the name of the propery.
111      * @param namespacePrefix the namespace prefix of the propery.
112      * @param namespaceUri the namespace URI of the propery.
113      * @param text the text of the propery element.
114      * @param children the children of the propery element.
115      *
116      * @return the created RequestedProperty.
117      *
118      * @throws PropertyParseException if parsing the property fails for any reason.
119      */

120     protected RequestedProperty createRequestedProperty(String JavaDoc name, String JavaDoc namespacePrefix, String JavaDoc namespaceUri, List JavaDoc content) throws PropertyParseException {
121         return new RequestedPropertyImpl(name, namespaceUri);
122     }
123     
124     /**
125      * Returns an iterator for all selected properties
126      *
127      * @return an Iterator
128      */

129     public Iterator JavaDoc iterator () {
130         if (isAllProp == true)
131             throw new IllegalStateException JavaDoc ();
132         
133         return propertyList.iterator();
134     }
135     
136     
137     // interface methods
138

139     /**
140      * Method getRequestedProperties
141      *
142      * @return an Iterator to retrieve all RequestedProperty items
143      * @throws IllegalStateException when isAllProp == true
144      */

145     public Iterator JavaDoc getRequestedProperties() {
146         if (isAllProp)
147             throw new IllegalStateException JavaDoc ();
148         
149         return propertyList.iterator();
150     }
151     
152     /**
153      * Method isAllProp
154      *
155      * @return true, if all properties are requested
156      *
157      */

158     public boolean isAllProp() {
159         return isAllProp;
160     }
161
162     /**
163      * Set the isAllProp member.
164      */

165     public void setIsAllProp(boolean isAllProp) {
166         this.isAllProp = isAllProp;
167     }
168     
169     /**
170      * Checks, if the property identified by name and namespace, is requested
171      *
172      * @param name name of the property to be checked
173      * @param namespace namespace of the property to be checked
174      *
175      * @return true, if property is requested
176      */

177     public boolean contains (String JavaDoc name, String JavaDoc namespace) {
178         if (isAllProp)
179             return true;
180         
181         RequestedProperty prop = new RequestedPropertyImpl (name, namespace);
182         return propertyList.contains (prop);
183     }
184     
185     /**
186      * Checks, if the NodeProperty is a RequestedProperty
187      *
188      * @param property NodeProperty to be checked
189      *
190      * @return true, if property is requested
191      *
192      */

193     public boolean contains (NodeProperty property) {
194         if (isAllProp)
195             return true;
196         
197         RequestedProperty prop =
198             new RequestedPropertyImpl (property.getName(),
199                                        property.getNamespace());
200         return propertyList.contains (prop);
201     }
202     
203     
204 // /**
205
// * Method equals
206
// *
207
// * @param o an Object
208
// *
209
// * @return true if equal
210
// */
211
// public boolean equals (Object o) {
212
// if (! (o instanceof RequestedProperties))
213
// return false;
214
//
215
// RequestedProperties p = (RequestedProperties) o;
216
//
217
// RequestedProperties sThis, sOther;
218
//
219
// Iterator it = p.iterator();
220
// int size = propertyList.size();
221
//
222
// for (int i = 0; i < size; i++) {
223
// if (!it.hasNext())
224
// return false;
225
//
226
// sThis = (SelectedProperty) propertyList.get (i);
227
// sOther = (SelectedProperty) it.next();
228
//
229
// if (!sThis.equals(sOther))
230
// return false;
231
// }
232
// if (it.hasNext())
233
// return false;
234
//
235
// return true;
236
// }
237
//
238

239     /**
240      * Method toString
241      *
242      * @return string representation
243      */

244     public String JavaDoc toString () {
245         Iterator JavaDoc it = iterator();
246         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
247         
248         while (it.hasNext()) {
249             sb.append (((RequestedProperty)it.next()).toString());
250             if (it.hasNext())
251                 sb.append (", ");
252         }
253         return sb.toString();
254     }
255     
256     
257     /**
258      * Method addSelectedProperty
259      *
260      * @param prop a SelectedProperty
261      */

262     public void addProperty (RequestedProperty prop) {
263         if (isAllProp == true)
264             throw new IllegalStateException JavaDoc ();
265         
266         propertyList.add (prop);
267     }
268 }
269
270
Popular Tags