Statistics
| Revision:

root / tmp / org.txm.core / src / java / org / txm / scripts / importer / XPathResult.groovy @ 2473

History | View | Annotate | Download (7.2 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: 2017-04-11 15:30:35 +0200 (mar. 11 avril 2017) $
25
// $LastChangedRevision: 3426 $
26
// $LastChangedBy: mdecorde $
27
//
28
package org.txm.scripts.importer;
29

    
30
import org.txm.importer.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.setXIncludeAware(true);
76
                factory.setNamespaceAware(namespaceAware); // never forget this!
77

    
78
                DocumentBuilder builder = factory.newDocumentBuilder();
79
                doc = builder.parse(xmlfile);
80

    
81
                XPathFactory xfactory = XPathFactory.newInstance();
82
                xpath = xfactory.newXPath();
83
                xpath.setNamespaceContext(new PersonalNamespaceContext());
84
        }
85

    
86
        public Document getDocument() {
87
                return doc;
88
        }
89

    
90
        public def getNodes(String query) {
91
                def rnodes = [];
92

    
93
                XPathExpression expr = xpath.compile(query);
94
                Object result = expr.evaluate(doc.getDocumentElement(), XPathConstants.NODESET);
95
                if (result instanceof NodeList) {
96
                        NodeList nodes = (NodeList) result;
97
                        for (int i = 0; i < nodes.getLength(); i++) {
98
                                rnodes.add(nodes.item(i));
99
                        }
100
                } else {
101
                        result.getClass()
102
                        rnodes << result
103
                }
104
                return rnodes;
105
        }
106

    
107
        public String getXpathResponse(String query) {
108
                XPathExpression expr = xpath.compile(query);
109
                Object result = expr.evaluate(doc.getDocumentElement(), XPathConstants.NODESET);
110

    
111
                NodeList nodes = (NodeList) result;
112
                for (int i = 0; i < nodes.getLength(); i++) {
113
                        //println nodes.item(i)
114
                        return (nodes.item(i).getNodeValue());
115
                }
116
        }
117

    
118
        public ArrayList<String> getXpathResponses(String query) {
119
                ArrayList<String> xresult = new ArrayList<String>();
120
                XPathExpression expr = xpath.compile(query);
121
                Object result = expr.evaluate(doc.getDocumentElement(), XPathConstants.NODESET);
122

    
123
                NodeList nodes = (NodeList) result;
124
                for (int i = 0; i < nodes.getLength(); i++) {
125
                        //println nodes.item(i)
126
                        xresult.add((nodes.item(i).getNodeValue()));
127
                }
128
                return xresult
129
        }
130

    
131
        public String getXpathResponse(String query, String devaultValue) {
132
                String rez = getXpathResponse(query);
133
                if (rez == null)
134
                        return devaultValue;
135
                return rez;
136
        }
137

    
138
        public void close() {
139
                xpath = null;
140
                doc = null;
141
        }
142

    
143
        /**
144
         * OBSOLETE VERSION FOR TXM return the node text content given a XPath
145
         * "//path.../.../@attr"
146
         * 
147
         * @param path
148
         * @param attr
149
         *            null or "" return the text of the tag targeted
150
         * @return
151
         */
152
        /*
153
         * public String getXpathResponse(String path, String attr) throws
154
         * ParserConfigurationException, SAXException, IOException,
155
         * XPathExpressionException { String Query = path;
156
         * 
157
         * //println "XPath : "+Query; XPathFactory factory =
158
         * XPathFactory.newInstance(); XPath xpath = factory.newXPath();
159
         * xpath.setNamespaceContext(new PersonalNamespaceContext());
160
         * XPathExpression expr = xpath.compile(Query);
161
         * 
162
         * Object result = expr.evaluate(doc, XPathConstants.NODESET); // don't work
163
         * on windows NodeList nodes = (NodeList) result; //println
164
         * "result xpath : "+nodes.getLength(); for(int i = 0 ; i <
165
         * nodes.getLength() ; i++) { //println
166
         * "item : "+nodes.item(i).getLocalName(); if(attr != null &&
167
         * !attr.equals("")) return
168
         * nodes.item(i).getAttributes().getNamedItem(attr).getNodeValue(); else
169
         * return nodes.item(i).getTextContent();
170
         * 
171
         * } return ""; }
172
         */
173

    
174
        static public String getXpathResponse(File xmlfile, String query, String devaultValue) {
175
                String rez = getXpathResponse(xmlfile, query);
176
                if (rez == null)
177
                        return devaultValue;
178
                return rez;
179
        }
180

    
181
        static public String getXpathResponse(File xmlfile, String query, String devaultValue, boolean namespaceAware) {
182
                String rez = getXpathResponse(xmlfile, query, namespaceAware);
183
                if (rez == null)
184
                        return devaultValue;
185
                return rez;
186
        }
187

    
188
        /**
189
         * Gets the xpath response.
190
         *
191
         * @param xmlfile the xmlfile
192
         * @param query the query
193
         * @return the xpath response
194
         */
195
        static public String getXpathResponse(File xmlfile, String query) {
196
                return getXpathResponse(xmlfile, query, true);
197
        }
198

    
199
        /**
200
         * Gets the xpath response.
201
         *
202
         * @param xmlfile the xmlfile
203
         * @param query the query
204
         * @return the xpath response
205
         */
206
        static public String getXpathResponse(File xmlfile, String query, boolean namespaceAware) {
207
                XPathResult result = new XPathResult(xmlfile);
208
                return result.getXpathResponse(query);
209
                //                DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
210
                //                factory.setNamespaceAware(namespaceAware); // never forget this!
211
                //
212
                //                DocumentBuilder builder = factory.newDocumentBuilder();
213
                //                Document doc = builder.parse(xmlfile);
214
                //
215
                //                XPathFactory xfactory = XPathFactory.newInstance();
216
                //                XPath xpath = xfactory.newXPath();
217
                //                xpath.setNamespaceContext(new PersonalNamespaceContext());
218
                //
219
                //                XPathExpression expr = xpath.compile(query);
220
                //                Object result = expr.evaluate(doc, XPathConstants.NODESET);
221
                //
222
                //                NodeList nodes = (NodeList) result;
223
                //                println "size: "+result.getLength()
224
                //                for (int i = 0; i < nodes.getLength(); i++) {
225
                //                        return (nodes.item(i).getNodeValue());
226
                //                }
227
        }
228

    
229
        /**
230
         * The main method.
231
         *
232
         * @param args the arguments
233
         */
234
        public static void main(String[] args) {
235
                println("main of XPathResult")
236
                File xmlFile = new File("/home/mdecorde/runtime-rcpapplication.product/corpora/DEMOCRATLYON3/txm/DEMOCRATLYON3/DiderotEssais.xml")
237
                XPathResult processor = new XPathResult(xmlFile)
238
                for (def node : processor.getNodes(xmlFile, "TEI/text/@date")) {
239
                        println "node: $node"
240
                }
241
                //value = XPathResult.getXpathResponse(new File("/home/mdecorde/xml/bfm/strasb.xml"), "tei:TEI/tei:teiHeader/tei:revisionDesc/tei:change[contains(.,'étiquetage morpho')]");
242
                //println "value: $value"
243
        }
244
}