我有一个针对XQuery(* .xqy)源文件生成xqDoc(类似于JavaDoc)的Java应用程序。

我在以下位置有一个Maven项目:https://github.com/lcahlander/xqdoc-core.git
我想对.xqy中的所有src/main/ml-modules/root/**/*.xqy文件运行以下java代码,并将结果分别放在xqDoc/**/*.xml中:

HashMap uriMap = new HashMap();
uriMap.put(XPathDriver.XPATH_PREFIX, XPathDriver.XPATH_URI);
InputStream is = Files.newInputStream(Paths.get(cmd.getOptionValue("f")));
controller = new XQDocController(XQDocController.JUL2017);
controller.setPredefinedFunctionNamespaces(uriMap);

XQDocPayload payload = controller.process(is, "");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource isOut = new InputSource();
isOut.setCharacterStream(new StringReader(payload.getXQDocXML()));

Document doc = db.parse(isOut);

xqDoc解析器也可以从命令行运行,如下所示:
java -jar xqdoc-core-0.8-jar-with-dependencies.jar -Dfn=http://www.w3.org/2003/05/xpath-functions -Dxdmp=http://marklogic.com/xdmp -f filepath
我想创建gradle任务generateXQDoc

最佳答案

像这样的事情应该起作用(未经测试)。您可以调整硬编码的路径以使用项目属性,但应该足以演示如何遍历文件集中的每个文件并执行

task generateXQDoc {
  description = 'Generate XQDocs'

  doLast {
    def sourceDir = 'src/main/ml-modules'
    File targetDir = new File('xqDoc')

    HashMap uriMap = new HashMap();
    uriMap.put(XPathDriver.XPATH_PREFIX, XPathDriver.XPATH_URI);
    controller = new XQDocController(XQDocController.JUL2017);
    controller.setPredefinedFunctionNamespaces(uriMap);

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();

    def xqueryFiles = fileTree(dir: sourceDir, include: '**/*.xq*')
    xqueryFiles.each { file ->

      InputStream is = Files.newInputStream(file));
      XQDocPayload payload = controller.process(is, "");

      String relativePath = new File(sourceDir).toURI().relativize(file.toURI()).getPath();
      File outputFile = new File(targetDir, relativePath)
      outputFile.parentFile.mkdirs()

      outputFile.write(payload.getXQDocXML())
    }
  }
}

10-05 18:59