Coverage Report - org.apache.maven.surefire.report.PrettyPrintXMLWriter
 
Classes in this File Line Coverage Branch Coverage Complexity
PrettyPrintXMLWriter
0%
0/94
0%
0/38
2.357
 
 1  
 package org.apache.maven.surefire.report;
 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.PrintWriter;
 23  
 import java.util.LinkedList;
 24  
 import org.codehaus.plexus.util.xml.XMLWriter;
 25  
 
 26  
 public class PrettyPrintXMLWriter
 27  
     implements XMLWriter
 28  
 {
 29  
     private final PrintWriter writer;
 30  
 
 31  0
     private final LinkedList elementStack = new LinkedList();
 32  
 
 33  
     private boolean tagInProgress;
 34  
 
 35  
     private int depth;
 36  
 
 37  
     private final String lineIndenter;
 38  
 
 39  
     private final String encoding;
 40  
 
 41  
     private final String docType;
 42  
 
 43  
     private boolean readyForNewLine;
 44  
 
 45  
     private boolean tagIsEmpty;
 46  
 
 47  
     public PrettyPrintXMLWriter( PrintWriter writer )
 48  
     {
 49  0
         this( writer, null, null );
 50  0
     }
 51  
 
 52  
     private PrettyPrintXMLWriter( PrintWriter writer, String lineIndenter, String encoding, String doctype )
 53  0
     {
 54  0
         this.writer = writer;
 55  
 
 56  0
         this.lineIndenter = lineIndenter;
 57  
 
 58  0
         this.encoding = encoding;
 59  
 
 60  0
         this.docType = doctype;
 61  
 
 62  0
         if ( docType != null || encoding != null )
 63  
         {
 64  0
             writeDocumentHeaders();
 65  
         }
 66  0
     }
 67  
 
 68  
     private PrettyPrintXMLWriter( PrintWriter writer, String encoding, String doctype )
 69  
     {
 70  0
         this( writer, "  ", encoding, doctype );
 71  0
     }
 72  
 
 73  
     public void startElement( String name )
 74  
     {
 75  0
         tagIsEmpty = false;
 76  
 
 77  0
         finishTag();
 78  
 
 79  0
         write( "<" );
 80  
 
 81  0
         write( name );
 82  
 
 83  0
         elementStack.addLast( name );
 84  
 
 85  0
         tagInProgress = true;
 86  
 
 87  0
         depth++;
 88  
 
 89  0
         readyForNewLine = true;
 90  
 
 91  0
         tagIsEmpty = true;
 92  0
     }
 93  
 
 94  
     public void writeText( String text )
 95  
     {
 96  0
         writeText( text, true );
 97  0
     }
 98  
 
 99  
     public void writeMarkup( String text )
 100  
     {
 101  0
         writeText( text, false );
 102  0
     }
 103  
 
 104  
     private void writeText( String text, boolean escapeXml )
 105  
     {
 106  0
         readyForNewLine = false;
 107  
 
 108  0
         tagIsEmpty = false;
 109  
 
 110  0
         finishTag();
 111  
 
 112  0
         if ( escapeXml )
 113  
         {
 114  0
             text = escapeXml( text );
 115  
         }
 116  
 
 117  0
         write( text );
 118  0
     }
 119  
 
 120  
     private static String escapeXml( String text )
 121  
     {
 122  0
         StringBuffer sb = new StringBuffer( text.length() * 2 );
 123  0
         for ( int i = 0; i < text.length(); i++ )
 124  
         {
 125  0
             char c = text.charAt( i );
 126  0
             if ( c < 32 )
 127  
             {
 128  0
                 if ( c == '\n' || c == '\r' || c == '\t' )
 129  
                 {
 130  0
                     sb.append( c );
 131  
                 }
 132  
                 else
 133  
                 {
 134  
                     // uh-oh!  This character is illegal in XML 1.0!
 135  
                     // http://www.w3.org/TR/1998/REC-xml-19980210#charsets
 136  
                     // we're going to deliberately doubly-XML escape it...
 137  
                     // there's nothing better we can do! :-(
 138  
                     // SUREFIRE-456
 139  0
                     sb.append( "&amp;#" ).append( (int) c ).append( ';' );
 140  
                 }
 141  
             }
 142  0
             else if ( c == '<' )
 143  
             {
 144  0
                 sb.append( "&lt;" );
 145  
             }
 146  0
             else if ( c == '>' )
 147  
             {
 148  0
                 sb.append( "&gt;" );
 149  
             }
 150  0
             else if ( c == '&' )
 151  
             {
 152  0
                 sb.append( "&amp;" );
 153  
             }
 154  0
             else if ( c == '"' )
 155  
             {
 156  0
                 sb.append( "&quot;" );
 157  
             }
 158  0
             else if ( c == '\'' )
 159  
             {
 160  0
                 sb.append( "&apos;" );
 161  
             }
 162  
             else
 163  
             {
 164  0
                 sb.append( c );
 165  
             }
 166  
         }
 167  0
         return sb.toString();
 168  
     }
 169  
 
 170  
     public void addAttribute( String key, String value )
 171  
     {
 172  0
         write( " " );
 173  
 
 174  0
         write( key );
 175  
 
 176  0
         write( "=\"" );
 177  
 
 178  0
         write( escapeXml( value ) );
 179  
 
 180  0
         write( "\"" );
 181  0
     }
 182  
 
 183  
     public void endElement()
 184  
     {
 185  0
         depth--;
 186  
 
 187  0
         if ( tagIsEmpty )
 188  
         {
 189  0
             write( "/" );
 190  
 
 191  0
             readyForNewLine = false;
 192  
 
 193  0
             finishTag();
 194  
 
 195  0
             elementStack.removeLast();
 196  
         }
 197  
         else
 198  
         {
 199  0
             finishTag();
 200  
 
 201  0
             write( "</" + elementStack.removeLast() + ">" );
 202  
         }
 203  
 
 204  0
         readyForNewLine = true;
 205  0
     }
 206  
 
 207  
     private void write( String str )
 208  
     {
 209  0
         writer.write( str );
 210  0
     }
 211  
 
 212  
     private void finishTag()
 213  
     {
 214  0
         if ( tagInProgress )
 215  
         {
 216  0
             write( ">" );
 217  
         }
 218  
 
 219  0
         tagInProgress = false;
 220  
 
 221  0
         if ( readyForNewLine )
 222  
         {
 223  0
             endOfLine();
 224  
         }
 225  0
         readyForNewLine = false;
 226  
 
 227  0
         tagIsEmpty = false;
 228  0
     }
 229  
 
 230  
     protected void endOfLine()
 231  
     {
 232  0
         write( "\n" );
 233  
 
 234  0
         for ( int i = 0; i < depth; i++ )
 235  
         {
 236  0
             write( lineIndenter );
 237  
         }
 238  0
     }
 239  
 
 240  
     private void writeDocumentHeaders()
 241  
     {
 242  0
         write( "<?xml version=\"1.0\"" );
 243  
 
 244  0
         if ( encoding != null )
 245  
         {
 246  0
             write( " encoding=\"" + encoding + "\"" );
 247  
         }
 248  
 
 249  0
         write( "?>" );
 250  
 
 251  0
         endOfLine();
 252  
 
 253  0
         if ( docType != null )
 254  
         {
 255  0
             write( "<!DOCTYPE " );
 256  
 
 257  0
             write( docType );
 258  
 
 259  0
             write( ">" );
 260  
 
 261  0
             endOfLine();
 262  
         }
 263  0
     }
 264  
 }