Statistics
| Revision:

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

History | View | Annotate | Download (3.6 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 881 mdecorde
                if (parser !=null) parser.close();
50 881 mdecorde
                parser = null;
51 881 mdecorde
        }
52 881 mdecorde
53 881 mdecorde
        protected void closeForError() {
54 881 mdecorde
                parser.close();
55 881 mdecorde
        }
56 881 mdecorde
57 881 mdecorde
        public boolean process()
58 881 mdecorde
        {
59 881 mdecorde
                try {
60 881 mdecorde
                        for (int event = parser.next(); event != XMLStreamConstants.END_DOCUMENT; event = parser.next()) {
61 881 mdecorde
                                switch (event) {
62 881 mdecorde
                                        case XMLStreamConstants.NAMESPACE:
63 881 mdecorde
                                                processNamespace();
64 881 mdecorde
                                                break;
65 881 mdecorde
                                        case XMLStreamConstants.START_ELEMENT:
66 881 mdecorde
                                                localname = parser.getLocalName();
67 881 mdecorde
                                                processStartElement();
68 881 mdecorde
                                                break;
69 881 mdecorde
                                        case XMLStreamConstants.CHARACTERS:
70 881 mdecorde
                                                processCharacters();
71 881 mdecorde
                                                break;
72 881 mdecorde
                                        case XMLStreamConstants.PROCESSING_INSTRUCTION:
73 881 mdecorde
                                                processProcessingInstruction();
74 881 mdecorde
                                                break;
75 881 mdecorde
                                        case XMLStreamConstants.DTD:
76 881 mdecorde
                                                processDTD();
77 881 mdecorde
                                                break;
78 881 mdecorde
                                        case XMLStreamConstants.CDATA:
79 881 mdecorde
                                                processCDATA();
80 881 mdecorde
                                                break;
81 881 mdecorde
                                        case XMLStreamConstants.COMMENT:
82 881 mdecorde
                                                processComment();
83 881 mdecorde
                                                break;
84 881 mdecorde
                                        case XMLStreamConstants.END_ELEMENT:
85 881 mdecorde
                                                localname = parser.getLocalName();
86 881 mdecorde
                                                processEndElement();
87 881 mdecorde
                                                break;
88 881 mdecorde
                                        case XMLStreamConstants.END_DOCUMENT:
89 881 mdecorde
                                                processEndDocument();
90 881 mdecorde
                                                break;
91 881 mdecorde
                                        case XMLStreamConstants.ENTITY_REFERENCE:
92 881 mdecorde
                                                processEntityReference();
93 881 mdecorde
                                                break;
94 881 mdecorde
                                }
95 881 mdecorde
                        }
96 881 mdecorde
                } catch(Exception e) {
97 881 mdecorde
                        println("Unexpected error while parsing file "+inputurl+" : "+e);
98 881 mdecorde
                        println("Location line: "+parser.getLocation().getLineNumber()+" character: "+parser.getLocation().getColumnNumber());
99 881 mdecorde
                        org.txm.utils.logger.Log.printStackTrace(e);
100 881 mdecorde
                        parser.close();
101 881 mdecorde
                        return false;
102 881 mdecorde
                }
103 881 mdecorde
104 881 mdecorde
                after(); // if you need to do something before closing the parser();
105 881 mdecorde
                return true;
106 881 mdecorde
        }
107 881 mdecorde
108 881 mdecorde
        public def getLocation() {
109 881 mdecorde
                if (parser != null)
110 881 mdecorde
                        return "Line: "+parser.getLocation().getLineNumber()+" Col: "+parser.getLocation().        getColumnNumber()
111 881 mdecorde
                return null;
112 881 mdecorde
        }
113 881 mdecorde
114 881 mdecorde
        protected void processNamespace() {
115 881 mdecorde
        }
116 881 mdecorde
117 881 mdecorde
        protected void processStartElement()
118 881 mdecorde
        {
119 881 mdecorde
        }
120 881 mdecorde
121 881 mdecorde
        protected void processCharacters()
122 881 mdecorde
        {
123 881 mdecorde
        }
124 881 mdecorde
125 881 mdecorde
        protected void processProcessingInstruction()
126 881 mdecorde
        {
127 881 mdecorde
        }
128 881 mdecorde
129 881 mdecorde
        protected void processDTD()
130 881 mdecorde
        {
131 881 mdecorde
        }
132 881 mdecorde
133 881 mdecorde
        protected void processCDATA()
134 881 mdecorde
        {
135 881 mdecorde
        }
136 881 mdecorde
137 881 mdecorde
        protected void processComment()
138 881 mdecorde
        {
139 881 mdecorde
        }
140 881 mdecorde
141 881 mdecorde
        protected void processEndElement()
142 881 mdecorde
        {
143 881 mdecorde
        }
144 881 mdecorde
145 881 mdecorde
        protected void processEndDocument() {
146 881 mdecorde
        }
147 881 mdecorde
148 881 mdecorde
        protected void processEntityReference() {
149 881 mdecorde
        }
150 881 mdecorde
151 881 mdecorde
        public static void main(String[] args)
152 881 mdecorde
        {
153 881 mdecorde
                File input = new File("/home/mdecorde/xml/xiinclude/master.xml")
154 881 mdecorde
                File output = new File("/home/mdecorde/xml/xiinclude/merged.xml")
155 881 mdecorde
                if (!(input.exists() && input.canRead())) {
156 881 mdecorde
                        println "cannot found $input";
157 881 mdecorde
                        return;
158 881 mdecorde
                }
159 881 mdecorde
                def builder = new StaxParser(input.toURI().toURL());
160 881 mdecorde
                if (builder.process()) {
161 881 mdecorde
                        println "success ? "+ValidateXml.test(output);
162 881 mdecorde
                } else {
163 881 mdecorde
                        println "failure !"
164 881 mdecorde
                }
165 881 mdecorde
        }
166 881 mdecorde
}