KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > generation > TraversableSourceDescriptionGenerator


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.generation;
17
18 import java.io.IOException JavaDoc;
19 import java.util.Map JavaDoc;
20
21 import org.apache.avalon.framework.parameters.Parameters;
22 import org.apache.cocoon.ProcessingException;
23 import org.apache.cocoon.xml.XMLUtils;
24 import org.apache.cocoon.components.source.InspectableSource;
25 import org.apache.cocoon.components.source.LockableSource;
26 import org.apache.cocoon.components.source.RestrictableSource;
27 import org.apache.cocoon.components.source.VersionableSource;
28 import org.apache.cocoon.components.source.helpers.GroupSourcePermission;
29 import org.apache.cocoon.components.source.helpers.PrincipalSourcePermission;
30 import org.apache.cocoon.components.source.helpers.SourceLock;
31 import org.apache.cocoon.components.source.helpers.SourcePermission;
32 import org.apache.cocoon.components.source.helpers.SourceProperty;
33 import org.apache.cocoon.environment.SourceResolver;
34 import org.apache.excalibur.source.SourceException;
35 import org.apache.excalibur.source.TraversableSource;
36 import org.xml.sax.SAXException JavaDoc;
37 import org.xml.sax.helpers.AttributesImpl JavaDoc;
38
39 /**
40  * This Generator augments the output of the TraversableGenerator.
41  * <p>
42  * It adds:
43  * <ul>
44  * <li>version information if the Source implements VersionableSource.</li>
45  * <li>locking information if the Source implements Lockablesource.</li>
46  * <li>permission information if the Source implements RestrictableSource.</li>
47  * <li>and describes its SourceProperties if the Source implements InspectableSource.</li>
48  * </ul>
49  * </p>
50  * <p>
51  * Sitemap parameters that can be specified to control processing are:
52  * <ul>
53  * <li><code>version</code> (<code>true</code>)
54  * whether to generate versioning information.</li>
55  * <li><code>locking</code> (<code>true</code>)
56  * whether to generate locking information.</li>
57  * <li><code>permission</code> (<code>true</code>)
58  * whether to generate permission information.</li>
59  * <li><code>properties</code> (<code>true</code>)
60  * whether to generate source property information.</li>
61  * </ul>
62  * </p>
63  *
64  * @author <a HREF="mailto:stephan@apache.org">Stephan Michels</a>
65  * @author <a HREF="mailto:unico@hippo.nl">Unico Hommes</a>
66  * @version $Id: TraversableSourceDescriptionGenerator.java 240314 2005-08-26 19:22:14Z vgritsenko $
67  */

