@Test public void testGetDateFormatEvaluator() { Evaluator dateFormatEval = EvaluatorBag.getDateFormatEvaluator(); resolver.context = new ContextImpl(null, resolver, null, Context.FULL_DUMP, Collections.EMPTY_MAP, null, null); long time = System.currentTimeMillis(); assertEquals(new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date(time - 2*86400*1000)), dateFormatEval.evaluate("'NOW-2DAYS','yyyy-MM-dd HH:mm'", resolver.context)); Map<String, Object> map = new HashMap<String, Object>(); map.put("key", new Date(time)); resolver.addNamespace("A", map); assertEquals(new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date(time)), dateFormatEval.evaluate("A.key, 'yyyy-MM-dd HH:mm'", resolver.context)); }
public static Evaluator getDateFormatEvaluator() { return new Evaluator() { public String evaluate(String expression, Context context) { List l = parseParams(expression, context.getVariableResolver()); if (l.size() != 2) { throw new DataImportHandlerException(SEVERE, "'formatDate()' must have two parameters "); } Object o = l.get(0); Object format = l.get(1); if (format instanceof VariableWrapper) { VariableWrapper wrapper = (VariableWrapper) format; o = wrapper.resolve(); if (o == null) { format = wrapper.varName; LOG.warn("Deprecated syntax used. The syntax of formatDate has been changed to formatDate(<var>, '<date_format_string>'). " + "The old syntax will stop working in Solr 1.5"); } else { format = o.toString(); } } String dateFmt = format.toString(); SimpleDateFormat fmt = new SimpleDateFormat(dateFmt); Date date = null; if (o instanceof VariableWrapper) { VariableWrapper variableWrapper = (VariableWrapper) o; Object variableval = variableWrapper.resolve(); if (variableval instanceof Date) { date = (Date) variableval; } else { String s = variableval.toString(); try { date = DataImporter.DATE_TIME_FORMAT.get().parse(s); } catch (ParseException exp) { wrapAndThrow(SEVERE, exp, "Invalid expression for date"); } } } else { String datemathfmt = o.toString(); datemathfmt = datemathfmt.replaceAll("NOW", ""); try { date = dateMathParser.parseMath(datemathfmt); } catch (ParseException e) { wrapAndThrow(SEVERE, e, "Invalid expression for date"); } } return fmt.format(date); } }; }
EvaluatorBag { public static Evaluator getDateFormatEvaluator() { return new Evaluator() { public String evaluate(String expression, Context context) { List l = parseParams(expression, context.getVariableResolver()); if (l.size() != 2) { throw new DataImportHandlerException(SEVERE, "'formatDate()' must have two parameters "); } Object o = l.get(0); Object format = l.get(1); if (format instanceof VariableWrapper) { VariableWrapper wrapper = (VariableWrapper) format; o = wrapper.resolve(); if (o == null) { format = wrapper.varName; LOG.warn("Deprecated syntax used. The syntax of formatDate has been changed to formatDate(<var>, '<date_format_string>'). " + "The old syntax will stop working in Solr 1.5"); } else { format = o.toString(); } } String dateFmt = format.toString(); SimpleDateFormat fmt = new SimpleDateFormat(dateFmt); Date date = null; if (o instanceof VariableWrapper) { VariableWrapper variableWrapper = (VariableWrapper) o; Object variableval = variableWrapper.resolve(); if (variableval instanceof Date) { date = (Date) variableval; } else { String s = variableval.toString(); try { date = DataImporter.DATE_TIME_FORMAT.get().parse(s); } catch (ParseException exp) { wrapAndThrow(SEVERE, exp, "Invalid expression for date"); } } } else { String datemathfmt = o.toString(); datemathfmt = datemathfmt.replaceAll("NOW", ""); try { date = dateMathParser.parseMath(datemathfmt); } catch (ParseException e) { wrapAndThrow(SEVERE, e, "Invalid expression for date"); } } return fmt.format(date); } }; } }
EvaluatorBag { public static Evaluator getDateFormatEvaluator() { return new Evaluator() { public String evaluate(String expression, Context context) { List l = parseParams(expression, context.getVariableResolver()); if (l.size() != 2) { throw new DataImportHandlerException(SEVERE, "'formatDate()' must have two parameters "); } Object o = l.get(0); Object format = l.get(1); if (format instanceof VariableWrapper) { VariableWrapper wrapper = (VariableWrapper) format; o = wrapper.resolve(); if (o == null) { format = wrapper.varName; LOG.warn("Deprecated syntax used. The syntax of formatDate has been changed to formatDate(<var>, '<date_format_string>'). " + "The old syntax will stop working in Solr 1.5"); } else { format = o.toString(); } } String dateFmt = format.toString(); SimpleDateFormat fmt = new SimpleDateFormat(dateFmt); Date date = null; if (o instanceof VariableWrapper) { VariableWrapper variableWrapper = (VariableWrapper) o; Object variableval = variableWrapper.resolve(); if (variableval instanceof Date) { date = (Date) variableval; } else { String s = variableval.toString(); try { date = DataImporter.DATE_TIME_FORMAT.get().parse(s); } catch (ParseException exp) { wrapAndThrow(SEVERE, exp, "Invalid expression for date"); } } } else { String datemathfmt = o.toString(); datemathfmt = datemathfmt.replaceAll("NOW", ""); try { date = dateMathParser.parseMath(datemathfmt); } catch (ParseException e) { wrapAndThrow(SEVERE, e, "Invalid expression for date"); } } return fmt.format(date); } }; }  }
EvaluatorBag { public static Evaluator getDateFormatEvaluator() { return new Evaluator() { public String evaluate(String expression, Context context) { List l = parseParams(expression, context.getVariableResolver()); if (l.size() != 2) { throw new DataImportHandlerException(SEVERE, "'formatDate()' must have two parameters "); } Object o = l.get(0); Object format = l.get(1); if (format instanceof VariableWrapper) { VariableWrapper wrapper = (VariableWrapper) format; o = wrapper.resolve(); if (o == null) { format = wrapper.varName; LOG.warn("Deprecated syntax used. The syntax of formatDate has been changed to formatDate(<var>, '<date_format_string>'). " + "The old syntax will stop working in Solr 1.5"); } else { format = o.toString(); } } String dateFmt = format.toString(); SimpleDateFormat fmt = new SimpleDateFormat(dateFmt); Date date = null; if (o instanceof VariableWrapper) { VariableWrapper variableWrapper = (VariableWrapper) o; Object variableval = variableWrapper.resolve(); if (variableval instanceof Date) { date = (Date) variableval; } else { String s = variableval.toString(); try { date = DataImporter.DATE_TIME_FORMAT.get().parse(s); } catch (ParseException exp) { wrapAndThrow(SEVERE, exp, "Invalid expression for date"); } } } else { String datemathfmt = o.toString(); datemathfmt = datemathfmt.replaceAll("NOW", ""); try { date = dateMathParser.parseMath(datemathfmt); } catch (ParseException e) { wrapAndThrow(SEVERE, e, "Invalid expression for date"); } } return fmt.format(date); } }; }  static Evaluator getSqlEscapingEvaluator(); static Evaluator getSolrQueryEscapingEvaluator(); static Evaluator getUrlEvaluator(); static Evaluator getDateFormatEvaluator(); static List parseParams(String expression, VariableResolver vr); }
EvaluatorBag { public static Evaluator getDateFormatEvaluator() { return new Evaluator() { public String evaluate(String expression, Context context) { List l = parseParams(expression, context.getVariableResolver()); if (l.size() != 2) { throw new DataImportHandlerException(SEVERE, "'formatDate()' must have two parameters "); } Object o = l.get(0); Object format = l.get(1); if (format instanceof VariableWrapper) { VariableWrapper wrapper = (VariableWrapper) format; o = wrapper.resolve(); if (o == null) { format = wrapper.varName; LOG.warn("Deprecated syntax used. The syntax of formatDate has been changed to formatDate(<var>, '<date_format_string>'). " + "The old syntax will stop working in Solr 1.5"); } else { format = o.toString(); } } String dateFmt = format.toString(); SimpleDateFormat fmt = new SimpleDateFormat(dateFmt); Date date = null; if (o instanceof VariableWrapper) { VariableWrapper variableWrapper = (VariableWrapper) o; Object variableval = variableWrapper.resolve(); if (variableval instanceof Date) { date = (Date) variableval; } else { String s = variableval.toString(); try { date = DataImporter.DATE_TIME_FORMAT.get().parse(s); } catch (ParseException exp) { wrapAndThrow(SEVERE, exp, "Invalid expression for date"); } } } else { String datemathfmt = o.toString(); datemathfmt = datemathfmt.replaceAll("NOW", ""); try { date = dateMathParser.parseMath(datemathfmt); } catch (ParseException e) { wrapAndThrow(SEVERE, e, "Invalid expression for date"); } } return fmt.format(date); } }; }  static Evaluator getSqlEscapingEvaluator(); static Evaluator getSolrQueryEscapingEvaluator(); static Evaluator getUrlEvaluator(); static Evaluator getDateFormatEvaluator(); static List parseParams(String expression, VariableResolver vr); static final String DATE_FORMAT_EVALUATOR; static final String URL_ENCODE_EVALUATOR; static final String ESCAPE_SOLR_QUERY_CHARS; static final String SQL_ESCAPE_EVALUATOR; }