Statistics
| Revision:

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

History | View | Annotate | Download (1.7 kB)

1 1000 mdecorde
package org.txm.scripts.importer
2 881 mdecorde
3 881 mdecorde
import javax.xml.stream.*;
4 881 mdecorde
import java.net.URL;
5 881 mdecorde
6 881 mdecorde
/**
7 881 mdecorde
 * Check if a file contains a tag
8 881 mdecorde
 * @author mdecorde
9 881 mdecorde
 *
10 881 mdecorde
 */
11 881 mdecorde
class HasElement {
12 881 mdecorde
13 881 mdecorde
        HashSet<String> localnames = new HashSet<String>();
14 881 mdecorde
        File xmlfile;
15 1687 mdecorde
        def inputData
16 881 mdecorde
        XMLStreamReader parser;
17 881 mdecorde
        HashMap<String, Boolean> elements = [:];
18 881 mdecorde
19 881 mdecorde
        public HasElement(File xmlfile, String localname) {
20 881 mdecorde
                localnames << localname;
21 881 mdecorde
                this.xmlfile = xmlfile;
22 881 mdecorde
                elements[localname] = false;
23 881 mdecorde
24 881 mdecorde
                init();
25 881 mdecorde
        }
26 881 mdecorde
27 881 mdecorde
        private void init() {
28 881 mdecorde
                try {
29 881 mdecorde
30 881 mdecorde
//                        println "xmlfile: "+xmlfile
31 881 mdecorde
//                        println "xmlfile exists: "+xmlfile.exists()
32 881 mdecorde
//                        println "uri: "+ xmlfile.toURI()
33 881 mdecorde
//                        println "url: "+ xmlfile.toURI().toURL()
34 881 mdecorde
//
35 1687 mdecorde
                        inputData = xmlfile.toURI().toURL().openStream();
36 881 mdecorde
                        def inputFactory = XMLInputFactory.newInstance();
37 881 mdecorde
                        parser = inputFactory.createXMLStreamReader(inputData);
38 881 mdecorde
                } catch (Exception ex) {
39 881 mdecorde
                        System.out.println("IOException while parsing: "+ex);
40 881 mdecorde
                        ex.printStackTrace();
41 881 mdecorde
                }
42 881 mdecorde
        }
43 881 mdecorde
44 881 mdecorde
        public boolean process() {
45 1687 mdecorde
                if (inputData == null) return false;
46 881 mdecorde
                if (parser == null) return false;
47 881 mdecorde
48 881 mdecorde
                for (int event = parser.next(); event != XMLStreamConstants.END_DOCUMENT; event = parser.next()) {
49 881 mdecorde
                        if (event == XMLStreamConstants.START_ELEMENT) {
50 881 mdecorde
                                String localname = parser.getLocalName();
51 881 mdecorde
                                if (localnames.contains(localname)) {
52 881 mdecorde
                                        parser.close();
53 1687 mdecorde
                                        inputData.close();
54 881 mdecorde
                                        return true;
55 881 mdecorde
                                }
56 881 mdecorde
                        }
57 881 mdecorde
                }
58 881 mdecorde
                parser.close();
59 1687 mdecorde
                inputData.close();
60 1687 mdecorde
                return false;
61 881 mdecorde
        }
62 881 mdecorde
63 881 mdecorde
        public static void main(String[] args) {
64 881 mdecorde
                File dir = new File("/home/mdecorde/xml/lamoplat")
65 881 mdecorde
                println dir.listFiles()
66 881 mdecorde
                for (File xmlfile : dir.listFiles()) {
67 881 mdecorde
                        if (!xmlfile.getName().endsWith(".xml")) continue
68 881 mdecorde
                        //println xmlfile
69 881 mdecorde
                        println "$xmlfile: "+ new HasElement(xmlfile, "text").process();
70 881 mdecorde
                }
71 881 mdecorde
        }
72 881 mdecorde
}