1 16 package org.apache.cocoon.portal.event.impl; 17 18 import java.util.List ; 19 import java.util.Map ; 20 import java.util.ArrayList ; 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 37 public class PageLabelEventConverter extends AbstractLogEnabled 38 implements EventConverter, Serviceable, ThreadSafe { 39 40 protected PageLabelManager labelManager; 41 42 private static final String ENCODE = "&ENCODE"; 43 private static final String DECODE = "&DECODE"; 44 45 protected ServiceManager manager; 46 47 50 public void service(ServiceManager manager) throws ServiceException { 51 this.manager = manager; 52 this.labelManager = (PageLabelManager)manager.lookup(PageLabelManager.ROLE); 53 } 54 55 58 public String encode(Event event) { 59 60 String pageLabel = this.labelManager.getCurrentLabel(); 61 if (pageLabel == null) { 62 pageLabel = ""; 63 } 64 Map map = this.labelManager.getPageEventMap(); 65 String encode = pageLabel + ENCODE; 66 List list = (List )map.get(encode); 67 68 if (null == list) { 69 list = new ArrayList (); 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 84 public Event decode(String value) { 85 String pageLabel = this.labelManager.getCurrentLabel(); 86 if (pageLabel == null) { 87 pageLabel = ""; 88 } 89 Map map = this.labelManager.getPageEventMap(); 90 List list = (List )map.get(pageLabel + DECODE); 91 92 if ( null != list ) { 93 int index = new Integer (value).intValue(); 94 if (index < list.size()) { 95 return (Event)list.get(index); 96 } 97 } 98 return null; 99 } 100 101 104 public void start() { 105 String label = this.labelManager.setCurrentLabel(); 106 Map map = this.labelManager.getPageEventMap(); 107 if (label == null) { 108 label = ""; 109 } 110 String encode = label + ENCODE; 111 String decode = label + DECODE; 112 113 List list = (List )map.get(encode); 114 115 if (null != list) { 116 map.put(decode, list); 117 map.remove(encode); 118 } 119 } 120 121 124 public void finish() { 125 } 127 128 131 public boolean isMarshallEvents() { 132 return this.labelManager.isMarshallEvents(); 133 } 134 } | Popular Tags |