Statistics
| Revision:

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

History | View | Annotate | Download (3.8 kB)

1
package org.txm.scripts.importer
2

    
3
import groovy.transform.CompileStatic
4
import java.io.File;
5
import javax.xml.stream.XMLStreamConstants;
6
import javax.xml.stream.XMLOutputFactory;
7
import javax.xml.stream.XMLStreamWriter;
8
import java.net.URL;
9
import javax.xml.stream.*;
10
import org.txm.importer.filters.*;
11
import org.txm.importer.PersonalNamespaceContext;
12
import org.txm.importer.*
13

    
14
@CompileStatic
15
class StaxParser {
16

    
17
        protected URL inputurl;
18
        protected InputStream inputData;
19
        protected XMLInputFactory factory;
20
        protected XMLStreamReader parser;
21
        
22
        
23
        protected static PersonalNamespaceContext Nscontext = new PersonalNamespaceContext();
24
        
25
        protected String localname;
26
        
27
        public StaxParser(File infile)
28
        {
29
                this(infile.toURI().toURL());
30
        }
31
        
32
        public StaxParser(URL inputurl)
33
        {
34
                this.inputurl = inputurl;
35
                inputData = inputurl.openStream();
36
                factory = XMLInputFactory.newInstance();
37
                
38
                parser = factory.createXMLStreamReader(inputData);
39
        }
40
        
41
        protected void before()
42
        {
43
                
44
        }
45
        
46
        protected void after()
47
        {
48
                factory = null;
49
                if (parser != null) parser.close();
50
                if (inputData != null) inputData.close();
51
                parser = null;
52
        }
53
        
54
        protected void closeForError() {
55
                if (parser != null) parser.close();
56
                if (inputData != null) inputData.close();
57
        }
58

    
59
        public boolean process()
60
        {
61
                try {
62
                        for (int event = parser.next(); event != XMLStreamConstants.END_DOCUMENT; event = parser.next()) {
63
                                switch (event) {
64
                                        case XMLStreamConstants.NAMESPACE:
65
                                                processNamespace();
66
                                                break;
67
                                        case XMLStreamConstants.START_ELEMENT:
68
                                                localname = parser.getLocalName();
69
                                                processStartElement();
70
                                                break;                
71
                                        case XMLStreamConstants.CHARACTERS:
72
                                                processCharacters();
73
                                                break;
74
                                        case XMLStreamConstants.PROCESSING_INSTRUCTION:
75
                                                processProcessingInstruction();
76
                                                break;
77
                                        case XMLStreamConstants.DTD:
78
                                                processDTD();
79
                                                break;
80
                                        case XMLStreamConstants.CDATA:
81
                                                processCDATA();
82
                                                break;
83
                                        case XMLStreamConstants.COMMENT:
84
                                                processComment();
85
                                                break;
86
                                        case XMLStreamConstants.END_ELEMENT:
87
                                                localname = parser.getLocalName();
88
                                                processEndElement();        
89
                                                break;
90
                                        case XMLStreamConstants.END_DOCUMENT:
91
                                                processEndDocument();
92
                                                break;
93
                                        case XMLStreamConstants.ENTITY_REFERENCE:
94
                                                processEntityReference();
95
                                                break;
96
                                }
97
                        }
98
                } catch(Exception e) {
99
                        println("Unexpected error while parsing file "+inputurl+" : "+e);
100
                        println("Location line: "+parser.getLocation().getLineNumber()+" character: "+parser.getLocation().getColumnNumber());
101
                        org.txm.utils.logger.Log.printStackTrace(e);
102
                        if (parser != null) parser.close();
103
                if (inputData != null) inputData.close();
104
                        return false;
105
                }
106
                
107
                after(); // if you need to do something before closing the parser();
108
                return true;
109
        }
110

    
111
        public def getLocation() {
112
                if (parser != null)
113
                        return "Line: "+parser.getLocation().getLineNumber()+" Col: "+parser.getLocation().        getColumnNumber()
114
                return null;
115
        }
116
        
117
        protected void processNamespace() {
118
        }
119
        
120
        protected void processStartElement()
121
        {
122
        }
123

    
124
        protected void processCharacters()
125
        {
126
        }
127
        
128
        protected void processProcessingInstruction()
129
        {
130
        }
131
        
132
        protected void processDTD()
133
        {
134
        }
135
        
136
        protected void processCDATA()
137
        {
138
        }
139
        
140
        protected void processComment()
141
        {
142
        }
143
        
144
        protected void processEndElement()
145
        {
146
        }
147
        
148
        protected void processEndDocument() {
149
        }
150
        
151
        protected void processEntityReference() {
152
        }
153
        
154
        public static void main(String[] args)
155
        {
156
                File input = new File("/home/mdecorde/xml/xiinclude/master.xml")
157
                File output = new File("/home/mdecorde/xml/xiinclude/merged.xml")
158
                if (!(input.exists() && input.canRead())) {
159
                        println "cannot found $input";
160
                        return;
161
                }
162
                def builder = new StaxParser(input.toURI().toURL());
163
                if (builder.process()) {
164
                        println "success ? "+ValidateXml.test(output);
165
                } else {
166
                        println "failure !"
167
                }
168
        }
169
}