View Javadoc

1   package net.sourceforge.blogentis.utils;
2   
3   //-----------------------------------------------------------------------
4   //Blogentis - a blog publishing platform.
5   //Copyright (C) 2004 Tassos Bassoukos <abassouk@gmail.com>
6   //
7   //This library is free software; you can redistribute it and/or
8   //modify it under the terms of the GNU Lesser General Public
9   //License as published by the Free Software Foundation; either
10  //version 2.1 of the License, or (at your option) any later version.
11  //
12  //This library is distributed in the hope that it will be useful,
13  //but WITHOUT ANY WARRANTY; without even the implied warranty of
14  //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  //Lesser General Public License for more details.
16  //
17  //You should have received a copy of the GNU Lesser General Public
18  //License along with this library; if not, write to the Free Software
19  //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  //-----------------------------------------------------------------------
21  //
22  //$Id: AbsoluteLinkURL.java,v 1.2 2004/10/28 10:45:51 tassos Exp $
23  //
24  
25  import java.io.UnsupportedEncodingException;
26  import java.net.URLEncoder;
27  import java.text.DecimalFormat;
28  import java.text.NumberFormat;
29  import java.util.Date;
30  import java.util.HashMap;
31  import java.util.Iterator;
32  import java.util.Map;
33  import java.util.Set;
34  
35  import net.sourceforge.blogentis.om.Blog;
36  import net.sourceforge.blogentis.om.Comment;
37  import net.sourceforge.blogentis.om.Post;
38  import net.sourceforge.blogentis.om.Section;
39  import net.sourceforge.blogentis.turbine.BlogParameterParser;
40  
41  import org.apache.commons.logging.Log;
42  import org.apache.commons.logging.LogFactory;
43  import org.apache.torque.TorqueException;
44  import org.apache.turbine.services.pull.ApplicationTool;
45  import org.apache.turbine.util.RunData;
46  
47  public class AbsoluteLinkURL
48          implements ApplicationTool {
49      private static Log log = LogFactory.getLog(AbsoluteLinkURL.class);
50  
51      public static final NumberFormat YearFormat = new DecimalFormat("0000");
52  
53      public static final NumberFormat MonthFormat = new DecimalFormat("00");
54  
55      public static final NumberFormat DayFormat = new DecimalFormat("00");
56  
57      protected boolean absolute_url = true;
58  
59      protected String blogName = null;
60  
61      protected StringBuffer buffer = new StringBuffer();
62  
63      protected DateSpecification date = null;
64  
65      protected boolean hasQueryData = false;
66  
67      protected Map map = new HashMap();
68  
69      protected int postId = -1;
70  
71      protected String prefix = null;
72  
73      protected String sectionName = null;
74  
75      protected String fragmentName = null;
76  
77      protected String template = null;
78  
79      private String postFragment = null;
80  
81      public AbsoluteLinkURL() {
82          prefix = LinkFactoryService.getBaseURI();
83      }
84  
85      public AbsoluteLinkURL addMap(Map m)
86              throws ClassCastException {
87          Set keys = m.keySet();
88          for(Iterator i = keys.iterator(); i.hasNext();) {
89              String name = (String)i.next();
90              addQueryData(name, (String)m.get(name));
91          }
92          return this;
93      }
94  
95      public AbsoluteLinkURL addQueryData(String name, String value) {
96          if (value != null) {
97              map.put(name, value);
98              hasQueryData = true;
99          }
100         return this;
101     }
102 
103     public AbsoluteLinkURL clear() {
104         buffer.delete(0, buffer.length());
105         map.clear();
106         hasQueryData = false;
107         template = null;
108         blogName = null;
109         date = null;
110         postId = -1;
111         postFragment = null;
112         sectionName = null;
113         fragmentName = null;
114         return this;
115     }
116 
117     private void fillQueryData()
118             throws UnsupportedEncodingException {
119         if (!hasQueryData)
120             return;
121         buffer.append("?");
122         for(Iterator i = map.entrySet().iterator(); i.hasNext();) {
123             Map.Entry e = (Map.Entry)i.next();
124             buffer.append(URLEncoder.encode((String)e.getKey(), "utf-8"));
125             buffer.append("=");
126             buffer.append(URLEncoder.encode((String)e.getValue(), "utf-8"));
127             if (i.hasNext())
128                 buffer.append("&amp;");
129         }
130         hasQueryData = false;
131     }
132 
133     public String getBlogName() {
134         return blogName;
135     }
136 
137     public AbsoluteLinkURL permaLink(Blog blog) {
138         clear();
139         setBlog(blog);
140         return this;
141     }
142 
143     public AbsoluteLinkURL permaLink(Post post) {
144         try {
145             permaLink(post.getBlog());
146             setPost(post);
147         } catch (TorqueException e) {}
148         return this;
149     }
150 
151     public AbsoluteLinkURL permaLink(Comment c) {
152         try {
153             permaLink(c.getPost());
154             fragmentName = "c" + c.getCommentId();
155         } catch (TorqueException e) {}
156         return this;
157     }
158 
159     public AbsoluteLinkURL permaLink(Section section) {
160         try {
161             permaLink(section.getBlog());
162             setSection(section);
163         } catch (TorqueException e) {}
164         return this;
165     }
166 
167     public String getTemplate() {
168         return template;
169     }
170 
171     public AbsoluteLinkURL setBlog(Blog blog) {
172         if (blog != null)
173             setBlogName(blog.getName());
174         else
175             blogName = null;
176         return this;
177     }
178 
179     public void setBlogName(String string) {
180         blogName = string;
181     }
182 
183     public AbsoluteLinkURL setFile(String file) {
184         clear();
185         if (file.charAt(0) == '/')
186             file = file.substring(1);
187         buffer.append(file);
188         return this;
189     }
190 
191     public AbsoluteLinkURL setPost(int post) {
192         postId = post;
193         return this;
194     }
195 
196     public AbsoluteLinkURL setPost(Post post) {
197         if (post == null) {
198             postId = -1;
199             postFragment = null;
200         } else {
201             postId = post.getPostId();
202             postFragment = post.getUriFragment();
203             if (postFragment != null) {
204                 setDate(post.getPostedTime());
205             }
206             if (blogName == null) {
207                 try {
208                     setBlog(post.getBlog());
209                 } catch (TorqueException e) {}
210             }
211         }
212         return this;
213     }
214 
215     public void setTemplate(String string) {
216         template = string;
217     }
218 
219     public AbsoluteLinkURL thisPage(RunData data) {
220         BlogParameterParser pp = (BlogParameterParser)data.getParameters();
221         if (pp.getBlog() != null) {
222             setBlog(pp.getBlog());
223             setPost(pp.getPost());
224             if (postId == -1) {
225                 if (pp.getDate() != null) {
226                     date = pp.getDate();
227                 } else {
228                     if (pp.getSection() != null)
229                         setSection(pp.getSection());
230                 }
231             }
232         }
233         if (pp.getRequestedTemplate() != null) {
234             template = pp.getRequestedTemplate();
235         }
236         return this;
237     }
238 
239     public AbsoluteLinkURL thisBlog(RunData data) {
240         BlogParameterParser pp = (BlogParameterParser)data.getParameters();
241         if (pp.getBlog() != null)
242             setBlog(pp.getBlog());
243         return this;
244     }
245 
246     public String toString() {
247         try {
248             if (blogName != null) {
249                 buffer.append(blogName);
250                 if (postId != -1 && postFragment == null) {
251                     buffer.append("/post/");
252                     buffer.append(postId);
253                 } else {
254                     if (sectionName != null) {
255                         buffer.append("/");
256                         buffer.append(sectionName);
257                     }
258                     if (date != null) {
259                         buffer.append("/");
260                         buffer.append(YearFormat.format(date.getYear()));
261                         if (date.hasMonth()) {
262                             buffer.append("/");
263                             buffer.append(MonthFormat.format(date.getMonth()));
264                             if (date.hasDay()) {
265                                 buffer.append("/");
266                                 buffer
267                                     .append(MonthFormat.format(date.getDay()));
268                             }
269                         }
270                         if (postFragment != null) {
271                             buffer.append("/");
272                             buffer.append(postFragment);
273                         }
274                     }
275                 }
276             }
277             if (template != null) {
278                 if (buffer.lastIndexOf("/") != buffer.length() - 1)
279                     buffer.append("/");
280                 buffer.append(template);
281             }
282             if (fragmentName != null) {
283                 buffer.append("#");
284                 buffer.append(fragmentName);
285             }
286             try {
287                 fillQueryData();
288             } catch (UnsupportedEncodingException e) {
289                 log.warn(e); //should not happen, it's using utf-8!
290             }
291             String rv = prefix + buffer;
292             if (log.isDebugEnabled())
293                 log.debug("link: " + rv);
294             return rv;
295         } finally {
296             clear();
297         }
298     }
299 
300     public AbsoluteLinkURL setAction(String action) {
301         addQueryData("action", action);
302         return this;
303     }
304 
305     public void init(Object data) {
306         refresh();
307     }
308 
309     public void refresh() {
310         clear();
311         prefix = LinkFactoryService.getBaseURI();
312     }
313 
314     public AbsoluteLinkURL setPage(String page) {
315         setTemplate(page);
316         return this;
317     }
318 
319     public AbsoluteLinkURL setSection(String name) {
320         sectionName = name;
321         return this;
322     }
323 
324     public AbsoluteLinkURL setSection(Section sec) {
325         if (sec != null)
326             sectionName = sec.getName();
327         else
328             sectionName = null;
329         return this;
330     }
331 
332     public AbsoluteLinkURL setDate(DateSpecification date) {
333         this.date = date;
334         return this;
335     }
336 
337     public AbsoluteLinkURL setDate(Date date) {
338         this.date = new DateSpecification(date);
339         return this;
340     }
341 
342     public AbsoluteLinkURL defaultTemplate() {
343         this.template = null;
344         return this;
345     }
346 
347     public AbsoluteLinkURL setImage(String image) {
348         setPage("images/" + image);
349         return this;
350     }
351 
352     public void setFragment(String string) {
353         fragmentName = string;
354     }
355 }