KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > portal > event > impl > PageLabelEventConverter


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.event.impl;
17
18 import java.util.List JavaDoc;
19 import java.util.Map JavaDoc;
20 import java.util.ArrayList JavaDoc;
21
22 import org.apache.avalon.framework.logger.AbstractLogEnabled;
23 import org.apache.avalon.framework.service.Serviceable;
24 import org.apache.avalon.framework.service.ServiceException;
25 import org.apache.avalon.framework.service.ServiceManager;
26 import org.apache.avalon.framework.thread.ThreadSafe;
27 import org.apache.cocoon.portal.impl.PageLabelManager;
28 import org.apache.cocoon.portal.event.Event;
29 import org.apache.cocoon.portal.event.EventConverter;
30
31 /**
32  * Convert events from and into strings.
33  * @author Ralph Goers
34  *
35  * @version CVS $Id: $
36  */

37 public class PageLabelEventConverter extends AbstractLogEnabled
38     implements EventConverter, Serviceable, ThreadSafe {
39
40     protected PageLabelManager labelManager;
41
42     private static final String JavaDoc ENCODE = "&ENCODE";
43     private static final String JavaDoc DECODE = "&DECODE";
44
45     protected ServiceManager manager;
46
47     /* (non-Javadoc)
48     * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
49     */

50     public void service(ServiceManager manager) throws ServiceException {
51         this.manager = manager;
52         this.labelManager = (PageLabelManager)manager.lookup(PageLabelManager.ROLE);
53     }
54
55     /* (non-Javadoc)
56      * @see org.apache.cocoon.portal.event.EventConverter#encode(org.apache.cocoon.portal.event.Event)
57      */

58     public String JavaDoc encode(Event event) {
59
60         String JavaDoc pageLabel = this.labelManager.getCurrentLabel();
61         if (pageLabel == null) {
62             pageLabel = "";
63         }
64         Map JavaDoc map = this.labelManager.getPageEventMap();
65         String JavaDoc encode = pageLabel + ENCODE;
66         List JavaDoc list = (List JavaDoc)map.get(encode);
67
68         if (null == list) {
69             list = new ArrayList JavaDoc();
70             map.put(encode, list);
71         }
72
73         int index = list.indexOf(event);
74         if ( index == -1 ) {
75           list.add(event);
76           index = list.size() - 1;
77         }
78         return String.valueOf(index);
79     }
80
81     /* (non-Javadoc)
82      * @see org.apache.cocoon.portal.event.EventConverter#decode(java.lang.String)
83      */

84     public Event decode(String JavaDoc value) {
85         String JavaDoc pageLabel = this.labelManager.getCurrentLabel();
86         if (pageLabel == null) {
87             pageLabel = "";
88         }
89         Map JavaDoc map = this.labelManager.getPageEventMap();
90         List JavaDoc list = (List JavaDoc)map.get(pageLabel + DECODE);
91
92         if ( null != list ) {
93             int index = new Integer JavaDoc(value).intValue();
94             if (index < list.size()) {
95                 return (Event)list.get(index);
96             }
97         }
98         return null;
99     }
100
101     /* (non-Javadoc)
102     * @see org.apache.cocoon.portal.event.EventConverter#start()
103     */

104     public void start() {
105         String JavaDoc label = this.labelManager.setCurrentLabel();
106         Map JavaDoc map = this.labelManager.getPageEventMap();
107         if (label == null) {
108             label = "";
109         }
110         String JavaDoc encode = label + ENCODE;
111         String JavaDoc decode = label + DECODE;
112
113         List JavaDoc list = (List JavaDoc)map.get(encode);
114
115         if (null != list) {
116             map.put(decode, list);
117             map.remove(encode);
118         }
119     }
120
121     /* (non-Javadoc)
122     * @see org.apache.cocoon.portal.event.EventConverter#finish()
123     */

124     public void finish() {
125         // nothing to do
126
}
127
128     /* (non-Javadoc)
129     * @see org.apache.cocoon.portal.event.EventConverter#isMarshallEvents()
130     */

131     public boolean isMarshallEvents() {
132         return this.labelManager.isMarshallEvents();
133     }
134 }
Popular Tags