View Javadoc
1   package org.codehaus.plexus.util.xml;
2   
3   /*
4    * Copyright The Codehaus Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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   * Write to an MXSerializer.
29   *
30   * @author <a href="mailto:brett@codehaus.org">Brett Porter</a>
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      * @todo Maybe the interface should allow IOExceptions on each?
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 }