Statistics
| Revision:

root / tmp / org.txm.groovy.core / src / groovy / org / txm / importer / StaxStackWriter.groovy @ 479

History | View | Annotate | Download (6.5 kB)

1
package org.txm.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
        
16
        public StaxStackWriter(File file) {
17
                this(new BufferedOutputStream(new FileOutputStream(file)));
18
        }
19
        
20
        public StaxStackWriter(File file, String encoding) {
21
                this(new BufferedOutputStream(new FileOutputStream(file)), encoding);
22
        }
23
        
24
        public StaxStackWriter(OutputStream output) {
25
                this(output, "UTF-8");//create a new file
26
        }
27
        
28
        public StaxStackWriter(OutputStream output, String encoding) {
29
                XMLOutputFactory factory = XMLOutputFactory.newInstance();
30
                this.output = output
31
                this.writer = factory.createXMLStreamWriter(output, encoding);//create a new file
32
        }
33

    
34
        Charset UTF8 = Charset.forName("UTF-8");
35
        public void write(String txt) {
36
                output.write(txt.getBytes(UTF8))
37
        }
38
        
39
        public void writeEndElements() {
40
                int size = events.size();
41
                for (int i = 0 ; i < size ; i++) {
42
                        writeEndElement();
43
                }
44
                events = [];
45
        }
46

    
47
        public void writeStartElements(def tagsToWrite) {
48
                for (String tag : tagsToWrite)
49
                        writeStartElement(tag);
50
        }
51

    
52
        public def getTagStack() {
53
                return events;
54
        }
55

    
56
        @Override
57
        public void close () throws XMLStreamException {
58
                writer.close();
59
                output.close();
60
        }
61

    
62
        @Override
63
        public void flush () throws XMLStreamException {
64
                writer.flush();
65

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

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

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

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

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

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

    
185
        @Override
186
        public void writeEndElement () throws XMLStreamException {
187
                writer.writeEndElement();
188
                events.pop()
189

    
190
        }
191

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

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

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

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

    
215
        @Override
216
        public void writeStartDocument () throws XMLStreamException {
217
                writer.writeStartDocument();
218

    
219
        }
220

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

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

    
232
        @Override
233
        public void writeStartElement (String localName) throws XMLStreamException {
234
                writer.writeStartElement(localName);
235
                events << localName
236

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

    
246
        @Override
247
        public void writeStartElement(String namespaceURI, String localName)
248
        throws XMLStreamException {
249
                writer.writeStartElement(namespaceURI, localName);
250
                events << localName
251
        }
252

    
253
        @Override
254
        public void writeStartElement(String prefix, String localName,
255
        String namespaceURI) throws XMLStreamException {
256
                writer.writeStartElement(prefix, localName, namespaceURI);
257
                events << localName
258
        }
259
}