| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 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 | |
|
| 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("<"); |
| 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(">"); |
| 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("&"); |
| 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("<"); |
| 109 | 26 | continue; |
| 110 | |
|
| 111 | |
case '>': |
| 112 | |
|
| 113 | 26 | builder.append(">"); |
| 114 | 26 | continue; |
| 115 | |
|
| 116 | |
case '&': |
| 117 | |
|
| 118 | 32 | builder.append("&"); |
| 119 | 32 | continue; |
| 120 | |
|
| 121 | |
case '"': |
| 122 | |
|
| 123 | 150 | if (!useApostropheForAttributes) |
| 124 | |
{ |
| 125 | 138 | builder.append("""); |
| 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("'"); |
| 137 | 0 | continue; |
| 138 | |
} |
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | |
|
| 143 | |
default: |
| 144 | |
|
| 145 | 1952702 | builder.append(ch); |
| 146 | |
} |
| 147 | |
} |
| 148 | 111656 | } |
| 149 | |
} |