Statistics
| Revision:

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

History | View | Annotate | Download (7 kB)

1 881 mdecorde
// Copyright © 2010-2013 ENS de Lyon.
2 881 mdecorde
// Copyright © 2007-2010 ENS de Lyon, CNRS, INRP, University of
3 881 mdecorde
// Lyon 2, University of Franche-Comté, University of Nice
4 881 mdecorde
// Sophia Antipolis, University of Paris 3.
5 881 mdecorde
//
6 881 mdecorde
// The TXM platform is free software: you can redistribute it
7 881 mdecorde
// and/or modify it under the terms of the GNU General Public
8 881 mdecorde
// License as published by the Free Software Foundation,
9 881 mdecorde
// either version 2 of the License, or (at your option) any
10 881 mdecorde
// later version.
11 881 mdecorde
//
12 881 mdecorde
// The TXM platform is distributed in the hope that it will be
13 881 mdecorde
// useful, but WITHOUT ANY WARRANTY; without even the implied
14 881 mdecorde
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 881 mdecorde
// PURPOSE. See the GNU General Public License for more
16 881 mdecorde
// details.
17 881 mdecorde
//
18 881 mdecorde
// You should have received a copy of the GNU General
19 881 mdecorde
// Public License along with the TXM platform. If not, see
20 881 mdecorde
// http://www.gnu.org/licenses.
21 881 mdecorde
//
22 881 mdecorde
//
23 881 mdecorde
//
24 881 mdecorde
// $LastChangedDate: 2017-04-11 15:30:35 +0200 (mar. 11 avril 2017) $
25 881 mdecorde
// $LastChangedRevision: 3426 $
26 881 mdecorde
// $LastChangedBy: mdecorde $
27 881 mdecorde
//
28 1000 mdecorde
package org.txm.scripts.importer;
29 881 mdecorde
30 881 mdecorde
import org.txm.importer.PersonalNamespaceContext;
31 881 mdecorde
32 881 mdecorde
import java.io.File;
33 881 mdecorde
import java.io.IOException;
34 881 mdecorde
import java.util.Iterator;
35 881 mdecorde
import org.w3c.dom.*;
36 881 mdecorde
import org.xml.sax.SAXException;
37 881 mdecorde
38 881 mdecorde
import javax.xml.bind.helpers.DefaultValidationEventHandler;
39 881 mdecorde
import javax.xml.parsers.*;
40 881 mdecorde
import javax.xml.xpath.*;
41 881 mdecorde
import javax.xml.stream.*;
42 881 mdecorde
import java.net.URL;
43 881 mdecorde
import java.util.Iterator;
44 881 mdecorde
45 881 mdecorde
// TODO: Auto-generated Javadoc
46 881 mdecorde
/**
47 881 mdecorde
 * The Class XPathResult.
48 881 mdecorde
 *
49 881 mdecorde
 * @authors ayepdieu, mdecorde, vchabanis
50 881 mdecorde
 *
51 881 mdecorde
 * return the id of a bfm tag <milestone/>
52 881 mdecorde
 */
