Coverage Report - org.apache.creadur.whisker.out.velocity.Indentation
 
Classes in this File Line Coverage Branch Coverage Complexity
Indentation
100%
14/14
100%
4/4
1.667
 
 1  
 /**
 2  
  * Licensed to the Apache Software Foundation (ASF) under one
 3  
  * or more contributor license agreements.  See the NOTICE file
 4  
  * distributed with this work for additional information
 5  
  * regarding copyright ownership.  The ASF licenses this file
 6  
  *  to you under the Apache License, Version 2.0 (the
 7  
  * "License"); you may not use this file except in compliance
 8  
  *  with the License.  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,
 13  
  * software distributed under the License is distributed on an
 14  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 15  
  * KIND, either express or implied.  See the License for the
 16  
  * specific language governing permissions and limitations
 17  
  * under the License.
 18  
  */
 19  
 package org.apache.creadur.whisker.out.velocity;
 20  
 
 21  
 import java.io.BufferedReader;
 22  
 import java.io.IOException;
 23  
 import java.io.StringReader;
 24  
 
 25  
 import org.apache.commons.lang3.StringUtils;
 26  
 
 27  
 /**
 28  
  * Manages indentation level.
 29  
  */
 30  20
 public class Indentation {
 31  
 
 32  
     /**
 33  
      * Creates appropriate indentation, padding with spaces.
 34  
      * @param indentation not null
 35  
      * @param source not null
 36  
      * @return appropriate indentation, not null
 37  
      * @throws IOException when source cannot be read
 38  
      */
 39  
     public final String indent(
 40  
             final int indentation, final Object source) throws IOException {
 41  36
         return this.indent(indentation, source, ' ');
 42  
     }
 43  
 
 44  
     /**
 45  
      * Creates appropriate indentation.
 46  
      * @param indentation not null
 47  
      * @param source not null
 48  
      * @param pad padding
 49  
      * @return appropriate indentation, not null
 50  
      * @throws IOException when source cannot be read
 51  
      */
 52  
     public final String indent(
 53  
             final int indentation, final Object source, final char pad)
 54  
                 throws IOException {
 55  
         final String result;
 56  36
         if (source == null) {
 57  2
             result = "";
 58  
         } else {
 59  34
             result = prefixLine(source, StringUtils.repeat(pad, indentation));
 60  
         }
 61  36
         return result;
 62  
     }
 63  
 
 64  
     /**
 65  
      * Creates appropriate indentation.
 66  
      * @param source not null
 67  
      * @param prefix not null
 68  
      * @return appropriate indentation, not null
 69  
      * @throws IOException when source cannot be read
 70  
      */
 71  
     private String prefixLine(final Object source, final String prefix)
 72  
             throws IOException {
 73  
         final String result;
 74  34
         final StringBuilder builder = new StringBuilder();
 75  34
         final BufferedReader lineReader =
 76  
             new BufferedReader(new StringReader(source.toString()));
 77  34
         String line = lineReader.readLine();
 78  68
         while (line != null) {
 79  34
             builder.append('\n').append(prefix).append(line);
 80  34
             line = lineReader.readLine();
 81  
         }
 82  34
         result = builder.toString();
 83  34
         return result;
 84  
     }
 85  
 }