001: /*
002: * WebFormatter.java
003: *
004: * Copyright (C) 1998-2002 Peter Graves
005: * $Id: WebFormatter.java,v 1.2 2002/10/04 23:52:52 piso Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.j;
023:
024: import gnu.regexp.RE;
025: import gnu.regexp.UncheckedRE;
026: import java.awt.Color;
027: import java.awt.Font;
028:
029: public final class WebFormatter extends Formatter implements
030: WebConstants {
031: // Includes '/' for "Parts/Attachments".
032: private static final RE headerRE = new UncheckedRE(
033: "^ *[a-zA-Z\\-/]+:");
034:
035: public WebFormatter(Buffer buffer) {
036: this .buffer = buffer;
037: }
038:
039: public final LineSegmentList formatLine(Line line) {
040: if (line instanceof WebLine) {
041: LineSegmentList list = ((WebLine) line).getSegmentList();
042: if (list == null) {
043: list = new LineSegmentList();
044: list.addSegment(new HtmlLineSegment("", 0));
045: }
046: return list;
047: }
048: clearSegmentList();
049: final String text = getDetabbedText(line);
050: if (line instanceof MessageHeaderLine) {
051: if (text.length() > 0) {
052: int i = text.indexOf(':');
053: if (i >= 0 && headerRE.getMatch(text) != null) {
054: addSegment(text, 0, i + 1, FORMAT_HEADER_NAME);
055: addSegment(text, i + 1, FORMAT_HEADER_VALUE);
056: return segmentList;
057: }
058: }
059: // Continuation line.
060: addSegment(text, FORMAT_HEADER_VALUE);
061: } else
062: addSegment(text, FORMAT_TEXT);
063: return segmentList;
064: }
065:
066: public final Color getColor(int format) {
067: return super .getColor(format & ~(FORMAT_BOLD | FORMAT_ITALIC));
068: }
069:
070: public int getStyle(int format) {
071: FormatTableEntry entry = getFormatTable().lookup(
072: format & ~(FORMAT_BOLD | FORMAT_ITALIC));
073: int style = entry != null ? entry.getStyle() : Font.PLAIN;
074: if ((format & FORMAT_BOLD) != 0)
075: style |= Font.BOLD;
076: else if ((format & FORMAT_ITALIC) != 0)
077: style |= Font.ITALIC;
078: return style;
079: }
080:
081: public final FormatTableEntry getFormatTableEntry(int format) {
082: return null;
083: }
084:
085: public final boolean getUnderline(int format) {
086: if ((format & FORMAT_WHITESPACE) != 0)
087: return false;
088: else
089: return (format & FORMAT_LINK) != 0;
090: }
091:
092: public FormatTable getFormatTable() {
093: if (formatTable == null) {
094: formatTable = new FormatTable("WebMode");
095: formatTable.addEntryFromPrefs(FORMAT_TEXT, "text");
096: formatTable.addEntryFromPrefs(FORMAT_LINK, "link",
097: "keyword");
098: formatTable.addEntryFromPrefs(FORMAT_WHITESPACE, "text");
099: formatTable.addEntryFromPrefs(FORMAT_DISABLED, "disabled");
100: formatTable.addEntryFromPrefs(FORMAT_HEADER_NAME,
101: "headerName", "keyword");
102: formatTable.addEntryFromPrefs(FORMAT_HEADER_VALUE,
103: "headerValue", "operator");
104: }
105: return formatTable;
106: }
107: }
|