Statistics
| Revision:

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

History | View | Annotate | Download (3.8 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: 2013-05-06 17:38:43 +0200 (lun., 06 mai 2013) $
25
// $LastChangedRevision: 2386 $
26
// $LastChangedBy: mdecorde $ 
27
//
28
package org.txm.importer.graal
29

    
30
import org.txm.importer.*;
31
import java.io.IOException;
32
import org.w3c.dom.*;
33
import org.xml.sax.SAXException;
34
import javax.xml.parsers.*;
35
import javax.xml.xpath.*;
36
import javax.xml.stream.*;
37
import java.net.URL;
38

    
39

    
40
// TODO: Auto-generated Javadoc
41
/**
42
 * The Class GraalXpath.
43
 *
44
 * @authors ayepdieu, mdecorde, vchabanis
45
 * 
46
 * return the id of a bfm tag <milestone/>
47
 */
48
public class GraalXpath
49
{
50
        
51
        /**
52
         * Gets the xpath response.
53
         *
54
         * @param fileName the xmlfile
55
         * @param N the id of the milestone
56
         * @return the id of a bfm tag <milestone/>
57
         */
58
        public static String getXpathResponse(def fileName, String N) throws ParserConfigurationException, SAXException, IOException, XPathExpressionException 
59
        {        
60
                if (System.getProperty("os.name").contains("Windows"))
61
                {
62
                        
63
                        XMLInputFactory factory = XMLInputFactory.newInstance();
64
                        FileInputStream inputStream = new FileInputStream(new File(fileName));
65
                        XMLStreamReader parser = factory.createXMLStreamReader(inputStream);
66

    
67
                        for (int event = parser.next(); event != XMLStreamConstants.END_DOCUMENT; event = parser.next()) 
68
                        {
69
                                if( event== XMLStreamConstants.START_ELEMENT)
70
                                {
71
                                        if(parser.getLocalName() == "milestone")
72
                                        {
73
                                                if(parser.getAttributeValue(null , "n")== N)
74
                                                {
75
                                                        if(parser.getAttributeValue(null , "unit")== "column")
76
                                                        {
77
                                                                //TODO : changer l'acces par une clef String au lieu d'un index, prob avec le namespace
78
                                                                return parser.getAttributeValue(1);
79
                                                        }
80
                                                }                
81
                                        }
82
                                }
83
                        }
84
                        println( "FAILED TO FIND MILESTONE WITH N = "+N);
85
                        return null;
86
                }
87
                else
88
                {
89
                        def testG = new XPathResult(new File(fileName));
90
                        String npage =testG.getXpathResponse("//tei:milestone[@unit='column' and @n='"+N+"']","xml:id")
91
                        return npage;
92
                        /*String Query = "//tei:milestone[@unit='column' and @n='"+N+"']"
93
                        DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
94
                    domFactory.setNamespaceAware(true); // never forget this!
95
                    DocumentBuilder builder = domFactory.newDocumentBuilder();
96
                    Document doc = builder.parse(fileName);
97
                   
98
                    XPathFactory factory = XPathFactory.newInstance();
99
                    XPath xpath = factory.newXPath();
100
                    xpath.setNamespaceContext(new PersonalNamespaceContext());
101
                    XPathExpression expr = xpath.compile(Query);
102

103
                    Object result = expr.evaluate(doc, XPathConstants.NODESET); // don't work on windows
104
                    NodeList nodes = (NodeList) result;
105
                    return nodes.item(0).getAttributes().getNamedItem("xml:id").getNodeValue();
106
                    */
107
                }
108
        }
109
        
110
        /**
111
         * The main method.
112
         *
113
         * @param args the arguments
114
         */
115
        public static void main(String[] args) {
116
                def testG = new GraalXpath();
117
                String npage =testG.getXpathResponse("C:\\Documents and Settings\\H\\Bureau\\resourcesTXM\\corpora\\data\\qgraal\\qgraal_cm_2009_03.xml","2")
118
                println "found npage : "+npage;
119
        }
120
}