Statistics
| Revision:

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

History | View | Annotate | Download (6.7 kB)

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