68 public class TraversableSourceDescriptionGenerator extends TraversableGenerator {
69
70     protected static final String JavaDoc MIME_TYPE_ATTR_NAME = "mimeType";
71
72     private static final String JavaDoc REVISION_ATTR_NAME = "revision";
73     private static final String JavaDoc REVISIONBRANCH_ATTR_NAME = "branch";
74
75     private static final String JavaDoc PROPERTIES_NODE_NAME = "properties";
76     private static final String JavaDoc PROPERTIES_NODE_QNAME = PREFIX + ":" + PROPERTIES_NODE_NAME;
77
78     private static final String JavaDoc PERMISSIONS_NODE_NAME = "permissions";
79     private static final String JavaDoc PERMISSIONS_NODE_QNAME = PREFIX + ":" + PERMISSIONS_NODE_NAME;
80     private static final String JavaDoc PERMISSION_NODE_NAME = "permission";
81     private static final String JavaDoc PERMISSION_NODE_QNAME = PREFIX + ":" + PERMISSION_NODE_NAME;
82
83     private static final String JavaDoc LOCKS_NODE_NAME = "locks";
84     private static final String JavaDoc LOCKS_NODE_QNAME = PREFIX + ":" + LOCKS_NODE_NAME;
85     private static final String JavaDoc LOCK_NODE_NAME = "lock";
86     private static final String JavaDoc LOCK_NODE_QNAME = PREFIX + ":" + LOCK_NODE_NAME;
87
88     private static final String JavaDoc PRINCIPAL_ATTR_NAME = "principal";
89     private static final String JavaDoc GROUP_ATTR_NAME = "group";
90     private static final String JavaDoc PRIVILEGE_ATTR_NAME = "privilege";
91     private static final String JavaDoc INHERITABLE_ATTR_NAME = "inheritable";
92     private static final String JavaDoc NEGATIVE_ATTR_NAME = "negative";
93
94     private static final String JavaDoc TYPE_ATTR_NAME = "type";
95     private static final String JavaDoc EXPIRATION_ATTR_NAME = "expiration";
96     private static final String JavaDoc EXCLUSIVE_ATTR_NAME = "exclusive";
97
98
99     /** Include properties into the description */
100     private boolean properties = true;
101
102     /** Include permissions into the description */
103     private boolean permissions = true;
104
105     /** Include locks into the description */
106     private boolean locks = true;
107
108     /** Include version into the description */
109     private boolean version = true;
110
111
112     /**
113      * Set the <code>SourceResolver</code>, objectModel <code>Map</code>,
114      * the source and sitemap <code>Parameters</code> used to process the request.
115      *
116      * @param resolver Source Resolver
117      * @param objectModel Object model.
118      * @param location Location of the source.
119      * @param parameters Parameters for the generator.
120      */

121     public void setup(SourceResolver resolver, Map JavaDoc objectModel,
122                       String JavaDoc location,
123                       Parameters parameters)
124         throws ProcessingException, SAXException JavaDoc, IOException JavaDoc {
125
126         super.setup(resolver, objectModel, location, parameters);
127
128         this.properties = parameters.getParameterAsBoolean("properties", true);
129         super.cacheKeyParList.add(String.valueOf(String.valueOf(this.properties)));
130
131         this.permissions = parameters.getParameterAsBoolean("permissions", true);
132         super.cacheKeyParList.add(String.valueOf(this.permissions));
133
134         this.locks = parameters.getParameterAsBoolean("locks", true);
135         super.cacheKeyParList.add(String.valueOf(this.locks));
136
137         this.version = parameters.getParameterAsBoolean("version", true);
138         super.cacheKeyParList.add(String.valueOf(this.version));
139
140         if (getLogger().isDebugEnabled()) {
141             getLogger().debug("properties: " + this.properties);
142             getLogger().debug("permissions: " + this.permissions);
143             getLogger().debug("locks: " + this.locks);
144             getLogger().debug("version: " + this.version);
145         }
146     }
147
148     /**
149      * Augments source nodes with additional information.
150      *
151      * @param source the Source to describe.
152      */

153     protected final void addContent(TraversableSource source)
154         throws SAXException JavaDoc, ProcessingException {
155
156         super.addContent(source);
157         try {
158             if (this.properties && (source instanceof InspectableSource)) {
159                 pushSourceProperties((InspectableSource) source);
160             }
161             if (this.permissions && (source instanceof RestrictableSource)) {
162                 pushSourcePermissions((RestrictableSource) source);
163             }
164             if (this.locks && (source instanceof LockableSource)) {
165                 pushSourceLocks((LockableSource) source);
166             }
167         } catch (SourceException e) {
168             throw new ProcessingException(e);
169         }
170
171     }
172
173     /**
174      * Augments source node elements with additional attributes describing the Source.
175      * The additional attributes are a <code>mimeType</code> attribute,
176      * and iff the Source is an instance of VersionableSource and the generator
177      * is configured to output versioning information two attributes:
178      * <code>revision</code> and <code>branch</code>.
179      *
180      * @param source the Source to describe.
181      */

182     protected void setNodeAttributes(TraversableSource source) throws SAXException JavaDoc, ProcessingException {
183         super.setNodeAttributes(source);
184         if (!source.isCollection()) {
185             String JavaDoc mimeType = source.getMimeType();
186             if (mimeType != null) {
187                 attributes.addAttribute("", MIME_TYPE_ATTR_NAME, MIME_TYPE_ATTR_NAME,
188                                         "CDATA", source.getMimeType());
189             }
190         }
191         if (this.version && source instanceof VersionableSource) {
192             try {
193                 VersionableSource versionablesource = (VersionableSource) source;
194                 if (versionablesource.isVersioned()) {
195                     if ((versionablesource.getSourceRevision()!=null) &&
196                         (versionablesource.getSourceRevision().length()>0)) {
197                         attributes.addAttribute("",
198                                                 REVISION_ATTR_NAME,
199                                                 REVISION_ATTR_NAME, "CDATA",
200                                                 versionablesource.getSourceRevision());
201                     }
202
203                     if ((versionablesource.getSourceRevisionBranch()!=null) &&
204                         (versionablesource.getSourceRevisionBranch().length()>
205                          0)) {
206                         attributes.addAttribute("",
207                                                 REVISIONBRANCH_ATTR_NAME,
208                                                 REVISIONBRANCH_ATTR_NAME,
209                                                 "CDATA",
210                                                 versionablesource.getSourceRevisionBranch());
211                     }
212                 }
213             } catch (SourceException e) {
214                 throw new ProcessingException(e);
215             }
216         }
217     }
218
219     /**
220      * Push a XML description about all properties, which
221      * the source owns.
222      *
223      * @param source the Source to describe.
224      */

225     private void pushSourceProperties(InspectableSource source)
226     throws SAXException JavaDoc, SourceException {
227
228         SourceProperty[] properties = source.getSourceProperties();
229         if (properties != null && properties.length > 0) {
230             this.contentHandler.startElement(URI, PROPERTIES_NODE_NAME,
231                                              PROPERTIES_NODE_QNAME, XMLUtils.EMPTY_ATTRIBUTES);
232             for (int i = 0; i < properties.length; i++) {
233                 SourceProperty property = properties[i];
234                 property.toSAX(this.contentHandler);
235             }
236             this.contentHandler.endElement(URI, PROPERTIES_NODE_NAME,
237                                            PROPERTIES_NODE_QNAME);
238         }
239     }
240
241
242     /**
243      * Push a XML description of all permissions of a source.
244      *
245      * @param source the Source to describe.
246      */

247     private void pushSourcePermissions(RestrictableSource source)
248       throws SAXException JavaDoc, SourceException {
249         SourcePermission[] permissions = source.getSourcePermissions();
250
251         if (permissions != null && permissions.length > 0) {
252             this.contentHandler.startElement(URI,
253                                              PERMISSIONS_NODE_NAME,
254                                              PERMISSIONS_NODE_QNAME,
255                                              XMLUtils.EMPTY_ATTRIBUTES);
256
257             for (int i = 0; i < permissions.length; i++) {
258                 AttributesImpl JavaDoc attributes = new AttributesImpl JavaDoc();
259
260                 if (permissions[i] instanceof PrincipalSourcePermission) {
261                     attributes.addAttribute("", PRINCIPAL_ATTR_NAME,
262                                             PRINCIPAL_ATTR_NAME, "CDATA",
263                                             ((PrincipalSourcePermission) permissions[i]).getPrincipal());
264                 } else if (permissions[i] instanceof GroupSourcePermission) {
265                     attributes.addAttribute("", GROUP_ATTR_NAME,
266                                             GROUP_ATTR_NAME, "CDATA",
267                                             ((GroupSourcePermission) permissions[i]).getGroup());
268                 }
269
270                 attributes.addAttribute("", PRIVILEGE_ATTR_NAME,
271                                         PRIVILEGE_ATTR_NAME, "CDATA",
272                                         permissions[i].getPrivilege());
273                 attributes.addAttribute("", INHERITABLE_ATTR_NAME,
274                                         INHERITABLE_ATTR_NAME, "CDATA",
275                                         String.valueOf(permissions[i].isInheritable()));
276                 attributes.addAttribute("", NEGATIVE_ATTR_NAME,
277                                         NEGATIVE_ATTR_NAME, "CDATA",
278                                         String.valueOf(permissions[i].isNegative()));
279
280                 this.contentHandler.startElement(URI,
281                                                  PERMISSION_NODE_NAME,
282                                                  PERMISSION_NODE_QNAME,
283                                                  attributes);
284                 this.contentHandler.endElement(URI,
285                                                PERMISSION_NODE_NAME,
286                                                PERMISSION_NODE_QNAME);
287             }
288
289             this.contentHandler.endElement(URI, PERMISSIONS_NODE_NAME,
290                                            PERMISSIONS_NODE_QNAME);
291         }
292     }
293
294     /**
295      * Push a XML description about all locks of a source.
296      *
297      * @param source the Source to describe.
298      */

299     public void pushSourceLocks(LockableSource source)
300       throws SAXException JavaDoc, SourceException {
301         SourceLock[] locks = source.getSourceLocks();
302
303         if (locks != null && locks.length > 0) {
304             this.contentHandler.startElement(URI, LOCKS_NODE_NAME,
305                                              LOCKS_NODE_QNAME,
306                                              XMLUtils.EMPTY_ATTRIBUTES);
307
308             for (int i = 0; locks.length > 0; i++) {
309                 SourceLock lock = locks[i];
310
311                 AttributesImpl JavaDoc attributes = new AttributesImpl JavaDoc();
312                 attributes.addAttribute("", PRINCIPAL_ATTR_NAME,
313                                         PRINCIPAL_ATTR_NAME, "CDATA",
314                                         lock.getSubject());
315                 attributes.addAttribute("", TYPE_ATTR_NAME, TYPE_ATTR_NAME,
316                                         "CDATA", lock.getType());
317                 attributes.addAttribute("", EXPIRATION_ATTR_NAME,
318                                         EXPIRATION_ATTR_NAME, "CDATA",
319                                         lock.getExpiration().toString());
320                 attributes.addAttribute("", INHERITABLE_ATTR_NAME,
321                                         INHERITABLE_ATTR_NAME, "CDATA",
322                                         String.valueOf(lock.isInheritable()));
323                 attributes.addAttribute("", EXCLUSIVE_ATTR_NAME,
324                                         EXCLUSIVE_ATTR_NAME, "CDATA",
325                                         String.valueOf(lock.isExclusive()));
326
327                 this.contentHandler.startElement(URI, LOCK_NODE_NAME,
328                                                  LOCK_NODE_QNAME, attributes);
329                 this.contentHandler.endElement(URI, LOCK_NODE_NAME,
330                                                LOCK_NODE_QNAME);
331             }
332
333             this.contentHandler.endElement(URI, LOCKS_NODE_NAME,
334                                            LOCKS_NODE_QNAME);
335         }
336     }
337
338 }
339
Popular Tags