org.conqat.lib.commons.html.HTMLWriter Maven / Gradle / Ivy
Show all versions of teamscale-lib-commons Show documentation
/*
* Copyright (c) CQSE GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.conqat.lib.commons.html;
import static org.conqat.lib.commons.html.EHTMLAttribute.SRC;
import static org.conqat.lib.commons.html.EHTMLAttribute.TYPE;
import static org.conqat.lib.commons.html.EHTMLElement.SCRIPT;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import org.conqat.lib.commons.filesystem.FileSystemUtils;
import org.conqat.lib.commons.string.StringUtils;
import org.conqat.lib.commons.xml.IXMLResolver;
import org.conqat.lib.commons.xml.XMLWriter;
/**
* This class is used for writing HTML.
*/
public class HTMLWriter extends XMLWriter {
/**
* Creates a new writer for HTML documents.
*
* @param file
* the file to write to.
*/
public HTMLWriter(File file) throws IOException {
this(new PrintStream(file, FileSystemUtils.UTF8_ENCODING));
}
/**
* Creates a new writer for HTML documents.
*
* @param stream
* the stream to print to.
*/
public HTMLWriter(OutputStream stream) {
super(new PrintWriter(wrapStream(stream)), new HTMLResolver());
}
/**
* Helper method for {@link #HTMLWriter(OutputStream)} that wraps the output stream in a writer and
* asserts no exception is thrown.
*/
private static OutputStreamWriter wrapStream(OutputStream stream) {
try {
return new OutputStreamWriter(stream, FileSystemUtils.UTF8_ENCODING);
} catch (UnsupportedEncodingException e) {
throw new AssertionError("UTF-8 should be supported!");
}
}
/**
* Creates a new writer for HTML documents.
*
* @param writer
* the writer to print to.
*/
public HTMLWriter(PrintWriter writer) {
super(writer, new HTMLResolver());
}
/**
* This adds a default header for HTML files consisting of the XML header and a DOCTYPE of the xhtml
* frameset DTD.
*
* XML version is set to "1.0", encoding provided by a parameter, and doc type definition to XHTML
* 1.0 Frameset.
*/
public void addStdHeader(String encoding) {
addHeader("1.0", encoding);
addPublicDocTypeDefinition(EHTMLElement.HTML, "-//W3C//DTD XHTML 1.0 Frameset//EN",
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd");
}
/**
* This adds a default header for HTML files consisting of the XML header and a DOCTYPE of the xhtml
* frameset DTD.
*
* XML version is set to "1.0", encoding to "UTF-8", and doc type definition to XHTML 1.0 Frameset.
*/
public void addStdHeader() {
addStdHeader(FileSystemUtils.UTF8_ENCODING);
}
/**
* {@inheritDoc}
*
* Made this public here.
*/
@Override
public void addRawString(String html) {
super.addRawString(html);
}
/**
* Adds a line separator with closing and open tag (see {@link #addNewLine()}.
*/
public void addRawNewLine() {
addRawString(StringUtils.LINE_SEPARATOR);
}
/** Inserts a script tag that loads JavaScript from a separate file. */
public void addExternalJavaScript(String scriptFilePath) {
// this is required, as some browsers choke on a directly closed
// script element
insertEmptyElement(SCRIPT, SRC, scriptFilePath, TYPE, "text/javascript");
}
/**
* Inserts an empty element but ensures that it is not closed using the shorthand syntax (e.g.
* <div />
).
*
* This is useful since some browsers choke on some shorthand elements, e.g. divs or JavaScript
* tags.
*
* @see #openElement(EHTMLElement, Object...)
*/
public void insertEmptyElement(EHTMLElement element, Object... attributes) {
openElement(element, attributes);
addText(StringUtils.EMPTY_STRING);
closeElement(element);
}
/**
* Adds an attribute to the currently open element but checks in addition if the attribute may be
* added at all.
*
* @throws HTMLWriterException
* if the attribute is not allowed for the current element.
*/
@Override
public void addAttribute(EHTMLAttribute attribute, Object value) {
if (!getCurrentElement().allowsAttribute(attribute)) {
throw new HTMLWriterException("Attribute " + attribute + " not allowed for element " + getCurrentElement());
}
super.addAttribute(attribute, value);
}
/** The resolver used for the {@link HTMLWriter}. */
public static class HTMLResolver implements IXMLResolver {
/** {@inheritDoc} */
@Override
public String resolveAttributeName(EHTMLAttribute attribute) {
return attribute.toString();
}
/** {@inheritDoc} */
@Override
public String resolveElementName(EHTMLElement element) {
return element.toString();
}
/** {@inheritDoc} */
@Override
public Class getAttributeClass() {
return EHTMLAttribute.class;
}
}
}