KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > portal > impl > PageLabelLinkService


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.impl;
17
18 import java.io.UnsupportedEncodingException JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.List JavaDoc;
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 /**
32  * The PageLabelLinkService generates links for named items defined in the layout portal.xml.
33  * Links for other entities are passed to the DefaultLinkService to be resolved.
34  *
35  * @author Ralph Goers
36  *
37  * @version CVS $Id:$
38  */

39 public class PageLabelLinkService extends DefaultLinkService {
40
41     protected String JavaDoc apectName;
42
43     /** The label manager */
44     protected PageLabelManager labelManager;
45
46     /* (non-Javadoc)
47     * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
48     */

49     public void service(ServiceManager manager) throws ServiceException {
50         super.service(manager);
51         this.labelManager = (PageLabelManager)this.manager.lookup(PageLabelManager.ROLE);
52     }
53
54     /**
55      * Get the uri for the coplet containing event
56      *
57      * @param event The event to find
58      * @return A URI
59      */

60     public String JavaDoc getLinkURI(Event event) {
61         return getLinkURI(event, null);
62     }
63
64     /**
65      * Get the uri for the coplet containing event
66      *
67      * @param event The event to find
68      * @param secure true if a secure protocol is required, false otherwise.
69      * @return A URI
70      */

71     public String JavaDoc getLinkURI(Event event, Boolean JavaDoc 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 JavaDoc 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 JavaDoc)e.getData()).intValue();
87
88             Item item = layout.getItem(i);
89             if (item instanceof NamedItem) {
90                 StringBuffer JavaDoc key = new StringBuffer JavaDoc("");
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 JavaDoc buffer = new StringBuffer JavaDoc(info.getBase(secure));
97                     if (hasParams) {
98                         buffer.append('&');
99                     }
100                     else {
101                         buffer.append('?');
102                     }
103                     try {
104                         String JavaDoc encodedKey = NetUtils.encode(key.toString(), "utf-8");
105                         buffer.append(requestParameterName).append('=').append(encodedKey);
106                     } catch (UnsupportedEncodingException JavaDoc uee) {
107                         // ignore this as utf-8 is always supported
108
}
109                     return buffer.toString();
110                 }
111             }
112         }
113
114         String JavaDoc label = this.labelManager.getCurrentLabel();
115
116         return getLink(super.getLinkURI(event, secure), requestParameterName, label);
117     }
118
119     /**
120      * Get the uri for this coplet containing the additional events.
121      *
122      * @param events The events that will be processed by the generated uri.
123      * @return A URI
124      */

125     public String JavaDoc getLinkURI(List JavaDoc events) {
126         return getLinkURI(events, null);
127     }
128
129     /**
130      * Get the uri for this coplet containing the additional events.
131      *
132      * @param events The events that will be processed by the generated uri.
133      * @return A URI
134      */

135     public String JavaDoc getLinkURI(List JavaDoc events, Boolean JavaDoc 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 JavaDoc requestParameterName = this.labelManager.getRequestParameterName();
144         final LinkInfo info = this.getInfo();
145         final StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(info.getBase(secure));
146         boolean hasParams = info.hasParameters();
147         Iterator JavaDoc iter = events.iterator();
148         StringBuffer JavaDoc value = new StringBuffer JavaDoc("");
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 JavaDoc) 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 JavaDoc key = new StringBuffer JavaDoc("");
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 JavaDoc 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 JavaDoc uee) {
197                 // ignore this as utf-8 is always supported
198
}
199
200             return buffer.toString();
201         }
202
203         String JavaDoc label = this.labelManager.getCurrentLabel();
204
205         return getLink(super.getLinkURI(events), requestParameterName, label);
206     }
207
208     /* (non-Javadoc)
209     * @see org.apache.avalon.framework.activity.Disposable#dispose()
210     */

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     /*
222      * Generates the page label.
223      * @param item An Item.
224      * @param key The StringBuffer in which to create the page label.
225      */

226     private void getKey(Item item, StringBuffer JavaDoc 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     /*
246      * Append the page label to the link.
247      * @param link The link to add the label to.
248      * @param parmName The request parameter name.
249      * @param label The page label.
250      * @return The modified link.
251      */

252     private String JavaDoc getLink(String JavaDoc link, String JavaDoc parmName, String JavaDoc label)
253     {
254         if (label == null) {
255             return link;
256         }
257         StringBuffer JavaDoc uri = new StringBuffer JavaDoc(link);
258         if (link.indexOf('?') >= 0) {
259             uri.append('&');
260         } else {
261             uri.append('?');
262         }
263         try {
264             String JavaDoc encodedLabel = NetUtils.encode(label, "utf-8");
265             uri.append(parmName).append('=').append(encodedLabel);
266         } catch (UnsupportedEncodingException JavaDoc uee) {
267             // ignore this as utf-8 is always supported
268
}
269         return uri.toString();
270     }
271 }
272
Popular Tags