| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.serialize; |
| 9 |
|
| 10 |
import net.sf.saxon.Configuration; |
| 11 |
import net.sf.saxon.event.ProxyReceiver; |
| 12 |
import net.sf.saxon.event.Receiver; |
| 13 |
import net.sf.saxon.event.ReceiverOptions; |
| 14 |
import net.sf.saxon.expr.parser.Location; |
| 15 |
import net.sf.saxon.om.NamePool; |
| 16 |
import net.sf.saxon.om.NodeName; |
| 17 |
import net.sf.saxon.serialize.charcode.UTF8CharacterSet; |
| 18 |
import net.sf.saxon.serialize.codenorm.Normalizer; |
| 19 |
import net.sf.saxon.trans.XPathException; |
| 20 |
import net.sf.saxon.tree.util.FastStringBuffer; |
| 21 |
import net.sf.saxon.type.SchemaType; |
| 22 |
import net.sf.saxon.type.SimpleType; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
public class HTMLURIEscaper extends ProxyReceiver { |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
private static HTMLTagHashSet urlAttributes = new HTMLTagHashSet(47); |
| 40 |
private static HTMLTagHashSet urlCombinations = new HTMLTagHashSet(101); |
| 41 |
|
| 42 |
static { |
| 43 |
setUrlAttribute("form", "action"); |
| 44 |
setUrlAttribute("object", "archive"); |
| 45 |
setUrlAttribute("body", "background"); |
| 46 |
setUrlAttribute("q", "cite"); |
| 47 |
setUrlAttribute("blockquote", "cite"); |
| 48 |
setUrlAttribute("del", "cite"); |
| 49 |
setUrlAttribute("ins", "cite"); |
| 50 |
setUrlAttribute("object", "classid"); |
| 51 |
setUrlAttribute("object", "codebase"); |
| 52 |
setUrlAttribute("applet", "codebase"); |
| 53 |
setUrlAttribute("object", "data"); |
| 54 |
setUrlAttribute("button", "datasrc"); |
| 55 |
setUrlAttribute("div", "datasrc"); |
| 56 |
setUrlAttribute("input", "datasrc"); |
| 57 |
setUrlAttribute("object", "datasrc"); |
| 58 |
setUrlAttribute("select", "datasrc"); |
| 59 |
setUrlAttribute("span", "datasrc"); |
| 60 |
setUrlAttribute("table", "datasrc"); |
| 61 |
setUrlAttribute("textarea", "datasrc"); |
| 62 |
setUrlAttribute("script", "for"); |
| 63 |
setUrlAttribute("a", "href"); |
| 64 |
setUrlAttribute("a", "name"); |
| 65 |
setUrlAttribute("area", "href"); |
| 66 |
setUrlAttribute("link", "href"); |
| 67 |
setUrlAttribute("base", "href"); |
| 68 |
setUrlAttribute("img", "longdesc"); |
| 69 |
setUrlAttribute("frame", "longdesc"); |
| 70 |
setUrlAttribute("iframe", "longdesc"); |
| 71 |
setUrlAttribute("head", "profile"); |
| 72 |
setUrlAttribute("script", "src"); |
| 73 |
setUrlAttribute("input", "src"); |
| 74 |
setUrlAttribute("frame", "src"); |
| 75 |
setUrlAttribute("iframe", "src"); |
| 76 |
setUrlAttribute("img", "src"); |
| 77 |
setUrlAttribute("img", "usemap"); |
| 78 |
setUrlAttribute("input", "usemap"); |
| 79 |
setUrlAttribute("object", "usemap"); |
| 80 |
} |
| 81 |
|
| 82 |
private static void setUrlAttribute(String element, String attribute) { |
| 83 |
urlAttributes.add(attribute); |
| 84 |
urlCombinations.add(element + '+' + attribute); |
| 85 |
} |
| 86 |
|
| 87 |
public boolean isUrlAttribute(NodeName element, NodeName attribute) { |
| 88 |
if (pool == null) { |
| 89 |
pool = getNamePool(); |
| 90 |
} |
| 91 |
String attributeName = attribute.getDisplayName(); |
| 92 |
if (!urlAttributes.contains(attributeName)) { |
| 93 |
return false; |
| 94 |
} |
| 95 |
String elementName = element.getDisplayName(); |
| 96 |
return urlCombinations.contains(elementName + '+' + attributeName); |
| 97 |
} |
| 98 |
|
| 99 |
protected NodeName currentElement; |
| 100 |
protected boolean escapeURIAttributes = true; |
| 101 |
protected NamePool pool; |
| 102 |
|
| 103 |
public HTMLURIEscaper(Receiver nextReceiver) { |
| 104 |
super(nextReceiver); |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
public void startDocument(int properties) throws XPathException { |
| 112 |
nextReceiver.startDocument(properties); |
| 113 |
pool = getPipelineConfiguration().getConfiguration().getNamePool(); |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
public void startElement(NodeName nameCode, SchemaType typeCode, Location location, int properties) throws XPathException { |
| 125 |
currentElement = nameCode; |
| 126 |
nextReceiver.startElement(nameCode, typeCode, location, properties); |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
public void attribute(NodeName nameCode, SimpleType typeCode, CharSequence value, Location locationId, int properties) throws XPathException { |
| 146 |
if (escapeURIAttributes && |
| 147 |
isUrlAttribute(currentElement, nameCode) && |
| 148 |
(properties & ReceiverOptions.DISABLE_ESCAPING) == 0) { |
| 149 |
nextReceiver.attribute(nameCode, typeCode, escapeURL(value, true, getConfiguration()), locationId, |
| 150 |
properties | ReceiverOptions.DISABLE_CHARACTER_MAPS); |
| 151 |
} else { |
| 152 |
nextReceiver.attribute(nameCode, typeCode, value, locationId, properties); |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
public static CharSequence escapeURL(CharSequence url, boolean normalize, Configuration config) throws XPathException { |
| 168 |
|
| 169 |
for (int i = url.length() - 1; i >= 0; i--) { |
| 170 |
char ch = url.charAt(i); |
| 171 |
if (ch < 32 || ch > 126) { |
| 172 |
if (normalize) { |
| 173 |
CharSequence normalized = new Normalizer(Normalizer.C, config).normalize(url); |
| 174 |
return reallyEscapeURL(normalized); |
| 175 |
} else { |
| 176 |
return reallyEscapeURL(url); |
| 177 |
} |
| 178 |
} |
| 179 |
} |
| 180 |
return url; |
| 181 |
} |
| 182 |
|
| 183 |
private static CharSequence reallyEscapeURL(CharSequence url) { |
| 184 |
FastStringBuffer sb = new FastStringBuffer(url.length() + 20); |
| 185 |
final String hex = "0123456789ABCDEF"; |
| 186 |
byte[] array = new byte[4]; |
| 187 |
|
| 188 |
for (int i = 0; i < url.length(); i++) { |
| 189 |
char ch = url.charAt(i); |
| 190 |
if (ch < 32 || ch > 126) { |
| 191 |
int used = UTF8CharacterSet.getUTF8Encoding(ch, |
| 192 |
(i + 1 < url.length() ? url.charAt(i + 1) : ' '), array); |
| 193 |
for (int b = 0; b < used; b++) { |
| 194 |
|
| 195 |
int v = ((int) array[b]) & 0xff; |
| 196 |
sb.append('%'); |
| 197 |
sb.append(hex.charAt(v / 16)); |
| 198 |
sb.append(hex.charAt(v % 16)); |
| 199 |
} |
| 200 |
|
| 201 |
} else { |
| 202 |
sb.append(ch); |
| 203 |
} |
| 204 |
} |
| 205 |
return sb; |
| 206 |
} |
| 207 |
} |
| 208 |
|