Statistics
| Revision:

root / tmp / org.txm.core / src / groovy / org / txm / importer / XPathResult.groovy @ 187

History | View | Annotate | Download (6.9 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: 2016-03-29 09:51:35 +0200 (Tue, 29 Mar 2016) $
25
// $LastChangedRevision: 3185 $
26
// $LastChangedBy: mdecorde $ 
27
//
28
package org.txm.importer;
29

    
30
import org.txm.importer.graal.PersonalNamespaceContext;
31

    
32
import java.io.File;
33
import java.io.IOException;
34
import java.util.Iterator;
35
import org.w3c.dom.*;
36
import org.xml.sax.SAXException;
37

    
38
import javax.xml.bind.helpers.DefaultValidationEventHandler;
39
import javax.xml.parsers.*;
40
import javax.xml.xpath.*;
41
import javax.xml.stream.*;
42
import java.net.URL;
43
import java.util.Iterator;
44

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

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

    
134
        /**
135

136
         * OBSOLETE VERSION FOR TXM return the node text content given a XPath
137

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

    
181
        /**
182
         * Gets the xpath response.
183
         *
184
         * @param xmlfile the xmlfile
185
         * @param query the query
186
         * @return the xpath response
187
         */
188
        static public String getXpathResponse(File xmlfile, String query) {
189
                return getXpathResponse(xmlfile, query, true);
190
        }
191
        
192
        /**
193
         * Gets the xpath response.
194
         *
195
         * @param xmlfile the xmlfile
196
         * @param query the query
197
         * @return the xpath response
198
         */
199
        static public String getXpathResponse(File xmlfile, String query, boolean namespaceAware) {
200
                XPathResult result = new XPathResult(xmlfile);
201
                return result.getXpathResponse(query);
202
//                DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
203
//                factory.setNamespaceAware(namespaceAware); // never forget this!
204
//                
205
//                DocumentBuilder builder = factory.newDocumentBuilder();
206
//                Document doc = builder.parse(xmlfile);
207
//                
208
//                XPathFactory xfactory = XPathFactory.newInstance();
209
//                XPath xpath = xfactory.newXPath();
210
//                xpath.setNamespaceContext(new PersonalNamespaceContext());
211
//                
212
//                XPathExpression expr = xpath.compile(query);
213
//                Object result = expr.evaluate(doc, XPathConstants.NODESET);
214
//                
215
//                NodeList nodes = (NodeList) result;
216
//                println "size: "+result.getLength()
217
//                for (int i = 0; i < nodes.getLength(); i++) {
218
//                        return (nodes.item(i).getNodeValue());
219
//                }
220
        }
221

    
222
        /**
223
         * The main method.
224
         *
225
         * @param args the arguments
226
         */
227
        public static void main(String[] args) {
228
                println("main of XPathResult")
229
                File xmlFile = new File("/home/mdecorde/TXM/corpora/superphenix/txm/SUPERPHENIX/CreysSuper_04_0175.xml")
230
                String value = XPathResult.getXpathResponse(xmlFile, "TEI/text/@date");
231
                println "value: $value"
232
                //value = XPathResult.getXpathResponse(new File("/home/mdecorde/xml/bfm/strasb.xml"), "tei:TEI/tei:teiHeader/tei:revisionDesc/tei:change[contains(.,'étiquetage morpho')]");
233
                //println "value: $value"
234
        }
235
}