Exemplo n.º 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));
  }
Exemplo n.º 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);
      }
    }