Debian Codegraph @

net/sf/saxon/serialize/XHTML5Emitter.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
9 package net.sf.saxon.serialize;
10
11 import net.sf.saxon.expr.parser.Location;
12 import net.sf.saxon.lib.NamespaceConstant;
13 import net.sf.saxon.om.NodeName;
14 import net.sf.saxon.trans.XPathException;
15 import net.sf.saxon.value.Whitespace;
16
17 import java.io.IOException;
18 import java.util.Collections;
19 import java.util.HashSet;
20 import java.util.Set;
21
22
23 /**
24 * XHTML5Emitter is an Emitter that generates XHTML 5 output.
25 * It is the same as XMLEmitter except that it follows the legacy HTML browser
26 * compatibility rules: for example, generating empty elements such as [BR /], and
27 * using [p][/p] for empty paragraphs rather than [p/]
28 */
29
30 public class XHTML5Emitter extends XMLEmitter {
31
32 private static String[] html5ElementNames = {
33 "a", "abbr", "address", "area", "article", "aside", "audio",
34 "b", "base", "bdi", "bdo", "blockquote", "body", "br", "button",
35 "canvas", "caption", "cite", "code", "col", "colgroup", /*"command",*/
36 "datalist", "dd", "del", "details", "dfn", "dialog", "div", "dl", "dt",
37 "em", "embed",
38 "fieldset", "figcaption", "figure", "footer", "form",
39 "h1", "h2", "h3", "h4", "h5", "h6", "head", "header", "hgroup", "hr", "html",
40 "i", "iframe", "img", "input", "ins",
41 "kbd", "keygen",
42 "label", "legend", "li", "link",
43 "map", "mark", "menu", "meta", "meter",
44 "nav", "noscript",
45 "object", "ol", "optgroup", "option", "output",
46 "p", "param", "pre", "progress",
47 "q",
48 "rp", "rt", "ruby",
49 "s", "samp", "script", "section", "select", "small", "source", "span", "strong", "style", "sub", "summary", "sup",
50 "table", "tbody", "td", "textarea", "tfoot", "th", "thead", "time", "title", "tr", "track",
51 "u", "ul",
52 "var", "video",
53 "wbr"
54 };
55
56 static Set<String> html5Elements = new HashSet<String>(128);
57
58 static Set<String> emptyTags5 = new HashSet<String>(31);
59
60 private static String[] emptyTagNames5 = {
61 "area", "base", "br", "col", /*"command",*/ "embed", "hr", "img", "input", "keygen", "link", "meta", "param",
62 "source", "track", "wbr"
63 };
64
65
66 static {
67 Collections.addAll(emptyTags5, emptyTagNames5);
68 Collections.addAll(html5Elements, html5ElementNames);
69 }
70
71
72 private boolean isRecognizedHtmlElement(NodeName name) {
73 return name.hasURI(NamespaceConstant.XHTML) ||
74 name.hasURI("") && html5Elements.contains(name.getLocalPart().toLowerCase());
75
76 }
77
78 /**
79 * Output the document type declaration
80 *
81 * @param name the qualified name of the element
82 * @param displayName The element nam
83 * @param systemId The DOCTYP system identifier
84 * @param publicId The DOCTYPE public identifier
85 * @throws net.sf.saxon.trans.XPathException
86 * if an error occurs writing to the output
87 */
88 @Override
89 protected void writeDocType(NodeName name, String displayName, String systemId, String publicId) throws XPathException {
90 if (systemId == null &&
91 isRecognizedHtmlElement(name) && name.getLocalPart().toLowerCase().equals("html")) {
92 try {
93 writer.write("<!DOCTYPE " + displayName + ">");
94 } catch (IOException e) {
95 throw new XPathException(e);
96 }
97 } else if (systemId != null) {
98 super.writeDocType(name, displayName, systemId, publicId);
99 }
100 }
101
102 protected boolean writeDocTypeWithNullSystemId() {
103 return true;
104 }
105
106 /**
107 * Close an empty element tag.
108 */
109
110 protected String emptyElementTagCloser(String displayName, /*@NotNull*/ NodeName name) {
111 if (isRecognizedHtmlElement(name) && emptyTags5.contains(name.getLocalPart())) {
112 return "/>";
113 } else {
114 return "></" + displayName + '>';
115 }
116 }
117
118 /**
119 * Character data.
120 */
121 @Override
122 public void characters(CharSequence chars, Location locationId, int properties) throws XPathException {
123 if (!started && Whitespace.isWhite(chars)) {
124 // Ignore whitespace before the first start tag. This isn't explicit in the spec, but
125 // we would otherwise need to buffer such whitespace, because we need to output a DOCTYPE
126 // declaration based on the content of the first element tag.
127 } else {
128 super.characters(chars, locationId, properties);
129 }
130 }
131 }
132