Coverage Report - org.apache.maven.tools.plugin.generator.PrettyPrintXMLWriter
 
Classes in this File Line Coverage Branch Coverage Complexity
PrettyPrintXMLWriter
80% 
100% 
1.056
 
 1  
 package org.apache.maven.tools.plugin.generator;
 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.io.Writer;
 24  
 
 25  
 /**
 26  
  * Copied from plexus-utils 1.3-SNAPSHOT as we can't upgrade it yet.
 27  
  * This class can be removed when a newer version of plexus-utils is included with Maven
 28  
  *
 29  
  * @see org.codehaus.plexus.util.xml.PrettyPrintXMLWriter
 30  
  */
 31  
 public class PrettyPrintXMLWriter
 32  
     extends org.codehaus.plexus.util.xml.PrettyPrintXMLWriter
 33  
 {
 34  
 
 35  1
     private static final String LS = System.getProperty( "line.separator" );
 36  
 
 37  
     private PrintWriter writer;
 38  
 
 39  
     private String lineIndenter;
 40  
 
 41  
     private int depth;
 42  
 
 43  
     public PrettyPrintXMLWriter( PrintWriter writer, String lineIndenter )
 44  
     {
 45  0
         this( writer, lineIndenter, null, null );
 46  0
     }
 47  
 
 48  
     public PrettyPrintXMLWriter( Writer writer, String lineIndenter )
 49  
     {
 50  0
         this( new PrintWriter( writer ), lineIndenter );
 51  0
     }
 52  
 
 53  
     public PrettyPrintXMLWriter( PrintWriter writer )
 54  
     {
 55  1
         this( writer, null, null );
 56  1
     }
 57  
 
 58  
     public PrettyPrintXMLWriter( Writer writer )
 59  
     {
 60  1
         this( new PrintWriter( writer ) );
 61  1
     }
 62  
 
 63  
     public PrettyPrintXMLWriter( PrintWriter writer, String lineIndenter, String encoding, String doctype )
 64  
     {
 65  1
         super( writer, lineIndenter, encoding, doctype );
 66  
 
 67  1
         setWriter( writer );
 68  
 
 69  1
         setLineIndenter( lineIndenter );
 70  1
     }
 71  
 
 72  
     public PrettyPrintXMLWriter( Writer writer, String lineIndenter, String encoding, String doctype )
 73  
     {
 74  0
         this( new PrintWriter( writer ), lineIndenter, encoding, doctype );
 75  0
     }
 76  
 
 77  
     public PrettyPrintXMLWriter( PrintWriter writer, String encoding, String doctype )
 78  
     {
 79  1
         this( writer, "  ", encoding, doctype );
 80  1
     }
 81  
 
 82  
     public PrettyPrintXMLWriter( Writer writer, String encoding, String doctype )
 83  
     {
 84  0
         this( new PrintWriter( writer ), encoding, doctype );
 85  0
     }
 86  
 
 87  
     /**
 88  
      * Write a string to the underlying writer
 89  
      *
 90  
      * @param str
 91  
      */
 92  
     private void write( String str )
 93  
     {
 94  189
         getWriter().write( str );
 95  189
     }
 96  
 
 97  
     /**
 98  
      * Get the string used as line indenter
 99  
      *
 100  
      * @return the line indenter
 101  
      */
 102  
     protected String getLineIndenter()
 103  
     {
 104  152
         return lineIndenter;
 105  
     }
 106  
 
 107  
     /**
 108  
      * Set the string used as line indenter
 109  
      *
 110  
      * @param lineIndenter
 111  
      */
 112  
     protected void setLineIndenter( String lineIndenter )
 113  
     {
 114  1
         this.lineIndenter = lineIndenter;
 115  1
     }
 116  
 
 117  
     /**
 118  
      * Write the end of line character (using system line separator)
 119  
      * and start new line with indentation
 120  
      */
 121  
     protected void endOfLine()
 122  
     {
 123  37
         write( LS );
 124  
 
 125  189
         for ( int i = 0; i < getDepth(); i++ )
 126  
         {
 127  152
             write( getLineIndenter() );
 128  
         }
 129  37
     }
 130  
 
 131  
     /**
 132  
      * Set the underlying writer
 133  
      *
 134  
      * @param writer
 135  
      */
 136  
     protected void setWriter( PrintWriter writer )
 137  
     {
 138  1
         this.writer = writer;
 139  1
     }
 140  
 
 141  
     /**
 142  
      * Get the underlying writer
 143  
      *
 144  
      * @return the underlying writer
 145  
      */
 146  
     protected PrintWriter getWriter()
 147  
     {
 148  189
         return writer;
 149  
     }
 150  
 
 151  
     /**
 152  
      * Set the current depth in the xml indentation
 153  
      *
 154  
      * @param depth
 155  
      */
 156  
     protected void setDepth( int depth )
 157  
     {
 158  54
         this.depth = depth;
 159  54
     }
 160  
 
 161  
     /**
 162  
      * Get the current depth in the xml indentation
 163  
      *
 164  
      * @return
 165  
      */
 166  
     protected int getDepth()
 167  
     {
 168  243
         return depth;
 169  
     }
 170  
 
 171  
     public void startElement( String name )
 172  
     {
 173  27
         super.startElement( name );
 174  
 
 175  27
         setDepth( getDepth() + 1 );
 176  27
     }
 177  
 
 178  
     public void endElement()
 179  
     {
 180  27
         super.endElement();
 181  
 
 182  27
         setDepth( getDepth() - 1 );
 183  27
     }
 184  
 
 185  
 }