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 1000 mdecorde
package org.txm.scripts.scripts
2 788 mdecorde
3 788 mdecorde
import javax.xml.stream.*;
4 788 mdecorde
5 1000 mdecorde
import org.txm.importer.StaxIdentityParser;
6 788 mdecorde
7 788 mdecorde
8 788 mdecorde
path = ""
9 788 mdecorde
paths = [:]
10 788 mdecorde
11 788 mdecorde
usePaths=true
12 788 mdecorde
useAttributes=true
13 788 mdecorde
useAttributeValues=false
14 788 mdecorde
15 788 mdecorde
inputFile = new File("/home/mdecorde/xml/xmlw/test.xml")
16 788 mdecorde
17 788 mdecorde
try {
18 788 mdecorde
        usePaths=args["usePaths"]
19 788 mdecorde
        useAttributes=args["useAttributes"]
20 788 mdecorde
        useAttributeValues=args["useAttributeValues"]
21 788 mdecorde
        inputFile=args["inputFile"]
22 788 mdecorde
} catch(Exception e) { } // set the "args" map binding to set usePaths, useAttributes,useAttributeValues, inputFile
23 788 mdecorde
24 788 mdecorde
inputFile = new File("/home/mdecorde/xml/xmlw/test.xml")
25 788 mdecorde
26 788 mdecorde
27 788 mdecorde
inputurl = inputFile.toURI().toURL()
28 788 mdecorde
inputData = inputurl.openStream()
29 788 mdecorde
factory = XMLInputFactory.newInstance()
30 788 mdecorde
parser = factory.createXMLStreamReader(inputData)
31 788 mdecorde
32 788 mdecorde
try {
33 788 mdecorde
        for (int event = parser.next(); event != XMLStreamConstants.END_DOCUMENT; event = parser.next()) {
34 788 mdecorde
                switch (event) {
35 788 mdecorde
                        case XMLStreamConstants.NAMESPACE:
36 788 mdecorde
                                break
37 788 mdecorde
                        case XMLStreamConstants.START_ELEMENT:
38 788 mdecorde
                                localname = parser.getLocalName()
39 788 mdecorde
                                processStartElement()
40 788 mdecorde
                                break
41 788 mdecorde
                        case XMLStreamConstants.CHARACTERS:
42 788 mdecorde
                                break
43 788 mdecorde
                        case XMLStreamConstants.END_ELEMENT:
44 788 mdecorde
                                localname = parser.getLocalName()
45 788 mdecorde
                                processEndElement()
46 788 mdecorde
                                break
47 788 mdecorde
                        case XMLStreamConstants.PROCESSING_INSTRUCTION:
48 788 mdecorde
                                break
49 788 mdecorde
                        case XMLStreamConstants.DTD:
50 788 mdecorde
                                break
51 788 mdecorde
                        case XMLStreamConstants.CDATA:
52 788 mdecorde
                                break
53 788 mdecorde
                        case XMLStreamConstants.COMMENT:
54 788 mdecorde
                                break
55 788 mdecorde
                        case XMLStreamConstants.END_DOCUMENT:
56 788 mdecorde
                                break
57 788 mdecorde
                        case XMLStreamConstants.ENTITY_REFERENCE:
58 788 mdecorde
                                break
59 788 mdecorde
                }
60 788 mdecorde
        }
61 788 mdecorde
} catch(Exception e) {
62 788 mdecorde
        println("Unexpected error while parsing file "+inputurl+" : "+e);
63 788 mdecorde
        e.printStackTrace();
64 1688 mdecorde
        if (parser != null) parser.close();
65 1688 mdecorde
        if (inputData != null) inputData.close();
66 788 mdecorde
        return false;
67 788 mdecorde
}
68 788 mdecorde
69 1688 mdecorde
if (parser != null) parser.close();
70 1688 mdecorde
if (inputData != null) inputData.close();
71 1688 mdecorde
72 788 mdecorde
return paths
73 788 mdecorde
74 788 mdecorde
public void processEndElement() {
75 788 mdecorde
        if (usePaths) {
76 788 mdecorde
                path = path.substring(0, path.lastIndexOf("/"))
77 788 mdecorde
        }
78 788 mdecorde
}
79 788 mdecorde
80 788 mdecorde
public void processStartElement() {
81 788 mdecorde
        String localname = parser.getLocalName();
82 788 mdecorde
        if (usePaths) {
83 788 mdecorde
                path = path + "/$localname"
84 788 mdecorde
        } else {
85 788 mdecorde
                path = localname;
86 788 mdecorde
        }
87 788 mdecorde
88 788 mdecorde
        if (useAttributes) {
89 788 mdecorde
                if (useAttributeValues) {
90 788 mdecorde
                        for(int i = 0 ; i < parser.getAttributeCount() ; i++)
91 788 mdecorde
                                if (!parser.getAttributeLocalName(i).matches(UNICATTRS))
92 788 mdecorde
                                        path += "@"+parser.getAttributeLocalName(i)+"="+parser.getAttributeValue(i)
93 788 mdecorde
                } else {
94 788 mdecorde
                        for(int i = 0 ; i < parser.getAttributeCount() ; i++)
95 788 mdecorde
                                path += "@"+parser.getAttributeLocalName(i)
96 788 mdecorde
                }
97 788 mdecorde
        }
98 788 mdecorde
99 788 mdecorde
        println "++"
100 788 mdecorde
        plusOne()
101 788 mdecorde
        //super.processStartElement() // don't write :)
102 788 mdecorde
}
103 788 mdecorde
104 788 mdecorde
private void plusOne() {
105 788 mdecorde
        if (!paths.containsKey(path)) {
106 788 mdecorde
                paths[path] = 1
107 788 mdecorde
        } else {
108 788 mdecorde
                paths[path] =  paths[path]+1
109 788 mdecorde
        }
110 788 mdecorde
}
111 788 mdecorde