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

时间:2018-11-10 12:53:26

标签: java maven gradle build.gradle xquery

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

我在以下位置有一个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

2 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:0)

这就是我最终使用过滤后的复制任务进行开发的原因。

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"
}

这是被调用的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));
    }
}

xqDoc代码库在https://github.com/lcahlander/xqdoc

显示xqDoc文档的代码在https://github.com/lcahlander/marklogic-xqdoc-display