1 package org.codehaus.plexus.util.xml;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import java.io.IOException;
20 import java.util.ArrayList;
21 import java.util.Collections;
22 import java.util.List;
23 import java.util.Stack;
24
25 import org.codehaus.plexus.util.xml.pull.XmlSerializer;
26
27
28
29
30
31
32
33 public class SerializerXMLWriter implements XMLWriter {
34 private final XmlSerializer serializer;
35
36 private final String namespace;
37
38 private final Stack<String> elements = new Stack<String>();
39
40 private List<Exception> exceptions;
41
42 public SerializerXMLWriter(String namespace, XmlSerializer serializer) {
43 this.serializer = serializer;
44 this.namespace = namespace;
45 }
46
47 @Override
48 public void startElement(String name) {
49 try {
50 serializer.startTag(namespace, name);
51 elements.push(name);
52 } catch (IOException e) {
53 storeException(e);
54 }
55 }
56
57 @Override
58 public void addAttribute(String key, String value) {
59 try {
60 serializer.attribute(namespace, key, value);
61 } catch (IOException e) {
62 storeException(e);
63 }
64 }
65
66 @Override
67 public void writeText(String text) {
68 try {
69 serializer.text(text);
70 } catch (IOException e) {
71 storeException(e);
72 }
73 }
74
75 @Override
76 public void writeMarkup(String text) {
77 try {
78 serializer.cdsect(text);
79 } catch (IOException e) {
80 storeException(e);
81 }
82 }
83
84 @Override
85 public void endElement() {
86 try {
87 serializer.endTag(namespace, elements.pop());
88 } catch (IOException e) {
89 storeException(e);
90 }
91 }
92
93
94
95
96 private void storeException(IOException e) {
97 if (exceptions == null) {
98 exceptions = new ArrayList<Exception>();
99 }
100 exceptions.add(e);
101 }
102
103 public List<Exception> getExceptions() {
104 return exceptions == null ? Collections.<Exception>emptyList() : exceptions;
105 }
106 }