| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 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 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 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", |
| 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", "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 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 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 |
|
| 108 |
|
| 109 |
|
| 110 |
protected String emptyElementTagCloser(String displayName, NodeName name) { |
| 111 |
if (isRecognizedHtmlElement(name) && emptyTags5.contains(name.getLocalPart())) { |
| 112 |
return "/>"; |
| 113 |
} else { |
| 114 |
return "></" + displayName + '>'; |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
@Override |
| 122 |
public void characters(CharSequence chars, Location locationId, int properties) throws XPathException { |
| 123 |
if (!started && Whitespace.isWhite(chars)) { |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
} else { |
| 128 |
super.characters(chars, locationId, properties); |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
|