Statistics
| Revision:

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

History | View | Annotate | Download (1.7 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
        def inputData
16
        XMLStreamReader parser;
17
        HashMap<String, Boolean> elements = [:];
18

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

    
24
                init();
25
        }
26

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

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