简体   繁体   English

如何在gradle项目中对所有类型的文件运行java方法?

[英]How do I run a java method against all files of a type within a gradle project?

I have a java application that generates xqDoc (similar to JavaDoc) against an XQuery (*.xqy) source file. 我有一个针对XQuery(* .xqy)源文件生成xqDoc(类似于JavaDoc)的Java应用程序。

I have a maven project at: https://github.com/lcahlander/xqdoc-core.git 我在以下位置有一个maven项目: https://github.com/lcahlander/xqdoc-core.git : https://github.com/lcahlander/xqdoc-core.git

That I want to run the following java code against all .xqy files in src/main/ml-modules/root/**/*.xqy and place the results respectively in xqDoc/**/*.xml : 我想对src/main/ml-modules/root/**/*.xqy中的所有.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);

The xqDoc parser could also be run from the command line as 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

I want to create the gradle task generateXQDoc 我想创建gradle任务generateXQDoc

Some thing like this should work (untested). 像这样的事情应该起作用(未经测试)。 You can adjust the hard-coded paths to use project properties, but should be enough to demonstrate how to iterate over each file in the fileset and execute 您可以调整硬编码的路径以使用项目属性,但是应该足以演示如何遍历文件集中的每个文件并执行

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())
    }
  }
}

This is what I ended up developing using the filtered copy task. 这就是我最终使用过滤后的复制任务进行开发的结果。

import org.apache.tools.ant.filters.BaseFilterReader

buildscript {
    repositories {
        jcenter()
    }

    dependencies {
        classpath files('lib/xqdoc-1.9-jar-with-dependencies.jar')
    }
}

plugins {
  id "net.saliman.properties" version "1.4.6"
  id "com.marklogic.ml-gradle" version "3.6.0"
}

repositories {
  jcenter()
  maven { url "http://developer.marklogic.com/maven2/" }
  maven { url "http://repository.cloudera.com/artifactory/cloudera-repos/" }
}

configurations {
  mlcp {
    resolutionStrategy {
      force "xml-apis:xml-apis:1.4.01"
    }
  }
}

dependencies {
    mlcp "com.marklogic:mlcp:9.0.6"
    mlcp files("marklogic/lib")
}

class XQDocFilter extends BaseFilterReader {
    XQDocFilter(Reader input) {
        super(new StringReader(new org.exquery.xqdoc.MarkLogicProcessor().process(input.text)))
    }
}

/**
 * Generate the xqDoc files from the XQuery source code
 */
task generateXQDocs(type: Copy) {
  into 'xqDoc'
  from 'src/main/ml-modules/root'
  include '**/*.xqy'
  rename { it - '.xqy' + '.xml' } 
  includeEmptyDirs = false
  filter XQDocFilter
}

/**
 * Deploy the xqDoc files to the content repository
 */
 task importXQDoc(type: com.marklogic.gradle.task.MlcpTask) {
  classpath = configurations.mlcp
  command = "IMPORT"
  database = "emh-accelerator-content"
  input_file_path = "xqDoc"
  output_collections = "xqdoc"
  output_uri_replace = ".*xqDoc,'/xqDoc'"
  document_type = "mixed"
}

And here is the Java class being called. 这是被调用的Java类。

public class MarkLogicProcessor {

    public String process(String txt) throws XQDocException, ParserConfigurationException, IOException, SAXException {
        HashMap uriMap = new HashMap();
        uriMap.put("fn", "http://www.w3.org/2003/05/xpath-functions");
        uriMap.put("cts", "http://marklogic.com/cts"); // MarkLogic Server search functions (Core Text Services)
        uriMap.put("dav", "DAV:"); // Used with WebDAV
        uriMap.put("dbg", "http://marklogic.com/xdmp/debug"); // Debug Built-In functions
        uriMap.put("dir", "http://marklogic.com/xdmp/directory"); // MarkLogic Server directory XML
        uriMap.put("err", "http://www.w3.org/2005/xqt-errors"); // namespace for XQuery and XPath errors
        uriMap.put("error", "http://marklogic.com/xdmp/error"); // MarkLogic Server error namespace
        uriMap.put("local", "http://www.w3.org/2005/xquery-local-functions"); // local namespace for functions defined in main modules
        uriMap.put("lock", "http://marklogic.com/xdmp/lock"); // MarkLogic Server locks
        uriMap.put("map", "http://marklogic.com/xdmp/map"); // MarkLogic Server maps
        uriMap.put("math", "http://marklogic.com/xdmp/math"); // math Built-In functions
        uriMap.put("prof", "http://marklogic.com/xdmp/profile"); // profile Built-In functions
        uriMap.put("prop", "http://marklogic.com/xdmp/property"); // MarkLogic Server properties
        uriMap.put("sec", "http://marklogic.com/xdmp/security"); // security Built-In functions
        uriMap.put("sem", "http://marklogic.com/semantics"); // semantic Built-In functions
        uriMap.put("spell", "http://marklogic.com/xdmp/spell"); // spelling correction functions
        uriMap.put("xdmp", "http://marklogic.com/xdmp"); // MarkLogic Server Built-In functions
        uriMap.put("xml", "http://www.w3.org/XML/1998/namespace"); // XML namespace
        uriMap.put("xmlns", "http://www.w3.org/2000/xmlns/"); // xmlns namespace
        uriMap.put("xqe", "http://marklogic.com/xqe"); // deprecated MarkLogic Server xqe namespace
        uriMap.put("xqterr", "http://www.w3.org/2005/xqt-errors"); // XQuery test suite errors (same as err)
        uriMap.put("xs", "http://www.w3.org/2001/XMLSchema"); // XML Schema namespace
        ANTLRInputStream inputStream = new ANTLRInputStream(txt);
        XQueryLexer markupLexer = new XQueryLexer(inputStream);
        CommonTokenStream commonTokenStream = new CommonTokenStream(markupLexer);
        XQueryParser markupParser = new XQueryParser(commonTokenStream);

        XQueryParser.ModuleContext fileContext = markupParser.module();
        StringBuffer buffer = new StringBuffer();


        XQueryVisitor visitor = new XQueryVisitor(buffer, uriMap);
        visitor.visit(fileContext);
        return DocumentUtility.getStringFromDoc(DocumentUtility.getDocumentFromBuffer(buffer));
    }
}

The xqDoc codebase is here https://github.com/lcahlander/xqdoc xqDoc代码库在这里https://github.com/lcahlander/xqdoc

The code to display the xqDoc documents is here https://github.com/lcahlander/marklogic-xqdoc-display 显示xqDoc文档的代码在这里https://github.com/lcahlander/marklogic-xqdoc-display

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM