1 package net.sourceforge.blogentis.turbine;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 import java.util.List;
26 import java.util.NoSuchElementException;
27 import java.util.StringTokenizer;
28
29 import javax.servlet.http.HttpServletRequest;
30
31 import net.sourceforge.blogentis.om.Blog;
32 import net.sourceforge.blogentis.om.Post;
33 import net.sourceforge.blogentis.om.PostPeer;
34 import net.sourceforge.blogentis.om.Section;
35 import net.sourceforge.blogentis.om.SectionPeer;
36 import net.sourceforge.blogentis.utils.AbsoluteLinkURL;
37 import net.sourceforge.blogentis.utils.BlogManagerService;
38 import net.sourceforge.blogentis.utils.BlogRequestWrapper;
39 import net.sourceforge.blogentis.utils.DateSpecification;
40 import net.sourceforge.blogentis.utils.LinkFactoryService;
41 import net.sourceforge.blogentis.utils.LinkStack;
42
43 import org.apache.commons.logging.Log;
44 import org.apache.commons.logging.LogFactory;
45 import org.apache.torque.TorqueException;
46 import org.apache.torque.util.Criteria;
47 import org.apache.turbine.util.parser.DefaultParameterParser;
48
49 public class BlogParameterParser
50 extends DefaultParameterParser {
51 private static Log log = LogFactory.getLog(BlogParameterParser.class);
52
53 protected Blog blog = null;
54
55 protected int postId = -1;
56
57 protected Post post = null;
58
59 protected DateSpecification date = null;
60
61 protected LinkStack links = new LinkStack();
62
63 protected Section section = null;
64
65 protected String template = null;
66
67 public BlogParameterParser() {
68 super();
69 }
70
71 public BlogParameterParser(String characterEncoding) {
72 super(characterEncoding);
73 }
74
75 public void setRequest(HttpServletRequest req) {
76
77
78
79
80
81
82 String path = req.getServletPath();
83 BlogRequestWrapper hrw = (BlogRequestWrapper)req;
84 hrw.setPathInfo("");
85 super.setRequest(hrw);
86 links = new LinkStack();
87 AbsoluteLinkURL link = LinkFactoryService.getLink().clear();
88 try {
89 path = path.replaceFirst("^/+", "");
90 path = path.replaceAll("//+", "/");
91 StringTokenizer stok = new StringTokenizer(path, "/", false);
92
93 setTemplate( "Index.vm");
94 if (!stok.hasMoreTokens()){
95 setAggregateBlog();
96 return;
97 }
98 String token = stok.nextToken();
99 if (token.equals("blog")) {
100 setAggregateBlog();
101 return;
102 } else {
103 try {
104 setBlog(token);
105 } catch (TorqueException e) {
106 setTemplate("Default.vm");
107
108 return;
109 }
110 token = stok.nextToken();
111 }
112 if (token.equals("post")) {
113 int pid = Integer.parseInt(stok.nextToken());
114 setPost(PostPeer.retrieveByPK(pid));
115 token = stok.nextToken();
116 } else {
117 if (determineSection(token)) {
118 token = stok.nextToken();
119 }
120 if (token.matches("^//d//d//d//d$")) {
121 token = parseDateTokens(token, stok, link);
122 if (Character.isLowerCase(token.charAt(0))
123 || Character.isDigit(token.charAt(0))) {
124 token = lookupPostByFragment(token, stok);
125 }
126 }
127 }
128 if (token != null && token.length() > 0
129 && Character.isUpperCase(token.charAt(0))) {
130 setTemplate(token + ".vm");
131 template = token;
132 token = stok.nextToken();
133 }
134 while (stok.hasMoreElements()) {
135 setString(token, stok.nextToken());
136 token = stok.nextToken();
137 }
138 } catch (NoSuchElementException e) {
139 log.debug("finished");
140 } catch (Exception e) {
141 log.error("Could not parse parameters", e);
142 }
143 links.initFromParameters(this);
144 }
145
146 /***
147 *
148 */
149 private void setTemplate(String template) {
150 setString("template",template);
151 }
152
153 private void setAggregateBlog() {
154
155 }
156
157 private String lookupPostByFragment(String token, StringTokenizer stok)
158 throws TorqueException {
159 Criteria c = new Criteria();
160 c.add(PostPeer.URI_FRAGMENT, token);
161 if (blog != null)
162 c.add(PostPeer.BLOG_ID, blog.getBlogId());
163 date.addToCriteria(c, PostPeer.POSTED_TIME);
164 List list = PostPeer.doSelect(c);
165 if (list.size() > 0) {
166 setPost((Post)list.get(0));
167 token = stok.nextToken();
168 }
169 return token;
170 }
171
172 private String parseDateTokens(String token, StringTokenizer stok,
173 AbsoluteLinkURL link) {
174 date = new DateSpecification(Integer.parseInt(token));
175 setTemplate("ViewYear.vm");
176 token = stok.nextToken();
177 if (token.matches("^//d//d$")) {
178 date.setMonth(Integer.parseInt(token));
179 setTemplate("Index.vm");
180 token = stok.nextToken();
181 if (token.matches("^//d//d$")) {
182 date.setDay(Integer.parseInt(token));
183 token = stok.nextToken();
184 setTemplate( "Index.vm");
185 }
186 }
187 return token;
188 }
189
190 private void setPost(Post post) {
191 postId = (post != null) ? post.getPostId() : -1;
192 add("postId", postId);
193 setTemplate("ViewPost.vm");
194 this.post = post;
195 }
196
197 public boolean hasPostId() {
198 return postId != -1;
199 }
200
201 public Blog getBlog() {
202 return blog;
203 }
204
205 public DateSpecification getDate() {
206 return date;
207 }
208
209 public int getPostId() {
210 return postId;
211 }
212
213 public Post getPost() {
214 return post;
215 }
216
217 private void setBlog(String name)
218 throws TorqueException {
219 blog = BlogManagerService.getBlog(name);
220 add("blog", name);
221 }
222
223 public void dispose() {
224 clear();
225 super.dispose();
226 }
227
228 public void clear() {
229 blog = null;
230 postId = -1;
231 date = null;
232 links = new LinkStack();
233 post = null;
234 section = null;
235 template = null;
236 super.clear();
237 }
238
239 public Section getSection() {
240 return section;
241 }
242
243 private boolean determineSection(String token) {
244 if (blog == null)
245 return false;
246 try {
247 section = SectionPeer
248 .retrieveSectionByPath(blog.getBlogId(), token);
249 return true;
250 } catch (TorqueException ignored) {
251 return false;
252 }
253 }
254
255 public LinkStack getLinks() {
256 return links;
257 }
258
259 public String getRequestedTemplate() {
260 return template;
261 }
262 }