Debian Codegraph @

net/sf/saxon/serialize/MetaTagAdjuster.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.ProxyReceiver;
11 import net.sf.saxon.event.Receiver;
12 import net.sf.saxon.expr.parser.ExplicitLocation;
13 import net.sf.saxon.expr.parser.Location;
14 import net.sf.saxon.lib.NamespaceConstant;
15 import net.sf.saxon.lib.SaxonOutputKeys;
16 import net.sf.saxon.om.*;
17 import net.sf.saxon.trans.XPathException;
18 import net.sf.saxon.tree.util.AttributeCollectionImpl;
19 import net.sf.saxon.type.BuiltInAtomicType;
20 import net.sf.saxon.type.SchemaType;
21 import net.sf.saxon.type.SimpleType;
22 import net.sf.saxon.type.Untyped;
23 import net.sf.saxon.value.Whitespace;
24
25 import javax.xml.transform.OutputKeys;
26 import java.util.ArrayList;
27 import java.util.List;
28 import java.util.Properties;
29
30 /**
31 * The MetaTagAdjuster adds a meta element to the content of the head element, indicating
32 * the required content type and encoding; it also removes any existing meta element
33 * containing this information
34 */
35
36 public class MetaTagAdjuster extends ProxyReceiver {
37
38 boolean seekingHead = true;
39 int droppingMetaTags = -1;
40 boolean inMetaTag = false;
41 boolean foundHead = false;
42 /*@Nullable*/ String headPrefix = null;
43 String headURI = "";
44 NodeName metaCode;
45 String requiredURI = "";
46 AttributeCollectionImpl attributes;
47 List<NamespaceBinding> namespaces = new ArrayList<NamespaceBinding>();
48 String encoding;
49 String mediaType;
50 int level = 0;
51 boolean isXHTML = false;
52 int htmlVersion = 4;
53
54 /**
55 * Create a new MetaTagAdjuster
56 *
57 * @param next the next receiver in the pipeline
58 */
59
60 public MetaTagAdjuster(Receiver next) {
61 super(next);
62 }
63
64 /**
65 * Set output properties
66 *
67 * @param details the serialization properties
68 */
69
70 public void setOutputProperties(Properties details) {
71 encoding = details.getProperty(OutputKeys.ENCODING);
72 if (encoding == null) {
73 encoding = "UTF-8";
74 }
75 mediaType = details.getProperty(OutputKeys.MEDIA_TYPE);
76 if (mediaType == null) {
77 mediaType = "text/html";
78 }
79 String htmlVn = details.getProperty(SaxonOutputKeys.HTML_VERSION);
80 if (htmlVn == null && !isXHTML) {
81 htmlVn = details.getProperty(OutputKeys.VERSION);
82 }
83 if (htmlVn != null && htmlVn.startsWith("5")) {
84 htmlVersion = 5;
85 }
86 }
87
88 /**
89 * Indicate whether we're handling HTML or XHTML
90 */
91
92 public void setIsXHTML(boolean xhtml) {
93 isXHTML = xhtml;
94 if (xhtml) {
95 requiredURI = NamespaceConstant.XHTML;
96 } else {
97 requiredURI = "";
98 }
99 }
100
101 /**
102 * Compare a name: case-blindly in the case of HTML, case-sensitive for XHTML
103 */
104
105 private boolean comparesEqual(String name1, String name2) {
106 if (isXHTML) {
107 return name1.equals(name2);
108 } else {
109 return name1.equalsIgnoreCase(name2);
110 }
111 }
112
113 private boolean matchesName(NodeName name, String local) {
114 if (isXHTML) {
115 if (!name.getLocalPart().equals(local)) {
116 return false;
117 }
118 if (htmlVersion == 5) {
119 return name.hasURI("") || name.hasURI(NamespaceConstant.XHTML);
120 } else {
121 return name.hasURI(NamespaceConstant.XHTML);
122 }
123 } else {
124 return name.getLocalPart().equalsIgnoreCase(local);
125 }
126 }
127
128 /**
129 * Notify the start of an element
130 * @param nameCode integer code identifying the name of the element within the name pool.
131 * @param typeCode integer code identifying the element's type within the name pool.
132 * @param location
133 * @param properties properties of the element node
134 */
135
136 public void startElement(NodeName nameCode, SchemaType typeCode, Location location, int properties) throws XPathException {
137 if (droppingMetaTags == level) {
138 metaCode = nameCode;
139 if (matchesName(nameCode, "meta")) {
140 inMetaTag = true;
141 attributes.clear();
142 namespaces.clear();
143 return;
144 }
145 }
146 level++;
147 nextReceiver.startElement(nameCode, typeCode, location, properties);
148 if (seekingHead) {
149 if (matchesName(nameCode, "head")) {
150 foundHead = true;
151 headPrefix = nameCode.getPrefix();
152 headURI = nameCode.getURI();
153 }
154 }
155
156 }
157
158 /**
159 * Notify an attribute. Attributes are notified after the startElement event, and before any
160 * children. Namespaces and attributes may be intermingled.
161 *
162 * @param nameCode The name of the attribute
163 * @param typeCode The type of the attribute
164 * @param locationId the location of the node in the source, or of the instruction that created it
165 * @param properties Bit significant value. The following bits are defined:
166 * <dl>
167 * <dd>DISABLE_ESCAPING</dd> <dt>Disable escaping for this attribute</dt>
168 * <dd>NO_SPECIAL_CHARACTERS</dd> <dt>Attribute value contains no special characters</dt>
169 * </dl>
170 * @throws IllegalStateException: attempt to output an attribute when there is no open element
171 * start tag
172 */
173
174 public void attribute(NodeName nameCode, SimpleType typeCode, CharSequence value, Location locationId, int properties) throws XPathException {
175 if (inMetaTag) {
176 attributes.addAttribute(nameCode, typeCode, value.toString(), locationId, properties);
177 } else {
178 nextReceiver.attribute(nameCode, typeCode, value, locationId, properties);
179 }
180 }
181
182 /**
183 * Notify a namespace. Namespaces are notified <b>after</b> the startElement event, and before
184 * any children for the element. The namespaces that are reported are only required
185 * to include those that are different from the parent element; however, duplicates may be reported.
186 * A namespace must not conflict with any namespaces already used for element or attribute names.
187 *
188 * @param namespaceBindings the prefix/uri pair representing the namespace binding
189 * @param properties any special properties to be passed on this call
190 * @throws IllegalStateException: attempt to output a namespace when there is no open element
191 * start tag
192 */
193 @Override
194 public void namespace(NamespaceBindingSet namespaceBindings, int properties) throws XPathException {
195 if (inMetaTag) {
196 for (NamespaceBinding ns : namespaceBindings) {
197 namespaces.add(ns);
198 }
199 } else {
200 nextReceiver.namespace(namespaceBindings, properties);
201 }
202 }
203
204 /**
205 * Notify the start of the content, that is, the completion of all attributes and namespaces.
206 * Note that the initial receiver of output from XSLT instructions will not receive this event,
207 * it has to detect it itself. Note that this event is reported for every element even if it has
208 * no attributes, no namespaces, and no content.
209 */
210
211
212 public void startContent() throws XPathException {
213 if (foundHead) {
214 foundHead = false;
215 nextReceiver.startContent();
216 FingerprintedQName metaCode = new FingerprintedQName(headPrefix, headURI, "meta");
217 nextReceiver.startElement(metaCode, Untyped.getInstance(), null, 0);
218 nextReceiver.attribute(new NoNamespaceName("http-equiv"), BuiltInAtomicType.UNTYPED_ATOMIC, "Content-Type", null, 0);
219 nextReceiver.attribute(new NoNamespaceName("content"), BuiltInAtomicType.UNTYPED_ATOMIC, mediaType + "; charset=" + encoding, null, 0);
220 nextReceiver.startContent();
221 droppingMetaTags = level;
222 seekingHead = false;
223 attributes = new AttributeCollectionImpl(getConfiguration());
224 nextReceiver.endElement();
225 }
226 if (!inMetaTag) {
227 nextReceiver.startContent();
228 }
229 }
230
231 /**
232 * End of element
233 */
234
235 public void endElement() throws XPathException {
236 if (inMetaTag) {
237 inMetaTag = false;
238 // if there was an http-equiv="ContentType" attribute, discard the meta element entirely
239 boolean found = false;
240 for (int i = 0; i < attributes.getLength(); i++) {
241 String name = attributes.getLocalName(i);
242 if (comparesEqual(name, "http-equiv")) {
243 String value = Whitespace.trim(attributes.getValue(i));
244 if (value.equalsIgnoreCase("Content-Type")) {
245 // case-blind comparison even for XHTML
246 found = true;
247 break;
248 }
249 }
250 }
251 if (!found) {
252 // this was a meta element, but not one of the kind that we discard
253 nextReceiver.startElement(metaCode, Untyped.getInstance(), ExplicitLocation.UNKNOWN_LOCATION, 0);
254 for (int i = 0; i < attributes.getLength(); i++) {
255 NodeName name = attributes.getNodeName(i);
256 SimpleType typeCode = attributes.getTypeAnnotation(i);
257 String value = attributes.getValue(i);
258 Location locationId = attributes.getLocation(i);
259 int properties = attributes.getProperties(i);
260 nextReceiver.attribute(name, typeCode, value, locationId, properties);
261 }
262 for (NamespaceBinding nb : namespaces) {
263 nextReceiver.namespace(nb, 0);
264 }
265 nextReceiver.startContent();
266 nextReceiver.endElement();
267 }
268 } else {
269 level--;
270 if (droppingMetaTags == level + 1) {
271 droppingMetaTags = -1;
272 }
273 nextReceiver.endElement();
274 }
275 }
276
277 }