View Javadoc

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 org.itracker.web.taglib;
20  
21  import java.net.MalformedURLException;
22  import java.util.Locale;
23  
24  import javax.servlet.http.HttpSession;
25  import javax.servlet.jsp.JspException;
26  import javax.servlet.jsp.tagext.BodyTagSupport;
27  
28  import org.apache.oro.text.regex.MalformedPatternException;
29  import org.apache.oro.text.regex.Pattern;
30  import org.apache.oro.text.regex.PatternCompiler;
31  import org.apache.oro.text.regex.Perl5Compiler;
32  import org.apache.oro.text.regex.Perl5Matcher;
33  import org.apache.oro.text.regex.Perl5Substitution;
34  import org.apache.oro.text.regex.Util;
35  import org.apache.struts.taglib.TagUtils;
36  import org.itracker.core.resources.ITrackerResources;
37  import org.itracker.services.util.HTMLUtilities;
38  import org.itracker.services.util.ProjectUtilities;
39  import org.itracker.web.util.Constants;
40  
41  
42  /**
43    * Formats an ITracker Issue History entry.  Will rewrite the entry to include
44    * links to other issues automatically based on the strings provided in the
45    * resource bundles under the itracker.web.issuenames key.  This key can contain
46    * a pipe seperated list of names to look for and then matches in the pattern
47    * ([names])(\d+).
48    */
49  public class FormatHistoryEntryTag extends BodyTagSupport {
50      /**
51  	 * 
52  	 */
53  	private static final long serialVersionUID = 1L;
54  	private static Perl5Matcher matcher = new Perl5Matcher();
55      private static PatternCompiler compiler = new Perl5Compiler();
56  
57      private String text = null;
58      private String issueNamesKey = "itracker.web.issuenames";
59      private String forward ="viewissue";
60      private String paramName = "id";
61      private String paramValue = "$2";
62      private String textPattern = "$1 $2";
63      private String styleClass = "history";
64      private int projectOptions = 0;
65  
66      public String getForward() {
67        	return forward;
68      }
69  
70      public void setForward(String value) {
71      	  forward = value;
72      }
73  
74      public String getParamName() {
75        	return paramName;
76      }
77  
78      public void setParamName(String value) {
79      	  paramName = value;
80      }
81  
82      public Object getParamValue() {
83        	return paramValue;
84      }
85  
86      public void setParamValue(Object value) {
87      	  paramValue = (value != null ? value.toString() : null);
88      }
89  
90      public String getIssueNamesKey() {
91          return issueNamesKey;
92      }
93  
94      public void setIssueNamesKey(String value) {
95          issueNamesKey = value;
96      }
97  
98      public String getTextPattern() {
99          return textPattern;
100     }
101 
102     public void setTextPattern(String value) {
103         textPattern = value;
104     }
105 
106     public String getStyleClass() {
107       	return styleClass;
108     }
109 
110     public void setStyleClass(String value) {
111     	  styleClass = value;
112     }
113 
114     public int getProjectOptions() {
115       	return projectOptions;
116     }
117 
118     public void setProjectOptions(int value) {
119     	  projectOptions = value;
120     }
121 
122     public int doStartTag() throws JspException {
123         text = null;
124         return EVAL_BODY_BUFFERED;
125     }
126 
127     public int doAfterBody() throws JspException {
128         if(bodyContent != null) {
129             String value = bodyContent.getString().trim();
130             if(value.length() > 0) {
131                 text = value;
132             }
133         }
134         return SKIP_BODY;
135     }
136 
137     public int doEndTag() throws JspException {
138         if(text != null) {
139             Locale locale = null;
140 
141             HttpSession session = pageContext.getSession();
142             if(session != null) {
143                 locale = (Locale) session.getAttribute(Constants.LOCALE_KEY);
144             }
145 
146             if(ProjectUtilities.hasOption(ProjectUtilities.OPTION_SURPRESS_HISTORY_HTML, projectOptions)) {
147                 text = HTMLUtilities.removeMarkup(text);
148             } else if(ProjectUtilities.hasOption(ProjectUtilities.OPTION_LITERAL_HISTORY_HTML, projectOptions)) {
149                 text = HTMLUtilities.escapeTags(text);
150             } else {
151                 text = HTMLUtilities.newlinesToBreaks(text);
152             }
153 
154             try {
155                 Pattern pattern = compiler.compile("(" + ITrackerResources.getString(issueNamesKey, locale) + ")\\s(\\d+)", Perl5Compiler.CASE_INSENSITIVE_MASK);
156 
157                 StringBuffer buf = new StringBuffer("<a href=\"");
158                 try {
159                     //buf.append(RequestUtils.computeURL(pageContext, forward, null, null, null, null, null, false));
160                     buf.append(TagUtils.getInstance().computeURL(pageContext, forward, null, null, null, null, null, null, false));
161                 } catch(MalformedURLException murle) {
162                     buf.append(forward);
163                 }
164                 buf.append("?" + paramName + "=" + paramValue + "\" ");
165                 buf.append("class=\"" + styleClass + "\">");
166                 buf.append(textPattern);
167                 buf.append("</a>");
168 
169                 text = Util.substitute(matcher, pattern, new Perl5Substitution(buf.toString()), text, Util.SUBSTITUTE_ALL);
170             } catch(MalformedPatternException mpe) {
171             }
172 
173             if(ProjectUtilities.hasOption(ProjectUtilities.OPTION_SURPRESS_HISTORY_HTML, projectOptions) ||
174                ProjectUtilities.hasOption(ProjectUtilities.OPTION_LITERAL_HISTORY_HTML, projectOptions)) {
175                 text = "<pre>" + text + "</pre>";
176             }
177 
178            // ResponseUtils.write(pageContext, text);
179             TagUtils.getInstance().write(pageContext, text);
180         }
181 
182         clearState();
183         return (EVAL_PAGE);
184     }
185 
186     public void release() {
187         super.release();
188         clearState();
189     }
190 
191     private void clearState() {
192         text = null;
193         issueNamesKey = "itracker.web.issuenames";
194         forward ="viewissue";
195         paramName = "id";
196         paramValue = "$2";
197         textPattern = "$1 $2";
198         styleClass = "history";
199     }
200 }