Statistics
| Revision:

ccc / projets / CMC2ELAN / src / XPathResult.groovy @ 2

History | View | Annotate | Download (6.4 kB)

1
// Copyright © 2010-2013 ENS de Lyon.
2
// Copyright © 2007-2010 ENS de Lyon, CNRS, INRP, University of
3
// Lyon 2, University of Franche-Comté, University of Nice
4
// Sophia Antipolis, University of Paris 3.
5
// 
6
// The TXM platform is free software: you can redistribute it
7
// and/or modify it under the terms of the GNU General Public
8
// License as published by the Free Software Foundation,
9
// either version 2 of the License, or (at your option) any
10
// later version.
11
// 
12
// The TXM platform is distributed in the hope that it will be
13
// useful, but WITHOUT ANY WARRANTY; without even the implied
14
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15
// PURPOSE. See the GNU General Public License for more
16
// details.
17
// 
18
// You should have received a copy of the GNU General
19
// Public License along with the TXM platform. If not, see
20
// http://www.gnu.org/licenses.
21
// 
22
// 
23
// 
24
// $LastChangedDate: 2014-10-06 11:42:50 +0200 (lun., 06 oct. 2014) $
25
// $LastChangedRevision: 2842 $
26
// $LastChangedBy: mdecorde $ 
27
//
28

    
29
import java.io.File;
30
import java.io.IOException;
31
import java.util.Iterator;
32
import org.w3c.dom.*;
33
import org.xml.sax.SAXException;
34

    
35
import javax.xml.bind.helpers.DefaultValidationEventHandler;
36
import javax.xml.parsers.*;
37
import javax.xml.xpath.*;
38
import javax.xml.stream.*;
39
import java.net.URL;
40
import java.util.Iterator;
41

    
42
// TODO: Auto-generated Javadoc
43
/**
44
 * The Class XPathResult.
45
 *
46
 * @authors ayepdieu, mdecorde, vchabanis
47
 * 
48
 * return the id of a bfm tag <milestone/>
49
 */
