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 1000 mdecorde
package org.txm.scripts.scripts
2 321 mdecorde
3 321 mdecorde
import javax.xml.stream.XMLInputFactory;
4 321 mdecorde
import javax.xml.stream.XMLStreamConstants;
5 321 mdecorde
6 321 mdecorde
File xmlFile = new File("/home/mdecorde/xml/errorxml/error.xml")
7 321 mdecorde
8 321 mdecorde
def inputurl = xmlFile.toURI().toURL();
9 321 mdecorde
def inputData = inputurl.openStream();
10 321 mdecorde
def factory = XMLInputFactory.newInstance();
11 321 mdecorde
def parser = factory.createXMLStreamReader(inputData);
12 321 mdecorde
13 321 mdecorde
opened = []
14 321 mdecorde
locations = []
15 321 mdecorde
structPath = "/"
16 321 mdecorde
17 321 mdecorde
try {
18 321 mdecorde
for (int event = parser.next(); event != XMLStreamConstants.END_DOCUMENT; event = parser.next()) {
19 321 mdecorde
        switch (event) {
20 321 mdecorde
                case XMLStreamConstants.START_ELEMENT:
21 321 mdecorde
                String localname = parser.getLocalName();
22 321 mdecorde
                        startElement(localname, parser.getLocation().getLineNumber());
23 321 mdecorde
                break;
24 321 mdecorde
                case XMLStreamConstants.END_ELEMENT:
25 321 mdecorde
                        String localname = parser.getLocalName();
26 321 mdecorde
                        endElement(localname);
27 321 mdecorde
                break
28 321 mdecorde
        }
29 321 mdecorde
}
30 321 mdecorde
} catch(Exception e) {
31 321 mdecorde
        println "** Error while parsing: "+e
32 321 mdecorde
        println "** Error occured at : "+structPath+" line "+parser.getLocation().getLineNumber()
33 321 mdecorde
        println "** Opened tags: "+opened
34 321 mdecorde
        println "** at lines: "+locations
35 321 mdecorde
}
36 321 mdecorde
37 1688 mdecorde
if (parser != null) parser.close();
38 1688 mdecorde
if (inputData != null) inputData.close();
39 1688 mdecorde
40 321 mdecorde
public boolean startElement(String localname, def loc) {
41 321 mdecorde
        println structPath
42 321 mdecorde
        structPath += localname+"/"
43 321 mdecorde
        opened << localname
44 321 mdecorde
        locations << loc
45 321 mdecorde
        return true;
46 321 mdecorde
}
47 321 mdecorde
48 321 mdecorde
public boolean endElement(String localname) {
49 321 mdecorde
        if (structPath.length() > 1) {
50 321 mdecorde
                def lastTag = opened[-1]
51 321 mdecorde
                if (localname != lastTag) {
52 321 mdecorde
                        println "ERROR: closing tag '$localname' does not match last opened tag '$lastTag'"
53 321 mdecorde
                        println opened
54 321 mdecorde
                        return false;
55 321 mdecorde
                }
56 321 mdecorde
57 321 mdecorde
                int idx = structPath.substring(0,structPath.length()-1).lastIndexOf("/");
58 321 mdecorde
                if (idx > 0)
59 321 mdecorde
                        structPath = structPath.substring(0, idx+1)
60 321 mdecorde
                //
61 321 mdecorde
                opened.pop()
62 321 mdecorde
                locations.pop()
63 321 mdecorde
64 321 mdecorde
                return true
65 321 mdecorde
        }
66 321 mdecorde
}