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 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   * Write to an MXSerializer.
29   *
30   * @author <a href="mailto:brett@codehaus.org">Brett Porter</a>
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       * @todo Maybe the interface should allow IOExceptions on each?
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 }