Fri, 24 Apr 2020 03:58:51 +0100
Merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 1996,2010, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. |
aoqi@0 | 8 | * |
aoqi@0 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 13 | * accompanied this code). |
aoqi@0 | 14 | * |
aoqi@0 | 15 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 18 | * |
aoqi@0 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 20 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 21 | * questions. |
aoqi@0 | 22 | */ |
aoqi@0 | 23 | |
aoqi@0 | 24 | |
aoqi@0 | 25 | |
aoqi@0 | 26 | import java.io.BufferedWriter; |
aoqi@0 | 27 | import java.io.File; |
aoqi@0 | 28 | import java.io.IOException; |
aoqi@0 | 29 | import java.io.Writer; |
aoqi@0 | 30 | import java.net.URL; |
aoqi@0 | 31 | import java.text.MessageFormat; |
aoqi@0 | 32 | import java.util.ResourceBundle; |
aoqi@0 | 33 | |
aoqi@0 | 34 | /** |
aoqi@0 | 35 | * A class to facilitate writing HTML via a stream. |
aoqi@0 | 36 | */ |
aoqi@0 | 37 | public class HTMLWriter |
aoqi@0 | 38 | { |
aoqi@0 | 39 | /** |
aoqi@0 | 40 | * Create an HTMLWriter object, using a default doctype for HTML 3.2. |
aoqi@0 | 41 | * @param out a Writer to which to write the generated HTML |
aoqi@0 | 42 | * @throws IOException if there is a problem writing to the underlying stream |
aoqi@0 | 43 | */ |
aoqi@0 | 44 | public HTMLWriter(Writer out) throws IOException { |
aoqi@0 | 45 | this(out, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 3.2//EN\">"); |
aoqi@0 | 46 | } |
aoqi@0 | 47 | |
aoqi@0 | 48 | /** |
aoqi@0 | 49 | * Create an HTMLWriter object, using a specifed doctype header. |
aoqi@0 | 50 | * @param out a Writer to which to write the generated HTML |
aoqi@0 | 51 | * @param docType a string containing a doctype header for the HTML to be generetaed |
aoqi@0 | 52 | * @throws IOException if there is a problem writing to the underlying stream |
aoqi@0 | 53 | */ |
aoqi@0 | 54 | public HTMLWriter(Writer out, String docType) throws IOException { |
aoqi@0 | 55 | if (out instanceof BufferedWriter) |
aoqi@0 | 56 | this.out = (BufferedWriter) out; |
aoqi@0 | 57 | else |
aoqi@0 | 58 | this.out = new BufferedWriter(out); |
aoqi@0 | 59 | this.out.write(docType); |
aoqi@0 | 60 | this.out.newLine(); |
aoqi@0 | 61 | } |
aoqi@0 | 62 | |
aoqi@0 | 63 | /** |
aoqi@0 | 64 | * Create an HTMLWriter object, using a specified bundle for localizing messages. |
aoqi@0 | 65 | * @param out a Writer to which to write the generated HTML |
aoqi@0 | 66 | * @param i18n a resource bundle to use to localize messages |
aoqi@0 | 67 | * @throws IOException if there is a problem writing to the underlying stream |
aoqi@0 | 68 | */ |
aoqi@0 | 69 | public HTMLWriter(Writer out, ResourceBundle i18n) throws IOException { |
aoqi@0 | 70 | this(out); |
aoqi@0 | 71 | this.i18n = i18n; |
aoqi@0 | 72 | } |
aoqi@0 | 73 | |
aoqi@0 | 74 | |
aoqi@0 | 75 | /** |
aoqi@0 | 76 | * Create an HTMLWriter object, using a specifed doctype header and |
aoqi@0 | 77 | * using a specified bundle for l0calizing messages. |
aoqi@0 | 78 | * @param out a Writer to which to write the generated HTML |
aoqi@0 | 79 | * @param docType a string containing a doctype header for the HTML to be generetaed |
aoqi@0 | 80 | * @param i18n a resource bundle to use to localize messages |
aoqi@0 | 81 | * @throws IOException if there is a problem writing to the underlying stream |
aoqi@0 | 82 | */ |
aoqi@0 | 83 | public HTMLWriter(Writer out, String docType, ResourceBundle i18n) throws IOException { |
aoqi@0 | 84 | this(out, docType); |
aoqi@0 | 85 | this.i18n = i18n; |
aoqi@0 | 86 | } |
aoqi@0 | 87 | |
aoqi@0 | 88 | /** |
aoqi@0 | 89 | * Set the reource bundle to be used for localizing messages. |
aoqi@0 | 90 | * @param i18n the resource bundle to be used for localizing messages |
aoqi@0 | 91 | */ |
aoqi@0 | 92 | public void setResourceBundle(ResourceBundle i18n) { |
aoqi@0 | 93 | this.i18n = i18n; |
aoqi@0 | 94 | } |
aoqi@0 | 95 | |
aoqi@0 | 96 | /** |
aoqi@0 | 97 | * Flush the stream, and the underlying output stream. |
aoqi@0 | 98 | * @throws IOException if there is a problem writing to the underlying stream |
aoqi@0 | 99 | */ |
aoqi@0 | 100 | public void flush() throws IOException { |
aoqi@0 | 101 | out.flush(); |
aoqi@0 | 102 | } |
aoqi@0 | 103 | |
aoqi@0 | 104 | /** |
aoqi@0 | 105 | * Close the stream, and the underlying output stream. |
aoqi@0 | 106 | * @throws IOException if there is a problem closing the underlying stream |
aoqi@0 | 107 | */ |
aoqi@0 | 108 | public void close() throws IOException { |
aoqi@0 | 109 | out.close(); |
aoqi@0 | 110 | } |
aoqi@0 | 111 | |
aoqi@0 | 112 | /** |
aoqi@0 | 113 | * Write a newline to the underlying output stream. |
aoqi@0 | 114 | * @throws IOException if there is a problem writing to the underlying stream |
aoqi@0 | 115 | */ |
aoqi@0 | 116 | public void newLine() throws IOException { |
aoqi@0 | 117 | out.newLine(); |
aoqi@0 | 118 | } |
aoqi@0 | 119 | |
aoqi@0 | 120 | /** |
aoqi@0 | 121 | * Start an HTML tag. If a prior tag has been started, it will |
aoqi@0 | 122 | * be closed first. Once a tag has been opened, attributes for the |
aoqi@0 | 123 | * tag may be written out, followed by body content before finally |
aoqi@0 | 124 | * ending the tag. |
aoqi@0 | 125 | * @param tag the tag to be started |
aoqi@0 | 126 | * @throws IOException if there is a problem writing to the underlying stream |
aoqi@0 | 127 | * @see #writeAttr |
aoqi@0 | 128 | * @see #write |
aoqi@0 | 129 | * @see #endTag |
aoqi@0 | 130 | */ |
aoqi@0 | 131 | public void startTag(String tag) throws IOException { |
aoqi@0 | 132 | if (state == IN_TAG) { |
aoqi@0 | 133 | out.write(">"); |
aoqi@0 | 134 | state = IN_BODY; |
aoqi@0 | 135 | } |
aoqi@0 | 136 | //newLine(); |
aoqi@0 | 137 | out.write("<"); |
aoqi@0 | 138 | out.write(tag); |
aoqi@0 | 139 | state = IN_TAG; |
aoqi@0 | 140 | } |
aoqi@0 | 141 | |
aoqi@0 | 142 | /** |
aoqi@0 | 143 | * Finish an HTML tag. It is expected that a call to endTag will match |
aoqi@0 | 144 | * a corresponding earlier call to startTag, but there is no formal check |
aoqi@0 | 145 | * for this. |
aoqi@0 | 146 | * @param tag the tag to be closed. |
aoqi@0 | 147 | * @throws IOException if there is a problem writing to the underlying stream |
aoqi@0 | 148 | */ |
aoqi@0 | 149 | public void endTag(String tag) throws IOException { |
aoqi@0 | 150 | if (state == IN_TAG) { |
aoqi@0 | 151 | out.write(">"); |
aoqi@0 | 152 | state = IN_BODY; |
aoqi@0 | 153 | out.newLine(); |
aoqi@0 | 154 | } |
aoqi@0 | 155 | out.write("</"); |
aoqi@0 | 156 | out.write(tag); |
aoqi@0 | 157 | out.write(">"); |
aoqi@0 | 158 | //out.newLine(); // PATCHED, jjg |
aoqi@0 | 159 | state = IN_BODY; |
aoqi@0 | 160 | } |
aoqi@0 | 161 | |
aoqi@0 | 162 | /** |
aoqi@0 | 163 | * Finish an empty element tag, such as a META, BASE or LINK tag. |
aoqi@0 | 164 | * This is expected to correspond with a startTag. |
aoqi@0 | 165 | * @param tag the tag which is being closed. this is only useful for |
aoqi@0 | 166 | * validation, it is not written out |
aoqi@0 | 167 | * @throws IllegalStateException if this call does not follow startTag |
aoqi@0 | 168 | * (stream is not currently inside a tag) |
aoqi@0 | 169 | * @throws IOException if there is a problem writing to the underlying stream |
aoqi@0 | 170 | */ |
aoqi@0 | 171 | public void endEmptyTag(String tag) throws IOException { |
aoqi@0 | 172 | if (state != IN_TAG) |
aoqi@0 | 173 | throw new IllegalStateException(); |
aoqi@0 | 174 | |
aoqi@0 | 175 | out.write(">"); |
aoqi@0 | 176 | state = IN_BODY; |
aoqi@0 | 177 | out.newLine(); |
aoqi@0 | 178 | } |
aoqi@0 | 179 | |
aoqi@0 | 180 | /** |
aoqi@0 | 181 | * Write an attribute for a tag. A tag must previously have been started. |
aoqi@0 | 182 | * All tag attributes must be written before any body text is written. |
aoqi@0 | 183 | * The value will be quoted if necessary when writing it to the underlying |
aoqi@0 | 184 | * stream. No check is made that the attribute is valid for the current tag. |
aoqi@0 | 185 | * @param name the name of the attribute to be written |
aoqi@0 | 186 | * @param value the value of the attribute to be written |
aoqi@0 | 187 | * @throws IllegalStateException if the stream is not in a state to |
aoqi@0 | 188 | * write attributes -- e.g. if this call does not follow startTag or other |
aoqi@0 | 189 | * calls of writteAttr |
aoqi@0 | 190 | * @throws IOException if there is a problem writing to the underlying stream |
aoqi@0 | 191 | */ |
aoqi@0 | 192 | public void writeAttr(String name, String value) throws IOException { |
aoqi@0 | 193 | if (state != IN_TAG) |
aoqi@0 | 194 | throw new IllegalStateException(); |
aoqi@0 | 195 | |
aoqi@0 | 196 | out.write(" "); |
aoqi@0 | 197 | out.write(name); |
aoqi@0 | 198 | out.write("="); |
aoqi@0 | 199 | boolean alpha = true; |
aoqi@0 | 200 | for (int i = 0; i < value.length() && alpha; i++) |
aoqi@0 | 201 | alpha = Character.isLetter(value.charAt(i)); |
aoqi@0 | 202 | if (!alpha) |
aoqi@0 | 203 | out.write("\""); |
aoqi@0 | 204 | out.write(value); |
aoqi@0 | 205 | if (!alpha) |
aoqi@0 | 206 | out.write("\""); |
aoqi@0 | 207 | } |
aoqi@0 | 208 | |
aoqi@0 | 209 | /** |
aoqi@0 | 210 | * Write an attribute for a tag. A tag must previously have been started. |
aoqi@0 | 211 | * All tag attributes must be written before any body text is written. |
aoqi@0 | 212 | * The value will be quoted if necessary when writing it to the underlying |
aoqi@0 | 213 | * stream. No check is made that the attribute is valid for the current tag. |
aoqi@0 | 214 | * @param name the name of the attribute to be written |
aoqi@0 | 215 | * @param value the value of the attribute to be written |
aoqi@0 | 216 | * @throws IllegalStateException if the stream is not in a state to |
aoqi@0 | 217 | * write attributes -- e.g. if this call does not follow startTag or other |
aoqi@0 | 218 | * calls of writteAttr |
aoqi@0 | 219 | * @throws IOException if there is a problem writing to the underlying stream |
aoqi@0 | 220 | */ |
aoqi@0 | 221 | public void writeAttr(String name, int value) throws IOException { |
aoqi@0 | 222 | writeAttr(name, Integer.toString(value)); |
aoqi@0 | 223 | } |
aoqi@0 | 224 | |
aoqi@0 | 225 | /** |
aoqi@0 | 226 | * Write a line of text, followed by a newline. |
aoqi@0 | 227 | * The text will be escaped as necessary. |
aoqi@0 | 228 | * @param text the text to be written. |
aoqi@0 | 229 | * @throws IOException if there is a problem closing the underlying stream |
aoqi@0 | 230 | */ |
aoqi@0 | 231 | public void writeLine(String text) throws IOException { |
aoqi@0 | 232 | write(text); |
aoqi@0 | 233 | out.newLine(); |
aoqi@0 | 234 | } |
aoqi@0 | 235 | |
aoqi@0 | 236 | /** |
aoqi@0 | 237 | * Write body text, escaping it as necessary. |
aoqi@0 | 238 | * If this call follows a call of startTag, the open tag will be |
aoqi@0 | 239 | * closed -- meaning that no more attributes can be written until another |
aoqi@0 | 240 | * tag is started. If the text value is null, the current tag will still |
aoqi@0 | 241 | * be closed, but no other text will be written. |
aoqi@0 | 242 | * @param text the text to be written, may be null or zero length. |
aoqi@0 | 243 | * @throws IOException if there is a problem writing to the underlying stream |
aoqi@0 | 244 | */ |
aoqi@0 | 245 | public void write(String text) throws IOException { |
aoqi@0 | 246 | if (state == IN_TAG) { |
aoqi@0 | 247 | out.write(">"); |
aoqi@0 | 248 | state = IN_BODY; |
aoqi@0 | 249 | } |
aoqi@0 | 250 | |
aoqi@0 | 251 | if (text == null) |
aoqi@0 | 252 | return; |
aoqi@0 | 253 | |
aoqi@0 | 254 | // check to see if there are any special characters |
aoqi@0 | 255 | boolean specialChars = false; |
aoqi@0 | 256 | for (int i = 0; i < text.length() && !specialChars; i++) { |
aoqi@0 | 257 | switch (text.charAt(i)) { |
aoqi@0 | 258 | case '<': case '>': case '&': |
aoqi@0 | 259 | specialChars = true; |
aoqi@0 | 260 | } |
aoqi@0 | 261 | } |
aoqi@0 | 262 | |
aoqi@0 | 263 | // if there are special characters write the string character at a time; |
aoqi@0 | 264 | // otherwise, write it out as is |
aoqi@0 | 265 | if (specialChars) { |
aoqi@0 | 266 | for (int i = 0; i < text.length(); i++) { |
aoqi@0 | 267 | char c = text.charAt(i); |
aoqi@0 | 268 | switch (c) { |
aoqi@0 | 269 | case '<': out.write("<"); break; |
aoqi@0 | 270 | case '>': out.write(">"); break; |
aoqi@0 | 271 | case '&': out.write("&"); break; |
aoqi@0 | 272 | default: out.write(c); |
aoqi@0 | 273 | } |
aoqi@0 | 274 | } |
aoqi@0 | 275 | } |
aoqi@0 | 276 | else |
aoqi@0 | 277 | out.write(text); |
aoqi@0 | 278 | } |
aoqi@0 | 279 | |
aoqi@0 | 280 | /** |
aoqi@0 | 281 | * Write a basic HTML entity, such as or { . |
aoqi@0 | 282 | * @param entity the entity to write |
aoqi@0 | 283 | * @throws IOException if there is a problem writing to the underlying stream |
aoqi@0 | 284 | */ |
aoqi@0 | 285 | public void writeEntity(String entity) throws IOException { |
aoqi@0 | 286 | if (state == IN_TAG) { |
aoqi@0 | 287 | out.write(">"); |
aoqi@0 | 288 | state = IN_BODY; |
aoqi@0 | 289 | } |
aoqi@0 | 290 | out.write(entity); |
aoqi@0 | 291 | } |
aoqi@0 | 292 | |
aoqi@0 | 293 | /** |
aoqi@0 | 294 | * Write an image tag, using a specified path for the image source attribute. |
aoqi@0 | 295 | * @param imagePath the path for the image source |
aoqi@0 | 296 | * @throws IOException if there is a problem closing the underlying stream |
aoqi@0 | 297 | */ |
aoqi@0 | 298 | public void writeImage(String imagePath) throws IOException { |
aoqi@0 | 299 | startTag(IMAGE); |
aoqi@0 | 300 | writeAttr(SRC, imagePath); |
aoqi@0 | 301 | } |
aoqi@0 | 302 | |
aoqi@0 | 303 | /** |
aoqi@0 | 304 | * Write an image tag, using a specified path for the image source attribute. |
aoqi@0 | 305 | * @param imageURL the url for the image source |
aoqi@0 | 306 | * @throws IOException if there is a problem closing the underlying stream |
aoqi@0 | 307 | */ |
aoqi@0 | 308 | public void writeImage(URL imageURL) throws IOException { |
aoqi@0 | 309 | writeImage(imageURL.toString()); |
aoqi@0 | 310 | } |
aoqi@0 | 311 | |
aoqi@0 | 312 | /** |
aoqi@0 | 313 | * Write a hypertext link. |
aoqi@0 | 314 | * @param anchor the target for the link |
aoqi@0 | 315 | * @param body the body text for the link |
aoqi@0 | 316 | * @throws IOException if there is a problem closing the underlying stream |
aoqi@0 | 317 | */ |
aoqi@0 | 318 | public void writeLink(String anchor, String body) throws IOException { |
aoqi@0 | 319 | startTag(A); |
aoqi@0 | 320 | writeAttr(HREF, anchor); |
aoqi@0 | 321 | write(body); |
aoqi@0 | 322 | endTag(A); |
aoqi@0 | 323 | } |
aoqi@0 | 324 | |
aoqi@0 | 325 | /** |
aoqi@0 | 326 | * Write a hypertext link. |
aoqi@0 | 327 | * @param file the target for the link |
aoqi@0 | 328 | * @param body the body text for the link |
aoqi@0 | 329 | * @throws IOException if there is a problem closing the underlying stream |
aoqi@0 | 330 | */ |
aoqi@0 | 331 | public void writeLink(File file, String body) throws IOException { |
aoqi@0 | 332 | startTag(A); |
aoqi@0 | 333 | StringBuffer sb = new StringBuffer(); |
aoqi@0 | 334 | String path = file.getPath().replace(File.separatorChar, '/'); |
aoqi@0 | 335 | if (file.isAbsolute() && !path.startsWith("/")) |
aoqi@0 | 336 | sb.append('/'); |
aoqi@0 | 337 | sb.append(path); |
aoqi@0 | 338 | writeAttr(HREF, sb.toString()); |
aoqi@0 | 339 | write(body); |
aoqi@0 | 340 | endTag(A); |
aoqi@0 | 341 | } |
aoqi@0 | 342 | |
aoqi@0 | 343 | /** |
aoqi@0 | 344 | * Write a hypertext link. |
aoqi@0 | 345 | * @param file the target and body for the link |
aoqi@0 | 346 | * @throws IOException if there is a problem closing the underlying stream |
aoqi@0 | 347 | */ |
aoqi@0 | 348 | public void writeLink(File file) throws IOException { |
aoqi@0 | 349 | writeLink(file, file.getPath()); |
aoqi@0 | 350 | } |
aoqi@0 | 351 | |
aoqi@0 | 352 | /** |
aoqi@0 | 353 | * Write a hypertext link. |
aoqi@0 | 354 | * @param url the target for the link |
aoqi@0 | 355 | * @param body the body text for the link |
aoqi@0 | 356 | * @throws IOException if there is a problem closing the underlying stream |
aoqi@0 | 357 | */ |
aoqi@0 | 358 | public void writeLink(URL url, String body) throws IOException { |
aoqi@0 | 359 | startTag(A); |
aoqi@0 | 360 | writeAttr(HREF, url.toString()); |
aoqi@0 | 361 | write(body); |
aoqi@0 | 362 | endTag(A); |
aoqi@0 | 363 | } |
aoqi@0 | 364 | |
aoqi@0 | 365 | /** |
aoqi@0 | 366 | * Write the destination marker for a hypertext link. |
aoqi@0 | 367 | * @param anchor the destination marker for hypertext links |
aoqi@0 | 368 | * @param body the body text for the marker |
aoqi@0 | 369 | * @throws IOException if there is a problem closing the underlying stream |
aoqi@0 | 370 | */ |
aoqi@0 | 371 | public void writeLinkDestination(String anchor, String body) throws IOException { |
aoqi@0 | 372 | startTag(A); |
aoqi@0 | 373 | writeAttr(NAME, anchor); |
aoqi@0 | 374 | write(body); |
aoqi@0 | 375 | endTag(A); |
aoqi@0 | 376 | } |
aoqi@0 | 377 | |
aoqi@0 | 378 | /** |
aoqi@0 | 379 | * Write a parameter tag. |
aoqi@0 | 380 | * @param name the name of the parameter |
aoqi@0 | 381 | * @param value the value of the parameter |
aoqi@0 | 382 | * @throws IOException if there is a problem closing the underlying stream |
aoqi@0 | 383 | */ |
aoqi@0 | 384 | public void writeParam(String name, String value) throws IOException { |
aoqi@0 | 385 | startTag(PARAM); |
aoqi@0 | 386 | writeAttr(NAME, name); |
aoqi@0 | 387 | writeAttr(VALUE, value); |
aoqi@0 | 388 | } |
aoqi@0 | 389 | |
aoqi@0 | 390 | /** |
aoqi@0 | 391 | * Write a style attribute. |
aoqi@0 | 392 | * @param value the value for the style atrtribute |
aoqi@0 | 393 | * @throws IOException if there is a problem closing the underlying stream |
aoqi@0 | 394 | */ |
aoqi@0 | 395 | public void writeStyleAttr(String value) throws IOException { |
aoqi@0 | 396 | writeAttr(STYLE, value); |
aoqi@0 | 397 | } |
aoqi@0 | 398 | |
aoqi@0 | 399 | /** |
aoqi@0 | 400 | * Write a localized message, using a specified resource bundle. |
aoqi@0 | 401 | * @param i18n the resource bundle used to localize the message |
aoqi@0 | 402 | * @param key the key for the message to be localized |
aoqi@0 | 403 | * @throws IOException if there is a problem closing the underlying stream |
aoqi@0 | 404 | */ |
aoqi@0 | 405 | public void write(ResourceBundle i18n, String key) throws IOException { |
aoqi@0 | 406 | write(getString(i18n, key)); |
aoqi@0 | 407 | } |
aoqi@0 | 408 | |
aoqi@0 | 409 | /** |
aoqi@0 | 410 | * Write a localized message, using a specified resource bundle. |
aoqi@0 | 411 | * @param i18n the resource bundle used to localize the message |
aoqi@0 | 412 | * @param key the key for the message to be localized |
aoqi@0 | 413 | * @param arg an argument to be formatted into the localized message |
aoqi@0 | 414 | * @throws IOException if there is a problem closing the underlying stream |
aoqi@0 | 415 | */ |
aoqi@0 | 416 | public void write(ResourceBundle i18n, String key, Object arg) throws IOException { |
aoqi@0 | 417 | write(getString(i18n, key, arg)); |
aoqi@0 | 418 | } |
aoqi@0 | 419 | |
aoqi@0 | 420 | /** |
aoqi@0 | 421 | * Write a localized message, using a specified resource bundle. |
aoqi@0 | 422 | * @param i18n the resource bundle used to localize the message |
aoqi@0 | 423 | * @param key the key for the message to be localized |
aoqi@0 | 424 | * @param args arguments to be formatted into the localized message |
aoqi@0 | 425 | * @throws IOException if there is a problem closing the underlying stream |
aoqi@0 | 426 | */ |
aoqi@0 | 427 | public void write(ResourceBundle i18n, String key, Object[] args) throws IOException { |
aoqi@0 | 428 | write(getString(i18n, key, args)); |
aoqi@0 | 429 | } |
aoqi@0 | 430 | |
aoqi@0 | 431 | /** |
aoqi@0 | 432 | * Write a localized message, using the default resource bundle. |
aoqi@0 | 433 | * @param key the key for the message to be localized |
aoqi@0 | 434 | * @throws IOException if there is a problem closing the underlying stream |
aoqi@0 | 435 | */ |
aoqi@0 | 436 | public void writeI18N(String key) throws IOException { |
aoqi@0 | 437 | write(getString(i18n, key)); |
aoqi@0 | 438 | } |
aoqi@0 | 439 | |
aoqi@0 | 440 | /** |
aoqi@0 | 441 | * Write a localized message, using the default resource bundle. |
aoqi@0 | 442 | * @param key the key for the message to be localized |
aoqi@0 | 443 | * @param arg an argument to be formatted into the localized message |
aoqi@0 | 444 | * @throws IOException if there is a problem closing the underlying stream |
aoqi@0 | 445 | */ |
aoqi@0 | 446 | public void writeI18N(String key, Object arg) throws IOException { |
aoqi@0 | 447 | write(getString(i18n, key, arg)); |
aoqi@0 | 448 | } |
aoqi@0 | 449 | |
aoqi@0 | 450 | /** |
aoqi@0 | 451 | * Write a localized message, using the default resource bundle. |
aoqi@0 | 452 | * @param key the key for the message to be localized |
aoqi@0 | 453 | * @param args arguments to be formatted into the localized message |
aoqi@0 | 454 | * @throws IOException if there is a problem closing the underlying stream |
aoqi@0 | 455 | */ |
aoqi@0 | 456 | public void writeI18N(String key, Object[] args) throws IOException { |
aoqi@0 | 457 | write(getString(i18n, key, args)); |
aoqi@0 | 458 | } |
aoqi@0 | 459 | |
aoqi@0 | 460 | private String getString(ResourceBundle rb, String key, Object... args) { |
aoqi@0 | 461 | String s = rb.getString(key); |
aoqi@0 | 462 | return MessageFormat.format(s, args); |
aoqi@0 | 463 | } |
aoqi@0 | 464 | |
aoqi@0 | 465 | /** The HTML "a" tag. */ |
aoqi@0 | 466 | public static final String A = "a"; |
aoqi@0 | 467 | /** The HTML "align" attribute. */ |
aoqi@0 | 468 | public static final String ALIGN = "align"; |
aoqi@0 | 469 | /** The HTML "b" tag. */ |
aoqi@0 | 470 | public static final String B = "b"; |
aoqi@0 | 471 | /** The HTML "body" tag. */ |
aoqi@0 | 472 | public static final String BODY = "body"; |
aoqi@0 | 473 | /** The HTML "border" attribute. */ |
aoqi@0 | 474 | public static final String BORDER = "border"; |
aoqi@0 | 475 | /** The HTML "br" tag. */ |
aoqi@0 | 476 | public static final String BR = "br"; |
aoqi@0 | 477 | /** The HTML "class" attribute. */ |
aoqi@0 | 478 | public static final String CLASS = "class"; |
aoqi@0 | 479 | /** The HTML "classid" attribute. */ |
aoqi@0 | 480 | public static final String CLASSID = "classid"; |
aoqi@0 | 481 | /** The HTML "code" tag. */ |
aoqi@0 | 482 | public static final String CODE = "code"; |
aoqi@0 | 483 | /** The HTML "color" attribte. */ |
aoqi@0 | 484 | public static final String COLOR = "color"; |
aoqi@0 | 485 | /** The HTML "col" attribute value. */ |
aoqi@0 | 486 | public static final String COL = "col"; |
aoqi@0 | 487 | /** The HTML "dd" tag. */ |
aoqi@0 | 488 | public static final String DD = "dd"; |
aoqi@0 | 489 | /** The HTML "div" tag. */ |
aoqi@0 | 490 | public static final String DIV = "div"; |
aoqi@0 | 491 | /** The HTML "dl" tag. */ |
aoqi@0 | 492 | public static final String DL = "dl"; |
aoqi@0 | 493 | /** The HTML "dt" tag. */ |
aoqi@0 | 494 | public static final String DT = "dt"; |
aoqi@0 | 495 | /** The HTML "font" tag. */ |
aoqi@0 | 496 | public static final String FONT = "font"; |
aoqi@0 | 497 | /** The HTML "h1" tag. */ |
aoqi@0 | 498 | public static final String H1 = "h1"; |
aoqi@0 | 499 | /** The HTML "h2" tag. */ |
aoqi@0 | 500 | public static final String H2 = "h2"; |
aoqi@0 | 501 | /** The HTML "h3" tag. */ |
aoqi@0 | 502 | public static final String H3 = "h3"; |
aoqi@0 | 503 | /** The HTML "h4" tag. */ |
aoqi@0 | 504 | public static final String H4 = "h4"; |
aoqi@0 | 505 | /** The HTML "h5" tag. */ |
aoqi@0 | 506 | public static final String H5 = "h5"; |
aoqi@0 | 507 | /** The HTML "head" tag. */ |
aoqi@0 | 508 | public static final String HEAD = "head"; |
aoqi@0 | 509 | /** The HTML "href" attribute. */ |
aoqi@0 | 510 | public static final String HREF = "href"; |
aoqi@0 | 511 | /** The HTML "html" tag. */ |
aoqi@0 | 512 | public static final String HTML = "html"; |
aoqi@0 | 513 | /** The HTML "hr" tag. */ |
aoqi@0 | 514 | public static final String HR = "hr"; |
aoqi@0 | 515 | /** The HTML "i" tag. */ |
aoqi@0 | 516 | public static final String I = "i"; |
aoqi@0 | 517 | /** The HTML "id" tag. */ |
aoqi@0 | 518 | public static final String ID = "id"; |
aoqi@0 | 519 | /** The HTML "image" tag. */ |
aoqi@0 | 520 | public static final String IMAGE = "image"; |
aoqi@0 | 521 | /** The HTML "left" attribute value. */ |
aoqi@0 | 522 | public static final String LEFT = "left"; |
aoqi@0 | 523 | /** The HTML "li" tag. */ |
aoqi@0 | 524 | public static final String LI = "li"; |
aoqi@0 | 525 | /** The HTML "link" tag. */ |
aoqi@0 | 526 | public static final String LINK = "link"; |
aoqi@0 | 527 | /** The HTML "name" attribute. */ |
aoqi@0 | 528 | public static final String NAME = "name"; |
aoqi@0 | 529 | /** The HTML "object" tag. */ |
aoqi@0 | 530 | public static final String OBJECT = "object"; |
aoqi@0 | 531 | /** The HTML "p" tag. */ |
aoqi@0 | 532 | public static final String PARAM = "param"; |
aoqi@0 | 533 | /** The HTML "param" tag. */ |
aoqi@0 | 534 | public static final String P = "p"; |
aoqi@0 | 535 | /** The HTML "rel" attribute value. */ |
aoqi@0 | 536 | public static final String REL = "rel"; |
aoqi@0 | 537 | /** The HTML "right" attribute value. */ |
aoqi@0 | 538 | public static final String RIGHT = "right"; |
aoqi@0 | 539 | /** The HTML "row" attribute value. */ |
aoqi@0 | 540 | public static final String ROW = "row"; |
aoqi@0 | 541 | /** The HTML "script" tag. */ |
aoqi@0 | 542 | public static final String SCRIPT = "script"; |
aoqi@0 | 543 | /** The HTML "small" tag. */ |
aoqi@0 | 544 | public static final String SMALL = "small"; |
aoqi@0 | 545 | /** The HTML "span" tag. */ |
aoqi@0 | 546 | public static final String SPAN = "span"; |
aoqi@0 | 547 | /** The HTML "src" attribute. */ |
aoqi@0 | 548 | public static final String SRC = "src"; |
aoqi@0 | 549 | /** The HTML "scope" attribute. */ |
aoqi@0 | 550 | public static final String SCOPE = "scope"; |
aoqi@0 | 551 | /** The HTML "style" attribute. */ |
aoqi@0 | 552 | public static final String STYLE = "style"; |
aoqi@0 | 553 | /** The HTML "table" tag. */ |
aoqi@0 | 554 | public static final String TABLE = "table"; |
aoqi@0 | 555 | /** The HTML "td" tag. */ |
aoqi@0 | 556 | public static final String TD = "td"; |
aoqi@0 | 557 | /** The HTML type for JavaScript. */ |
aoqi@0 | 558 | public static final String TEXT_JAVASCRIPT = "text/javascript"; |
aoqi@0 | 559 | /** The HTML "title"attribute. */ |
aoqi@0 | 560 | public static final String TITLE = "title"; |
aoqi@0 | 561 | /** The HTML "th" tag. */ |
aoqi@0 | 562 | public static final String TH = "th"; |
aoqi@0 | 563 | /** The HTML "top" attribute value. */ |
aoqi@0 | 564 | public static final String TOP = "top"; |
aoqi@0 | 565 | /** The HTML "tr" tag. */ |
aoqi@0 | 566 | public static final String TR = "tr"; |
aoqi@0 | 567 | /** The HTML "type" attribute. */ |
aoqi@0 | 568 | public static final String TYPE = "type"; |
aoqi@0 | 569 | /** The HTML "ul" tag. */ |
aoqi@0 | 570 | public static final String UL = "ul"; |
aoqi@0 | 571 | /** The HTML "valign" attribute. */ |
aoqi@0 | 572 | public static final String VALIGN = "valign"; |
aoqi@0 | 573 | /** The HTML "value" attribute. */ |
aoqi@0 | 574 | public static final String VALUE = "value"; |
aoqi@0 | 575 | |
aoqi@0 | 576 | |
aoqi@0 | 577 | private BufferedWriter out; |
aoqi@0 | 578 | private int state; |
aoqi@0 | 579 | private ResourceBundle i18n; |
aoqi@0 | 580 | private static final int IN_TAG = 1; |
aoqi@0 | 581 | private static final int IN_BODY = 2; |
aoqi@0 | 582 | } |