1 16 package org.apache.cocoon.portal.impl; 17 18 import java.io.UnsupportedEncodingException ; 19 import java.util.Iterator ; 20 import java.util.List ; 21 22 import org.apache.cocoon.portal.event.Event; 23 import org.apache.cocoon.portal.event.impl.ChangeAspectDataEvent; 24 import org.apache.cocoon.portal.layout.CompositeLayout; 25 import org.apache.cocoon.portal.layout.Item; 26 import org.apache.cocoon.portal.layout.NamedItem; 27 import org.apache.cocoon.util.NetUtils; 28 import org.apache.avalon.framework.service.ServiceException; 29 import org.apache.avalon.framework.service.ServiceManager; 30 31 39 public class PageLabelLinkService extends DefaultLinkService { 40 41 protected String apectName; 42 43 44 protected PageLabelManager labelManager; 45 46 49 public void service(ServiceManager manager) throws ServiceException { 50 super.service(manager); 51 this.labelManager = (PageLabelManager)this.manager.lookup(PageLabelManager.ROLE); 52 } 53 54 60 public String getLinkURI(Event event) { 61 return getLinkURI(event, null); 62 } 63 64 71 public String getLinkURI(Event event, Boolean secure) { 72 if (event == null) { 73 return this.getRefreshLinkURI(secure); 74 } 75 if (this.labelManager == null) { 76 return super.getLinkURI(event); 77 } 78 79 String requestParameterName = this.labelManager.getRequestParameterName(); 80 81 if (event instanceof ChangeAspectDataEvent && 82 ((ChangeAspectDataEvent) event).getTarget() instanceof CompositeLayout) { 83 84 ChangeAspectDataEvent e = (ChangeAspectDataEvent)event; 85 CompositeLayout layout = (CompositeLayout)e.getTarget(); 86 int i = ((Integer )e.getData()).intValue(); 87 88 Item item = layout.getItem(i); 89 if (item instanceof NamedItem) { 90 StringBuffer key = new StringBuffer (""); 91 getKey(item, key); 92 93 if (this.labelManager.getPageLabelEvents(key.toString()) != null) { 94 final LinkInfo info = this.getInfo(); 95 boolean hasParams = info.hasParameters(); 96 final StringBuffer buffer = new StringBuffer (info.getBase(secure)); 97 if (hasParams) { 98 buffer.append('&'); 99 } 100 else { 101 buffer.append('?'); 102 } 103 try { 104 String encodedKey = NetUtils.encode(key.toString(), "utf-8"); 105 buffer.append(requestParameterName).append('=').append(encodedKey); 106 } catch (UnsupportedEncodingException uee) { 107 } 109 return buffer.toString(); 110 } 111 } 112 } 113 114 String label = this.labelManager.getCurrentLabel(); 115 116 return getLink(super.getLinkURI(event, secure), requestParameterName, label); 117 } 118 119 125 public String getLinkURI(List events) { 126 return getLinkURI(events, null); 127 } 128 129 135 public String getLinkURI(List events, Boolean secure) { 136 if (events == null || events.size() == 0) { 137 return this.getRefreshLinkURI(secure); 138 } 139 if (this.labelManager == null) { 140 return super.getLinkURI(events); 141 } 142 143 String requestParameterName = this.labelManager.getRequestParameterName(); 144 final LinkInfo info = this.getInfo(); 145 final StringBuffer buffer = new StringBuffer (info.getBase(secure)); 146 boolean hasParams = info.hasParameters(); 147 Iterator iter = events.iterator(); 148 StringBuffer value = new StringBuffer (""); 149 150 while (iter.hasNext()) 151 { 152 Event event = (Event)iter.next(); 153 154 if (event instanceof ChangeAspectDataEvent && 155 ((ChangeAspectDataEvent) event).getTarget() instanceof CompositeLayout) { 156 157 ChangeAspectDataEvent e = (ChangeAspectDataEvent) event; 158 CompositeLayout layout = (CompositeLayout) e.getTarget(); 159 int i = ((Integer ) e.getData()).intValue(); 160 161 Item item = layout.getItem(i); 162 if (value.length() > 0) { 163 value.append('.'); 164 } 165 if (item instanceof NamedItem) { 166 if(value.length()>0) { 167 value.append(((NamedItem)item).getName()); 168 } 169 else { 170 StringBuffer key = new StringBuffer (""); 171 getKey(item, key); 172 value.append(key.toString()); 173 } 174 } 175 else { 176 value.append(Integer.toString(i)); 177 } 178 } 179 else { 180 String label = this.labelManager.getCurrentLabel(); 181 182 return getLink(super.getLinkURI(events, secure), requestParameterName, label); 183 } 184 } 185 186 if (value.length() > 0 && this.labelManager.getPageLabelEvents(value.toString()) != null) { 187 if (hasParams) { 188 buffer.append('&'); 189 } 190 else { 191 buffer.append('?'); 192 } 193 try { 194 buffer.append(requestParameterName).append('=') 195 .append(NetUtils.encode(value.toString(), "utf-8")); 196 } catch (UnsupportedEncodingException uee) { 197 } 199 200 return buffer.toString(); 201 } 202 203 String label = this.labelManager.getCurrentLabel(); 204 205 return getLink(super.getLinkURI(events), requestParameterName, label); 206 } 207 208 211 public void dispose() { 212 if (this.manager != null) { 213 if (this.labelManager != null) { 214 this.manager.release(this.labelManager); 215 this.labelManager = null; 216 } 217 } 218 super.dispose(); 219 } 220 221 226 private void getKey(Item item, StringBuffer key) { 227 CompositeLayout parentLayout = item.getParent(); 228 Item parentItem = parentLayout.getParent(); 229 230 if (parentItem != null) { 231 getKey(parentItem, key); 232 } 233 234 if (key.length() > 0) { 235 key.append('.'); 236 } 237 if (item instanceof NamedItem) { 238 key.append(((NamedItem) item).getName()); 239 } 240 else { 241 key.append(parentLayout.getItems().indexOf(item)); 242 } 243 } 244 245 252 private String getLink(String link, String parmName, String label) 253 { 254 if (label == null) { 255 return link; 256 } 257 StringBuffer uri = new StringBuffer (link); 258 if (link.indexOf('?') >= 0) { 259 uri.append('&'); 260 } else { 261 uri.append('?'); 262 } 263 try { 264 String encodedLabel = NetUtils.encode(label, "utf-8"); 265 uri.append(parmName).append('=').append(encodedLabel); 266 } catch (UnsupportedEncodingException uee) { 267 } 269 return uri.toString(); 270 } 271 } 272 | Popular Tags |