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 org.codehaus.plexus.util.xml.pull.XmlSerializer;
20
21 import java.io.IOException;
22 import java.util.ArrayList;
23 import java.util.Collections;
24 import java.util.List;
25 import java.util.Stack;
26
27
28
29
30
31
32
33 public class SerializerXMLWriter
34 implements XMLWriter
35 {
36 private final XmlSerializer serializer;
37
38 private final String namespace;
39
40 private final Stack<String> elements = new Stack<String>();
41
42 private List<Exception> exceptions;
43
44 public SerializerXMLWriter( String namespace, XmlSerializer serializer )
45 {
46 this.serializer = serializer;
47 this.namespace = namespace;
48 }
49
50 @Override
51 public void startElement( String name )
52 {
53 try
54 {
55 serializer.startTag( namespace, name );
56 elements.push( name );
57 }
58 catch ( IOException e )
59 {
60 storeException( e );
61 }
62 }
63
64 @Override
65 public void addAttribute( String key, String value )
66 {
67 try
68 {
69 serializer.attribute( namespace, key, value );
70 }
71 catch ( IOException e )
72 {
73 storeException( e );
74 }
75 }
76
77 @Override
78 public void writeText( String text )
79 {
80 try
81 {
82 serializer.text( text );
83 }
84 catch ( IOException e )
85 {
86 storeException( e );
87 }
88 }
89
90 @Override
91 public void writeMarkup( String text )
92 {
93 try
94 {
95 serializer.cdsect( text );
96 }
97 catch ( IOException e )
98 {
99 storeException( e );
100 }
101 }
102
103 @Override
104 public void endElement()
105 {
106 try
107 {
108 serializer.endTag( namespace, elements.pop() );
109 }
110 catch ( IOException e )
111 {
112 storeException( e );
113 }
114 }
115
116
117
118
119 private void storeException( IOException e )
120 {
121 if ( exceptions == null )
122 {
123 exceptions = new ArrayList<Exception>();
124 }
125 exceptions.add( e );
126 }
127
128 public List<Exception> getExceptions()
129 {
130 return exceptions == null ? Collections.<Exception>emptyList() : exceptions;
131 }
132
133 }