Debian Codegraph @

net/sf/saxon/serialize/XHTMLPrefixRemover.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.Location;
13 import net.sf.saxon.lib.NamespaceConstant;
14 import net.sf.saxon.om.FingerprintedQName;
15 import net.sf.saxon.om.NamespaceBinding;
16 import net.sf.saxon.om.NamespaceBindingSet;
17 import net.sf.saxon.om.NodeName;
18 import net.sf.saxon.trans.XPathException;
19 import net.sf.saxon.type.SchemaType;
20 import net.sf.saxon.type.SimpleType;
21
22 /**
23 * Filter to change elements in the XHTML, SVG, or MathML namespaces so they have no prefix (that is,
24 * to make these the default namespace). This filter must be followed by a NamespaceReducer in case
25 * there are any attributes in these namespaces, as this will cause the namespace declarations to
26 * be reinstated.
27 */
28
29 public class XHTMLPrefixRemover extends ProxyReceiver {
30
31 public XHTMLPrefixRemover(Receiver next) {
32 super(next);
33 }
34 /**
35 * Is the active namespace one that requires special (X)HTML(5) prefix treatment?
36 * @param uri URI of the namespace
37 * @return true if requires special treatment
38 */
39 private boolean isSpecial(String uri) {
40 return uri.equals(NamespaceConstant.XHTML) ||
41 uri.equals(NamespaceConstant.SVG) ||
42 uri.equals(NamespaceConstant.MATHML);
43 }
44 /**
45 * Notify the start of an element
46 *
47 * Specific treatment of elements in XHTML, SVG and MathML namespaces forces a namespace to be emitted too,
48 * rather than by higher level default behaviour.
49 * @param elemName integer code identifying the name of the element within the name pool.
50 * @param typeCode integer code identifying the element's type within the name pool.
51 * @param location
52 * @param properties properties of the element node
53 */
54 @Override
55 public void startElement(NodeName elemName, SchemaType typeCode, Location location, int properties) throws XPathException {
56 if (isSpecial(elemName.getURI())) {
57 if (!elemName.getPrefix().isEmpty()) {
58 elemName = new FingerprintedQName("", elemName.getURI(), elemName.getLocalPart());
59 }
60 super.startElement(elemName, typeCode, location, properties);
61 super.namespace(new NamespaceBinding("", elemName.getURI()), properties);
62 } else {
63 super.startElement(elemName, typeCode, location, properties);
64 }
65 }
66
67
68 /**
69 * Notify a namespace. Namespaces are notified <b>after</b> the startElement event, and before
70 * any children for the element. The namespaces that are reported are only required
71 * to include those that are different from the parent element; however, duplicates may be reported.
72 * A namespace must not conflict with any namespaces already used for element or attribute names.
73 *
74 * Any special namespaces (for XHTML, SVG and MathML) are not written out -
75 * they are specifically emitted with elements or attributes requiring them
76 *
77 * @param namespaceBindings the prefix/uri pair representing the namespace binding
78 * @param properties any special properties to be passed on this call
79 * @throws IllegalStateException: attempt to output a namespace when there is no open element
80 * start tag
81 */
82 @Override
83 public void namespace(NamespaceBindingSet namespaceBindings, int properties) throws XPathException {
84 for (NamespaceBinding ns : namespaceBindings) {
85 if (!isSpecial(ns.getURI())) {
86 super.namespace(ns, properties);
87 }
88 }
89 }
90
91 /**
92 * Notify an attribute. Attributes are notified after the startElement event, and before any
93 * children. Namespaces and attributes may be intermingled.
94 *
95 * Specific treatment of attributes in XHTML, SVG and MathML namespaces forces a namespace to be emitted too,
96 * rather than by higher level default behaviour.
97 *
98 * @param nameCode The name of the attribute
99 * @param typeCode The type of the attribute
100 * @param locationId the location of the node in the source, or of the instruction that created it
101 * @param properties Bit significant value. The following bits are defined:
102 * <dl>
103 * <dd>DISABLE_ESCAPING</dd> <dt>Disable escaping for this attribute</dt>
104 * <dd>NO_SPECIAL_CHARACTERS</dd> <dt>Attribute value contains no special characters</dt>
105 * </dl>
106 * @throws IllegalStateException: attempt to output an attribute when there is no open element
107 * start tag
108 */
109 public void attribute(NodeName nameCode, SimpleType typeCode, CharSequence value, Location locationId, int properties)
110 throws XPathException {
111 if(isSpecial(nameCode.getURI())) {
112 super.namespace(nameCode.getNamespaceBinding(),properties);
113 }
114 super.attribute(nameCode, typeCode, value, locationId, properties);
115 }
116 }