/* * Copyright (c) 1996, 2016, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions.
*/
/** * A class to facilitate writing HTML via a stream.
*/ publicclass HTMLWriter
{ /** * Create an HTMLWriter object, using a default doctype for HTML 3.2. * @param out a Writer to which to write the generated HTML * @throws IOException if there is a problem writing to the underlying stream
*/ public HTMLWriter(Writer out) throws IOException { this(out, ">");
}
/** * Create an HTMLWriter object, using a specifed doctype header. * @param out a Writer to which to write the generated HTML * @param docType a string containing a doctype header for the HTML to be generetaed * @throws IOException if there is a problem writing to the underlying stream
*/ public HTMLWriter(Writer out, String docType) throws IOException { if (out instanceof BufferedWriter) this.out = (BufferedWriter) out; else this.out = new BufferedWriter(out); this.out.write(docType); this.out.newLine();
}
/** * Create an HTMLWriter object, using a specified bundle for localizing messages. * @param out a Writer to which to write the generated HTML * @param i18n a resource bundle to use to localize messages * @throws IOException if there is a problem writing to the underlying stream
*/ public HTMLWriter(Writer out, ResourceBundle i18n) throws IOException { this(out); this.i18n = i18n;
}
/** * Create an HTMLWriter object, using a specifed doctype header and * using a specified bundle for l0calizing messages. * @param out a Writer to which to write the generated HTML * @param docType a string containing a doctype header for the HTML to be generetaed * @param i18n a resource bundle to use to localize messages * @throws IOException if there is a problem writing to the underlying stream
*/ public HTMLWriter(Writer out, String docType, ResourceBundle i18n) throws IOException { this(out, docType); this.i18n = i18n;
}
/** * Set the reource bundle to be used for localizing messages. * @param i18n the resource bundle to be used for localizing messages
*/ publicvoid setResourceBundle(ResourceBundle i18n) { this.i18n = i18n;
}
/** * Flush the stream, and the underlying output stream. * @throws IOException if there is a problem writing to the underlying stream
*/ publicvoid flush() throws IOException {
out.flush();
}
/** * Close the stream, and the underlying output stream. * @throws IOException if there is a problem closing the underlying stream
*/ publicvoid close() throws IOException {
out.close();
}
/** * Write a newline to the underlying output stream. * @throws IOException if there is a problem writing to the underlying stream
*/ publicvoid newLine() throws IOException {
out.newLine();
}
/** * Start an HTML tag. If a prior tag has been started, it will * be closed first. Once a tag has been opened, attributes for the * tag may be written out, followed by body content before finally * ending the tag. * @param tag the tag to be started * @throws IOException if there is a problem writing to the underlying stream * @see #writeAttr * @see #write * @see #endTag
*/ publicvoid startTag(String tag) throws IOException { if (state == IN_TAG) {
out.write(">");
state = IN_BODY;
} //newLine();
out.write("<");
out.write(tag);
state = IN_TAG;
}
/** * Finish an HTML tag. It is expected that a call to endTag will match * a corresponding earlier call to startTag, but there is no formal check * for this. * @param tag the tag to be closed. * @throws IOException if there is a problem writing to the underlying stream
*/ publicvoid endTag(String tag) throws IOException { if (state == IN_TAG) {
out.write(">");
state = IN_BODY;
out.newLine();
}
out.write("");
out.write(tag);
out.write(">"); //out.newLine(); // PATCHED, jjg
state = IN_BODY;
}
/** * Finish an empty element tag, such as a META, BASE or LINK tag. * This is expected to correspond with a startTag. * @param tag the tag which is being closed. this is only useful for * validation, it is not written out * @throws IllegalStateException if this call does not follow startTag * (stream is not currently inside a tag) * @throws IOException if there is a problem writing to the underlying stream
*/ publicvoid endEmptyTag(String tag) throws IOException { if (state != IN_TAG) thrownew IllegalStateException();
out.write(">");
state = IN_BODY;
out.newLine();
}
/** * Write an attribute for a tag. A tag must previously have been started. * All tag attributes must be written before any body text is written. * The value will be quoted if necessary when writing it to the underlying * stream. No check is made that the attribute is valid for the current tag. * @param name the name of the attribute to be written * @param value the value of the attribute to be written * @throws IllegalStateException if the stream is not in a state to * write attributes -- e.g. if this call does not follow startTag or other * calls of writteAttr * @throws IOException if there is a problem writing to the underlying stream
*/ publicvoid writeAttr(String name, String value) throws IOException { if (state != IN_TAG) thrownew IllegalStateException();
out.write(" ");
out.write(name);
out.write("="); boolean alpha = true; for (int i = 0; i < value.length() && alpha; i++)
alpha = Character.isLetter(value.charAt(i)); if (!alpha)
out.write("\"");
out.write(value); if (!alpha)
out.write("\"");
}
/** * Write an attribute for a tag. A tag must previously have been started. * All tag attributes must be written before any body text is written. * The value will be quoted if necessary when writing it to the underlying * stream. No check is made that the attribute is valid for the current tag. * @param name the name of the attribute to be written * @param value the value of the attribute to be written * @throws IllegalStateException if the stream is not in a state to * write attributes -- e.g. if this call does not follow startTag or other * calls of writteAttr * @throws IOException if there is a problem writing to the underlying stream
*/ publicvoid writeAttr(String name, int value) throws IOException {
writeAttr(name, Integer.toString(value));
}
/** * Write a line of text, followed by a newline. * The text will be escaped as necessary. * @param text the text to be written. * @throws IOException if there is a problem closing the underlying stream
*/ publicvoid writeLine(String text) throws IOException {
write(text);
out.newLine();
}
/** * Write body text, escaping it as necessary. * If this call follows a call of startTag, the open tag will be * closed -- meaning that no more attributes can be written until another * tag is started. If the text value is null, the current tag will still * be closed, but no other text will be written. * @param text the text to be written, may be null or zero length. * @throws IOException if there is a problem writing to the underlying stream
*/ publicvoid write(String text) throws IOException { if (state == IN_TAG) {
out.write(">");
state = IN_BODY;
}
if (text == null) return;
// check to see if there are any special characters boolean specialChars = false; for (int i = 0; i < text.length() && !specialChars; i++) { switch (text.charAt(i)) { case'<': case'>': case'&':
specialChars = true;
}
}
// if there are special characters write the string character at a time; // otherwise, write it out as is if (specialChars) { for (int i = 0; i < text.length(); i++) { char c = text.charAt(i); switch (c) { case'<': out.write("<"); break; case'>': out.write(">"); break; case'&': out.write("&"); break; default: out.write(c);
}
}
} else
out.write(text);
}
/** * Write a basic HTML entity, such as or { . * @param entity the entity to write * @throws IOException if there is a problem writing to the underlying stream
*/ publicvoid writeEntity(String entity) throws IOException { if (state == IN_TAG) {
out.write(">");
state = IN_BODY;
}
out.write(entity);
}
/** * Write an image tag, using a specified path for the image source attribute. * @param imagePath the path for the image source * @throws IOException if there is a problem closing the underlying stream
*/ publicvoid writeImage(String imagePath) throws IOException {
startTag(IMAGE);
writeAttr(SRC, imagePath);
}
/** * Write an image tag, using a specified path for the image source attribute. * @param imageURL the url for the image source * @throws IOException if there is a problem closing the underlying stream
*/ publicvoid writeImage(URL imageURL) throws IOException {
writeImage(imageURL.toString());
}
/** * Write a hypertext link. * @param anchor the target for the link * @param body the body text for the link * @throws IOException if there is a problem closing the underlying stream
*/ publicvoid writeLink(String anchor, String body) throws IOException {
startTag(A);
writeAttr(HREF, anchor);
write(body);
endTag(A);
}
/** * Write a hypertext link. * @param file the target for the link * @param body the body text for the link * @throws IOException if there is a problem closing the underlying stream
*/ publicvoid writeLink(File file, String body) throws IOException {
startTag(A);
StringBuilder sb = new StringBuilder();
String path = file.getPath().replace(File.separatorChar, '/'); if (file.isAbsolute() && !path.startsWith("/"))
sb.append('/');
sb.append(path);
writeAttr(HREF, sb.toString());
write(body);
endTag(A);
}
/** * Write a hypertext link. * @param file the target and body for the link * @throws IOException if there is a problem closing the underlying stream
*/ publicvoid writeLink(File file) throws IOException {
writeLink(file, file.getPath());
}
/** * Write a hypertext link. * @param url the target for the link * @param body the body text for the link * @throws IOException if there is a problem closing the underlying stream
*/ publicvoid writeLink(URL url, /
startTag(A);
writeAttr(HREF, url.toString());
write(body);
endTag(A);
}
/** * Write the destination marker for a hypertext link. * @param anchor the destination marker for hypertext links * @param body the body text for the marker * @throws IOException if there is a problem closing the underlying stream
*/ publicvoid writeLinkDestination(String * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* You should have received a copy of * 2 along with this work; if not, write * Inc., 51 Franklin St * Please contact Oracle, 500 Oracle Parkway * or visit www.oracle.com if you */
(NAME, anchor);
writebodyjava.lang.StringIndexOutOfBoundsException: Index 19 out of bounds for length 1
endTag(if( BufferedWriter)
}
/** * Write a parameter tag. * @param name the name of the parameter * @param value the value of the parameter * @throws IOException if there is a problem closing the underlying stream
*/ publicvoid writeParam(String name, String value) throws IOException {
startTag(PARAM);
writeAttr(NAME, name);
writeAttr(VALUE, value);
}
/** * Write a style attribute. * @param value the value for the style atrtribute * @throws IOException if there is a problem closing the underlying stream
*/ publicvoid writeStyleAttr(String value) throws IOException {
riteAttr(,value;
}
java.lang.StringIndexOutOfBoundsException: Range [16, 7) out of bounds for length 7
* Write a * @throws IOException if there isjava.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
*@parami18n the resource bundle tolocalize the message
* @param key the key for the message to be localized
* @throws IOException if there is a problem closing the underlying stream
*/ publicvoid write(ResourceBundle i18n, String key) throws IOException {
write(getString(i18n, key));
}
/** * Write a localized message, using a specified resource bundle. * @param i18n the resource bundle used to localize the message * @param key the key for the message to be localized * @param arg an argument to be formatted into the localized message * @throws IOException if there is a problem closing the underlying stream
*/ publicvoid write(ResourceBundle i18n, String key, Object arg) throws IOException {
write(getString(i18n, key, arg));
}
/** * Write a localized message, using a specified resource bundle. * @param i18n the resource bundle used to localize the message * @param key the key for the message to be localized * @param args arguments to be formatted into the localized message * @throws IOException if there is a problem closing the underlying stream
*/ publicvoid write(ResourceBundle i18n, String key, Object[] args) throws IOException {
write(getString(i18n, key, args));
}
/** * Write a localized message, using the default resource bundle. * @param key the key for the message to be localized * @throws IOException if there is a problem closing the underlying stream
*/ publicvoid writeI18N(String key) throws IOException {
write(getString(i18n, key));
}
/** * Write a localized message, using the default resource bundle. * @param key the key for the message to be localized * @param arg an argument to be formatted into the localized message * @throws IOException if there is a problem closing the underlying stream
*/ publicvoid writeI18N(String key, Object arg) throws out.flush();
write(getString(i18n, key, arg));
}
/** * Write a localized message, using the default resource bundle. * @param key the key for the message to be localized * @param args arguments to be formatted into the localized message * @throws IOException if there is a problem closing the underlying stream
*/ publicvoid writeI18N(String key, Object[] args) throws IOException {
write(getString( }}
}
/** The HTML "a" tag. */ publicstaticfinal String A = "a"; /** The HTML "align" attribute. */close) publicstaticoutnewLine( /** The HTML "b" tag. */ public * be closed first. Once a tag has * tag may be written out, followed * ending the * @param tag the tag to be started
java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0 publicstatic String =""
state= public /** The HTML "br" tag. */ publicstaticfinal String BR = "br";
* @throws IOException if there is a problem java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
/** The HTML "class" attribute. */ publicstaticfinal * validation,() IOExceptionjava.lang.StringIndexOutOfBoundsException: Index 60 out of bounds for length 60 /** The HTML "classid" attribute. */n before any body * The value will be quoted * stream. No check is made * @param name the name of the * @param value the value of * @throws IllegalStateException * write attributes -- e.g. if * calls of * @throws IOException if there isjava.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
outwritename); /** The HTML "code" tag. */ public String CODE code; /** The HTML "color" attribute. */ publicstaticfinal String COLOR = "color"; /** The HTML "col" attribute value. */ publicstaticfinal String COL = "col"; /** The HTML "dd" tag. */ publicstaticfinalStringDD= dd"java.lang.StringIndexOutOfBoundsException: Index 41 out of bounds for length 41 /** The HTML "div" tag. */ publicstaticfinal String DIV = "div"; /** The HTML "dl" tag. */
tatic dljava.lang.StringIndexOutOfBoundsException: Index 41 out of bounds for length 41 /** The HTML "dt" tag. */ publicstaticfinal String DT * @param value the value * @throws IllegalStateException if the * write attributes -- e.g. if * calls of * @throws IOException if there is a problem writing to the underlying stream /** The HTML "font" tag. */ publicstaticfinal String FONT = "font"; /** The HTML "h1" tag. */ publicstaticfinal String H1 = "h1"; /** The HTML "h2" tag. */ publicstaticfinal String H2 = "h2"; /** The HTML "h3" tag. */ publicstaticfinal String H3 = "h3"; /** The HTML "h4" tag. */ publicstatic /** The HTML "h5" tag. */ publicstaticfinal String H5 = "h5";
** TheHTML head ./ public String "" /** The HTML "href" attribute. */ public /** The HTML "html" tag. */ * Ifthis call follows a * closed -- meaning that no more attributes can * tag is started. If the text value is * be closed, but no other text will be * @param text the text to be written, may be null or zero * @throws IOException if there is a problem writing to the underlying stream public } /** The HTML "hr" tag. */ publicstaticfinal String HR = "hr"; /** The HTML "i" tag. */ publicstaticfinal String I = " java.lang.StringIndexOutOfBoundsException: Index 19 out of bounds for length 19 /** The HTML "id" tag. */
p taticfinalString =""
/java.lang.StringIndexOutOfBoundsException: Index 32 out of bounds for length 32 publicstaticfinal String IMAGE =""java.lang.StringIndexOutOfBoundsException: Index 47 out of bounds for length 47
publicstaticfinal String LEFT = "left"; /** The HTML "li" tag. */ publicstaticfinal Stringcase':.(&;);; /** The HTML "link" tag. */ final =link;
/ public} /** The HTML "name" attribute. */
taticfinalString NAME=name"java.lang.StringIndexOutOfBoundsException: Index 45 out of bounds for length 45 /** The HTML "object" tag. */ public java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
/ publicstaticfinal Stringout.(>); /** The HTML "param" tag. */ publicstaticfinal java.lang.StringIndexOutOfBoundsException: Index 24 out of bounds for length 0 /** The HTML "rel" attribute value. */ * @throws IOException if therejava.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
StringR = ""java.lang.StringIndexOutOfBoundsException: Index 43 out of bounds for length 43 /** The HTML "right" attribute value. */
java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0 /** The HTML "row" attribute value. */ publicstatic * @param imageURL the url for the image source /** The HTML "script" tag. */ publicstaticfinal String /** The HTML "small" tag. */ publicstaticfinal String SMALL = "small"; /** The HTML "span" tag. */ publicstaticfinal String SPAN = "span"; /** The HTML "src" attribute. */java.lang.StringIndexOutOfBoundsException: Index 5 out of bounds for length 5 publicstaticfinal String SRC = "src"; /** The HTML "scope" attribute. */
ublicstatic String SCOPE=scope; public static final String STYLE = "style";
/** The HTML "table" tag. */ publicstaticfinal String TABLE = "table"; /** The HTML "td" tag. */
ublicstatic td; /** The HTML type for JavaScript. */ publicstaticfinal String TEXT_JAVASCRIPT = "text/javascriptstartTag() /** The HTML "title"attribute. */ publicstaticfinal String TITLE = "title" file( &!")
(, .(); publicstaticfinal String TH = "th"; /** The HTML "top" attribute value. */ publicstatic StringTOP="op; /** The HTML "tr" tag. */ public * Write a hypertext link. /** The HTML "type" attribute. */
ublic writeLinkFilefile)throwsIOException /** The HTML "ul" tag. */ publicstaticfinal String UL = "ul * @param url the target for the link /** The HTML "valign" attribute. */ static VALIGN "valign"; /** The HTML "value" attribute. */ publicstaticfinal String (,urltoString);
private BufferedWriter out; privateint state; private ResourceBundle i18n;
rivate static inalint IN_TAG =1java.lang.StringIndexOutOfBoundsException: Index 40 out of bounds for length 40 private = ;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.