KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > cowsultants > itracker > web > taglib > FormatHistoryEntryTag


1 /*
2  * This software was designed and created by Jason Carroll.
3  * Copyright (c) 2002, 2003, 2004 Jason Carroll.
4  * The author can be reached at jcarroll@cowsultants.com
5  * ITracker website: http://www.cowsultants.com
6  * ITracker forums: http://www.cowsultants.com/phpBB/index.php
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it only under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  */

18
19 package cowsultants.itracker.web.taglib;
20
21 import java.io.*;
22 import java.net.*;
23 import java.util.*;
24 import java.text.*;
25 import javax.servlet.*;
26 import javax.servlet.http.*;
27 import javax.servlet.jsp.*;
28 import javax.servlet.jsp.tagext.*;
29
30 import org.apache.oro.text.regex.*;
31 import org.apache.struts.util.*;
32
33 import cowsultants.itracker.ejb.client.resources.*;
34 import cowsultants.itracker.ejb.client.util.*;
35 import cowsultants.itracker.web.util.*;
36
37 /**
38   * Formats an ITracker Issue History entry. Will rewrite the entry to include
39   * links to other issues automatically based on the strings provided in the
40   * resource bundles under the itracker.web.issuenames key. This key can contain
41   * a pipe seperated list of names to look for and then matches in the pattern
42   * ([names])(\d+).
43   */

44 public class FormatHistoryEntryTag extends BodyTagSupport {
45     private static Perl5Matcher matcher = new Perl5Matcher();
46     private static PatternCompiler compiler = new Perl5Compiler();
47
48     private String JavaDoc text = null;
49     private String JavaDoc issueNamesKey = "itracker.web.issuenames";
50     private String JavaDoc forward ="viewissue";
51     private String JavaDoc paramName = "id";
52     private String JavaDoc paramValue = "$2";
53     private String JavaDoc textPattern = "$1 $2";
54     private String JavaDoc styleClass = "history";
55     private int projectOptions = 0;
56
57     public String JavaDoc getForward() {
58         return forward;
59     }
60
61     public void setForward(String JavaDoc value) {
62           forward = value;
63     }
64
65     public String JavaDoc getParamName() {
66         return paramName;
67     }
68
69     public void setParamName(String JavaDoc value) {
70           paramName = value;
71     }
72
73     public Object JavaDoc getParamValue() {
74         return paramValue;
75     }
76
77     public void setParamValue(Object JavaDoc value) {
78           paramValue = (value != null ? value.toString() : null);
79     }
80
81     public String JavaDoc getIssueNamesKey() {
82         return issueNamesKey;
83     }
84
85     public void setIssueNamesKey(String JavaDoc value) {
86         issueNamesKey = value;
87     }
88
89     public String JavaDoc getTextPattern() {
90         return textPattern;
91     }
92
93     public void setTextPattern(String JavaDoc value) {
94         textPattern = value;
95     }
96
97     public String JavaDoc getStyleClass() {
98         return styleClass;
99     }
100
101     public void setStyleClass(String JavaDoc value) {
102           styleClass = value;
103     }
104
105     public int getProjectOptions() {
106         return projectOptions;
107     }
108
109     public void setProjectOptions(int value) {
110           projectOptions = value;
111     }
112
113     public int doStartTag() throws JspException {
114         text = null;
115         return EVAL_BODY_BUFFERED;
116     }
117
118     public int doAfterBody() throws JspException {
119         if(bodyContent != null) {
120             String JavaDoc value = bodyContent.getString().trim();
121             if(value.length() > 0) {
122                 text = value;
123             }
124         }
125         return SKIP_BODY;
126     }
127
128     public int doEndTag() throws JspException {
129         if(text != null) {
130             Locale currLocale = null;
131
132             HttpSession session = pageContext.getSession();
133             if(session != null) {
134                 currLocale = (Locale) session.getAttribute(Constants.LOCALE_KEY);
135             }
136
137             if(ProjectUtilities.hasOption(ProjectUtilities.OPTION_SURPRESS_HISTORY_HTML, projectOptions)) {
138                 text = HTMLUtilities.removeMarkup(text);
139             } else if(ProjectUtilities.hasOption(ProjectUtilities.OPTION_LITERAL_HISTORY_HTML, projectOptions)) {
140                 text = HTMLUtilities.escapeTags(text);
141             } else {
142                 text = HTMLUtilities.newlinesToBreaks(text);
143             }
144
145             try {
146                 Pattern pattern = compiler.compile("(" + ITrackerResources.getString(issueNamesKey, currLocale) + ")\\s(\\d+)", Perl5Compiler.CASE_INSENSITIVE_MASK);
147
148                 StringBuffer JavaDoc buf = new StringBuffer JavaDoc("<a HREF=\"");
149                 try {
150                     buf.append(RequestUtils.computeURL(pageContext, forward, null, null, null, null, null, false));
151                 } catch(MalformedURLException murle) {
152                     buf.append(forward);
153                 }
154                 buf.append("?" + paramName + "=" + paramValue + "\" ");
155                 buf.append("class=\"" + styleClass + "\">");
156                 buf.append(textPattern);
157                 buf.append("</a>");
158
159                 text = Util.substitute(matcher, pattern, new Perl5Substitution(buf.toString()), text, Util.SUBSTITUTE_ALL);
160             } catch(MalformedPatternException mpe) {
161             }
162
163             if(ProjectUtilities.hasOption(ProjectUtilities.OPTION_SURPRESS_HISTORY_HTML, projectOptions) ||
164                ProjectUtilities.hasOption(ProjectUtilities.OPTION_LITERAL_HISTORY_HTML, projectOptions)) {
165                 text = "<pre>" + text + "</pre>";
166             }
167
168             ResponseUtils.write(pageContext, text);
169         }
170
171         clearState();
172         return (EVAL_PAGE);
173     }
174
175     public void release() {
176         super.release();
177         clearState();
178     }
179
180     private void clearState() {
181         text = null;
182         issueNamesKey = "itracker.web.issuenames";
183         forward ="viewissue";
184         paramName = "id";
185         paramValue = "$2";
186         textPattern = "$1 $2";
187         styleClass = "history";
188     }
189 }
190
Popular Tags