Statistics
| Revision:

root / tmp / org.txm.groovy.core / src / groovy / org / txm / macroproto / ApplyXQueryMacro.groovy @ 822

History | View | Annotate | Download (2.7 kB)

1
// STANDARD DECLARATIONS
2
package org.txm.macroproto
3

    
4

    
5
import org.kohsuke.args4j.*
6
import groovy.transform.Field
7
import org.txm.rcpapplication.swt.widget.parameters.*
8
import org.txm.searchengine.cqp.corpus.*
9
import net.sf.saxon.*
10
import net.sf.saxon.query.*
11
import net.sf.saxon.om.*
12
import javax.xml.transform.*
13
import javax.xml.transform.sax.*
14
import javax.xml.transform.stream.*
15
import org.xml.sax.*
16
import javax.xml.xpath.*
17
import net.sf.saxon.event.*
18
import org.w3c.dom.*
19
import net.sf.saxon.s9api.*
20
import javax.xml.parsers.*
21

    
22
// BEGINNING OF PARAMETERS
23

    
24
if (!(corpusViewSelection instanceof Corpus)) {
25
        println "Error: Selection must be a corpus"
26
        return false;
27
}
28

    
29
@Field @Option(name="xqFile", usage="a Xquery file", widget="FileOpen", required=true, def="C:/Temp/foo.xq")
30
def xqFile
31
@Field @Option(name="outFile", usage="optional output file", widget="File", required=false, def="")
32
def outFile
33
@Field @Option(name="debug", usage="an example file", widget="Boolean", required=true, def="false")
34
def debug
35

    
36
// Open the parameters input dialog box
37
if (!ParametersDialog.open(this)) return;
38

    
39
// END OF PARAMETERS
40

    
41
println "corpora selection: "+corpusViewSelection
42
if (!xqFile.getName().endsWith(".xq")) {
43
        println "Error: Xquery selected file is not a '.xd' file: $xdFile"
44
        return false;
45
}
46

    
47
MainCorpus mainCorpus = ((Corpus)corpusViewSelection).getMainCorpus();
48
File binDir = mainCorpus.getBaseDirectory();
49
File txmDir = new File(binDir, "txm/"+mainCorpus.getName());
50

    
51
if (!txmDir.exists()) {
52
        println "Error: the 'txm' directory does not exist: $txmDir"
53
        return false;
54
}
55

    
56
def xmlFiles = txmDir.listFiles();
57
if (xmlFiles == null || xmlFiles.size() == 0) {
58
        println "Error: no file found in $txmDir"
59
        return false;
60
}
61

    
62
String query = """<matches>
63
  { 
64
    for \$t in fn:collection('$txmDir')
65
      for \$w in \$t//tei:w
66
        let \$pos := \$w/txm:ana[@type="#frpos"]/text()
67
        return <match>{\$w/@id}</match>
68
  }
69
</matches>
70
"""
71

    
72
Processor processor = new Processor(false)
73
XQueryCompiler xqc = processor.newXQueryCompiler()
74
xqc.declareNamespace("tei", "http://www.tei-c.org/ns/1.0")
75
xqc.declareNamespace("txm", "http://textometrie.org/1.0")
76
xqc.declareNamespace("fn", "http://www.w3.org/2005/xpath-functions")
77
XQueryExecutable exp = xqc.compile(query)
78

    
79
DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();
80
dfactory.setNamespaceAware(true);
81
Document dom = dfactory.newDocumentBuilder().newDocument();
82
exp.load().run(new DOMDestination(dom));
83
if (outFile instanceof File && outFile.getName().length() > 0) {
84
        def writer = outFile.newWriter("UTF-8")
85
        writer.println dom.getDocumentElement()
86
        writer.close()
87
        println "Result written in "+outFile.getAbsolutePath()
88
} else {
89
        println dom.getDocumentElement()
90
}