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