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   * @version $Id$
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      public void startElement( String name )
51      {
52          try
53          {
54              serializer.startTag( namespace, name );
55              elements.push( name );
56          }
57          catch ( IOException e )
58          {
59              storeException( e );
60          }
61      }
62  
63      public void addAttribute( String key, String value )
64      {
65          try
66          {
67              serializer.attribute( namespace, key, value );
68          }
69          catch ( IOException e )
70          {
71              storeException( e );
72          }
73      }
74  
75      public void writeText( String text )
76      {
77          try
78          {
79              serializer.text( text );
80          }
81          catch ( IOException e )
82          {
83              storeException( e );
84          }
85      }
86  
87      public void writeMarkup( String text )
88      {
89          try
90          {
91              serializer.cdsect( text );
92          }
93          catch ( IOException e )
94          {
95              storeException( e );
96          }
97      }
98  
99      public void endElement()
100     {
101         try
102         {
103             serializer.endTag( namespace, (String) elements.pop() );
104         }
105         catch ( IOException e )
106         {
107             storeException( e );
108         }
109     }
110 
111     /**
112      * @todo Maybe the interface should allow IOExceptions on each?
113      */
114     private void storeException( IOException e )
115     {
116         if ( exceptions == null )
117         {
118             exceptions = new ArrayList<Exception>();
119         }
120         exceptions.add( e );
121     }
122 
123     public List<Exception> getExceptions()
124     {
125         return exceptions == null ? Collections.<Exception>emptyList() : exceptions;
126     }
127 
128 }