예제 #1
0
  /**
   * 设置关键字高亮
   *
   * @param query 查询对象
   * @param list 设置高亮的内容列表
   * @param subLength 截取长度
   * @param fields 字段名
   */
  public List<T> keywordsHighlight(
      BooleanQuery query, List<T> list, int subLength, String... fields) {
    Analyzer analyzer = new IKAnalyzer();
    Formatter formatter = new SimpleHTMLFormatter("<span class=\"highlight\">", "</span>");
    Highlighter highlighter = new Highlighter(formatter, new QueryScorer(query));
    highlighter.setTextFragmenter(new SimpleFragmenter(subLength));

    for (T entity : list) {
      try {
        for (String field : fields) {
          String text = StringUtils.replaceHtml((String) Reflections.invokeGetter(entity, field));
          // 设置高亮字段
          String description = highlighter.getBestFragment(analyzer, field, text);
          if (description != null) {
            Reflections.invokeSetter(entity, fields[0], description);
            break;
          }
          Reflections.invokeSetter(entity, fields[0], StringUtils.abbr(text, subLength * 2));
        }
      } catch (IOException e) {
        e.printStackTrace();
      } catch (InvalidTokenOffsetsException e) {
        e.printStackTrace();
      }
    }
    return list;
  }
예제 #2
0
 /**
  * 添加数据(通过annotation.ExportField添加数据)
  *
  * @return list 数据列表
  */
 public <E> ExportExcel setDataList(List<E> list) {
   for (E e : list) {
     int colunm = 0;
     Row row = this.addRow();
     StringBuilder sb = new StringBuilder();
     for (Object[] os : annotationList) {
       ExcelField ef = (ExcelField) os[0];
       Object val = null;
       // Get entity value
       try {
         if (StringUtils.isNotBlank(ef.value())) {
           val = Reflections.invokeGetter(e, ef.value());
         } else {
           if (os[1] instanceof Field) {
             val = Reflections.invokeGetter(e, ((Field) os[1]).getName());
           } else if (os[1] instanceof Method) {
             val =
                 Reflections.invokeMethod(
                     e, ((Method) os[1]).getName(), new Class[] {}, new Object[] {});
           }
         }
         // If is dict, get dict label
         if (StringUtils.isNotBlank(ef.dictType())) {
           val = DictUtils.getDictLabel(val == null ? "" : val.toString(), ef.dictType(), "");
         }
       } catch (Exception ex) {
         // Failure to ignore
         log.info(ex.toString());
         val = "";
       }
       this.addCell(row, colunm++, val, ef.align(), ef.fieldType());
       sb.append(val + ", ");
     }
     log.debug("Write success: [" + row.getRowNum() + "] " + sb.toString());
   }
   return this;
 }