root / tmp / org.txm.core / src / java / org / txm / scripts / importer / XPathResult.groovy @ 1688
History | View | Annotate | Download (7 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 def getNodes(String query) { |
87 |
def rnodes = []; |
88 |
|
89 |
XPathExpression expr = xpath.compile(query);
|
90 |
Object result = expr.evaluate(doc.getDocumentElement(), XPathConstants.NODESET); |
91 |
|
92 |
NodeList nodes = (NodeList) result; |
93 |
for (int i = 0; i < nodes.getLength(); i++) { |
94 |
rnodes.add(nodes.item(i)); |
95 |
} |
96 |
return rnodes;
|
97 |
} |
98 |
|
99 |
public String getXpathResponse(String query) { |
100 |
XPathExpression expr = xpath.compile(query);
|
101 |
Object result = expr.evaluate(doc.getDocumentElement(), XPathConstants.NODESET); |
102 |
|
103 |
NodeList nodes = (NodeList) result; |
104 |
for (int i = 0; i < nodes.getLength(); i++) { |
105 |
//println nodes.item(i)
|
106 |
return (nodes.item(i).getNodeValue());
|
107 |
} |
108 |
} |
109 |
|
110 |
public ArrayList<String> getXpathResponses(String query) { |
111 |
ArrayList<String> xresult = new ArrayList<String>(); |
112 |
XPathExpression expr = xpath.compile(query);
|
113 |
Object result = expr.evaluate(doc.getDocumentElement(), XPathConstants.NODESET); |
114 |
|
115 |
NodeList nodes = (NodeList) result; |
116 |
for (int i = 0; i < nodes.getLength(); i++) { |
117 |
//println nodes.item(i)
|
118 |
xresult.add((nodes.item(i).getNodeValue())); |
119 |
} |
120 |
return xresult
|
121 |
} |
122 |
|
123 |
public String getXpathResponse(String query, String devaultValue) { |
124 |
String rez = getXpathResponse(query);
|
125 |
if (rez == null) |
126 |
return devaultValue;
|
127 |
return rez;
|
128 |
} |
129 |
|
130 |
public void close() { |
131 |
xpath = null;
|
132 |
doc = null;
|
133 |
} |
134 |
|
135 |
/**
|
136 |
|
137 |
* OBSOLETE VERSION FOR TXM return the node text content given a XPath
|
138 |
|
139 |
* "//path.../.../@attr"
|
140 |
*
|
141 |
* @param path
|
142 |
* @param attr
|
143 |
* null or "" return the text of the tag targeted
|
144 |
* @return
|
145 |
*/
|
146 |
/*
|
147 |
* public String getXpathResponse(String path, String attr) throws
|
148 |
* ParserConfigurationException, SAXException, IOException,
|
149 |
* XPathExpressionException { String Query = path;
|
150 |
*
|
151 |
* //println "XPath : "+Query; XPathFactory factory =
|
152 |
* XPathFactory.newInstance(); XPath xpath = factory.newXPath();
|
153 |
* xpath.setNamespaceContext(new PersonalNamespaceContext());
|
154 |
* XPathExpression expr = xpath.compile(Query);
|
155 |
*
|
156 |
* Object result = expr.evaluate(doc, XPathConstants.NODESET); // don't work
|
157 |
* on windows NodeList nodes = (NodeList) result; //println
|
158 |
* "result xpath : "+nodes.getLength(); for(int i = 0 ; i <
|
159 |
* nodes.getLength() ; i++) { //println
|
160 |
* "item : "+nodes.item(i).getLocalName(); if(attr != null &&
|
161 |
* !attr.equals("")) return
|
162 |
* nodes.item(i).getAttributes().getNamedItem(attr).getNodeValue(); else
|
163 |
* return nodes.item(i).getTextContent();
|
164 |
*
|
165 |
* } return ""; }
|
166 |
*/
|
167 |
|
168 |
static public String getXpathResponse(File xmlfile, String query, String devaultValue) { |
169 |
String rez = getXpathResponse(xmlfile, query);
|
170 |
if (rez == null) |
171 |
return devaultValue;
|
172 |
return rez;
|
173 |
} |
174 |
|
175 |
static public String getXpathResponse(File xmlfile, String query, String devaultValue, boolean namespaceAware) { |
176 |
String rez = getXpathResponse(xmlfile, query, namespaceAware);
|
177 |
if (rez == null) |
178 |
return devaultValue;
|
179 |
return rez;
|
180 |
} |
181 |
|
182 |
/**
|
183 |
* Gets the xpath response.
|
184 |
*
|
185 |
* @param xmlfile the xmlfile
|
186 |
* @param query the query
|
187 |
* @return the xpath response
|
188 |
*/
|
189 |
static public String getXpathResponse(File xmlfile, String query) { |
190 |
return getXpathResponse(xmlfile, query, true); |
191 |
} |
192 |
|
193 |
/**
|
194 |
* Gets the xpath response.
|
195 |
*
|
196 |
* @param xmlfile the xmlfile
|
197 |
* @param query the query
|
198 |
* @return the xpath response
|
199 |
*/
|
200 |
static public String getXpathResponse(File xmlfile, String query, boolean namespaceAware) { |
201 |
XPathResult result = new XPathResult(xmlfile);
|
202 |
return result.getXpathResponse(query);
|
203 |
// DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
204 |
// factory.setNamespaceAware(namespaceAware); // never forget this!
|
205 |
//
|
206 |
// DocumentBuilder builder = factory.newDocumentBuilder();
|
207 |
// Document doc = builder.parse(xmlfile);
|
208 |
//
|
209 |
// XPathFactory xfactory = XPathFactory.newInstance();
|
210 |
// XPath xpath = xfactory.newXPath();
|
211 |
// xpath.setNamespaceContext(new PersonalNamespaceContext());
|
212 |
//
|
213 |
// XPathExpression expr = xpath.compile(query);
|
214 |
// Object result = expr.evaluate(doc, XPathConstants.NODESET);
|
215 |
//
|
216 |
// NodeList nodes = (NodeList) result;
|
217 |
// println "size: "+result.getLength()
|
218 |
// for (int i = 0; i < nodes.getLength(); i++) {
|
219 |
// return (nodes.item(i).getNodeValue());
|
220 |
// }
|
221 |
} |
222 |
|
223 |
/**
|
224 |
* The main method.
|
225 |
*
|
226 |
* @param args the arguments
|
227 |
*/
|
228 |
public static void main(String[] args) { |
229 |
println("main of XPathResult")
|
230 |
File xmlFile = new File("/home/mdecorde/TXM/corpora/superphenix/txm/SUPERPHENIX/CreysSuper_04_0175.xml") |
231 |
String value = XPathResult.getXpathResponse(xmlFile, "TEI/text/@date"); |
232 |
println "value: $value"
|
233 |
//value = XPathResult.getXpathResponse(new File("/home/mdecorde/xml/bfm/strasb.xml"), "tei:TEI/tei:teiHeader/tei:revisionDesc/tei:change[contains(.,'étiquetage morpho')]");
|
234 |
//println "value: $value"
|
235 |
} |
236 |
} |