53 881 mdecorde
public class XPathResult {
54 881 mdecorde
55 881 mdecorde
        /** The doc. */
56 881 mdecorde
        Document doc;
57 881 mdecorde
        XPath xpath;
58 881 mdecorde
59 881 mdecorde
        /**
60 881 mdecorde
         * Instantiates a new x path result.
61 881 mdecorde
         *
62 881 mdecorde
         * @param file the file
63 881 mdecorde
         */
64 881 mdecorde
        public XPathResult(File xmlfile) {
65 881 mdecorde
                this(xmlfile, true)
66 881 mdecorde
        }
67 881 mdecorde
68 881 mdecorde
        /**
69 881 mdecorde
         * Instantiates a new x path result.
70 881 mdecorde
         *
71 881 mdecorde
         * @param file the file
72 881 mdecorde
         */
73 881 mdecorde
        public XPathResult(File xmlfile, boolean namespaceAware) {
74 881 mdecorde
                DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
75 881 mdecorde
                factory.setXIncludeAware(true);
76 881 mdecorde
                factory.setNamespaceAware(namespaceAware); // never forget this!
77 881 mdecorde
78 881 mdecorde
                DocumentBuilder builder = factory.newDocumentBuilder();
79 881 mdecorde
                doc = builder.parse(xmlfile);
80 881 mdecorde
81 881 mdecorde
                XPathFactory xfactory = XPathFactory.newInstance();
82 881 mdecorde
                xpath = xfactory.newXPath();
83 881 mdecorde
                xpath.setNamespaceContext(new PersonalNamespaceContext());
84 881 mdecorde
        }
85 881 mdecorde
86 881 mdecorde
        public def getNodes(String query) {
87 881 mdecorde
                def rnodes = [];
88 881 mdecorde
89 881 mdecorde
                XPathExpression expr = xpath.compile(query);
90 881 mdecorde
                Object result = expr.evaluate(doc.getDocumentElement(), XPathConstants.NODESET);
91 881 mdecorde
92 881 mdecorde
                NodeList nodes = (NodeList) result;
93 881 mdecorde
                for (int i = 0; i < nodes.getLength(); i++) {
94 881 mdecorde
                        rnodes.add(nodes.item(i));
95 881 mdecorde
                }
96 881 mdecorde
                return rnodes;
97 881 mdecorde
        }
98 881 mdecorde
99 881 mdecorde
        public String getXpathResponse(String query) {
100 881 mdecorde
                XPathExpression expr = xpath.compile(query);
101 881 mdecorde
                Object result = expr.evaluate(doc.getDocumentElement(), XPathConstants.NODESET);
102 881 mdecorde
103 881 mdecorde
                NodeList nodes = (NodeList) result;
104 881 mdecorde
                for (int i = 0; i < nodes.getLength(); i++) {
105 881 mdecorde
                        //println nodes.item(i)
106 881 mdecorde
                        return (nodes.item(i).getNodeValue());
107 881 mdecorde
                }
108 881 mdecorde
        }
109 881 mdecorde
110 881 mdecorde
        public ArrayList<String> getXpathResponses(String query) {
111 881 mdecorde
                ArrayList<String> xresult = new ArrayList<String>();
112 881 mdecorde
                XPathExpression expr = xpath.compile(query);
113 881 mdecorde
                Object result = expr.evaluate(doc.getDocumentElement(), XPathConstants.NODESET);
114 881 mdecorde
115 881 mdecorde
                NodeList nodes = (NodeList) result;
116 881 mdecorde
                for (int i = 0; i < nodes.getLength(); i++) {
117 881 mdecorde
                        //println nodes.item(i)
118 881 mdecorde
                        xresult.add((nodes.item(i).getNodeValue()));
119 881 mdecorde
                }
120 881 mdecorde
                return xresult
121 881 mdecorde
        }
122 881 mdecorde
123 881 mdecorde
        public String getXpathResponse(String query, String devaultValue) {
124 881 mdecorde
                String rez = getXpathResponse(query);
125 881 mdecorde
                if (rez == null)
126 881 mdecorde
                        return devaultValue;
127 881 mdecorde
                return rez;
128 881 mdecorde
        }
129 881 mdecorde
130 881 mdecorde
        public void close() {
131 881 mdecorde
                xpath = null;
132 881 mdecorde
                doc = null;
133 881 mdecorde
        }
134 881 mdecorde
135 881 mdecorde
        /**
136 881 mdecorde

137 881 mdecorde
         * OBSOLETE VERSION FOR TXM return the node text content given a XPath
138 881 mdecorde

139 881 mdecorde
         * "//path.../.../@attr"
140 881 mdecorde
         *
141 881 mdecorde
         * @param path
142 881 mdecorde
         * @param attr
143 881 mdecorde
         *            null or "" return the text of the tag targeted
144 881 mdecorde
         * @return
145 881 mdecorde
         */
146 881 mdecorde
        /*
147 881 mdecorde
         * public String getXpathResponse(String path, String attr) throws
148 881 mdecorde
         * ParserConfigurationException, SAXException, IOException,
149 881 mdecorde
         * XPathExpressionException { String Query = path;
150 881 mdecorde
         *
151 881 mdecorde
         * //println "XPath : "+Query; XPathFactory factory =
152 881 mdecorde
         * XPathFactory.newInstance(); XPath xpath = factory.newXPath();
153 881 mdecorde
         * xpath.setNamespaceContext(new PersonalNamespaceContext());
154 881 mdecorde
         * XPathExpression expr = xpath.compile(Query);
155 881 mdecorde
         *
156 881 mdecorde
         * Object result = expr.evaluate(doc, XPathConstants.NODESET); // don't work
157 881 mdecorde
         * on windows NodeList nodes = (NodeList) result; //println
158 881 mdecorde
         * "result xpath : "+nodes.getLength(); for(int i = 0 ; i <
159 881 mdecorde
         * nodes.getLength() ; i++) { //println
160 881 mdecorde
         * "item : "+nodes.item(i).getLocalName(); if(attr != null &&
161 881 mdecorde
         * !attr.equals("")) return
162 881 mdecorde
         * nodes.item(i).getAttributes().getNamedItem(attr).getNodeValue(); else
163 881 mdecorde
         * return nodes.item(i).getTextContent();
164 881 mdecorde
         *
165 881 mdecorde
         * } return ""; }
166 881 mdecorde
         */
167 881 mdecorde
168 881 mdecorde
        static public String getXpathResponse(File xmlfile, String query, String devaultValue) {
169 881 mdecorde
                String rez = getXpathResponse(xmlfile, query);
170 881 mdecorde
                if (rez == null)
171 881 mdecorde
                        return devaultValue;
172 881 mdecorde
                return rez;
173 881 mdecorde
        }
174 881 mdecorde
175 881 mdecorde
        static public String getXpathResponse(File xmlfile, String query, String devaultValue, boolean namespaceAware) {
176 881 mdecorde
                String rez = getXpathResponse(xmlfile, query, namespaceAware);
177 881 mdecorde
                if (rez == null)
178 881 mdecorde
                        return devaultValue;
179 881 mdecorde
                return rez;
180 881 mdecorde
        }
181 881 mdecorde
182 881 mdecorde
        /**
183 881 mdecorde
         * Gets the xpath response.
184 881 mdecorde
         *
185 881 mdecorde
         * @param xmlfile the xmlfile
186 881 mdecorde
         * @param query the query
187 881 mdecorde
         * @return the xpath response
188 881 mdecorde
         */
189 881 mdecorde
        static public String getXpathResponse(File xmlfile, String query) {
190 881 mdecorde
                return getXpathResponse(xmlfile, query, true);
191 881 mdecorde
        }
192 881 mdecorde
193 881 mdecorde
        /**
194 881 mdecorde
         * Gets the xpath response.
195 881 mdecorde
         *
196 881 mdecorde
         * @param xmlfile the xmlfile
197 881 mdecorde
         * @param query the query
198 881 mdecorde
         * @return the xpath response
199 881 mdecorde
         */
200 881 mdecorde
        static public String getXpathResponse(File xmlfile, String query, boolean namespaceAware) {
201 881 mdecorde
                XPathResult result = new XPathResult(xmlfile);
202 881 mdecorde
                return result.getXpathResponse(query);
203 881 mdecorde
//                DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
204 881 mdecorde
//                factory.setNamespaceAware(namespaceAware); // never forget this!
205 881 mdecorde
//
206 881 mdecorde
//                DocumentBuilder builder = factory.newDocumentBuilder();
207 881 mdecorde
//                Document doc = builder.parse(xmlfile);
208 881 mdecorde
//
209 881 mdecorde
//                XPathFactory xfactory = XPathFactory.newInstance();
210 881 mdecorde
//                XPath xpath = xfactory.newXPath();
211 881 mdecorde
//                xpath.setNamespaceContext(new PersonalNamespaceContext());
212 881 mdecorde
//
213 881 mdecorde
//                XPathExpression expr = xpath.compile(query);
214 881 mdecorde
//                Object result = expr.evaluate(doc, XPathConstants.NODESET);
215 881 mdecorde
//
216 881 mdecorde
//                NodeList nodes = (NodeList) result;
217 881 mdecorde
//                println "size: "+result.getLength()
218 881 mdecorde
//                for (int i = 0; i < nodes.getLength(); i++) {
219 881 mdecorde
//                        return (nodes.item(i).getNodeValue());
220 881 mdecorde
//                }
221 881 mdecorde
        }
222 881 mdecorde
223 881 mdecorde
        /**
224 881 mdecorde
         * The main method.
225 881 mdecorde
         *
226 881 mdecorde
         * @param args the arguments
227 881 mdecorde
         */
228 881 mdecorde
        public static void main(String[] args) {
229 881 mdecorde
                println("main of XPathResult")
230 881 mdecorde
                File xmlFile = new File("/home/mdecorde/TXM/corpora/superphenix/txm/SUPERPHENIX/CreysSuper_04_0175.xml")
231 881 mdecorde
                String value = XPathResult.getXpathResponse(xmlFile, "TEI/text/@date");
232 881 mdecorde
                println "value: $value"
233 881 mdecorde
                //value = XPathResult.getXpathResponse(new File("/home/mdecorde/xml/bfm/strasb.xml"), "tei:TEI/tei:teiHeader/tei:revisionDesc/tei:change[contains(.,'étiquetage morpho')]");
234 881 mdecorde
                //println "value: $value"
235 881 mdecorde
        }
236 881 mdecorde
}