| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.serialize; |
| 9 |
|
| 10 |
import net.sf.saxon.event.Receiver; |
| 11 |
import net.sf.saxon.event.ReceiverOptions; |
| 12 |
import net.sf.saxon.expr.parser.Location; |
| 13 |
import net.sf.saxon.lib.NamespaceConstant; |
| 14 |
import net.sf.saxon.om.NodeName; |
| 15 |
import net.sf.saxon.serialize.codenorm.Normalizer; |
| 16 |
import net.sf.saxon.trans.XPathException; |
| 17 |
import net.sf.saxon.type.SimpleType; |
| 18 |
|
| 19 |
import java.util.HashSet; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
public class XHTMLURIEscaper extends HTMLURIEscaper { |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
private static HashSet<String> urlTable = new HashSet<String>(70); |
| 35 |
private static HashSet<String> attTable = new HashSet<String>(20); |
| 36 |
|
| 37 |
private static void setUrlAttribute(String element, String attribute) { |
| 38 |
attTable.add(attribute); |
| 39 |
urlTable.add(element + "+" + attribute); |
| 40 |
} |
| 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 |
public XHTMLURIEscaper(Receiver next) { |
| 83 |
super(next); |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
private static boolean isURLAttribute(NodeName elcode, NodeName atcode) { |
| 91 |
if (!elcode.hasURI(NamespaceConstant.XHTML)) { |
| 92 |
return false; |
| 93 |
} |
| 94 |
if (!atcode.hasURI("")) { |
| 95 |
return false; |
| 96 |
} |
| 97 |
String attName = atcode.getLocalPart(); |
| 98 |
return attTable.contains(attName) && urlTable.contains(elcode.getLocalPart() + "+" + attName); |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
public void attribute(NodeName attName, SimpleType typeCode, CharSequence value, Location locationId, int properties) throws XPathException { |
| 118 |
if (escapeURIAttributes && |
| 119 |
isURLAttribute(currentElement, attName) && |
| 120 |
(properties & ReceiverOptions.DISABLE_ESCAPING) == 0) { |
| 121 |
CharSequence normalized = (isAllAscii(value) |
| 122 |
? value |
| 123 |
: new Normalizer(Normalizer.C, getConfiguration()).normalize(value)); |
| 124 |
|
| 125 |
getNextReceiver().attribute( |
| 126 |
attName, typeCode, escapeURL(normalized, true, getConfiguration()), locationId, |
| 127 |
properties | ReceiverOptions.DISABLE_CHARACTER_MAPS); |
| 128 |
} else { |
| 129 |
getNextReceiver().attribute( |
| 130 |
attName, typeCode, value, locationId, properties); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
private static boolean isAllAscii( CharSequence value) { |
| 135 |
for (int i = 0; i < value.length(); i++) { |
| 136 |
if (value.charAt(i) > 127) { |
| 137 |
return false; |
| 138 |
} |
| 139 |
} |
| 140 |
return true; |
| 141 |
} |
| 142 |
|
| 143 |
} |
| 144 |
|