1818import edu .stanford .nlp .time .Timex ;
1919import edu .stanford .nlp .util .CoreMap ;
2020
21+ import edu .stanford .nlp .util .StringUtils ;
2122import org .apache .commons .lang3 .StringEscapeUtils ;
2223
23- public class SUTimeServlet extends HttpServlet
24- {
25- SUTimePipeline pipeline = null ;
24+ public class SUTimeServlet extends HttpServlet {
2625
27- public void init ()
28- throws ServletException
29- {
26+ private SUTimePipeline pipeline ; // = null;
27+
28+ @ Override
29+ public void init () throws ServletException {
3030 String dataDir = getServletContext ().getRealPath ("/WEB-INF/data" );
3131 String taggerFilename = dataDir + "/english-left3words-distsim.tagger" ;
3232 Properties pipelineProps = new Properties ();
@@ -37,18 +37,17 @@ public void init()
3737 }
3838
3939 public static boolean parseBoolean (String value ) {
40- if (value == null || value . equals ( "" )) {
40+ if (StringUtils . isNullOrEmpty ( value )) {
4141 return false ;
4242 }
4343 if (value .equalsIgnoreCase ("on" )) {
4444 return true ;
4545 }
46- return Boolean .valueOf (value );
46+ return Boolean .parseBoolean (value );
4747 }
4848
4949 public void doGet (HttpServletRequest request , HttpServletResponse response )
50- throws ServletException , IOException
51- {
50+ throws ServletException , IOException {
5251 if (request .getCharacterEncoding () == null ) {
5352 request .setCharacterEncoding ("utf-8" );
5453 }
@@ -61,9 +60,9 @@ public void doGet(HttpServletRequest request, HttpServletResponse response)
6160 include (request , response );
6261 }
6362
63+ @ Override
6464 public void doPost (HttpServletRequest request , HttpServletResponse response )
65- throws ServletException , IOException
66- {
65+ throws ServletException , IOException {
6766 doGet (request , response );
6867 }
6968
@@ -79,8 +78,7 @@ private String getRuleFilepaths(String... files) {
7978 return sb .toString ();
8079 }
8180
82- private Properties getTimeAnnotatorProperties (HttpServletRequest request )
83- {
81+ private Properties getTimeAnnotatorProperties (HttpServletRequest request ) {
8482 // Parses request and set up properties for time annotators
8583 boolean markTimeRanges =
8684 parseBoolean (request .getParameter ("markTimeRanges" ));
@@ -93,9 +91,8 @@ private Properties getTimeAnnotatorProperties(HttpServletRequest request)
9391 String heuristicLevel = request .getParameter ("relativeHeuristicLevel" );
9492 Options .RelativeHeuristicLevel relativeHeuristicLevel =
9593 Options .RelativeHeuristicLevel .NONE ;
96- if (heuristicLevel != null && !heuristicLevel .equals ("" )) {
97- relativeHeuristicLevel =
98- Options .RelativeHeuristicLevel .valueOf (heuristicLevel );
94+ if ( ! StringUtils .isNullOrEmpty (heuristicLevel )) {
95+ relativeHeuristicLevel = Options .RelativeHeuristicLevel .valueOf (heuristicLevel );
9996 }
10097 String ruleFile = null ;
10198 if (readRules ) {
@@ -132,7 +129,7 @@ private Properties getTimeAnnotatorProperties(HttpServletRequest request)
132129 return props ;
133130 }
134131
135- private void displayAnnotation (PrintWriter out , String query , Annotation anno , boolean includeOffsets ) {
132+ private static void displayAnnotation (PrintWriter out , String query , Annotation anno , boolean includeOffsets ) {
136133 List <CoreMap > timexAnns = anno .get (TimeAnnotations .TimexAnnotations .class );
137134 List <String > pieces = new ArrayList <>();
138135 List <Boolean > tagged = new ArrayList <>();
@@ -214,24 +211,23 @@ private void displayAnnotation(PrintWriter out, String query, Annotation anno, b
214211
215212 private void addResults (HttpServletRequest request ,
216213 HttpServletResponse response )
217- throws IOException
218- {
214+ throws IOException {
219215 // if we can't handle UTF-8, need to do something like this...
220216 //String originalQuery = request.getParameter("q");
221217 //String query = WebappUtil.convertString(originalQuery);
222-
218+
223219 String query = request .getParameter ("q" );
224220 String dateString = request .getParameter ("d" );
225221 // TODO: this always returns true...
226- boolean dateError = !pipeline .isDateOkay (dateString );
222+ boolean dateError = ! pipeline .isDateOkay (dateString );
227223 boolean includeOffsets = parseBoolean (request .getParameter ("includeOffsets" ));
228224 PrintWriter out = response .getWriter ();
229225 if (dateError ) {
230- out .println ("<br><br>Warning: unparseable date " +
226+ out .println ("<br><br>Warning: unparseable date " +
231227 StringEscapeUtils .escapeHtml4 (dateString ));
232228 }
233229
234- if (query != null && ! query . equals ( "" )) {
230+ if ( ! StringUtils . isNullOrEmpty ( query )) {
235231 Properties props = getTimeAnnotatorProperties (request );
236232 String annotatorType = request .getParameter ("annotator" );
237233 if (annotatorType == null ) {
@@ -243,7 +239,7 @@ private void addResults(HttpServletRequest request,
243239 out .println ("<h3>Annotated Text</h3> <em>(tagged using " + annotatorType + "</em>)" );
244240 displayAnnotation (out , query , anno , includeOffsets );
245241 } else {
246- out .println ("<br><br>Error creating annotator for " + annotatorType );
242+ out .println ("<br><br>Error creating annotator for " + StringEscapeUtils . escapeHtml4 ( annotatorType ) );
247243 }
248244
249245
@@ -252,4 +248,5 @@ private void addResults(HttpServletRequest request,
252248 }
253249
254250 private static final long serialVersionUID = 1L ;
251+
255252}
0 commit comments