/* * Copyright (c) 2004-2014, Willem Cazander * All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, are permitted provided * that the following conditions are met: * * * Redistributions of source code must retain the above copyright notice, this list of conditions and the * following disclaimer. * * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and * the following disclaimer in the documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ package org.x4o.sax3; import java.io.IOException; import java.io.Writer; import java.util.Arrays; import java.util.Calendar; import java.util.Collections; import java.util.List; import org.x4o.sax3.io.ContentCloseable; import org.x4o.sax3.io.SAX3PropertyConfig; import org.x4o.sax3.io.SAX3XMLConstants; import org.xml.sax.SAXException; import org.xml.sax.helpers.AttributesImpl; /** * ContentWriterHtml writes HTML events as SAX events to XML. * * @author Willem Cazander * @version 1.0 Apr 30, 2013 */ public class SAX3WriterHtml extends SAX3WriterEnum { static public final List HTML_VOID_TAGS = Collections.unmodifiableList(Tag.valuesVoidElement()); public SAX3WriterHtml(Writer out, String encoding) { super(new SAX3WriterXml(out, encoding), "", SAX3XMLConstants.NULL_NS_URI); getPropertyConfig().setProperty(SAX3WriterXml.OUTPUT_FOLD_EMPTY_TAGS, HTML_VOID_TAGS); } public SAX3PropertyConfig getPropertyConfig() { return getContentWriterWrapped().getPropertyConfig(); } public void printDocType(DocType doc) throws IOException { try { getContentWriterWrapped().startDTD(doc.getName(), doc.getPublicId(), doc.getSystemId()); } catch (SAXException e) { throw new IOException(e); } } public void printHtmlStart(String language) throws IOException { AttributesImpl atts = new AttributesImpl(); if (language != null) { atts.addAttribute("", "lang", "", "", language); } printTagStart(Tag.html, atts); } public void printHtmlEnd() throws IOException { printTagEnd(Tag.html); } public void printHeadMetaDate() throws IOException { Calendar cal = Calendar.getInstance(); printHeadMeta("date", cal.get(Calendar.YEAR) + "-" + (cal.get(Calendar.MONTH) + 1) + "-" + cal.get(Calendar.DAY_OF_MONTH)); } public void printHeadTitle(String title) throws IOException { printTagCharacters(Tag.title, title); } public void printHeadMetaContentType() throws IOException { AttributesImpl atts = new AttributesImpl(); atts.addAttribute("", "http-equiv", "", "", "Content-Type"); atts.addAttribute("", "content", "", "", "text/html"); atts.addAttribute("", "charset", "", "", getPropertyConfig().getPropertyString(SAX3WriterXml.OUTPUT_ENCODING)); printTagStartEnd(Tag.meta, atts); } public void printHeadMeta(String name, String content) throws IOException { AttributesImpl atts = new AttributesImpl(); atts.addAttribute("", "name", "", "", name); atts.addAttribute("", "content", "", "", content); printTagStartEnd(Tag.meta, atts); } public void printHeadLinkCss(String cssUrl) throws IOException { AttributesImpl atts = new AttributesImpl(); atts.addAttribute("", "rel", "", "", "stylesheet"); atts.addAttribute("", "type", "", "", "text/css"); atts.addAttribute("", "title", "", "", "Style"); atts.addAttribute("", "href", "", "", cssUrl); printTagStartEnd(Tag.link, atts); } public void printScriptSrc(String jsUrl) throws IOException { AttributesImpl atts = new AttributesImpl(); atts.addAttribute("", "type", "", "", "text/javascript"); atts.addAttribute("", "src", "", "", jsUrl); printTagStart(Tag.script, atts); printCharacters(" "); // NOTE: space needed to keep browsers happy printTagEnd(Tag.script); } public void printScriptInline(String script) throws IOException { AttributesImpl atts = new AttributesImpl(); atts.addAttribute("", "type", "", "", "text/javascript"); printTagStart(Tag.script, atts); printComment(script); printTagEnd(Tag.script); } public void printScriptNoDiv() throws IOException { printScriptNoDiv(null); } public void printScriptNoDiv(String text) throws IOException { if (text == null) { text = "JavaScript is disabled on your browser."; } printTagStart(Tag.noscript); printTagStart(Tag.div); printCharacters(text); printTagEnd(Tag.div); printTagEnd(Tag.noscript); } public void printHrefNamed(String name) throws IOException { AttributesImpl atts = new AttributesImpl(); atts.addAttribute("", "name", "", "", name); printTagStart(Tag.a, atts); printComment(" "); printTagEnd(Tag.a); } public void printHrefTarget(String href, String target, String text) throws IOException { AttributesImpl atts = new AttributesImpl(); atts.addAttribute("", "href", "", "", href); atts.addAttribute("", "target", "", "", target); printTagStart(Tag.a, atts); printCharacters(text); printTagEnd(Tag.a); } public void printHref(String href, String text) throws IOException { printHref(href, text, text); } public void printHref(String href, String text, String title) throws IOException { printHref(href, text, title, null); } public void printHref(String href, String text, String title, String spanClass) throws IOException { AttributesImpl atts = new AttributesImpl(); atts.addAttribute("", "href", "", "", href); if (title != null) { atts.addAttribute("", "title", "", "", title); } printTagStart(Tag.a, atts); if (spanClass != null) { atts = new AttributesImpl(); if (spanClass.length() > 0) { atts.addAttribute("", "class", "", "", spanClass); } printTagStart(Tag.span, atts); } printCharacters(text); if (spanClass != null) { printTagEnd(Tag.span); } printTagEnd(Tag.a); } public void printTagCharacters(Tag tag, String text, String tagClass) throws IOException { printTagCharacters(tag, text, tagClass, null); } public void printTagCharacters(Tag tag, String text, String tagClass, String tagId) throws IOException { printTagStart(tag, tagClass, tagId, null); if (text == null) { text = " "; } printCharacters(text); printTagEnd(tag); } public ContentCloseable printTag(Tag tag, String tagClass) throws IOException { return printTag(tag, tagClass, null, null); } public ContentCloseable printTag(Tag tag, Enum tagClassEnum) throws IOException { return printTag(tag, tagClassEnum, null); } public ContentCloseable printTag(Tag tag, Enum tagClassEnum, String tagId) throws IOException { return printTag(tag, tagClassEnum, tagId, null); } public ContentCloseable printTag(Tag tag, Enum tagClassEnum, String tagId, String typeId) throws IOException { return printTag(tag, tagClassEnum.name(), tagId, typeId); } public ContentCloseable printTag(Tag tag, String tagClass, String tagId, String typeId) throws IOException { printTagStart(tag, tagClass, tagId, typeId); return () -> printTagEnd(tag); } public void printTagStart(Tag tag, String tagClass) throws IOException { printTagStart(tag, tagClass, null, null); } public void printTagStart(Tag tag, Enum tagClassEnum) throws IOException { printTagStart(tag, tagClassEnum, null); } public void printTagStart(Tag tag, Enum tagClassEnum, String tagId) throws IOException { printTagStart(tag, tagClassEnum, tagId, null); } public void printTagStart(Tag tag, Enum tagClassEnum, String tagId, String typeId) throws IOException { printTagStart(tag, tagClassEnum.name(), tagId, typeId); } public void printTagStart(Tag tag, String tagClass, String tagId, String typeId) throws IOException { AttributesImpl atts = new AttributesImpl(); if (tagId != null && tagId.length() > 0) { atts.addAttribute("", "id", "", "", tagId); } if (tagClass != null && tagClass.length() > 0) { atts.addAttribute("", "class", "", "", tagClass); } if (typeId != null && typeId.length() > 0) { atts.addAttribute("", "type", "", "", typeId); } printTagStart(tag, atts); } public enum Tag { /* Deprecated TAGS */ frameset, frame(true), noframes, tt, font, dir, center, strike, big, basefont, acronym, applet, iframe, menuitem(true), keygen(true), command(true), /* HTML 4 TAGS */ html, head, title, meta(true), link(true), base(true), body, script, style, h1, h2, h3, h4, h5, h6, a, div, span, p, pre, img(true), hr(true), br(true), b, em, strong, small, noscript, ul, li, dl, dt, dd, ol, table, thead, tfoot, tbody, caption, th, tr, td, abbr, address, area(true), bdo, blockquote, cite, code, col(true), colgroup, del, dfn, i, ins, kbd, legend, map, menu, object, param(true), optgroup, q, s, samp, sub, u, var, form, fieldset, input(true), option, label, button, select, textarea, /* HTML 5 TAGS */ canvas, audio, video, source(true), embed(true), track(true), datalist, output, article, aside, bdi, details, dialog, summary, figure, figcaption, footer, header, hgroup, mark, meter, nav, progress, ruby, rt, rp, section, time, wbr(true), ; private final boolean voidElement; private Tag() { this(false); } private Tag(boolean voidElement) { this.voidElement = voidElement; } public boolean voidElement() { return voidElement; } static public List valuesVoidElement() { return Arrays.stream(values()).filter(v -> v.voidElement()).map(v -> v.name()).toList(); } } private final static String DOCTYPE_NAME = "HTML PUBLIC"; public enum DocType { /* Order from worst to better. */ HTML_5("html", "", ""), HTML_4_FRAMESET(DOCTYPE_NAME, "\"-//W3C//DTD HTML 4.01 Frameset//EN\"", "http://www.w3.org/TR/html4/frameset.dtd"), HTML_4_TRANSITIONAL(DOCTYPE_NAME, "\"-//W3C//DTD HTML 4.01 Transitional//EN\"", "http://www.w3.org/TR/html4/loose.dtd"), HTML_4_STRICT(DOCTYPE_NAME, "\"-//W3C//DTD HTML 4.01//EN\"", "http://www.w3.org/TR/html4/strict.dtd"), XHTML_1_FRAMESET(DOCTYPE_NAME, "\"-//W3C//DTD XHTML 1.0 Frameset//EN\"", "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd"), XHTML_1_TRANSITIONAL(DOCTYPE_NAME, "\"-//W3C//DTD XHTML 1.0 Transitional//EN\"", "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"), XHTML_1_STRICT(DOCTYPE_NAME, "\"-//W3C//DTD XHTML 1.0 Strict//EN\"", "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"), XHTML_11(DOCTYPE_NAME, "\"-//W3C//DTD XHTML 1.1//EN\"", "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"),; private final String name; private final String publicId; private final String systemId; private DocType(String name, String publicId, String systemId) { this.name = name; this.publicId = publicId; this.systemId = systemId; } /** * @return the name */ public String getName() { return name; } /** * @return the publicId */ public String getPublicId() { return publicId; } /** * @return the systemId */ public String getSystemId() { return systemId; } } }