Coverage Report - org.codehaus.plexus.util.xml.XmlStreamWriter
 
Classes in this File Line Coverage Branch Coverage Complexity
XmlStreamWriter
0%
0/45
0%
0/22
2,571
 
 1  
 package org.codehaus.plexus.util.xml;
 2  
 
 3  
 /*
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with the License.  You may obtain a copy of the License at
 11  
  *
 12  
  *   http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 14  
  * Unless required by applicable law or agreed to in writing,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.
 20  
  */
 21  
 
 22  
 import java.io.File;
 23  
 import java.io.FileNotFoundException;
 24  
 import java.io.FileOutputStream;
 25  
 import java.io.IOException;
 26  
 import java.io.OutputStream;
 27  
 import java.io.OutputStreamWriter;
 28  
 import java.io.StringWriter;
 29  
 import java.io.Writer;
 30  
 import java.util.regex.Matcher;
 31  
 import java.util.regex.Pattern;
 32  
 
 33  
 /**
 34  
  * Character stream that handles (or at least attemtps to) all the necessary Voodo to figure out the charset encoding of
 35  
  * the XML document written to the stream.
 36  
  * @author <a href="mailto:hboutemy@codehaus.org">Herve Boutemy</a>
 37  
  * @version $Id: XmlStreamWriter.java 591654 2007-11-03 17:14:56Z dennisl $
 38  
  * @since 1.4.4
 39  
  * @deprecated TO BE REMOVED from here when plexus-utils is upgraded to 1.4.5+ (and prerequisite upgraded to Maven 2.0.6)
 40  
  */
 41  
 public class XmlStreamWriter
 42  
 extends Writer
 43  
 {
 44  
     private static final int BUFFER_SIZE = 4096;
 45  
     
 46  0
     private StringWriter xmlPrologWriter = new StringWriter( BUFFER_SIZE );
 47  
     
 48  
     private OutputStream out;
 49  
 
 50  
     private Writer writer;
 51  
 
 52  
     private String encoding;
 53  
     
 54  
     public XmlStreamWriter( OutputStream out )
 55  0
     {
 56  0
         this.out = out;
 57  0
     }
 58  
     
 59  
     public XmlStreamWriter( File file )
 60  
     throws FileNotFoundException
 61  
     {
 62  0
         this( new FileOutputStream( file ) );
 63  0
     }
 64  
     
 65  
     public String getEncoding()
 66  
     {
 67  0
         return encoding;
 68  
     }
 69  
 
 70  
     public void close()
 71  
     throws IOException
 72  
     {
 73  0
         if ( writer == null )
 74  
         {
 75  0
             encoding = "UTF-8";
 76  0
             writer = new OutputStreamWriter( out, encoding );
 77  0
             writer.write( xmlPrologWriter.toString() );
 78  
         }
 79  0
         writer.close();
 80  0
     }
 81  
 
 82  
     public void flush()
 83  
     throws IOException
 84  
     {
 85  0
         if ( writer != null )
 86  
         {
 87  0
             writer.flush();
 88  
         }
 89  0
     }
 90  
 
 91  
     private void detectEncoding( char[] cbuf, int off, int len )
 92  
     throws IOException
 93  
     {
 94  0
         int size = len;
 95  0
         StringBuffer xmlProlog = xmlPrologWriter.getBuffer();
 96  0
         if ( xmlProlog.length() + len > BUFFER_SIZE )
 97  
         {
 98  0
             size = BUFFER_SIZE - xmlProlog.length();
 99  
         }
 100  0
         xmlPrologWriter.write( cbuf, off, size );
 101  
         
 102  
         // try to determine encoding
 103  0
         if ( xmlProlog.length() >= 5 )
 104  
         {
 105  0
             if ( xmlProlog.substring( 0, 5 ).equals( "<?xml" ) )
 106  
             {
 107  
                 // try to extract encoding from XML prolog
 108  0
                 int xmlPrologEnd = xmlProlog.indexOf( "?>" );
 109  0
                 if ( xmlPrologEnd > 0 )
 110  
                 {
 111  
                     // ok, full XML prolog written: let's extract encoding
 112  0
                     Matcher m = ENCODING_PATTERN.matcher( xmlProlog.substring( 0, xmlPrologEnd ) );
 113  0
                     if ( m.find() )
 114  
                     {
 115  0
                         encoding = m.group( 1 ).toUpperCase();
 116  0
                         encoding = encoding.substring( 1, encoding.length() - 1 );
 117  
                     }
 118  
                     else
 119  
                     {
 120  
                         // no encoding found in XML prolog: using default encoding
 121  0
                         encoding = "UTF-8";
 122  
                     }
 123  
                 }
 124  
                 else
 125  
                 {
 126  0
                     if ( xmlProlog.length() >= BUFFER_SIZE )
 127  
                     {
 128  
                         // no encoding found in first characters: using default encoding
 129  0
                         encoding = "UTF-8";
 130  
                     }
 131  
                 }
 132  
             }
 133  
             else
 134  
             {
 135  
                 // no XML prolog: using default encoding
 136  0
                 encoding = "UTF-8";
 137  
             }
 138  0
             if ( encoding != null )
 139  
             {
 140  
                 // encoding has been chosen: let's do it
 141  0
                 xmlPrologWriter = null;
 142  0
                 writer = new OutputStreamWriter( out, encoding );
 143  0
                 writer.write( xmlProlog.toString() );
 144  0
                 if ( len > size )
 145  
                 {
 146  0
                     writer.write( cbuf, off + size, len - size );
 147  
                 }
 148  
             }
 149  
         }
 150  0
     }
 151  
     
 152  
     public void write( char[] cbuf, int off, int len )
 153  
     throws IOException
 154  
     {
 155  0
         if ( xmlPrologWriter != null )
 156  
         {
 157  0
             detectEncoding( cbuf, off, len );
 158  
         }
 159  
         else
 160  
         {
 161  0
             writer.write( cbuf, off, len );
 162  
         }
 163  0
     }
 164  
 
 165  0
     static final Pattern ENCODING_PATTERN = XmlReader.ENCODING_PATTERN;
 166  
 }