Esempio n. 1
0
  public synchronized void returnParser(SAXParser parser) {
    try {
      if (!resetChecked) {
        try {
          parser.getClass().getDeclaredMethod("reset", (Class[]) null);
          resetAvailable = true;
        } catch (NoSuchMethodException e) {
          resetAvailable = false;
        }
        resetChecked = true;
      }

      if (resetAvailable) {
        parser.reset();
        parsers.push(parser);
      } else if (USE_INTERNAL_PARSER_IMPL) {
        ((XmlRpcSaxParser) parser).reset();
        parsers.push(parser);
      }
    } catch (Exception e) {
      e.printStackTrace(); // TODO log properly
    }
  }
  private final Document parseTemplateUsingPool(
      final Configuration configuration,
      final String documentName,
      final Reader reader,
      final ResourcePool<SAXParser> poolToBeUsed) {

    final SAXParser saxParser = poolToBeUsed.allocate();

    final TemplatePreprocessingReader templateReader =
        (reader instanceof TemplatePreprocessingReader
            ? (TemplatePreprocessingReader) reader
            : new TemplatePreprocessingReader(reader, 8192));

    try {

      /*
       * Parse the document
       */
      final Document document = doParse(configuration, documentName, templateReader, saxParser);

      if (this.canResetParsers) {
        try {
          /*
           * Reset the parser so that it can be used again.
           */
          saxParser.reset();
        } catch (final UnsupportedOperationException e) {
          if (this.logger.isWarnEnabled()) {
            this.logger.warn(
                "[THYMELEAF] The SAX Parser implementation being used (\"{}\") does not implement "
                    + "the \"reset\" operation. This will force Thymeleaf to re-create parser instances "
                    + "each time they are needed for parsing templates, which is more costly. Enabling template "
                    + "cache is recommended, and also using a parser library which implements \"reset\" such as "
                    + "xerces version 2.9.1 or newer.",
                saxParser.getClass().getName());
          }
          this.canResetParsers = false;
        }
      }

      return document;

    } catch (final IOException e) {
      throw new TemplateInputException("Exception parsing document", e);
    } catch (final TemplateProcessingException e) {
      throw e;
    } catch (final SAXParseException e) {
      final String message =
          String.format(
              "Exception parsing document: template=\"%s\", line %d - column %d",
              documentName,
              Integer.valueOf(e.getLineNumber()),
              Integer.valueOf(e.getColumnNumber()));
      throw new TemplateInputException(message, e);
    } catch (final SAXException e) {
      throw new TemplateInputException("Exception parsing document", e);
    } finally {

      if (this.canResetParsers) {
        poolToBeUsed.release(saxParser);
      } else {
        poolToBeUsed.discardAndReplace(saxParser);
      }
    }
  }