KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > portal > transformation > AbstractCopletTransformer


1 /*
2  * Copyright 1999-2002,2004-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.portal.transformation;
17
18 import java.util.Map JavaDoc;
19
20 import org.apache.avalon.framework.parameters.ParameterException;
21 import org.apache.avalon.framework.service.ServiceException;
22 import org.apache.avalon.framework.service.ServiceManager;
23 import org.apache.cocoon.environment.ObjectModelHelper;
24 import org.apache.cocoon.portal.Constants;
25 import org.apache.cocoon.portal.PortalService;
26 import org.apache.cocoon.portal.coplet.CopletInstanceData;
27 import org.apache.cocoon.transformation.AbstractSAXTransformer;
28 import org.xml.sax.SAXException JavaDoc;
29
30 /**
31  * Abstract transformer implementation that provides some useful methods and
32  * functionality. The portal service is stored in the instance variable
33  * {@link #portalService} and can be used.
34  * There are some methods to fetch a coplet instance data. {@link #getCopletInstanceData()}
35  * tries to get the instance associated with the current request and
36  * {@link #getCopletInstanceData(String)} fetches an instance with a given id.
37  *
38  * If you want to get the coplet instance data associated with the current request,
39  * there are three possibilities how the transformer obtains the information required
40  * for getting the coplet instance data - or more precisly its id:<br><br>
41  * 1) If it is used within a coplet pipeline and this pipeline is called using
42  * the "cocoon:" protocol, all required information is passed automatically.<br>
43  * 2) The id can be passed to the transformer as sitemap paremeters in the following way:
44  * <pre>&lt;map:transform type="coplet"&gt;
45  * &lt;map:parameter name="copletId" type="examplecoplet"/&gt;
46  * &lt;/map:transform&gt;</pre>
47  * 3) Any component can set the id as a string in the object model of the current request.
48  * This is the name of the key to be used: {@link org.apache.cocoon.portal.Constants#COPLET_ID_KEY}.
49  *
50  * @author <a HREF="mailto:cziegeler@s-und-n.de">Carsten Ziegeler</a>
51  * @version CVS $Id: AbstractCopletTransformer.java 227171 2005-08-03 06:08:26Z cziegeler $
52  */

53 public abstract class AbstractCopletTransformer
54 extends AbstractSAXTransformer {
55
56     /**
57      * Parameter name for the coplet id.
58      */

59     public static final String JavaDoc COPLET_ID_PARAM = "copletId";
60
61     /** The portal service. @since 2.1.8 */
62     protected PortalService portalService;
63
64     /**
65      * Try to get the coplet instance data belonging to the current request
66      * @return The coplet instance data
67      * @throws SAXException If an errors occurs or the instance data is not available
68      */

69     protected CopletInstanceData getCopletInstanceData()
70     throws SAXException JavaDoc {
71         CopletInstanceData cid = this.getCopletInstanceData(null);
72         if ( cid == null ) {
73             throw new SAXException JavaDoc("Could not find coplet instance data for the current pipeline.");
74         }
75         return cid;
76     }
77     
78     
79     /**
80      * Get the portal service.
81      * @deprecated Use directly the instance variable.
82      */

83     protected PortalService getPortalService()
84     throws SAXException JavaDoc {
85         return this.portalService;
86     }
87     
88     
89     /**
90      * Try to get the coplet instance data with the given id
91      * @param copletId The id of the coplet instance or null if this transformer
92      * is used inside a coplet pipeline
93      * @return The coplet instance data or null
94      * @throws SAXException If an error occurs
95      */

96     protected CopletInstanceData getCopletInstanceData(String JavaDoc copletId)
97     throws SAXException JavaDoc {
98         final Map JavaDoc context = (Map JavaDoc)objectModel.get(ObjectModelHelper.PARENT_CONTEXT);
99         
100         if ( copletId == null ) {
101             // determine coplet id
102
if (context != null) {
103                 copletId = (String JavaDoc)context.get(Constants.COPLET_ID_KEY);
104             } else {
105                 copletId = (String JavaDoc)objectModel.get(Constants.COPLET_ID_KEY);
106                 if ( copletId == null ) {
107                     try {
108                         copletId = this.parameters.getParameter(COPLET_ID_PARAM);
109                         
110                     } catch (ParameterException e) {
111                         throw new SAXException JavaDoc("copletId must be passed as parameter or in the object model within the parent context.");
112                     }
113                 }
114             }
115         }
116         if (copletId == null) {
117             throw new SAXException JavaDoc("copletId must be passed as parameter or in the object model within the parent context.");
118         }
119
120         CopletInstanceData object = this.portalService.getComponentManager().getProfileManager().getCopletInstanceData( copletId );
121             
122         return object;
123     }
124
125     /**
126      * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
127      */

128     public void service(ServiceManager manager) throws ServiceException {
129         super.service(manager);
130         this.portalService = (PortalService)this.manager.lookup(PortalService.ROLE);
131     }
132
133     /**
134      * @see org.apache.avalon.framework.activity.Disposable#dispose()
135      */

136     public void dispose() {
137         if ( this.portalService != null ) {
138             this.manager.release( this.portalService );
139             this.portalService = null;
140         }
141         super.dispose();
142     }
143 }
144
Popular Tags