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));
  }
 public XQueryExecutor(TraceExtension traceExtension) {
   this.traceExtension = traceExtension;
   Configuration configuration = Configuration.newConfiguration();
   if (!traceExtension.allowsOptimization()) {
     configuration.setConfigurationProperty(OPTIMIZATION_LEVEL, String.valueOf(NO_OPTIMIZATION));
   }
   configuration.setModuleURIResolver(new ClasspathModuleUriResolver());
   Processor processor = new Processor(configuration);
   xQueryCompiler = processor.newXQueryCompiler();
   StaticQueryContext staticQueryContext = xQueryCompiler.getUnderlyingStaticContext();
   staticQueryContext.setCodeInjector(traceExtension.getTraceCodeInjector());
 }
 private void givenParameter(final String parameterName, final QName qName)
     throws SaxonApiException {
   given(streamSourceFactory.createStreamSource(requestInputStream))
       .willReturn(new StreamSource());
   given(processor.newDocumentBuilder().build(any(StreamSource.class)))
       .willReturn(requestDocument);
   given(qNameFactory.createQName(parameterName)).willReturn(qName);
 }
 @BeforeMethod
 public void setUp() throws SaxonApiException {
   MockitoAnnotations.initMocks(this);
   given(processorFactory.createProcessor()).willReturn(processor);
   given(processor.newXQueryCompiler()).willReturn(xQueryCompiler);
   given(xQueryCompiler.compile(QUERY)).willReturn(xQueryExecutable);
   given(xQueryExecutable.load()).willReturn(xQueryEvaluator);
   given(inputSourceFactory.createInputSource(INPUT)).willReturn(inputSource);
   given(saxSourceFactory.createSAXSource(inputSource)).willReturn(saxSource);
   given(byteArrayOutputStreamFactory.createByteArrayOutputStream()).willReturn(outputStream);
 }
  public String execute(Node node, String expression) throws SaxonApiException {

    Processor proc = new Processor(false);
    XPathCompiler xpath = proc.newXPathCompiler();
    DocumentBuilder builder = proc.newDocumentBuilder();

    String source = getClass().getResource(map.getLogicalSource().getIdentifier()).getFile();

    XdmNode doc = builder.build(new File(source));
    String expre = replace(node, expression);
    expression =
        expression.replaceAll(
            "\\{" + expression.split("\\{")[1].split("\\}")[0] + "\\}", "'" + expre + "'");

    XPathSelector selector = xpath.compile(expression).load();
    selector.setContextItem(doc);

    // Evaluate the expression.
    Object result = selector.evaluate();

    return result.toString();
  }
 private void setTemplateMocks() throws SaxonApiException {
   given(inputSourceFactory.createInputSource(templateInputStream)).willReturn(inputSource);
   given(saxSourceFactory.createSAXSource(xmlReader, inputSource)).willReturn(templateSource);
   given(processor.newDocumentBuilder().build(templateSource)).willReturn(templateNode);
 }