Statistics
| Revision:

root / tmp / org.txm.core / src / java / org / txm / scripts / importer / StaxStackWriter.groovy @ 3045

History | View | Annotate | Download (6.8 kB)

1
package org.txm.scripts.importer
2

    
3
import java.nio.charset.Charset;
4

    
5
import javax.xml.namespace.NamespaceContext;
6
import javax.xml.stream.*;
7

    
8
import org.eclipse.core.runtime.Platform;
9

    
10
public class StaxStackWriter implements XMLStreamWriter {
11
        
12
        XMLStreamWriter writer;
13
        OutputStream output;
14
        def events = [];
15
        def debug = false
16
        
17
        public StaxStackWriter(File file) {
18
                this(new BufferedOutputStream(new FileOutputStream(file)));
19
        }
20
        
21
        public StaxStackWriter(File file, String encoding) {
22
                this(new BufferedOutputStream(new FileOutputStream(file)), encoding);
23
        }
24
        
25
        public StaxStackWriter(OutputStream output) {
26
                this(output, "UTF-8");//create a new file
27
        }
28
        
29
        public StaxStackWriter(OutputStream output, String encoding) {
30
                XMLOutputFactory factory = XMLOutputFactory.newInstance();
31
                this.output = output
32
                this.writer = factory.createXMLStreamWriter(output, encoding);//create a new file
33
        }
34
        
35
        Charset UTF8 = Charset.forName("UTF-8");
36
        public void write(String txt) {
37
                output.write(txt.getBytes(UTF8))
38
        }
39
        
40
        public void writeEndElements() {
41
                int size = events.size();
42
                for (int i = 0 ; i < size ; i++) {
43
                        writeEndElement();
44
                }
45
                events = [];
46
        }
47
        
48
        public void writeStartElements(def tagsToWrite) {
49
                for (String tag : tagsToWrite)
50
                        writeStartElement(tag);
51
        }
52
        
53
        public def getTagStack() {
54
                return events;
55
        }
56
        
57
        @Override
58
        public void close () throws XMLStreamException {
59
                writer.close();
60
                output.close();
61
        }
62
        
63
        @Override
64
        public void flush () throws XMLStreamException {
65
                writer.flush();
66
        }
67
        
68
        @Override
69
        public NamespaceContext getNamespaceContext() {
70
                return writer.getNamespaceContext();
71
        }
72
        
73
        @Override
74
        public String getPrefix (String uri) throws XMLStreamException {
75
                return writer.getPrefix(uri);
76
        }
77
        
78
        @Override
79
        public Object getProperty (String name) throws IllegalArgumentException {
80
                return writer.getProperty(name);
81
        }
82
        
83
        @Override
84
        public void setDefaultNamespace (String uri) throws XMLStreamException {
85
                writer.setDefaultNamespace(uri);
86
        }
87
        
88
        @Override
89
        public void setNamespaceContext(NamespaceContext context)
90
        throws XMLStreamException {
91
                writer.setNamespaceContext(context);
92
        }
93
        
94
        @Override
95
        public void setPrefix (String prefix, String uri) throws XMLStreamException {
96
                writer.setPrefix(prefix, uri);
97
        }
98
        
99
        @Override
100
        public void writeAttribute (String localName, String value) throws XMLStreamException {
101
                writer.writeAttribute(localName, value);
102
        }
103
        
104
        @Override
105
        public void writeAttribute (String namespaceURI, String localName, String value) throws XMLStreamException {
106
                writer.writeAttribute(namespaceURI, localName, value);
107
        }
108
        
109
        @Override
110
        public void writeAttribute (String prefix, String namespaceURI, String localName, String value) throws XMLStreamException {
111
                writer.writeAttribute(prefix, namespaceURI, localName, value);
112
        }
113
        
114
        @Override
115
        public void writeCData (String data) throws XMLStreamException {
116
                writer.writeCData(data);
117
        }
118
        
119
        @Override
120
        public void writeCharacters (String text) throws XMLStreamException {
121
                writer.writeCharacters(text);
122
        }
123
        
124
        @Override
125
        public void writeCharacters (char[] text, int start, int len) throws XMLStreamException {
126
                writer.writeCharacters(text, start, len);
127
        }
128
        
129
        @Override
130
        public void writeComment (String data) throws XMLStreamException {
131
                writer.writeComment(data);
132
        }
133
        
134
        @Override
135
        public void writeDTD (String dtd) throws XMLStreamException {
136
                writer.writeDTD(dtd);
137
        }
138
        
139
        @Override
140
        public void writeDefaultNamespace(String namespaceURI)
141
        throws XMLStreamException {
142
                writer.writeDefaultNamespace(namespaceURI);
143
        }
144
        
145
        /**
146
         * WARNING THE ELEMENT IS CLOSED
147
         * @param localName
148
         * @param text
149
         * @throws XMLStreamException
150
         */
151
        public void writeElement (String localName, String text) throws XMLStreamException {
152
                writer.writeStartElement(localName);
153
                writer.writeCharacters(text);
154
                writer.writeEndElement();
155
        }
156
        
157
        public void writeElement (String localName, def map, String text) throws XMLStreamException {
158
                writer.writeStartElement(localName);
159
                for (def key : map.keySet()) writeAttribute(key.toString(), map[key].toString());
160
                writer.writeCharacters(text);
161
                writer.writeEndElement();
162
        }
163
        
164
        @Override
165
        public void writeEmptyElement (String localName) throws XMLStreamException {
166
                writer.writeEmptyElement(localName);
167
        }
168
        
169
        public void writeEmptyElement (String localName, def map) throws XMLStreamException {
170
                writer.writeEmptyElement(localName);
171
                for (def key : map.keySet())
172
                        writeAttribute(key.toString(), map[key].toString());
173
        }
174
        
175
        @Override
176
        public void writeEmptyElement(String namespaceURI, String localName)
177
        throws XMLStreamException {
178
                writer.writeEmptyElement(namespaceURI, localName);
179
        }
180
        
181
        @Override
182
        public void writeEmptyElement (String prefix, String localName, String namespaceURI) throws XMLStreamException {
183
                writer.writeEmptyElement(prefix, localName, namespaceURI);
184
        }
185
        
186
        @Override
187
        public void writeEndDocument () throws XMLStreamException {
188
                writer.writeEndDocument();
189
        }
190
        
191
        @Override
192
        public void writeEndElement () throws XMLStreamException {
193
                writer.writeEndElement();
194
                events.remove(events.size()-1)
195
                if (debug) println "END: $events"
196
        }
197
        
198
        @Override
199
        public void writeEntityRef (String name) throws XMLStreamException {
200
                writer.writeEntityRef(name);
201
        }
202
        
203
        @Override
204
        public void writeNamespace(String prefix, String namespaceURI)
205
        throws XMLStreamException {
206
                writer.writeNamespace(prefix, namespaceURI);
207
        }
208
        
209
        @Override
210
        public void writeProcessingInstruction(String target)
211
        throws XMLStreamException {
212
                writer.writeProcessingInstruction(target);
213
        }
214
        
215
        @Override
216
        public void writeProcessingInstruction(String target, String data)
217
        throws XMLStreamException {
218
                writer.writeProcessingInstruction(target, data);
219
        }
220
        
221
        @Override
222
        public void writeStartDocument () throws XMLStreamException {
223
                writer.writeStartDocument();
224
        }
225
        
226
        @Override
227
        public void writeStartDocument (String version) throws XMLStreamException {
228
                writer.writeStartDocument(version);
229
        }
230
        
231
        @Override
232
        public void writeStartDocument(String encoding, String version)
233
        throws XMLStreamException {
234
                writer.writeStartDocument(encoding, version);
235
        }
236
        
237
        @Override
238
        public void writeStartElement (String localName) throws XMLStreamException {
239
                writer.writeStartElement(localName);
240
                events << localName
241
                if (debug) println "START $localName $events"
242
        }
243
        
244
        public void writeStartElement (String localName, def map) throws XMLStreamException {
245
                writeStartElement(localName);
246
                for (def key : map.keySet()) {
247
                        writeAttribute(key.toString(), map[key].toString());
248
                }
249
        }
250
        
251
        @Override
252
        public void writeStartElement(String namespaceURI, String localName)
253
        throws XMLStreamException {
254
                writer.writeStartElement(namespaceURI, localName);
255
                events << localName
256
                if (debug) println "START $localName $events"
257
        }
258
        
259
        @Override
260
        public void writeStartElement(String prefix, String localName,
261
                        String namespaceURI) throws XMLStreamException {
262
                writer.writeStartElement(prefix, localName, namespaceURI);
263
                events << localName
264
                if (debug) println "START $localName $prefix $events"
265
        }
266
}