001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.portal.event.impl;
018:
019: import java.util.List;
020: import java.util.Map;
021: import java.util.ArrayList;
022:
023: import org.apache.avalon.framework.logger.AbstractLogEnabled;
024: import org.apache.avalon.framework.service.Serviceable;
025: import org.apache.avalon.framework.service.ServiceException;
026: import org.apache.avalon.framework.service.ServiceManager;
027: import org.apache.avalon.framework.thread.ThreadSafe;
028: import org.apache.cocoon.portal.impl.PageLabelManager;
029: import org.apache.cocoon.portal.event.Event;
030: import org.apache.cocoon.portal.event.EventConverter;
031:
032: /**
033: * Convert events from and into strings.
034: * @author Ralph Goers
035: *
036: * @version CVS $Id: $
037: */
038: public class PageLabelEventConverter extends AbstractLogEnabled
039: implements EventConverter, Serviceable, ThreadSafe {
040:
041: protected PageLabelManager labelManager;
042:
043: private static final String ENCODE = "&ENCODE";
044: private static final String DECODE = "&DECODE";
045:
046: protected ServiceManager manager;
047:
048: /* (non-Javadoc)
049: * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
050: */
051: public void service(ServiceManager manager) throws ServiceException {
052: this .manager = manager;
053: this .labelManager = (PageLabelManager) manager
054: .lookup(PageLabelManager.ROLE);
055: }
056:
057: /* (non-Javadoc)
058: * @see org.apache.cocoon.portal.event.EventConverter#encode(org.apache.cocoon.portal.event.Event)
059: */
060: public String encode(Event event) {
061:
062: String pageLabel = this .labelManager.getCurrentLabel();
063: if (pageLabel == null) {
064: pageLabel = "";
065: }
066: Map map = this .labelManager.getPageEventMap();
067: String encode = pageLabel + ENCODE;
068: List list = (List) map.get(encode);
069:
070: if (null == list) {
071: list = new ArrayList();
072: map.put(encode, list);
073: }
074:
075: int index = list.indexOf(event);
076: if (index == -1) {
077: list.add(event);
078: index = list.size() - 1;
079: }
080: return String.valueOf(index);
081: }
082:
083: /* (non-Javadoc)
084: * @see org.apache.cocoon.portal.event.EventConverter#decode(java.lang.String)
085: */
086: public Event decode(String value) {
087: String pageLabel = this .labelManager.getCurrentLabel();
088: if (pageLabel == null) {
089: pageLabel = "";
090: }
091: Map map = this .labelManager.getPageEventMap();
092: List list = (List) map.get(pageLabel + DECODE);
093:
094: if (null != list) {
095: int index = new Integer(value).intValue();
096: if (index < list.size()) {
097: return (Event) list.get(index);
098: }
099: }
100: return null;
101: }
102:
103: /* (non-Javadoc)
104: * @see org.apache.cocoon.portal.event.EventConverter#start()
105: */
106: public void start() {
107: String label = this .labelManager.setCurrentLabel();
108: Map map = this .labelManager.getPageEventMap();
109: if (label == null) {
110: label = "";
111: }
112: String encode = label + ENCODE;
113: String decode = label + DECODE;
114:
115: List list = (List) map.get(encode);
116:
117: if (null != list) {
118: map.put(decode, list);
119: map.remove(encode);
120: }
121: }
122:
123: /* (non-Javadoc)
124: * @see org.apache.cocoon.portal.event.EventConverter#finish()
125: */
126: public void finish() {
127: // nothing to do
128: }
129:
130: /* (non-Javadoc)
131: * @see org.apache.cocoon.portal.event.EventConverter#isMarshallEvents()
132: */
133: public boolean isMarshallEvents() {
134: return this.labelManager.isMarshallEvents();
135: }
136: }
|