Statistics
| Revision:

root / tmp / org.txm.core / src / java / org / txm / scripts / importer / StaxParser.groovy @ 2473

History | View | Annotate | Download (3.8 kB)

1 1000 mdecorde
package org.txm.scripts.importer
2 881 mdecorde
3 881 mdecorde
import groovy.transform.CompileStatic
4 881 mdecorde
import java.io.File;
5 881 mdecorde
import javax.xml.stream.XMLStreamConstants;
6 881 mdecorde
import javax.xml.stream.XMLOutputFactory;
7 881 mdecorde
import javax.xml.stream.XMLStreamWriter;
8 881 mdecorde
import java.net.URL;
9 881 mdecorde
import javax.xml.stream.*;
10 881 mdecorde
import org.txm.importer.filters.*;
11 881 mdecorde
import org.txm.importer.PersonalNamespaceContext;
12 986 mdecorde
import org.txm.importer.*
13 881 mdecorde
14 881 mdecorde
@CompileStatic
15 881 mdecorde
class StaxParser {
16 881 mdecorde
17 881 mdecorde
        protected URL inputurl;
18 881 mdecorde
        protected InputStream inputData;
19 881 mdecorde
        protected XMLInputFactory factory;
20 881 mdecorde
        protected XMLStreamReader parser;
21 881 mdecorde
22 881 mdecorde
23 881 mdecorde
        protected static PersonalNamespaceContext Nscontext = new PersonalNamespaceContext();
24 881 mdecorde
25 881 mdecorde
        protected String localname;
26 881 mdecorde
27 881 mdecorde
        public StaxParser(File infile)
28 881 mdecorde
        {
29 881 mdecorde
                this(infile.toURI().toURL());
30 881 mdecorde
        }
31 881 mdecorde
32 881 mdecorde
        public StaxParser(URL inputurl)
33 881 mdecorde
        {
34 881 mdecorde
                this.inputurl = inputurl;
35 881 mdecorde
                inputData = inputurl.openStream();
36 881 mdecorde
                factory = XMLInputFactory.newInstance();
37 881 mdecorde
38 881 mdecorde
                parser = factory.createXMLStreamReader(inputData);
39 881 mdecorde
        }
40 881 mdecorde
41 881 mdecorde
        protected void before()
42 881 mdecorde
        {
43 881 mdecorde
44 881 mdecorde
        }
45 881 mdecorde
46 881 mdecorde
        protected void after()
47 881 mdecorde
        {
48 881 mdecorde
                factory = null;
49 1688 mdecorde
                if (parser != null) parser.close();
50 1688 mdecorde
                if (inputData != null) inputData.close();
51 881 mdecorde
                parser = null;
52 881 mdecorde
        }
53 881 mdecorde
54 881 mdecorde
        protected void closeForError() {
55 1688 mdecorde
                if (parser != null) parser.close();
56 1688 mdecorde
                if (inputData != null) inputData.close();
57 881 mdecorde
        }
58 881 mdecorde
59 881 mdecorde
        public boolean process()
60 881 mdecorde
        {
61 881 mdecorde
                try {
62 881 mdecorde
                        for (int event = parser.next(); event != XMLStreamConstants.END_DOCUMENT; event = parser.next()) {
63 881 mdecorde
                                switch (event) {
64 881 mdecorde
                                        case XMLStreamConstants.NAMESPACE:
65 881 mdecorde
                                                processNamespace();
66 881 mdecorde
                                                break;
67 881 mdecorde
                                        case XMLStreamConstants.START_ELEMENT:
68 881 mdecorde
                                                localname = parser.getLocalName();
69 881 mdecorde
                                                processStartElement();
70 881 mdecorde
                                                break;
71 881 mdecorde
                                        case XMLStreamConstants.CHARACTERS:
72 881 mdecorde
                                                processCharacters();
73 881 mdecorde
                                                break;
74 881 mdecorde
                                        case XMLStreamConstants.PROCESSING_INSTRUCTION:
75 881 mdecorde
                                                processProcessingInstruction();
76 881 mdecorde
                                                break;
77 881 mdecorde
                                        case XMLStreamConstants.DTD:
78 881 mdecorde
                                                processDTD();
79 881 mdecorde
                                                break;
80 881 mdecorde
                                        case XMLStreamConstants.CDATA:
81 881 mdecorde
                                                processCDATA();
82 881 mdecorde
                                                break;
83 881 mdecorde
                                        case XMLStreamConstants.COMMENT:
84 881 mdecorde
                                                processComment();
85 881 mdecorde
                                                break;
86 881 mdecorde
                                        case XMLStreamConstants.END_ELEMENT:
87 881 mdecorde
                                                localname = parser.getLocalName();
88 881 mdecorde
                                                processEndElement();
89 881 mdecorde
                                                break;
90 881 mdecorde
                                        case XMLStreamConstants.END_DOCUMENT:
91 881 mdecorde
                                                processEndDocument();
92 881 mdecorde
                                                break;
93 881 mdecorde
                                        case XMLStreamConstants.ENTITY_REFERENCE:
94 881 mdecorde
                                                processEntityReference();
95 881 mdecorde
                                                break;
96 881 mdecorde
                                }
97 881 mdecorde
                        }
98 881 mdecorde
                } catch(Exception e) {
99 881 mdecorde
                        println("Unexpected error while parsing file "+inputurl+" : "+e);
100 881 mdecorde
                        println("Location line: "+parser.getLocation().getLineNumber()+" character: "+parser.getLocation().getColumnNumber());
101 881 mdecorde
                        org.txm.utils.logger.Log.printStackTrace(e);
102 1688 mdecorde
                        if (parser != null) parser.close();
103 1688 mdecorde
                if (inputData != null) inputData.close();
104 881 mdecorde
                        return false;
105 881 mdecorde
                }
106 881 mdecorde
107 881 mdecorde
                after(); // if you need to do something before closing the parser();
108 881 mdecorde
                return true;
109 881 mdecorde
        }
110 881 mdecorde
111 881 mdecorde
        public def getLocation() {
112 881 mdecorde
                if (parser != null)
113 881 mdecorde
                        return "Line: "+parser.getLocation().getLineNumber()+" Col: "+parser.getLocation().        getColumnNumber()
114 881 mdecorde
                return null;
115 881 mdecorde
        }
116 881 mdecorde
117 881 mdecorde
        protected void processNamespace() {
118 881 mdecorde
        }
119 881 mdecorde
120 881 mdecorde
        protected void processStartElement()
121 881 mdecorde
        {
122 881 mdecorde
        }
123 881 mdecorde
124 881 mdecorde
        protected void processCharacters()
125 881 mdecorde
        {
126 881 mdecorde
        }
127 881 mdecorde
128 881 mdecorde
        protected void processProcessingInstruction()
129 881 mdecorde
        {
130 881 mdecorde
        }
131 881 mdecorde
132 881 mdecorde
        protected void processDTD()
133 881 mdecorde
        {
134 881 mdecorde
        }
135 881 mdecorde
136 881 mdecorde
        protected void processCDATA()
137 881 mdecorde
        {
138 881 mdecorde
        }
139 881 mdecorde
140 881 mdecorde
        protected void processComment()
141 881 mdecorde
        {
142 881 mdecorde
        }
143 881 mdecorde
144 881 mdecorde
        protected void processEndElement()
145 881 mdecorde
        {
146 881 mdecorde
        }
147 881 mdecorde
148 881 mdecorde
        protected void processEndDocument() {
149 881 mdecorde
        }
150 881 mdecorde
151 881 mdecorde
        protected void processEntityReference() {
152 881 mdecorde
        }
153 881 mdecorde
154 881 mdecorde
        public static void main(String[] args)
155 881 mdecorde
        {
156 881 mdecorde
                File input = new File("/home/mdecorde/xml/xiinclude/master.xml")
157 881 mdecorde
                File output = new File("/home/mdecorde/xml/xiinclude/merged.xml")
158 881 mdecorde
                if (!(input.exists() && input.canRead())) {
159 881 mdecorde
                        println "cannot found $input";
160 881 mdecorde
                        return;
161 881 mdecorde
                }
162 881 mdecorde
                def builder = new StaxParser(input.toURI().toURL());
163 881 mdecorde
                if (builder.process()) {
164 881 mdecorde
                        println "success ? "+ValidateXml.test(output);
165 881 mdecorde
                } else {
166 881 mdecorde
                        println "failure !"
167 881 mdecorde
                }
168 881 mdecorde
        }
169 881 mdecorde
}