Statistics
| Revision:

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

History | View | Annotate | Download (3.6 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
                parser = null;
51
        }
52
        
53
        protected void closeForError() {
54
                parser.close();
55
        }
56

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

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

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