Description
Write a tag to a Writer.
License
Open Source License
Parameter
Parameter | Description |
---|
w | the Writer to write to |
tag | the Tag to write |
attributes | the Tag's attributes |
Exception
Parameter | Description |
---|
IOException | if there is a problem writing |
Declaration
public static void write(Writer w, HTML.Tag tag, AttributeSet attributes) throws IOException
Method Source Code
//package com.java2s;
/*//from w ww .j a v a 2 s. c om
* $Source$
* $Revision$
*
* Copyright (C) 2001 Myles Chippendale
*
* Part of Melati (http://melati.org), a framework for the rapid
* development of clean, maintainable web applications.
*
* Melati is free software; Permission is granted to copy, distribute
* and/or modify this software under the terms either:
*
* a) the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option)
* any later version,
*
* or
*
* b) any version of the Melati Software License, as published
* at http://melati.org
*
* You should have received a copy of the GNU General Public License and
* the Melati Software License along with this program;
* if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA to obtain the
* GNU General Public License and visit http://melati.org to obtain the
* Melati Software License.
*
* Feel free to contact the Developers of Melati (http://melati.org),
* if you would like to work out a different arrangement than the options
* outlined here. It is our intention to allow Melati to be used by as
* wide an audience as possible.
*
* This program 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 for more details.
*
* Contact details for copyright holder:
*
* Myles Chippendale <mylesc At paneris.org>
*/
import java.io.IOException;
import java.io.Writer;
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;
import java.util.Enumeration;
import javax.swing.text.AttributeSet;
import javax.swing.text.html.HTML;
public class Main {
/**
* Write a tag to a Writer.
* @param w the Writer to write to
* @param tag the Tag to write
* @param attributes the Tag's attributes
* @throws IOException if there is a problem writing
*/
public static void write(Writer w, HTML.Tag tag, AttributeSet attributes) throws IOException {
w.write('<');
w.write(tag.toString());
for (Enumeration<?> a = attributes.getAttributeNames(); a.hasMoreElements();) {
Object n = a.nextElement();
if (attributes.isDefined(n)) {
w.write(' ');
w.write(n.toString());
w.write("=\"");
w.write(entitied(attributes.getAttribute(n).toString()));
w.write('"');
}
}
w.write('>');
}
/**
* Return the String with all high value ASCII characters
* replaced with HTML entities.
*
* @param s input String
* @param mapBR whether to replace line ends with html breaks
* @param encoding the encoding of the input string
* @param markup whether string is an sgml fragment
* @return the input with appropriate substitutions
*/
public static String entitied(String s, boolean mapBR, String encoding, boolean markup) {
int length = s.length();
int i;
String entity = null;
CharsetEncoder ce = null;
if (encoding != null) {
ce = Charset.forName(encoding).newEncoder();
}
for (i = 0; i < length && (entity = entityFor(s.charAt(i), mapBR, ce, markup)) == null; ++i)
;
if (entity == null)
return s;
StringBuffer b = new StringBuffer(length * 2);
for (int j = 0; j < i; ++j)
b.append(s.charAt(j));
b.append(entity);
char c;
for (++i; i < length; ++i) {
c = s.charAt(i);
entity = entityFor(c, mapBR, ce, markup);
if (entity != null) {
b.append(entity);
} else
b.append(c);
}
return b.toString();
}
/**
* Escape the given string as PCDATA without regard for any characters that
* cannot be encoded in some required character set.
* <p>
* This is for backward compatibility as it is used below.
*
* @param s the String to replace special characters from
* @return a new String with special characters replaced with entities
* @see #entitied(String, boolean, String, boolean)
*/
public static String entitied(String s) {
return entitied(s, true, null, false);
}
/**
* If the given character has special meaning in HTML or will not
* necessarily encode in the character set, then return an escape string.
* <p>
* The name of this method implies the character is escaped as a
* character entity but if the second argument is true then newlines
* are encoded as <BR>.
* This is not required for attribute values.
* <p>
* Which characters will necessarily encode depends on the charset.
* For backward compatibility if a charset is not passed we assume the
* character will encode.
* If a charset is passed and a character does not encode then we
* replace it with a numeric character reference (not an entity
* either but pretty similar).
*
* @param c character to lookup entity for
* @param mapBR whether to replace line ends
* @param ce an encoder
* @param markup whether string contains markup
* @return an entity or null
*/
public static String entityFor(char c, boolean mapBR, CharsetEncoder ce, boolean markup) {
switch (c) {
case '\n':
return mapBR && !markup ? "<BR>\n" : null;
case '<':
return markup ? null : "<";
case '>':
return markup ? null : ">";
case '&':
return markup ? null : "&";
// Unicode and ISO 8859-1
case 163:
return "£";
case 192:
return "À";
case 193:
return "Á";
case 194:
return "Â";
case 199:
return "Ç";
case 200:
return "È";
case 201:
return "É";
case 202:
return "Ê";
case 204:
return "Ì";
case 205:
return "Í";
case 206:
return "Î";
case 210:
return "Ò";
case 211:
return "Ó";
case 212:
return "Ô";
case 217:
return "Ù";
case 218:
return "Ú";
case 219:
return "Û";
case 224:
return "à";
case 225:
return "á";
case 226:
return "â";
case 228:
return "ä";
case 231:
return "ç";
case 232:
return "è";
case 233:
return "é";
case 234:
return "ê";
case 236:
return "ì";
case 237:
return "í";
case 238:
return "î";
case 242:
return "ò";
case 243:
return "ó";
case 244:
return "ô";
case 249:
return "ù";
case 250:
return "ú";
case 251:
return "û";
case 252:
return "ü";
case '"':
return markup ? null : """;
case '\'':
return markup ? null : "'";
default:
if (ce == null || ce.canEncode(c)) {
return null;
} else {
String result = "&#x" + Integer.toHexString(c) + ";";
//System.err.println("Cannot encode: " + c + " so encoded as: " + result);
return result;
}
}
}
}
Related
- rtfToBody(String rtf)
- rtfToHtml(Reader rtf)
- rtfToHtml(String rtf)
- stripHTML(String html)
- toBold(final T label)