Exemple #1
0
  public void process(Exchange exchange) throws Exception {
    notNull(getTemplate(), "template");

    if (isDeleteOutputFile()) {
      // add on completion so we can delete the file when the Exchange is done
      String fileName =
          ExchangeHelper.getMandatoryHeader(exchange, Exchange.XSLT_FILE_NAME, String.class);
      exchange.addOnCompletion(new XsltBuilderOnCompletion(fileName));
    }

    Transformer transformer = getTransformer();
    configureTransformer(transformer, exchange);

    ResultHandler resultHandler = resultHandlerFactory.createResult(exchange);
    Result result = resultHandler.getResult();
    // let's copy the headers before we invoke the transform in case they modify them
    Message out = exchange.getOut();
    out.copyFrom(exchange.getIn());

    // the underlying input stream, which we need to close to avoid locking files or other resources
    InputStream is = null;
    try {
      Source source;
      // only convert to input stream if really needed
      if (isInputStreamNeeded(exchange)) {
        is = exchange.getIn().getBody(InputStream.class);
        source = getSource(exchange, is);
      } else {
        Object body = exchange.getIn().getBody();
        source = getSource(exchange, body);
      }

      if (source instanceof StAXSource) {
        // Always convert StAXSource to SAXSource.
        // * Xalan and Saxon-B don't support StAXSource.
        // * The JDK default implementation (XSLTC) doesn't handle CDATA events
        //   (see com.sun.org.apache.xalan.internal.xsltc.trax.StAXStream2SAX).
        // * Saxon-HE/PE/EE seem to support StAXSource, but don't advertise this
        //   officially (via TransformerFactory.getFeature(StAXSource.FEATURE))
        source = new StAX2SAXSource(((StAXSource) source).getXMLStreamReader());
      }

      LOG.trace("Using {} as source", source);
      transformer.transform(source, result);
      LOG.trace("Transform complete with result {}", result);
      resultHandler.setBody(out);
    } finally {
      releaseTransformer(transformer);
      // IOHelper can handle if is is null
      IOHelper.close(is);
    }
  }
Exemple #2
0
  public void process(Exchange exchange) throws Exception {
    notNull(getTemplate(), "template");

    if (isDeleteOutputFile()) {
      // add on completion so we can delete the file when the Exchange is done
      String fileName =
          ExchangeHelper.getMandatoryHeader(exchange, Exchange.XSLT_FILE_NAME, String.class);
      exchange.addOnCompletion(new XsltBuilderOnCompletion(fileName));
    }

    Transformer transformer = getTransformer();
    configureTransformer(transformer, exchange);

    ResultHandler resultHandler = resultHandlerFactory.createResult(exchange);
    Result result = resultHandler.getResult();
    exchange.setProperty("isXalanTransformer", isXalanTransformer(transformer));
    // let's copy the headers before we invoke the transform in case they modify them
    Message out = exchange.getOut();
    out.copyFrom(exchange.getIn());

    // the underlying input stream, which we need to close to avoid locking files or other resources
    InputStream is = null;
    try {
      Source source;
      // only convert to input stream if really needed
      if (isInputStreamNeeded(exchange)) {
        is = exchange.getIn().getBody(InputStream.class);
        source = getSource(exchange, is);
      } else {
        Object body = exchange.getIn().getBody();
        source = getSource(exchange, body);
      }
      LOG.trace("Using {} as source", source);
      transformer.transform(source, result);
      LOG.trace("Transform complete with result {}", result);
      resultHandler.setBody(out);
    } finally {
      releaseTransformer(transformer);
      // IOHelper can handle if is is null
      IOHelper.close(is);
    }
  }