Statistics
| Revision:

ccc / projets / CMC2ELAN / src / StaxStackWriter.groovy @ 2

History | View | Annotate | Download (6.1 kB)

1

    
2

    
3
import javax.xml.namespace.NamespaceContext;
4
import javax.xml.stream.*;
5

    
6
public class StaxStackWriter implements XMLStreamWriter {
7

    
8
        public XMLStreamWriter writer;
9
        public Writer output;
10
        public File infile;
11
        public def events = [];
12
        
13
        public StaxStackWriter(def output) {
14
                XMLOutputFactory factory = XMLOutputFactory.newInstance();
15
                this.output = output
16
                this.writer = factory.createXMLStreamWriter(output);//create a new file
17
        }
18
        
19
        public StaxStackWriter(File infile) {
20
                this(infile.newWriter("UTF-8"))
21
                this.infile = infile
22
        }
23
        
24
        public File getInfile() {
25
                return infile;
26
        }
27

    
28
        public void write(String txt) {
29
                output.write(txt)
30
        }
31
        
32
        public void writeEndElements() {
33
                int size = events.size();
34
                for (int i = 0 ; i < size ; i++) {
35
                        writeEndElement();
36
                }
37
                events = [];
38
        }
39

    
40
        public void writeStartElements(def tagsToWrite) {
41
                for (String tag : tagsToWrite)
42
                        writeStartElement(tag);
43
        }
44

    
45
        public def getTagStack() {
46
                return events;
47
        }
48

    
49
        @Override
50
        public void close () throws XMLStreamException {
51
                writer.close();
52
                output.close();
53
        }
54

    
55
        @Override
56
        public void flush () throws XMLStreamException {
57
                writer.flush();
58

    
59
        }
60

    
61
        @Override
62
        public NamespaceContext getNamespaceContext() {
63
                return writer.getNamespaceContext();
64
        }
65

    
66
        @Override
67
        public String getPrefix (String uri) throws XMLStreamException {
68
                return writer.getPrefix(uri);
69
        }
70

    
71
        @Override
72
        public Object getProperty (String name) throws IllegalArgumentException {
73
                return writer.getProperty(name);
74
        }
75

    
76
        @Override
77
        public void setDefaultNamespace (String uri) throws XMLStreamException {
78
                writer.setDefaultNamespace(uri);
79
        }
80

    
81
        @Override
82
        public void setNamespaceContext(NamespaceContext context)
83
        throws XMLStreamException {
84
                writer.setNamespaceContext(context);
85
        }
86

    
87
        @Override
88
        public void setPrefix (String prefix, String uri) throws XMLStreamException {
89
                writer.setPrefix(prefix, uri);
90
        }
91

    
92
        @Override
93
        public void writeAttribute (String localName, String value) throws XMLStreamException {
94
                writer.writeAttribute(localName, value);
95
        }
96

    
97
        @Override
98
        public void writeAttribute (String namespaceURI, String localName, String value) throws XMLStreamException {
99
                writer.writeAttribute(namespaceURI, localName, value);
100
        }
101

    
102
        @Override
103
        public void writeAttribute (String prefix, String namespaceURI, String localName, String value) throws XMLStreamException {
104
                writer.writeAttribute(prefix, namespaceURI, localName, value);
105
        }
106

    
107
        @Override
108
        public void writeCData (String data) throws XMLStreamException {
109
                writer.writeCData(data);
110
        }
111

    
112
        @Override
113
        public void writeCharacters (String text) throws XMLStreamException {
114
                writer.writeCharacters(text);
115
        }
116

    
117
        @Override
118
        public void writeCharacters (char[] text, int start, int len) throws XMLStreamException {
119
                writer.writeCharacters(text, start, len);
120
        }
121

    
122
        @Override
123
        public void writeComment (String data) throws XMLStreamException {
124
                writer.writeComment(data);
125
        }
126

    
127
        @Override
128
        public void writeDTD (String dtd) throws XMLStreamException {
129
                writer.writeDTD(dtd);
130
        }
131

    
132
        @Override
133
        public void writeDefaultNamespace(String namespaceURI)
134
        throws XMLStreamException {
135
                writer.writeDefaultNamespace(namespaceURI);
136
        }
137

    
138
        public void writeElement (String localName, String text) throws XMLStreamException {
139
                writer.writeStartElement(localName);
140
                writer.writeCharacters(text);
141
                writer.writeEndElement();
142
        }
143
        
144
        public void writeElement (String localName, def map, String text) throws XMLStreamException {
145
                writer.writeStartElement(localName);
146
                for (def key : map.keySet()) writeAttribute(key.toString(), map[key].toString());
147
                writer.writeCharacters(text);
148
                writer.writeEndElement();
149
        }
150

    
151
        @Override
152
        public void writeEmptyElement (String localName) throws XMLStreamException {
153
                writer.writeEmptyElement(localName);
154
        }
155

    
156
        public void writeEmptyElement (String localName, def map) throws XMLStreamException {
157
                writer.writeEmptyElement(localName);
158
                for (def key : map.keySet())
159
                        writeAttribute(key.toString(), map[key].toString());
160
        }
161

    
162
        @Override
163
        public void writeEmptyElement(String namespaceURI, String localName)
164
        throws XMLStreamException {
165
                writer.writeEmptyElement(namespaceURI, localName);
166
        }
167

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

    
173
        @Override
174
        public void writeEndDocument () throws XMLStreamException {
175
                writer.writeEndDocument();
176
        }
177

    
178
        @Override
179
        public void writeEndElement () throws XMLStreamException {
180
                writer.writeEndElement();
181
                events.pop()
182

    
183
        }
184

    
185
        @Override
186
        public void writeEntityRef (String name) throws XMLStreamException {
187
                writer.writeEntityRef(name);
188
        }
189

    
190
        @Override
191
        public void writeNamespace(String prefix, String namespaceURI)
192
        throws XMLStreamException {
193
                writer.writeNamespace(prefix, namespaceURI);
194
        }
195

    
196
        @Override
197
        public void writeProcessingInstruction(String target)
198
        throws XMLStreamException {
199
                writer.writeProcessingInstruction(target);
200
        }
201

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

    
208
        @Override
209
        public void writeStartDocument () throws XMLStreamException {
210
                writer.writeStartDocument();
211

    
212
        }
213

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

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

    
225
        @Override
226
        public void writeStartElement (String localName) throws XMLStreamException {
227
                writer.writeStartElement(localName);
228
                events << localName
229

    
230
        }
231
        
232
        public void writeStartElement (String localName, def map) throws XMLStreamException {
233
                writeStartElement(localName);
234
                for (def key : map.keySet()) {
235
                        writeAttribute(key.toString(), map[key].toString());
236
                }
237
        }
238

    
239
        @Override
240
        public void writeStartElement(String namespaceURI, String localName)
241
        throws XMLStreamException {
242
                writer.writeStartElement(namespaceURI, localName);
243
                events << localName
244
        }
245

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