Statistics
| Revision:

root / tmp / org.txm.groovy.core / src / groovy / org / txm / macro / xml / XMLFileStatistics.groovy @ 1688

History | View | Annotate | Download (2.6 kB)

1
package org.txm.scripts.scripts
2

    
3
import javax.xml.stream.*;
4

    
5
import org.txm.importer.StaxIdentityParser;
6

    
7

    
8
path = ""
9
paths = [:]
10

    
11
usePaths=true
12
useAttributes=true
13
useAttributeValues=false
14

    
15
inputFile = new File("/home/mdecorde/xml/xmlw/test.xml")
16

    
17
try {
18
        usePaths=args["usePaths"]
19
        useAttributes=args["useAttributes"]
20
        useAttributeValues=args["useAttributeValues"]
21
        inputFile=args["inputFile"]
22
} catch(Exception e) { } // set the "args" map binding to set usePaths, useAttributes,useAttributeValues, inputFile
23

    
24
inputFile = new File("/home/mdecorde/xml/xmlw/test.xml")
25

    
26

    
27
inputurl = inputFile.toURI().toURL()
28
inputData = inputurl.openStream()
29
factory = XMLInputFactory.newInstance()
30
parser = factory.createXMLStreamReader(inputData)
31

    
32
try {
33
        for (int event = parser.next(); event != XMLStreamConstants.END_DOCUMENT; event = parser.next()) {
34
                switch (event) {
35
                        case XMLStreamConstants.NAMESPACE:
36
                                break
37
                        case XMLStreamConstants.START_ELEMENT:
38
                                localname = parser.getLocalName()
39
                                processStartElement()
40
                                break
41
                        case XMLStreamConstants.CHARACTERS:
42
                                break
43
                        case XMLStreamConstants.END_ELEMENT:
44
                                localname = parser.getLocalName()
45
                                processEndElement()
46
                                break
47
                        case XMLStreamConstants.PROCESSING_INSTRUCTION:
48
                                break
49
                        case XMLStreamConstants.DTD:
50
                                break
51
                        case XMLStreamConstants.CDATA:
52
                                break
53
                        case XMLStreamConstants.COMMENT:
54
                                break
55
                        case XMLStreamConstants.END_DOCUMENT:
56
                                break
57
                        case XMLStreamConstants.ENTITY_REFERENCE:
58
                                break
59
                }
60
        }
61
} catch(Exception e) {
62
        println("Unexpected error while parsing file "+inputurl+" : "+e);
63
        e.printStackTrace();
64
        if (parser != null) parser.close();
65
        if (inputData != null) inputData.close();
66
        return false;
67
}
68

    
69
if (parser != null) parser.close();
70
if (inputData != null) inputData.close();
71

    
72
return paths
73

    
74
public void processEndElement() {
75
        if (usePaths) {
76
                path = path.substring(0, path.lastIndexOf("/"))
77
        }
78
}
79

    
80
public void processStartElement() {
81
        String localname = parser.getLocalName();
82
        if (usePaths) {
83
                path = path + "/$localname"
84
        } else {
85
                path = localname;
86
        }
87

    
88
        if (useAttributes) {
89
                if (useAttributeValues) {
90
                        for(int i = 0 ; i < parser.getAttributeCount() ; i++)
91
                                if (!parser.getAttributeLocalName(i).matches(UNICATTRS))
92
                                        path += "@"+parser.getAttributeLocalName(i)+"="+parser.getAttributeValue(i)
93
                } else {
94
                        for(int i = 0 ; i < parser.getAttributeCount() ; i++)
95
                                path += "@"+parser.getAttributeLocalName(i)
96
                }
97
        }
98

    
99
        println "++"
100
        plusOne()
101
        //super.processStartElement() // don't write :)
102
}
103

    
104
private void plusOne() {
105
        if (!paths.containsKey(path)) {
106
                paths[path] = 1
107
        } else {
108
                paths[path] =  paths[path]+1
109
        }
110
}
111

    
112