Coverage Report - org.apache.myfaces.shared.renderkit.html.JavascriptContext
 
Classes in this File Line Coverage Branch Coverage Complexity
JavascriptContext
18%
10/53
11%
2/18
1.643
 
 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.myfaces.shared.renderkit.html;
 20  
 
 21  
 /**
 22  
  * The ScriptContext offers methods and fields
 23  
  * to help with rendering out a script and keeping a
 24  
  * proper formatting.
 25  
  */
 26  
 public class JavascriptContext
 27  
 {
 28  1
     private static final String LINE_SEPARATOR = System.getProperty(
 29  
             "line.separator", "\r\n");
 30  
     private static final char TABULATOR = '\t';
 31  
 
 32  
     private long currentIndentationLevel;
 33  18
     private StringBuilder buffer = new StringBuilder();
 34  18
     private boolean prettyPrint = false;
 35  
     /**
 36  
      * automatic formatting will render
 37  
      * new-lines and indents if blocks are opened
 38  
      * and closed - attention: you need to append
 39  
      * opening and closing brackets of blocks separately in this case!
 40  
      */
 41  18
     private boolean automaticFormatting = true;
 42  
 
 43  
     public JavascriptContext()
 44  18
     {
 45  
 
 46  18
     }
 47  
 
 48  
     public JavascriptContext(boolean prettyPrint)
 49  0
     {
 50  0
         this.prettyPrint = prettyPrint;
 51  0
     }
 52  
 
 53  
     public JavascriptContext(StringBuilder buf, boolean prettyPrint)
 54  0
     {
 55  0
         this.prettyPrint = prettyPrint;
 56  0
         this.buffer = buf;
 57  0
     }
 58  
 
 59  
     public void increaseIndent()
 60  
     {
 61  0
         currentIndentationLevel++;
 62  0
     }
 63  
 
 64  
     public void decreaseIndent()
 65  
     {
 66  0
         currentIndentationLevel--;
 67  
 
 68  0
         if (currentIndentationLevel < 0)
 69  
         {
 70  0
             currentIndentationLevel = 0;
 71  
         }
 72  0
     }
 73  
 
 74  
     public void prettyLine()
 75  
     {
 76  0
         if (prettyPrint)
 77  
         {
 78  0
             append(LINE_SEPARATOR);
 79  
 
 80  0
             for (int i = 0; i < getCurrentIndentationLevel(); i++)
 81  
             {
 82  0
                 append(TABULATOR);
 83  
             }
 84  
         }
 85  0
     }
 86  
 
 87  
     public void prettyLineIncreaseIndent()
 88  
     {
 89  0
         increaseIndent();
 90  0
         prettyLine();
 91  0
     }
 92  
 
 93  
     public void prettyLineDecreaseIndent()
 94  
     {
 95  0
         decreaseIndent();
 96  0
         prettyLine();
 97  0
     }
 98  
 
 99  
     public long getCurrentIndentationLevel()
 100  
     {
 101  0
         return currentIndentationLevel;
 102  
     }
 103  
 
 104  
     public void setCurrentIndentationLevel(long currentIndentationLevel)
 105  
     {
 106  0
         this.currentIndentationLevel = currentIndentationLevel;
 107  0
     }
 108  
 
 109  
     public JavascriptContext append(String str)
 110  
     {
 111  
 
 112  40
         if (automaticFormatting && str.length() == 1)
 113  
         {
 114  0
             boolean openBlock = str.equals("{");
 115  0
             boolean closeBlock = str.equals("}");
 116  
 
 117  0
             if (openBlock)
 118  
             {
 119  0
                 prettyLine();
 120  
             }
 121  0
             else if (closeBlock)
 122  
             {
 123  0
                 prettyLineDecreaseIndent();
 124  
             }
 125  
 
 126  0
             buffer.append(str);
 127  
 
 128  0
             if (openBlock)
 129  
             {
 130  0
                 prettyLineIncreaseIndent();
 131  
             }
 132  0
             else if (closeBlock)
 133  
             {
 134  0
                 prettyLine();
 135  
             }
 136  0
         }
 137  
         else
 138  
         {
 139  40
             buffer.append(str);
 140  
         }
 141  40
         return this;
 142  
     }
 143  
 
 144  
     public JavascriptContext append(char c)
 145  
     {
 146  0
         buffer.append(c);
 147  0
         return this;
 148  
     }
 149  
 
 150  
     public JavascriptContext append(int i)
 151  
     {
 152  0
         buffer.append(i);
 153  0
         return this;
 154  
     }
 155  
 
 156  
     public String toString()
 157  
     {
 158  20
         return buffer.toString();
 159  
     }
 160  
 }