Statistics
| Revision:

root / tmp / org.txm.groovy.core / src / groovy / org / txm / macroproto / GetXPathMacro.groovy @ 499

History | View | Annotate | Download (2.7 kB)

1
package org.txm.macroproto
2
// Copyright © 2015 - ENS de Lyon - http://textometrie.ens-lyon.fr
3
// @author sheiden
4
//
5
// This file is part of the TXM platform.
6
//
7
// The TXM platform is free software: you can redistribute it and/or modify
8
// it under the terms of the GNU General Public License as published by
9
// the Free Software Foundation, either version 3 of the License, or
10
// (at your option) any later version.
11
//
12
// The TXM platform is distributed in the hope that it will be useful,
13
// but WITHOUT ANY WARRANTY; without even the implied warranty of
14
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
// GNU General Public License for more details.
16
//
17
// You should have received a copy of the GNU General Public License
18
// along with the TXM platform.  If not, see <http://www.gnu.org/licenses/>.
19
//
20
// $LastChangedDate: 2013-05-02 11:28:42 +0200 (jeu., 02 mai 2013) $
21
// $LastChangedRevision: 2378 $
22
// $LastChangedBy: sheiden $
23
//
24

    
25
// STANDARD DECLARATIONS
26

    
27
import org.kohsuke.args4j.*
28
import groovy.transform.Field
29
import org.txm.rcp.swt.widget.parameters.*
30

    
31
import org.txm.importer.*
32

    
33
// BEGINNING OF PARAMETERS
34

    
35
// Declare each parameter here
36
// (available widget types: Query, File, Folder, String, Text, Boolean, Integer, Float and Date)
37

    
38
@Field @Option(name="srcDirectory", usage="XML source directory", widget="Folder", required=false, def="")
39
def srcDirectory
40

    
41
@Field @Option(name="srcFile", usage="XML source file", widget="File", required=false, def="")
42
def srcFile
43

    
44
@Field @Option(name="xPath", usage="XPath expression", widget="String", required=true, def="//tei:title/text()")
45
def xPath
46

    
47
// Open the parameters input dialog box
48
if (!ParametersDialog.open(this)) return;
49

    
50
// END OF PARAMETERS
51

    
52
if (srcDirectory==null && srcFile==null) { println "** GetXPathMacro: at least a source file or a source directory must be specified."; return}
53

    
54
if (srcDirectory!=null && srcDirectory.exists()) {
55

    
56
        def files = srcDirectory.listFiles()
57
        if (files == null || files.size() == 0) {
58
                println "No files in $srcDirectory"
59
                return;
60
        }
61
        files.sort()
62

    
63
        for (File xmlFile : files) {
64
                String name = xmlFile.getName();
65
                if (!name.endsWith(".xml")) continue;
66
        
67
                println "-- file '$xmlFile':"
68
                
69
                XPathResult xpathProcessor = new XPathResult(xmlFile)
70
                def value = xpathProcessor.getXpathResponse(xPath)
71
                if (value != null) { println(value) }
72
}
73

    
74
} else if (srcFile!=null && srcFile.exists()) {
75
        def xmlFile = srcFile
76
        String name = xmlFile.getName();
77
        if (!name.endsWith(".xml")) { println "** GetXPathMacro: source file extension must be '.xml'."; return}
78
                
79
        println "-- file '$xmlFile':"
80

    
81
        XPathResult xpathProcessor = new XPathResult(xmlFile)
82
        def value = xpathProcessor.getXpathResponse(xPath)
83
        if (value != null) { println(value) } else { println "No result." }
84
}