Coverage Report - org.codehaus.plexus.util.xml.XmlStreamWriter
 
Classes in this File Line Coverage Branch Coverage Complexity
XmlStreamWriter
87%
41/47
68%
15/22
2.571
 
 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.File;
 20  
 import java.io.FileNotFoundException;
 21  
 import java.io.FileOutputStream;
 22  
 import java.io.IOException;
 23  
 import java.io.OutputStream;
 24  
 import java.io.OutputStreamWriter;
 25  
 import java.io.StringWriter;
 26  
 import java.io.Writer;
 27  
 import java.util.Locale;
 28  
 import java.util.regex.Matcher;
 29  
 import java.util.regex.Pattern;
 30  
 
 31  
 /**
 32  
  * Character stream that handles (or at least attempts to) all the necessary Voodo to figure out the charset encoding of
 33  
  * the XML document written to the stream.
 34  
  * 
 35  
  * @author <a href="mailto:hboutemy@codehaus.org">Herve Boutemy</a>
 36  
  * @version $Id$
 37  
  * @since 1.4.4
 38  
  */
 39  
 public class XmlStreamWriter
 40  
     extends Writer
 41  
 {
 42  
     private static final int BUFFER_SIZE = 4096;
 43  
 
 44  37
     private StringWriter xmlPrologWriter = new StringWriter( BUFFER_SIZE );
 45  
 
 46  
     private OutputStream out;
 47  
 
 48  
     private Writer writer;
 49  
 
 50  
     private String encoding;
 51  
 
 52  
     public XmlStreamWriter( OutputStream out )
 53  37
     {
 54  37
         this.out = out;
 55  37
     }
 56  
 
 57  
     public XmlStreamWriter( File file )
 58  
         throws FileNotFoundException
 59  
     {
 60  2
         this( new FileOutputStream( file ) );
 61  2
     }
 62  
 
 63  
     public String getEncoding()
 64  
     {
 65  0
         return encoding;
 66  
     }
 67  
 
 68  
     public void close()
 69  
         throws IOException
 70  
     {
 71  37
         if ( writer == null )
 72  
         {
 73  2
             encoding = "UTF-8";
 74  2
             writer = new OutputStreamWriter( out, encoding );
 75  2
             writer.write( xmlPrologWriter.toString() );
 76  
         }
 77  37
         writer.close();
 78  37
     }
 79  
 
 80  
     public void flush()
 81  
         throws IOException
 82  
     {
 83  3
         if ( writer != null )
 84  
         {
 85  0
             writer.flush();
 86  
         }
 87  3
     }
 88  
 
 89  
     private void detectEncoding( char[] cbuf, int off, int len )
 90  
         throws IOException
 91  
     {
 92  55
         int size = len;
 93  55
         StringBuffer xmlProlog = xmlPrologWriter.getBuffer();
 94  55
         if ( xmlProlog.length() + len > BUFFER_SIZE )
 95  
         {
 96  0
             size = BUFFER_SIZE - xmlProlog.length();
 97  
         }
 98  55
         xmlPrologWriter.write( cbuf, off, size );
 99  
 
 100  
         // try to determine encoding
 101  55
         if ( xmlProlog.length() >= 5 )
 102  
         {
 103  35
             if ( xmlProlog.substring( 0, 5 ).equals( "<?xml" ) )
 104  
             {
 105  
                 // try to extract encoding from XML prolog
 106  10
                 int xmlPrologEnd = xmlProlog.indexOf( "?>" );
 107  10
                 if ( xmlPrologEnd > 0 )
 108  
                 {
 109  
                     // ok, full XML prolog written: let's extract encoding
 110  10
                     Matcher m = ENCODING_PATTERN.matcher( xmlProlog.substring( 0, xmlPrologEnd ) );
 111  10
                     if ( m.find() )
 112  
                     {
 113  9
                         encoding = m.group( 1 ).toUpperCase( Locale.ENGLISH );
 114  9
                         encoding = encoding.substring( 1, encoding.length() - 1 );
 115  
                     }
 116  
                     else
 117  
                     {
 118  
                         // no encoding found in XML prolog: using default encoding
 119  1
                         encoding = "UTF-8";
 120  
                     }
 121  10
                 }
 122  
                 else
 123  
                 {
 124  0
                     if ( xmlProlog.length() >= BUFFER_SIZE )
 125  
                     {
 126  
                         // no encoding found in first characters: using default encoding
 127  0
                         encoding = "UTF-8";
 128  
                     }
 129  
                 }
 130  10
             }
 131  
             else
 132  
             {
 133  
                 // no XML prolog: using default encoding
 134  25
                 encoding = "UTF-8";
 135  
             }
 136  35
             if ( encoding != null )
 137  
             {
 138  
                 // encoding has been chosen: let's do it
 139  35
                 xmlPrologWriter = null;
 140  35
                 writer = new OutputStreamWriter( out, encoding );
 141  35
                 writer.write( xmlProlog.toString() );
 142  35
                 if ( len > size )
 143  
                 {
 144  0
                     writer.write( cbuf, off + size, len - size );
 145  
                 }
 146  
             }
 147  
         }
 148  55
     }
 149  
 
 150  
     public void write( char[] cbuf, int off, int len )
 151  
         throws IOException
 152  
     {
 153  3297
         if ( xmlPrologWriter != null )
 154  
         {
 155  55
             detectEncoding( cbuf, off, len );
 156  
         }
 157  
         else
 158  
         {
 159  3242
             writer.write( cbuf, off, len );
 160  
         }
 161  3297
     }
 162  
 
 163  1
     static final Pattern ENCODING_PATTERN = XmlReader.ENCODING_PATTERN;
 164  
 }