net.sf.saxon.serialize.HTML50Emitter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of saxon-he Show documentation
Show all versions of saxon-he Show documentation
An OSGi bundle for Saxon-HE
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2013 Saxonica Limited.
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
// If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
// This Source Code Form is “Incompatible With Secondary Licenses”, as defined by the Mozilla Public License, v. 2.0.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
package net.sf.saxon.serialize;
import net.sf.saxon.lib.NamespaceConstant;
import net.sf.saxon.lib.SaxonOutputKeys;
import net.sf.saxon.om.NodeName;
import net.sf.saxon.trans.XPathException;
import javax.xml.transform.OutputKeys;
/**
* This class generates HTML 5.0 output
*/
public class HTML50Emitter extends HTMLEmitter {
static {
setEmptyTag("area");
setEmptyTag("base");
setEmptyTag("base");
setEmptyTag("basefont");
setEmptyTag("br");
setEmptyTag("col");
setEmptyTag("command");
setEmptyTag("embed");
setEmptyTag("frame");
setEmptyTag("hr");
setEmptyTag("img");
setEmptyTag("input");
setEmptyTag("isindex");
setEmptyTag("keygen");
setEmptyTag("link");
setEmptyTag("meta");
setEmptyTag("param");
setEmptyTag("source");
setEmptyTag("track");
setEmptyTag("wbr");
}
/**
* Constructor
*/
public HTML50Emitter() {
version = 5;
}
/**
* Decide whether an element is "serialized as an HTML element" in the language of the 3.0 specification
*
* @return true if the element is to be serialized as an HTML element
*/
@Override
protected boolean isHTMLElement(NodeName name) {
String uri = name.getURI();
return uri.equals("") || uri.equals(NamespaceConstant.XHTML);
}
protected void openDocument() throws XPathException {
String versionProperty = outputProperties.getProperty(SaxonOutputKeys.HTML_VERSION);
// Note, we recognize html-version even when running XSLT 2.0.
if (versionProperty == null) {
versionProperty = outputProperties.getProperty(OutputKeys.VERSION);
}
if (versionProperty != null) {
if (versionProperty.equals("5.0")) {
version = 5;
} else {
XPathException err = new XPathException("Unsupported HTML version: " + versionProperty);
err.setErrorCode("SESU0013");
throw err;
}
}
String systemId = outputProperties.getProperty(OutputKeys.DOCTYPE_SYSTEM);
String publicId = outputProperties.getProperty(OutputKeys.DOCTYPE_PUBLIC);
// Treat "" as equivalent to absent. This goes beyond what the spec strictly allows.
if ("".equals(systemId)) {
systemId = null;
}
if ("".equals(publicId)) {
publicId = null;
}
super.openDocument();
writeDocType(null, "html", systemId, publicId);
}
/**
* Output the document type declaration
*
* @param displayName The element name
* @param systemId The DOCTYP system identifier
* @param publicId The DOCTYPE public identifier
*/
protected void writeDocType(NodeName name, String displayName, String systemId, String publicId) throws XPathException {
try {
if (systemId == null && publicId == null) {
writer.write("\n");
} else {
super.writeDocType(name, displayName, systemId, publicId);
}
} catch (java.io.IOException err) {
throw new XPathException(err);
}
}
protected boolean writeDocTypeWithNullSystemId() {
return true;
}
}