Statistics
| Revision:

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

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