Java Swing HTML write(Writer w, HTML.Tag tag, AttributeSet attributes)

Here you can find the source of write(Writer w, HTML.Tag tag, AttributeSet attributes)

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 &lt;BR&gt;.
     * 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 : "&lt;";
        case '>':
            return markup ? null : "&gt;";
        case '&':
            return markup ? null : "&amp;";
        // Unicode and ISO 8859-1

        case 163:
            return "&pound;";
        case 192:
            return "&Agrave;";
        case 193:
            return "&Aacute;";
        case 194:
            return "&Acirc;";
        case 199:
            return "&Ccedil;";
        case 200:
            return "&Egrave;";
        case 201:
            return "&Eacute;";
        case 202:
            return "&Ecirc;";
        case 204:
            return "&Igrave;";
        case 205:
            return "&Iacute;";
        case 206:
            return "&Icirc;";
        case 210:
            return "&Ograve;";
        case 211:
            return "&Oacute;";
        case 212:
            return "&Ocirc;";
        case 217:
            return "&Ugrave;";
        case 218:
            return "&Uacute;";
        case 219:
            return "&Ucirc;";
        case 224:
            return "&agrave;";
        case 225:
            return "&aacute;";
        case 226:
            return "&acirc;";
        case 228:
            return "&auml;";
        case 231:
            return "&ccedil;";
        case 232:
            return "&egrave;";
        case 233:
            return "&eacute;";
        case 234:
            return "&ecirc;";
        case 236:
            return "&igrave;";
        case 237:
            return "&iacute;";
        case 238:
            return "&icirc;";
        case 242:
            return "&ograve;";
        case 243:
            return "&oacute;";
        case 244:
            return "&ocirc;";
        case 249:
            return "&ugrave;";
        case 250:
            return "&uacute;";
        case 251:
            return "&ucirc;";
        case 252:
            return "&uuml;";

        case '"':
            return markup ? null : "&quot;";
        case '\'':
            return markup ? null : "&#39;";
        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

  1. rtfToBody(String rtf)
  2. rtfToHtml(Reader rtf)
  3. rtfToHtml(String rtf)
  4. stripHTML(String html)
  5. toBold(final T label)