50
public class XPathResult {
51
        
52
        /** The doc. */
53
        Document doc;
54
        XPath xpath;
55

    
56
        /**
57
         * Instantiates a new x path result.
58
         *
59
         * @param file the file
60
         */
61
        public XPathResult(File xmlfile) {
62
                DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
63
                factory.setNamespaceAware(true); // never forget this!
64
                
65
                DocumentBuilder builder = factory.newDocumentBuilder();
66
                doc = builder.parse(xmlfile);
67
                
68
                XPathFactory xfactory = XPathFactory.newInstance();
69
                xpath = xfactory.newXPath();
70
                xpath.setNamespaceContext(new PersonalNamespaceContext());
71
        }
72
        
73
        public def getNodes(String query) {
74
                def rnodes = [];
75
                
76
                XPathExpression expr = xpath.compile(query);
77
                Object result = expr.evaluate(doc, XPathConstants.NODESET);
78
                
79
                NodeList nodes = (NodeList) result;
80
                for (int i = 0; i < nodes.getLength(); i++) {
81
                        rnodes.add(nodes.item(i));
82
                }
83
                return rnodes;
84
        }
85
                
86
        public String getXpathResponse(String query) {
87
                XPathExpression expr = xpath.compile(query);
88
                Object result = expr.evaluate(doc, XPathConstants.NODESET);
89
                
90
                NodeList nodes = (NodeList) result;
91
                for (int i = 0; i < nodes.getLength(); i++) {
92
                        return (nodes.item(i).getNodeValue());
93
                }
94
        }
95
        
96
        public ArrayList<String> getXpathResponses(String query) {
97
                ArrayList<String> xresult = new ArrayList<String>();
98
                XPathExpression expr = xpath.compile(query);
99
                Object result = expr.evaluate(doc, XPathConstants.NODESET);
100
                
101
                NodeList nodes = (NodeList) result;
102
                for (int i = 0; i < nodes.getLength(); i++) {
103
                        //println nodes.item(i)
104
                        xresult.add((nodes.item(i).getNodeValue()));
105
                }
106
                return xresult
107
        }
108
        
109
        public String getXpathResponse(String query, String devaultValue) {
110
                String rez = getXpathResponse(query);
111
                if (rez == null)
112
                        return devaultValue;
113
                return rez;
114
        }
115
        
116
        public void close() {
117
                xpath = null;
118
                doc = null;
119
        }
120

    
121
        /**
122

123
         * OBSOLETE VERSION FOR TXM return the node text content given a XPath
124

125
         * "//path.../.../@attr"
126
         * 
127
         * @param path
128
         * @param attr
129
         *            null or "" return the text of the tag targeted
130
         * @return
131
         */
132
        /*
133
         * public String getXpathResponse(String path, String attr) throws
134
         * ParserConfigurationException, SAXException, IOException,
135
         * XPathExpressionException { String Query = path;
136
         * 
137
         * //println "XPath : "+Query; XPathFactory factory =
138
         * XPathFactory.newInstance(); XPath xpath = factory.newXPath();
139
         * xpath.setNamespaceContext(new PersonalNamespaceContext());
140
         * XPathExpression expr = xpath.compile(Query);
141
         * 
142
         * Object result = expr.evaluate(doc, XPathConstants.NODESET); // don't work
143
         * on windows NodeList nodes = (NodeList) result; //println
144
         * "result xpath : "+nodes.getLength(); for(int i = 0 ; i <
145
         * nodes.getLength() ; i++) { //println
146
         * "item : "+nodes.item(i).getLocalName(); if(attr != null &&
147
         * !attr.matches("")) return
148
         * nodes.item(i).getAttributes().getNamedItem(attr).getNodeValue(); else
149
         * return nodes.item(i).getTextContent();
150
         * 
151
         * } return ""; }
152
         */
153
        
154
        static public String getXpathResponse(File xmlfile, String query, String devaultValue) {
155
                String rez = getXpathResponse(xmlfile, query);
156
                if (rez == null)
157
                        return devaultValue;
158
                return rez;
159
        }
160
        
161
        static public String getXpathResponse(File xmlfile, String query, String devaultValue, boolean namespaceAware) {
162
                String rez = getXpathResponse(xmlfile, query, namespaceAware);
163
                if (rez == null)
164
                        return devaultValue;
165
                return rez;
166
        }
167

    
168
        /**
169
         * Gets the xpath response.
170
         *
171
         * @param xmlfile the xmlfile
172
         * @param query the query
173
         * @return the xpath response
174
         */
175
        static public String getXpathResponse(File xmlfile, String query) {
176
                return getXpathResponse(xmlfile, query, true);
177
        }
178
        
179
        /**
180
         * Gets the xpath response.
181
         *
182
         * @param xmlfile the xmlfile
183
         * @param query the query
184
         * @return the xpath response
185
         */
186
        static public String getXpathResponse(File xmlfile, String query, boolean namespaceAware) {
187
                DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
188
                factory.setNamespaceAware(namespaceAware); // never forget this!
189
                
190
                DocumentBuilder builder = factory.newDocumentBuilder();
191
                Document doc = builder.parse(xmlfile);
192
                
193
                XPathFactory xfactory = XPathFactory.newInstance();
194
                XPath xpath = xfactory.newXPath();
195
                xpath.setNamespaceContext(new PersonalNamespaceContext());
196
                
197
                XPathExpression expr = xpath.compile(query);
198
                Object result = expr.evaluate(doc, XPathConstants.NODESET);
199
                
200
                NodeList nodes = (NodeList) result;
201
                for (int i = 0; i < nodes.getLength(); i++) {
202
                        return (nodes.item(i).getNodeValue());
203
                }
204
        }
205

    
206
        /**
207
         * The main method.
208
         *
209
         * @param args the arguments
210
         */
211
        public static void main(String[] args) {
212
                println("main of XPathResult")
213
                File xmlFile = new File("/home/mdecorde/TXM/corpora/superphenix/txm/SUPERPHENIX/CreysSuper_04_0175.xml")
214
                String value = XPathResult.getXpathResponse(xmlFile, "TEI/text/@date");
215
                println "value: $value"
216
                //value = XPathResult.getXpathResponse(new File("/home/mdecorde/xml/bfm/strasb.xml"), "tei:TEI/tei:teiHeader/tei:revisionDesc/tei:change[contains(.,'étiquetage morpho')]");
217
                //println "value: $value"
218
        }
219
}