Statistics
| Revision:

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

History | View | Annotate | Download (2.4 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
        parser.close();
65
        return false;
66
}
67

    
68
return paths
69

    
70
public void processEndElement() {
71
        if (usePaths) {
72
                path = path.substring(0, path.lastIndexOf("/"))
73
        }
74
}
75

    
76
public void processStartElement() {
77
        String localname = parser.getLocalName();
78
        if (usePaths) {
79
                path = path + "/$localname"
80
        } else {
81
                path = localname;
82
        }
83

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

    
95
        println "++"
96
        plusOne()
97
        //super.processStartElement() // don't write :)
98
}
99

    
100
private void plusOne() {
101
        if (!paths.containsKey(path)) {
102
                paths[path] = 1
103
        } else {
104
                paths[path] =  paths[path]+1
105
        }
106
}
107

    
108