Statistics
| Revision:

root / tmp / org.txm.groovy.core / src / groovy / org / txm / importer / HasElement.groovy @ 966

History | View | Annotate | Download (1.6 kB)

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