Statistics
| Revision:

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

History | View | Annotate | Download (1.2 kB)

1 1000 mdecorde
package org.txm.scripts.importer
2 881 mdecorde
3 881 mdecorde
import javax.xml.stream.*;
4 881 mdecorde
5 881 mdecorde
class MetadataGetter {
6 881 mdecorde
        public static String get(File xmlFile, String struct, String prop) {
7 881 mdecorde
8 881 mdecorde
                def inputData = xmlFile.toURI().toURL().openStream();
9 881 mdecorde
                def factory = XMLInputFactory.newInstance();
10 881 mdecorde
                XMLStreamReader parser = factory.createXMLStreamReader(inputData);
11 881 mdecorde
12 881 mdecorde
                for (int event = parser.next(); event != XMLStreamConstants.END_DOCUMENT; event = parser.next()) {
13 881 mdecorde
                        if (event == XMLStreamConstants.START_ELEMENT) {
14 881 mdecorde
                                if ("text".equals(parser.getLocalName())) {
15 881 mdecorde
                                        for (int i = 0 ; i < parser.getAttributeCount() ; i++) {
16 881 mdecorde
                                                if(prop.equals(parser.getAttributeLocalName(i))) {
17 881 mdecorde
                                                        String tmp = parser.getAttributeValue(i)
18 1688 mdecorde
                                                        if (parser != null) parser.close();
19 1688 mdecorde
                                                        if (inputData != null) inputData.close();
20 881 mdecorde
                                                        return tmp
21 881 mdecorde
                                                }
22 881 mdecorde
                                        }
23 881 mdecorde
                                }
24 881 mdecorde
                        }
25 881 mdecorde
                }
26 1688 mdecorde
                if (parser != null) parser.close();
27 1688 mdecorde
                if (inputData != null) inputData.close();
28 881 mdecorde
                return null;
29 881 mdecorde
        }
30 881 mdecorde
31 881 mdecorde
        public static void main(String[] args) {
32 881 mdecorde
                File xmlFile = new File("/home/mdecorde/TXM/corpora/superphenix/txm/SUPERPHENIX/CreysSuper_04_0175.xml")
33 881 mdecorde
                String struct = "text"
34 881 mdecorde
                String prop = "date"
35 881 mdecorde
                println MetadataGetter.get(xmlFile, struct, prop)
36 881 mdecorde
        }
37 881 mdecorde
}