Debian Codegraph @

net/sf/saxon/serialize/HTMLURIEscaper.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018 Saxonica Limited.
3 // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4 // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
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 * This class is used as a filter on the serialization pipeline; it performs the function
26 * of escaping URI-valued attributes in HTML
27 *
28 * @author Michael H. Kay
29 */
30
31 public class HTMLURIEscaper extends ProxyReceiver {
32
33 /**
34 * Table of attributes whose value is a URL
35 */
36
37 // we use two HashMaps to avoid unnecessary string concatenations
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"); // see second note in section B.2.1 of HTML 4 specification
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 * Start of a document node.
109 */
110
111 public void startDocument(int properties) throws XPathException {
112 nextReceiver.startDocument(properties);
113 pool = getPipelineConfiguration().getConfiguration().getNamePool();
114 }
115
116 /**
117 * Notify the start of an element
118 * @param nameCode integer code identifying the name of the element within the name pool.
119 * @param typeCode integer code identifying the element's type within the name pool.
120 * @param location
121 * @param properties properties of the element node
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 * Notify an attribute. Attributes are notified after the startElement event, and before any
131 * children. Namespaces and attributes may be intermingled.
132 *
133 * @param nameCode The name of the attribute
134 * @param typeCode The type of the attribute
135 * @param locationId the location of the node in the source, or of the instruction that created it
136 * @param properties Bit significant value. The following bits are defined:
137 * <dl>
138 * <dd>DISABLE_ESCAPING</dd> <dt>Disable escaping for this attribute</dt>
139 * <dd>NO_SPECIAL_CHARACTERS</dd> <dt>Attribute value contains no special characters</dt>
140 * </dl>
141 * @throws IllegalStateException: attempt to output an attribute when there is no open element
142 * start tag
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 * Escape a URI according to the HTML rules: that is, a non-ASCII character (specifically,
158 * a character outside the range 32 - 126) is replaced by the %HH encoding of the octets in
159 * its UTF-8 representation
160 *
161 * @param url the URI to be escaped
162 * @param normalize
163 * @return the URI after escaping non-ASCII characters
164 */
165
166 /*@NotNull*/
167 public static CharSequence escapeURL(CharSequence url, boolean normalize, Configuration config) throws XPathException {
168 // optimize for the common case where the string is all ASCII characters
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 //int v = (array[b]>=0 ? array[b] : 256 + array[b]);
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