KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > modules > output > AbstractOutputModule


1 /*
2  * Copyright 1999-2004 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.components.modules.output;
17
18 import org.apache.avalon.framework.activity.Disposable;
19 import org.apache.avalon.framework.configuration.Configurable;
20 import org.apache.avalon.framework.configuration.Configuration;
21 import org.apache.avalon.framework.configuration.ConfigurationException;
22 import org.apache.avalon.framework.logger.AbstractLogEnabled;
23
24 import org.apache.cocoon.environment.ObjectModelHelper;
25 import org.apache.cocoon.environment.Request;
26 import org.apache.cocoon.util.HashMap;
27
28 import java.util.Map JavaDoc;
29
30 /**
31  * AbstractOutputModule gives you the infrastructure for easily
32  * deploying more output modules.
33  *
34  * <p>In order to get at the logger, use <code>getLogger()</code>.</p>
35  *
36  * @author <a HREF="mailto:haul@apache.org">Christian Haul</a>
37  * @version CVS $Id: AbstractOutputModule.java 126292 2005-01-24 15:13:30Z vgritsenko $
38  */

39 public abstract class AbstractOutputModule extends AbstractLogEnabled
40     implements OutputModule, Configurable, Disposable {
41
42     /**
43      * Stores (global) configuration parameters as <code>key</code> /
44      * <code>value</code> pairs.
45      */

46     protected HashMap settings;
47
48     /**
49      * Configures the module.
50      *
51      * <p>Takes all elements nested in component declaration and stores
52      * them as key-value pairs in <code>settings</code>. Nested
53      * configuration option are not catered for. This way global
54      * configuration options can be used.</p>
55      *
56      * <p>For nested configurations override this function.</p>
57      */

58     public void configure(Configuration conf) throws ConfigurationException {
59         Configuration[] parameters = conf.getChildren();
60         // Ideally here should be length * 1.333(3) but simple +1 will do for lengths up to 3
61
this.settings = new HashMap(parameters.length + 1);
62         for (int i = 0; i < parameters.length; i++) {
63             String JavaDoc key = parameters[i].getName();
64             String JavaDoc val = parameters[i].getValue("");
65             this.settings.put(key, val);
66         }
67     }
68
69     /**
70      * Dispose
71      */

72     public void dispose() {
73         // Implemeted so that we don't need to implement it in every subclass
74
this.settings = null;
75     }
76
77     /**
78      * Utility method to store parameters in a map as request attribute until
79      * either {@link #rollback(Map, String)} or {@link #prepareCommit(Map, String)}
80      * is called.
81      * @param objectModel - the objectModel
82      * @param trans_place - request attribute name used for the transient data
83      * @param name - name of the attribute to set
84      * @param value - attribute value
85      */

86     protected void transientSetAttribute(Map objectModel, String JavaDoc trans_place, String JavaDoc name, Object JavaDoc value) {
87         final Request request = ObjectModelHelper.getRequest(objectModel);
88
89         Map map = (Map) request.getAttribute(trans_place);
90         if (map == null) {
91             // Need java.util.HashMap here since JXPath does not like the extended version...
92
map = new java.util.HashMap JavaDoc();
93         }
94
95         map.put(name, value);
96         request.setAttribute(trans_place, map);
97     }
98
99     /**
100      * Clears all uncommitted transient attributes.
101      *
102      * @param objectModel - the objectModel
103      * @param trans_place - request attribute name used for the transient data
104      */

105     protected void rollback(Map objectModel, String JavaDoc trans_place) {
106         ObjectModelHelper.getRequest(objectModel).removeAttribute(trans_place);
107     }
108
109     /**
110      * Returns a whether an transient attribute already exists.
111      * {@link #transientSetAttribute(Map, String, String, Object)} since the last call to
112      * {@link #rollback(Map, String)} or {@link #prepareCommit(Map, String)}
113      *
114      * @param objectModel - the objectModel
115      * @param trans_place - request attribute name used for the transient data
116      */

117     protected boolean attributeExists(Map objectModel, String JavaDoc trans_place, String JavaDoc name) {
118         final Request request = ObjectModelHelper.getRequest(objectModel);
119
120         Map map = (Map) request.getAttribute(trans_place);
121         if (map == null) {
122             return false;
123         }
124
125         return map.containsKey(name);
126     }
127
128     /**
129      * Returns a map containing all transient attributes and remove them i.e. attributes set with
130      * {@link #transientSetAttribute(Map, String, String, Object)} since the last call to
131      * {@link #rollback(Map, String)} or {@link #prepareCommit(Map, String)}
132      *
133      * @param objectModel - the objectModel
134      * @param trans_place - request attribute name used for the transient data
135      */

136     protected Map prepareCommit(Map objectModel, String JavaDoc trans_place) {
137         final Request request = ObjectModelHelper.getRequest(objectModel);
138
139         Map data = (Map) request.getAttribute(trans_place);
140         request.removeAttribute(trans_place);
141         return data;
142     }
143 }
144
Popular Tags