KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > portal > layout > renderer > aspect > impl > HistoryAspect


1 /*
2  * Copyright 1999-2002,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.portal.layout.renderer.aspect.impl;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import org.apache.cocoon.environment.ObjectModelHelper;
24 import org.apache.cocoon.environment.Request;
25 import org.apache.cocoon.environment.Session;
26 import org.apache.cocoon.portal.PortalService;
27 import org.apache.cocoon.portal.acting.helpers.CopletEventDescription;
28 import org.apache.cocoon.portal.acting.helpers.LayoutEventDescription;
29 import org.apache.cocoon.portal.coplet.CopletInstanceData;
30 import org.apache.cocoon.portal.layout.Layout;
31 import org.apache.cocoon.portal.layout.impl.CopletLayout;
32 import org.apache.cocoon.portal.layout.renderer.aspect.RendererAspectContext;
33 import org.xml.sax.ContentHandler JavaDoc;
34 import org.xml.sax.SAXException JavaDoc;
35
36
37 /**
38  * Save the current state of the layout into the session. Takes into account
39  * state already present as request attribute. Includes aspect data and parameters
40  * as well as aspect data and attributes of a coplet instance if layout is a
41  * coplet layout. This aspect does not add to the XML created by the renderer chain.
42  *
43  * <h2>Example XML</h2>
44  * <pre>
45  * &lt;!-- output from following renderers --&gt;
46  * </pre>
47  *
48  * <h2>Applicable to:</h2>
49  * <ul>
50  * <li>{@link org.apache.cocoon.portal.layout.Layout}</li>
51  * </ul>
52  *
53  * @author <a HREF="mailto:cziegeler@s-und-n.de">Carsten Ziegeler</a>
54  *
55  * @version CVS $Id: HistoryAspect.java 326423 2005-10-19 07:08:49Z cziegeler $
56  */

57 public class HistoryAspect
58     extends AbstractAspect {
59
60     /**
61      * Add the values to the state
62      * @param id
63      * @param state
64      * @param values
65      * @param isCopletEvent tells if the event has to be a coplet event
66      */

67     protected void addValues(String JavaDoc id, List JavaDoc state, Map JavaDoc values, String JavaDoc prefix,boolean isCopletEvent) {
68         final Iterator JavaDoc iter = values.entrySet().iterator();
69         while ( iter.hasNext() ) {
70             final Map.Entry JavaDoc entry = (Map.Entry JavaDoc)iter.next();
71             final String JavaDoc path = prefix + entry.getKey();
72             if(!isCopletEvent){
73                 LayoutEventDescription led = new LayoutEventDescription();
74                 led.path = path;
75                 led.layoutId = id;
76                 led.data = entry.getValue();
77                 state.add(led);
78             }else{
79                 CopletEventDescription ced = new CopletEventDescription();
80                 ced.path = path;
81                 ced.copletId = id;
82                 ced.data = entry.getValue();
83                 state.add(ced);
84             }
85         }
86     }
87
88     /* (non-Javadoc)
89      * @see org.apache.cocoon.portal.layout.renderer.aspect.RendererAspect#toSAX(org.apache.cocoon.portal.layout.renderer.aspect.RendererAspectContext, org.apache.cocoon.portal.layout.Layout, org.apache.cocoon.portal.PortalService, org.xml.sax.ContentHandler)
90      */

91     public void toSAX(RendererAspectContext context,
92                     Layout layout,
93                     PortalService service,
94                     ContentHandler JavaDoc handler)
95     throws SAXException JavaDoc {
96         if ( layout.getId() != null ) {
97             final Request request = ObjectModelHelper.getRequest(context.getObjectModel());
98             final Session session = request.getSession(false);
99             if ( session != null ) {
100                 List JavaDoc history = (List JavaDoc)session.getAttribute("portal-history");
101                 if ( history == null ) {
102        
103                     history = new ArrayList JavaDoc();
104                 }
105                 List JavaDoc state = (List JavaDoc)request.getAttribute("portal-history");
106                 if ( state == null ) {
107                     state = new ArrayList JavaDoc();
108                     request.setAttribute("portal-history", state);
109                     history.add(state);
110                 }
111                 
112                 this.addValues(layout.getId(), state, layout.getAspectDatas(), "aspectDatas/",false);
113                 this.addValues(layout.getId(), state, layout.getParameters(), "parameters/",false);
114                 
115                 // are we a coplet layout
116
if ( layout instanceof CopletLayout ) {
117                     CopletInstanceData cid = ((CopletLayout)layout).getCopletInstanceData();
118                     this.addValues(cid.getId(), state, cid.getAspectDatas(), "aspectDatas/",true);
119                     this.addValues(cid.getId(), state, cid.getAttributes(), "attributes/",true);
120                 }
121                 session.setAttribute("portal-history", history);
122             }
123         }
124         context.invokeNext(layout, service, handler);
125     }
126
127 }
128
Popular Tags