private static FlexibleStringExpander getInstance(
     String expression, char[] chars, int offset, int length, boolean useCache) {
   if (length == 0) {
     return nullExpr;
   }
   if (!useCache) {
     return parse(chars, offset, length);
   }
   // Remove the next nine lines to cache all expressions
   if (!expression.contains(openBracket)) {
     if (chars.length == length) {
       return new ConstSimpleElem(chars);
     } else {
       return new ConstOffsetElem(chars, offset, length);
     }
   }
   Key key = chars.length == length ? new SimpleKey(chars) : new OffsetKey(chars, offset, length);
   FlexibleStringExpander fse = exprCache.get(key);
   if (fse == null) {
     exprCache.put(key, parse(chars, offset, length));
     fse = exprCache.get(key);
   }
   return fse;
 }
Esempio n. 2
0
  /**
   * Compiles and caches a regexp pattern for the given string pattern.
   *
   * @param stringPattern a pattern string
   * @param caseSensitive case sensitive true/false
   * @return compiles and caches a regexp pattern for the given string pattern
   * @throws MalformedPatternException
   */
  private Pattern getTestPattern(String stringPattern, boolean caseSensitive)
      throws MalformedPatternException {
    Pattern pattern = compiledPatterns.get(stringPattern);
    if (pattern == null) {
      if (caseSensitive) {
        pattern = compiler.compile(stringPattern);
      } else {
        pattern = compiler.compile(stringPattern, Perl5Compiler.CASE_INSENSITIVE_MASK);
      }

      compiledPatterns.put(stringPattern, pattern);
      Debug.logVerbose(
          "Compiled and cached a pattern: '" + stringPattern + "' - " + Thread.currentThread(),
          module);
    }
    return pattern;
  }
Esempio n. 3
0
  public static ModelReader getModelReader(String delegatorName) throws GenericEntityException {
    DelegatorInfo delegatorInfo = EntityConfigUtil.getDelegatorInfo(delegatorName);

    if (delegatorInfo == null) {
      throw new GenericEntityConfException(
          "Could not find a delegator with the name " + delegatorName);
    }

    String tempModelName = delegatorInfo.entityModelReader;
    ModelReader reader = readers.get(tempModelName);

    if (reader == null) {
      reader = new ModelReader(tempModelName);
      // preload caches...
      reader.getEntityCache();
      reader = readers.putIfAbsentAndGet(tempModelName, reader);
    }
    return reader;
  }
  public void render(
      String name,
      String page,
      String info,
      String contentType,
      String encoding,
      HttpServletRequest request,
      HttpServletResponse response)
      throws ViewHandlerException {
    // some containers call filters on EVERY request, even forwarded ones,
    // so let it know that it came from the control servlet

    if (request == null) {
      throw new ViewHandlerException(
          "The HttpServletRequest object was null, how did that happen?");
    }
    if (UtilValidate.isEmpty(page)) {
      throw new ViewHandlerException("View page was null or empty, but must be specified");
    }
    if (UtilValidate.isEmpty(info)) {
      Debug.logInfo(
          "View info string was null or empty, (optionally used to specify an Entity that is mapped to the Entity Engine datasource that the report will use).",
          module);
    }

    // tell the ContextFilter we are forwarding
    request.setAttribute(ContextFilter.FORWARDED_FROM_SERVLET, Boolean.valueOf(true));
    Delegator delegator = (Delegator) request.getAttribute("delegator");
    if (delegator == null) {
      throw new ViewHandlerException("The delegator object was null, how did that happen?");
    }

    try {
      JasperReport report = (JasperReport) jasperReportsCompiledCache.get(page);
      if (report == null) {
        synchronized (this) {
          report = (JasperReport) jasperReportsCompiledCache.get(page);
          if (report == null) {
            InputStream is = context.getResourceAsStream(page);
            report = JasperCompileManager.compileReport(is);
            jasperReportsCompiledCache.put(page, report);
          }
        }
      }

      response.setContentType("application/xls");

      Map parameters = (Map) request.getAttribute("jrParameters");
      if (parameters == null) {
        parameters = UtilHttp.getParameterMap(request);
      }

      JRDataSource jrDataSource = (JRDataSource) request.getAttribute("jrDataSource");
      JasperPrint jp = null;
      if (jrDataSource == null) {
        String datasourceName = delegator.getEntityHelperName(info);
        if (UtilValidate.isNotEmpty(datasourceName)) {
          Debug.logInfo(
              "Filling report with connection from datasource: " + datasourceName, module);
          jp =
              JasperFillManager.fillReport(
                  report, parameters, ConnectionFactory.getConnection(datasourceName));
        } else {
          Debug.logInfo("Filling report with an empty JR datasource", module);
          jp = JasperFillManager.fillReport(report, parameters, new JREmptyDataSource());
        }
      } else {
        Debug.logInfo("Filling report with a passed in jrDataSource", module);
        jp = JasperFillManager.fillReport(report, parameters, jrDataSource);
      }

      if (jp.getPages().size() < 1) {
        throw new ViewHandlerException("Report is Empty (no results?)");
      } else {
        Debug.logInfo("Got report, there are " + jp.getPages().size() + " pages.", module);
      }
      JExcelApiExporter exporter = new JExcelApiExporter();
      exporter.setParameter(JExcelApiExporterParameter.JASPER_PRINT, jp);
      exporter.setParameter(JExcelApiExporterParameter.OUTPUT_STREAM, response.getOutputStream());
      exporter.exportReport();

    } catch (IOException ie) {
      throw new ViewHandlerException("IO Error in report", ie);
    } catch (java.sql.SQLException e) {
      throw new ViewHandlerException("Database error while running report", e);
    } catch (Exception e) {
      throw new ViewHandlerException("Error in report", e);
      // } catch (ServletException se) {
      // throw new ViewHandlerException("Error in region", se.getRootCause());
    }
  }