KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > WebFormatter


1 /*
2  * WebFormatter.java
3  *
4  * Copyright (C) 1998-2002 Peter Graves
5  * $Id: WebFormatter.java,v 1.2 2002/10/04 23:52:52 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.j;
23
24 import gnu.regexp.RE;
25 import gnu.regexp.UncheckedRE;
26 import java.awt.Color JavaDoc;
27 import java.awt.Font JavaDoc;
28
29 public final class WebFormatter extends Formatter implements WebConstants
30 {
31     // Includes '/' for "Parts/Attachments".
32
private static final RE headerRE = new UncheckedRE("^ *[a-zA-Z\\-/]+:");
33
34     public WebFormatter(Buffer buffer)
35     {
36         this.buffer = buffer;
37     }
38
39     public final LineSegmentList formatLine(Line line)
40     {
41         if (line instanceof WebLine) {
42             LineSegmentList list = ((WebLine)line).getSegmentList();
43             if (list == null) {
44                 list = new LineSegmentList();
45                 list.addSegment(new HtmlLineSegment("", 0));
46             }
47             return list;
48         }
49         clearSegmentList();
50         final String JavaDoc text = getDetabbedText(line);
51         if (line instanceof MessageHeaderLine) {
52             if (text.length() > 0) {
53                 int i = text.indexOf(':');
54                 if (i >= 0 && headerRE.getMatch(text) != null) {
55                     addSegment(text, 0, i+1, FORMAT_HEADER_NAME);
56                     addSegment(text, i+1, FORMAT_HEADER_VALUE);
57                     return segmentList;
58                 }
59             }
60             // Continuation line.
61
addSegment(text, FORMAT_HEADER_VALUE);
62         } else
63             addSegment(text, FORMAT_TEXT);
64         return segmentList;
65     }
66
67     public final Color JavaDoc getColor(int format)
68     {
69         return super.getColor(format & ~(FORMAT_BOLD | FORMAT_ITALIC));
70     }
71
72     public int getStyle(int format)
73     {
74         FormatTableEntry entry =
75             getFormatTable().lookup(format & ~(FORMAT_BOLD | FORMAT_ITALIC));
76         int style = entry != null ? entry.getStyle() : Font.PLAIN;
77         if ((format & FORMAT_BOLD) != 0)
78             style |= Font.BOLD;
79         else if ((format & FORMAT_ITALIC) != 0)
80             style |= Font.ITALIC;
81         return style;
82     }
83
84     public final FormatTableEntry getFormatTableEntry(int format)
85     {
86         return null;
87     }
88
89     public final boolean getUnderline(int format)
90     {
91         if ((format & FORMAT_WHITESPACE) != 0)
92             return false;
93         else
94             return (format & FORMAT_LINK) != 0;
95     }
96
97     public FormatTable getFormatTable()
98     {
99         if (formatTable == null) {
100             formatTable = new FormatTable("WebMode");
101             formatTable.addEntryFromPrefs(FORMAT_TEXT, "text");
102             formatTable.addEntryFromPrefs(FORMAT_LINK, "link", "keyword");
103             formatTable.addEntryFromPrefs(FORMAT_WHITESPACE, "text");
104             formatTable.addEntryFromPrefs(FORMAT_DISABLED, "disabled");
105             formatTable.addEntryFromPrefs(FORMAT_HEADER_NAME, "headerName", "keyword");
106             formatTable.addEntryFromPrefs(FORMAT_HEADER_VALUE, "headerValue", "operator");
107         }
108         return formatTable;
109     }
110 }
111
Popular Tags