Coverage Report - org.apache.tapestry5.dom.AbstractMarkupModel
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractMarkupModel
94%
45/48
90%
27/30
5
 
 1  
 //  Copyright 2008, 2009 The Apache Software Foundation
 2  
 //
 3  
 // Licensed under the Apache License, Version 2.0 (the "License");
 4  
 // you may not use this file except in compliance with the License.
 5  
 // You may obtain a copy of the License at
 6  
 //
 7  
 //     http://www.apache.org/licenses/LICENSE-2.0
 8  
 //
 9  
 // Unless required by applicable law or agreed to in writing, software
 10  
 // distributed under the License is distributed on an "AS IS" BASIS,
 11  
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12  
 // See the License for the specific language governing permissions and
 13  
 // limitations under the License.
 14  
 
 15  
 package org.apache.tapestry5.dom;
 16  
 
 17  
 import org.apache.tapestry5.ioc.internal.util.Defense;
 18  
 
 19  
 public abstract class AbstractMarkupModel implements MarkupModel
 20  
 {
 21  
     private final boolean useApostropheForAttributes;
 22  
 
 23  
     protected AbstractMarkupModel(boolean useApostropheForAttributes)
 24  454
     {
 25  454
         this.useApostropheForAttributes = useApostropheForAttributes;
 26  454
     }
 27  
 
 28  
     public char getAttributeQuote()
 29  
     {
 30  223312
         return useApostropheForAttributes ? '\'' : '"';
 31  
     }
 32  
 
 33  
     /**
 34  
      * Passes all characters but '<', '>' and '&' through unchanged.
 35  
      */
 36  
     public String encode(String content)
 37  
     {
 38  291634
         int length = content.length();
 39  
 
 40  291634
         StringBuilder builder = null;
 41  
 
 42  8963050
         for (int i = 0; i < length; i++)
 43  
         {
 44  8671416
             char ch = content.charAt(i);
 45  
 
 46  8671416
             switch (ch)
 47  
             {
 48  
                 case '<':
 49  
 
 50  560
                     if (builder == null)
 51  
                     {
 52  364
                         builder = new StringBuilder(2 * length);
 53  
 
 54  364
                         builder.append(content.substring(0, i));
 55  
                     }
 56  
 
 57  560
                     builder.append("&lt;");
 58  560
                     continue;
 59  
 
 60  
                 case '>':
 61  
 
 62  572
                     if (builder == null)
 63  
                     {
 64  32
                         builder = new StringBuilder(2 * length);
 65  
 
 66  32
                         builder.append(content.substring(0, i));
 67  
                     }
 68  
 
 69  572
                     builder.append("&gt;");
 70  572
                     continue;
 71  
 
 72  
                 case '&':
 73  
 
 74  137
                     if (builder == null)
 75  
                     {
 76  135
                         builder = new StringBuilder(2 * length);
 77  
 
 78  135
                         builder.append(content.substring(0, i));
 79  
                     }
 80  
 
 81  137
                     builder.append("&amp;");
 82  137
                     continue;
 83  
 
 84  
                 default:
 85  
 
 86  8670147
                     if (builder != null)
 87  29151
                         builder.append(ch);
 88  
             }
 89  
         }
 90  
 
 91  291634
         return builder == null ? content : builder.toString();
 92  
     }
 93  
 
 94  
     public void encodeQuoted(String content, StringBuilder builder)
 95  
     {
 96  111656
         Defense.notNull(content, "content");
 97  
 
 98  111656
         int length = content.length();
 99  
 
 100  2064592
         for (int i = 0; i < length; i++)
 101  
         {
 102  1952936
             char ch = content.charAt(i);
 103  
 
 104  1952936
             switch (ch)
 105  
             {
 106  
                 case '<':
 107  
 
 108  26
                     builder.append("&lt;");
 109  26
                     continue;
 110  
 
 111  
                 case '>':
 112  
 
 113  26
                     builder.append("&gt;");
 114  26
                     continue;
 115  
 
 116  
                 case '&':
 117  
 
 118  32
                     builder.append("&amp;");
 119  32
                     continue;
 120  
 
 121  
                 case '"':
 122  
 
 123  150
                     if (!useApostropheForAttributes)
 124  
                     {
 125  138
                         builder.append("&quot;");
 126  138
                         continue;
 127  
                     }
 128  
 
 129  12
                     builder.append(ch);
 130  12
                     continue;
 131  
 
 132  
                 case '\'':
 133  
 
 134  0
                     if (useApostropheForAttributes)
 135  
                     {
 136  0
                         builder.append("&apos;");
 137  0
                         continue;
 138  
                     }
 139  
 
 140  
 
 141  
                     // Fall through
 142  
 
 143  
                 default:
 144  
 
 145  1952702
                     builder.append(ch);
 146  
             }
 147  
         }
 148  111656
     }
 149  
 }