Debian Codegraph @

net/sf/saxon/serialize/XHTMLURIEscaper.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.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 * This class performs URI escaping for the XHTML output method. The logic for performing escaping
24 * is the same as the HTML output method, but the way in which attributes are identified for escaping
25 * is different, because XHTML is case-sensitive.
26 */
27
28 public class XHTMLURIEscaper extends HTMLURIEscaper {
29
30 /**
31 * Table of attributes whose value is a URL
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"); // 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 public XHTMLURIEscaper(Receiver next) {
83 super(next);
84 }
85
86 /**
87 * Determine whether a given attribute is a URL attribute
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 * Notify an attribute. Attributes are notified after the startElement event, and before any
103 * children. Namespaces and attributes may be intermingled.
104 *
105 * @param attName The name of the attribute
106 * @param typeCode The type of the attribute
107 * @param locationId the location of the node in the source, or of the instruction that created it
108 * @param properties Bit significant value. The following bits are defined:
109 * <dl>
110 * <dd>DISABLE_ESCAPING</dd> <dt>Disable escaping for this attribute</dt>
111 * <dd>NO_SPECIAL_CHARACTERS</dd> <dt>Attribute value contains no special characters</dt>
112 * </dl>
113 * @throws IllegalStateException: attempt to output an attribute when there is no open element
114 * start tag
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(/*@NotNull*/ 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