Exemple #1
0
  private void doTransform(InputStream istream, File dataFile, File outputFile, String baseUrl)
      throws SaxonApiException, TransformerFactoryConfigurationError, TransformerException {
    InputStream stylesheetUri = Jats2Spar.class.getResourceAsStream(RESOURCE_JATS2SPAR_XSL);
    Processor proc = new Processor(false);
    XsltCompiler comp = proc.newXsltCompiler();
    XsltExecutable exec;
    exec = comp.compile(new StreamSource(stylesheetUri));
    XsltTransformer transformer = exec.load();

    DocumentBuilder saxBuilder = proc.newDocumentBuilder();
    saxBuilder.setLineNumbering(true);
    saxBuilder.setDTDValidation(false);

    XdmNode source = saxBuilder.build(new StreamSource(istream));

    Serializer serializer = new Serializer();
    serializer.setOutputProperty(Serializer.Property.INDENT, "yes");
    serializer.setOutputFile(outputFile);
    transformer.setParameter(new QName("baseUri"), new XdmAtomicValue(baseUrl));
    transformer.setInitialContextNode(source);
    transformer.setDestination(serializer);
    transformer.setMessageListener(
        new MessageListener() {

          @Override
          public void message(XdmNode arg0, boolean arg1, SourceLocator arg2) {
            log.warn(
                String.format(
                    "Transformation Error: %s\n   Line Number: %s", arg0, arg2.getLineNumber()));
          }
        });
    transformer.transform();
    log.info(String.format("Finished xslt transformation for file: %s", dataFile));
  }
Exemple #2
0
    @Override
    public Sequence call(XPathContext context, Sequence[] arguments) throws XPathException {
      NodeInfo stylesheet = (NodeInfo) arguments[0].head();
      NodeInfo node = (NodeInfo) arguments[1].head();

      Evaluator eval = SearchBase.getEvaluator(context);
      XsltCompiler xsltCompiler = eval.getCompiler().getXsltCompiler();
      xsltCompiler.setErrorListener(eval.getErrorListener());
      try {
        // TODO: cache compiled xslt somewhere
        XsltExecutable xsltexec = xsltCompiler.compile(stylesheet);
        transformer = xsltexec.load();
        transformer.setSource(node);
        transformer.setErrorListener(eval.getErrorListener());
        if (arguments.length > 2) {
          bindParameters(arguments[2]);
        }
        XdmDestination dest = new XdmDestination();
        transformer.setDestination(dest);
        transformer.transform();
        ArrayList<TransformerException> runtimeErrors =
            ((TransformErrorListener) transformer.getErrorListener()).getErrors();
        if (!runtimeErrors.isEmpty()) {
          throw new XPathException(
              runtimeErrors.get(0).getMessage(),
              runtimeErrors.get(0).getLocator(),
              runtimeErrors.get(0));
        }
        XdmNode result = dest.getXdmNode();
        if (result == null) {
          return EmptySequence.getInstance();
        }
        return result.getUnderlyingNode();
      } catch (SaxonApiException e) {
        throw new XPathException(e);
      }
    }
  private TransformerHandler createTransformerHandler() {
    try {
      TransformerHandlerImpl transformerHandler =
          (TransformerHandlerImpl) GENERIC_FACTORY.newTransformerHandler(templates);

      TransformerImpl impl = (TransformerImpl) transformerHandler.getTransformer();
      XsltTransformer xslt = impl.getUnderlyingXsltTransformer();
      Controller controller = (Controller) xslt.getUnderlyingController();

      if (configuration != null) {
        for (Entry<String, Object> entry : configuration.entrySet()) {
          String name = entry.getKey();

          // is valid XSLT parameter name
          if (XSLT_PARAMETER_NAME_PATTERN.matcher(name).matches()) {
            xslt.setParameter(new QName(name), new XdmExternalObject(entry.getValue()));
          }
        }
      }

      controller.setErrorListener(new TraxErrorListener(LOG, source.toExternalForm()));

      if (context != null) {
        xslt.setParameter(CoreConstants.APPLICATION_CONTEXT_PARAM, new XdmExternalObject(context));
      }

      Map<String, Object> model = HttpContextHelper.getModel(parameters);
      xslt.setParameter(CoreConstants.MODEL_PARAM, new XdmExternalObject(model));

      if (parameters != null)
        try {
          ServletContext servletContext = HttpContextHelper.getServletContext(parameters);
          xslt.setParameter(
              CoreConstants.SERVLET_CONTEXT_PARAM, new XdmExternalObject(servletContext));

          HttpServletRequest servletRequest = HttpContextHelper.getRequest(parameters);
          HttpServletResponse servletResponse = HttpContextHelper.getResponse(parameters);

          if (servletRequest != null) {
            controller.setURIResolver(new SaxonResolver(servletRequest, servletResponse));

            xslt.setParameter(
                Message.LOCALE_PARAM,
                new XdmExternalObject(RequestContextUtils.getLocale(servletRequest)));

            xslt.setParameter(
                CoreConstants.SERVLET_REQUEST_PARAM, new XdmExternalObject(servletRequest));

            HttpSession session = servletRequest.getSession(false);
            if (session != null) {
              xslt.setParameter(
                  CoreConstants.SERVLET_SESSION_PARAM, new XdmExternalObject(session));
            }

            // add support for form binding
            xslt.setParameter(
                CoreConstants.REQUEST_CONTEXT_PARAM,
                new XdmExternalObject(
                    new RequestContext(servletRequest, servletResponse, servletContext, model)));
          }

          if (servletResponse != null) {
            xslt.setParameter(
                CoreConstants.SERVLET_RESPONSE_PARAM, new XdmExternalObject(servletResponse));
          }
        } catch (IllegalStateException e) {
          LOG.debug("Not a Servlet request!", e);

          //				@SuppressWarnings({"rawtypes", "unchecked"})
          //				HashMap<String, Object> model = (HashMap) parameters.get("model");
          //
          //				HttpServletRequest servletRequest = new VirtualRequest();
          //				servletRequest.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE,
          // context);
          //				RequestContext requestContext = new RequestContext(servletRequest, model);
          //
          //				transformer.setParameter(CoreConstants.REQUEST_CONTEXT_PARAM, requestContext);
        }

      return transformerHandler;
    } catch (TransformerConfigurationException ex) {
      throw new SetupException("Could not initialize transformer handler.", ex);
    }
  }
Exemple #4
0
 @Override
 protected void setParameter(StructuredQName name, Item value) {
   transformer.getUnderlyingController().setParameter(name, value);
 }