All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sf.saxon.serialize.HTML40Emitter Maven / Gradle / Ivy

There is a newer version: 12.5
Show newest version
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2018-2022 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.event.ReceiverOption;
import net.sf.saxon.lib.SaxonOutputKeys;
import net.sf.saxon.om.AttributeMap;
import net.sf.saxon.om.NamespaceMap;
import net.sf.saxon.om.NodeName;
import net.sf.saxon.s9api.Location;
import net.sf.saxon.trans.XPathException;
import net.sf.saxon.type.SchemaType;

import javax.xml.transform.OutputKeys;

/**
 * This class generates HTML 4.0 output
 */
public class HTML40Emitter extends HTMLEmitter {

    static {
        setEmptyTag("area");
        setEmptyTag("base");
        setEmptyTag("basefont");
        setEmptyTag("br");
        setEmptyTag("col");
        setEmptyTag("embed");
        setEmptyTag("frame");
        setEmptyTag("hr");
        setEmptyTag("img");
        setEmptyTag("input");
        setEmptyTag("isindex");
        setEmptyTag("link");
        setEmptyTag("meta");
        setEmptyTag("param");
    }


    /**
     * Constructor
     */

    public HTML40Emitter() {

    }

    /**
     * Decide whether an element is "serialized as an HTML element" in the language of the 3.0 specification
     *
     * @param name the name of the element
     * @return true if the element is to be serialized as an HTML element
     */
    @Override
    protected boolean isHTMLElement(NodeName name) {
        return name.getURI().equals("");
    }

    @Override
    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("4.0") || versionProperty.equals("4.01")) {
                version = 4;
            } else {
                XPathException err = new XPathException("Unsupported HTML version: " + versionProperty);
                err.setErrorCode("SESU0013");
                throw err;
            }
        }
        super.openDocument();
    }

    /**
     * Output element start tag
     *
     * @param elemName   the name of the element
     * @param type       the type annotation of the element
     * @param attributes the attributes of this element
     * @param namespaces the in-scope namespaces of this element: generally this is all the in-scope
     *                   namespaces, without relying on inheriting namespaces from parent elements
     * @param location   an object providing information about the module, line, and column where the node originated
     * @param properties bit-significant properties of the element node. If there are no relevant
     *                   properties, zero is supplied. The definitions of the bits are in class {@link ReceiverOption}
     * @throws XPathException if an error occurs
     */
    @Override
    public void startElement(NodeName elemName, SchemaType type, AttributeMap attributes, NamespaceMap namespaces, Location location, int properties) throws XPathException {
        if (!started) {
            openDocument();
            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;
            }

            if (systemId != null || publicId != null) {
                writeDocType(null, "html", systemId, publicId);
            }
            started = true;
        }
        super.startElement(elemName, type, attributes, namespaces, location, properties);
    }

    /**
     * Ask whether control characters should be rejected: true for HTML4, false for HTML5
     *
     * @return true if control characters should be rejected
     */
    @Override
    protected boolean rejectControlCharacters() {
        return true;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy