Statistics
| Revision:

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

History | View | Annotate | Download (6.7 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

    
69
        @Override
70
        public NamespaceContext getNamespaceContext() {
71
                return writer.getNamespaceContext();
72
        }
73

    
74
        @Override
75
        public String getPrefix (String uri) throws XMLStreamException {
76
                return writer.getPrefix(uri);
77
        }
78

    
79
        @Override
80
        public Object getProperty (String name) throws IllegalArgumentException {
81
                return writer.getProperty(name);
82
        }
83

    
84
        @Override
85
        public void setDefaultNamespace (String uri) throws XMLStreamException {
86
                writer.setDefaultNamespace(uri);
87
        }
88

    
89
        @Override
90
        public void setNamespaceContext(NamespaceContext context)
91
        throws XMLStreamException {
92
                writer.setNamespaceContext(context);
93
        }
94

    
95
        @Override
96
        public void setPrefix (String prefix, String uri) throws XMLStreamException {
97
                writer.setPrefix(prefix, uri);
98
        }
99

    
100
        @Override
101
        public void writeAttribute (String localName, String value) throws XMLStreamException {
102
                writer.writeAttribute(localName, value);
103
        }
104

    
105
        @Override
106
        public void writeAttribute (String namespaceURI, String localName, String value) throws XMLStreamException {
107
                writer.writeAttribute(namespaceURI, localName, value);
108
        }
109

    
110
        @Override
111
        public void writeAttribute (String prefix, String namespaceURI, String localName, String value) throws XMLStreamException {
112
                writer.writeAttribute(prefix, namespaceURI, localName, value);
113
        }
114

    
115
        @Override
116
        public void writeCData (String data) throws XMLStreamException {
117
                writer.writeCData(data);
118
        }
119

    
120
        @Override
121
        public void writeCharacters (String text) throws XMLStreamException {
122
                writer.writeCharacters(text);
123
        }
124

    
125
        @Override
126
        public void writeCharacters (char[] text, int start, int len) throws XMLStreamException {
127
                writer.writeCharacters(text, start, len);
128
        }
129

    
130
        @Override
131
        public void writeComment (String data) throws XMLStreamException {
132
                writer.writeComment(data);
133
        }
134

    
135
        @Override
136
        public void writeDTD (String dtd) throws XMLStreamException {
137
                writer.writeDTD(dtd);
138
        }
139

    
140
        @Override
141
        public void writeDefaultNamespace(String namespaceURI)
142
        throws XMLStreamException {
143
                writer.writeDefaultNamespace(namespaceURI);
144
        }
145

    
146
        public void writeElement (String localName, String text) throws XMLStreamException {
147
                writer.writeStartElement(localName);
148
                writer.writeCharacters(text);
149
                writer.writeEndElement();
150
        }
151
        
152
        public void writeElement (String localName, def map, String text) throws XMLStreamException {
153
                writer.writeStartElement(localName);
154
                for (def key : map.keySet()) writeAttribute(key.toString(), map[key].toString());
155
                writer.writeCharacters(text);
156
                writer.writeEndElement();
157
        }
158

    
159
        @Override
160
        public void writeEmptyElement (String localName) throws XMLStreamException {
161
                writer.writeEmptyElement(localName);
162
        }
163

    
164
        public void writeEmptyElement (String localName, def map) throws XMLStreamException {
165
                writer.writeEmptyElement(localName);
166
                for (def key : map.keySet())
167
                        writeAttribute(key.toString(), map[key].toString());
168
        }
169

    
170
        @Override
171
        public void writeEmptyElement(String namespaceURI, String localName)
172
        throws XMLStreamException {
173
                writer.writeEmptyElement(namespaceURI, localName);
174
        }
175

    
176
        @Override
177
        public void writeEmptyElement (String prefix, String localName, String namespaceURI) throws XMLStreamException {
178
                writer.writeEmptyElement(prefix, localName, namespaceURI);
179
        }
180

    
181
        @Override
182
        public void writeEndDocument () throws XMLStreamException {
183
                writer.writeEndDocument();
184
        }
185

    
186
        @Override
187
        public void writeEndElement () throws XMLStreamException {
188
                writer.writeEndElement();
189
                events.remove(events.size()-1)
190
                if (debug) println "END: $events"
191

    
192
        }
193

    
194
        @Override
195
        public void writeEntityRef (String name) throws XMLStreamException {
196
                writer.writeEntityRef(name);
197
        }
198

    
199
        @Override
200
        public void writeNamespace(String prefix, String namespaceURI)
201
        throws XMLStreamException {
202
                writer.writeNamespace(prefix, namespaceURI);
203
        }
204

    
205
        @Override
206
        public void writeProcessingInstruction(String target)
207
        throws XMLStreamException {
208
                writer.writeProcessingInstruction(target);
209
        }
210

    
211
        @Override
212
        public void writeProcessingInstruction(String target, String data)
213
        throws XMLStreamException {
214
                writer.writeProcessingInstruction(target, data);
215
        }
216

    
217
        @Override
218
        public void writeStartDocument () throws XMLStreamException {
219
                writer.writeStartDocument();
220

    
221
        }
222

    
223
        @Override
224
        public void writeStartDocument (String version) throws XMLStreamException {
225
                writer.writeStartDocument(version);
226
        }
227

    
228
        @Override
229
        public void writeStartDocument(String encoding, String version)
230
        throws XMLStreamException {
231
                writer.writeStartDocument(encoding, version);
232
        }
233

    
234
        @Override
235
        public void writeStartElement (String localName) throws XMLStreamException {
236
                writer.writeStartElement(localName);
237
                events << localName
238
                if (debug) println "START $localName $events"
239

    
240
        }
241
        
242
        public void writeStartElement (String localName, def map) throws XMLStreamException {
243
                writeStartElement(localName);
244
                for (def key : map.keySet()) {
245
                        writeAttribute(key.toString(), map[key].toString());
246
                }
247
        }
248

    
249
        @Override
250
        public void writeStartElement(String namespaceURI, String localName)
251
        throws XMLStreamException {
252
                writer.writeStartElement(namespaceURI, localName);
253
                events << localName
254
                if (debug) println "START $localName $events"
255
        }
256

    
257
        @Override
258
        public void writeStartElement(String prefix, String localName,
259
        String namespaceURI) throws XMLStreamException {
260
                writer.writeStartElement(prefix, localName, namespaceURI);
261
                events << localName
262
                if (debug) println "START $localName $prefix $events"
263
        }
264
}