Statistics
| Revision:

root / tmp / org.txm.groovy.core / src / groovy / org / txm / scripts / swb / FindUnclosedTag.groovy @ 1688

History | View | Annotate | Download (1.7 kB)

1
package org.txm.scripts.scripts
2

    
3
import javax.xml.stream.XMLInputFactory;
4
import javax.xml.stream.XMLStreamConstants;
5

    
6
File xmlFile = new File("/home/mdecorde/xml/errorxml/error.xml")
7

    
8
def inputurl = xmlFile.toURI().toURL();
9
def inputData = inputurl.openStream();
10
def factory = XMLInputFactory.newInstance();
11
def parser = factory.createXMLStreamReader(inputData);
12

    
13
opened = []
14
locations = []
15
structPath = "/"
16

    
17
try {
18
for (int event = parser.next(); event != XMLStreamConstants.END_DOCUMENT; event = parser.next()) {
19
        switch (event) {
20
                case XMLStreamConstants.START_ELEMENT:
21
                String localname = parser.getLocalName();
22
                        startElement(localname, parser.getLocation().getLineNumber());
23
                break;
24
                case XMLStreamConstants.END_ELEMENT:
25
                        String localname = parser.getLocalName();
26
                        endElement(localname);
27
                break
28
        }
29
}
30
} catch(Exception e) {
31
        println "** Error while parsing: "+e
32
        println "** Error occured at : "+structPath+" line "+parser.getLocation().getLineNumber()
33
        println "** Opened tags: "+opened
34
        println "** at lines: "+locations
35
}
36

    
37
if (parser != null) parser.close();
38
if (inputData != null) inputData.close();
39

    
40
public boolean startElement(String localname, def loc) {
41
        println structPath
42
        structPath += localname+"/"
43
        opened << localname
44
        locations << loc
45
        return true;
46
}
47

    
48
public boolean endElement(String localname) {
49
        if (structPath.length() > 1) {
50
                def lastTag = opened[-1]
51
                if (localname != lastTag) {
52
                        println "ERROR: closing tag '$localname' does not match last opened tag '$lastTag'"
53
                        println opened
54
                        return false;
55
                }
56
        
57
                int idx = structPath.substring(0,structPath.length()-1).lastIndexOf("/");
58
                if (idx > 0)
59
                        structPath = structPath.substring(0, idx+1)
60
                //
61
                opened.pop()
62
                locations.pop()
63
                        
64
                return true
65
        }
66
}