Statistics
| Revision:

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

History | View | Annotate | Download (1.6 kB)

1
package org.txm.scripts.importer
2

    
3
import javax.xml.stream.*;
4
import java.net.URL;
5

    
6
/**
7
 * Check if a file contains a tag
8
 * @author mdecorde
9
 *
10
 */
11
class HasElement {
12

    
13
        HashSet<String> localnames = new HashSet<String>();
14
        File xmlfile;
15
        XMLStreamReader parser;
16
        HashMap<String, Boolean> elements = [:];
17

    
18
        public HasElement(File xmlfile, String localname) {
19
                localnames << localname;
20
                this.xmlfile = xmlfile;
21
                elements[localname] = false;
22

    
23
                init();
24
        }
25

    
26
        private void init() {
27
                try {
28
                        
29
//                        println "xmlfile: "+xmlfile
30
//                        println "xmlfile exists: "+xmlfile.exists()
31
//                        println "uri: "+ xmlfile.toURI()
32
//                        println "url: "+ xmlfile.toURI().toURL()
33
//                        
34
                        def inputData = xmlfile.toURI().toURL().openStream();
35
                        def inputFactory = XMLInputFactory.newInstance();
36
                        parser = inputFactory.createXMLStreamReader(inputData);
37
                } catch (Exception ex) {
38
                        System.out.println("IOException while parsing: "+ex);
39
                        ex.printStackTrace();
40
                }
41
        }
42

    
43
        public boolean process() {
44
                if (parser == null) return false;
45
                
46
                for (int event = parser.next(); event != XMLStreamConstants.END_DOCUMENT; event = parser.next()) {
47
                        if (event == XMLStreamConstants.START_ELEMENT) {
48
                                String localname = parser.getLocalName();
49
                                if (localnames.contains(localname)) {
50
                                        parser.close();
51
                                        return true;
52
                                }
53
                        }
54
                }
55
                parser.close();
56
        }
57
        
58
        public static void main(String[] args) {
59
                File dir = new File("/home/mdecorde/xml/lamoplat")
60
                println dir.listFiles()
61
                for (File xmlfile : dir.listFiles()) {
62
                        if (!xmlfile.getName().endsWith(".xml")) continue
63
                        //println xmlfile
64
                        println "$xmlfile: "+ new HasElement(xmlfile, "text").process();
65
                }
66
        }
67
}