KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * ManFormatter.java
3  *
4  * Copyright (C) 2000-2002 Peter Graves
5  * $Id: ManFormatter.java,v 1.1.1.1 2002/09/24 16:08:26 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 public class ManFormatter extends Formatter
25 {
26     // States.
27
private static final int STATE_BOLD = STATE_LAST + 1;
28     private static final int STATE_UNDERLINE = STATE_LAST + 2;
29
30     // Formats.
31
private static final int MAN_FORMAT_PLAIN = 0;
32     private static final int MAN_FORMAT_BOLD = 1;
33     private static final int MAN_FORMAT_UNDERLINE = 2;
34
35     private final FastStringBuffer sb = new FastStringBuffer();
36     private final boolean apropos;
37
38     public ManFormatter(Buffer buffer, boolean apropos)
39     {
40         this.buffer = buffer;
41         this.apropos = apropos;
42     }
43
44     private void endToken(int state)
45     {
46         if (sb.length() > 0) {
47             int format;
48             switch (state) {
49                 case STATE_NEUTRAL:
50                     format = MAN_FORMAT_PLAIN;
51                     break;
52                 case STATE_BOLD:
53                     format = MAN_FORMAT_BOLD;
54                     break;
55                 case STATE_UNDERLINE:
56                     format = MAN_FORMAT_UNDERLINE;
57                     break;
58                 default:
59                     format = MAN_FORMAT_PLAIN;
60                     break;
61             }
62             addSegment(sb.toString(), format);
63             sb.setLength(0);
64         }
65     }
66
67     public LineSegmentList formatLine(Line line)
68     {
69         clearSegmentList();
70         if (line == null || line.length() == 0) {
71             addSegment("", MAN_FORMAT_PLAIN);
72             return segmentList;
73         }
74         if (apropos)
75             parseLineApropos(line);
76         else
77             parseLine(line);
78         return segmentList;
79     }
80
81     private void parseLineApropos(Line line)
82     {
83         String JavaDoc text = line.getText();
84         int index = text.indexOf(' ');
85         if (index >= 0) {
86             addSegment(text, 0, index, MAN_FORMAT_BOLD);
87             addSegment(text, index, MAN_FORMAT_PLAIN);
88         } else
89             addSegment(text, MAN_FORMAT_PLAIN);
90     }
91
92     private void parseLine(Line line)
93     {
94         final String JavaDoc text = ((ManLine)line).getRawText();
95         final int limit = text.length();
96         final int tabWidth = buffer.getTabWidth();
97         int state = STATE_NEUTRAL;
98         for (int i = 0, length = 0; i < limit; i++) {
99             final char c = text.charAt(i);
100             final char nextChar = i+1 < limit ? text.charAt(i+1) : 0;
101             switch (state) {
102                 case STATE_NEUTRAL:
103                     if (nextChar == '\b') {
104                         final char thirdChar = i+2 < limit ? text.charAt(i+2) : 0;
105                         if (thirdChar == c) {
106                             // Bold.
107
endToken(state);
108                             i += 2;
109                             state = STATE_BOLD;
110                             sb.append(c);
111                             length++;
112                             continue;
113                         } else if (c == '_') {
114                             // Underline.
115
endToken(state);
116                             i += 2;
117                             state = STATE_UNDERLINE;
118                             sb.append(thirdChar);
119                             length++;
120                             continue;
121                         } else {
122                             ++i;
123                             continue;
124                         }
125                     } else {
126                         if (c == '\t') {
127                             for (int j = tabWidth - length % tabWidth; j-- > 0; length++)
128                                 sb.append(" ");
129                         } else {
130                             sb.append(c);
131                             length++;
132                         }
133                     }
134                     break;
135                 case STATE_BOLD:
136                     if (c == '\b') {
137                         char prevChar = text.charAt(i-1);
138                         if (nextChar == prevChar)
139                             ++i;
140                         continue;
141                     } else if (nextChar == '\b') {
142                         final char thirdChar = i+2 < limit ? text.charAt(i+2) : 0;
143                         if (thirdChar == c) {
144                             // Bold.
145
i += 2;
146                             sb.append(c);
147                             length++;
148                             continue;
149                         }
150                         if (c == '_') {
151                             // Underline.
152
endToken(state);
153                             i += 2;
154                             state = STATE_UNDERLINE;
155                             sb.append(thirdChar);
156                             length++;
157                             continue;
158                         }
159                     } else {
160                         endToken(state);
161                         state = STATE_NEUTRAL;
162                         if (c == '\t') {
163                             for (int j = tabWidth - length % tabWidth; j-- > 0; length++)
164                                 sb.append(" ");
165                         } else {
166                             sb.append(c);
167                             length++;
168                         }
169                     }
170                     break;
171                 case STATE_UNDERLINE:
172                     if (c == '_' && nextChar == '\b') {
173                         final char thirdChar = i+2 < limit ? text.charAt(i+2) : 0;
174                         if (thirdChar != 0) {
175                             sb.append(thirdChar);
176                             length++;
177                         }
178                         i += 2;
179                     } else if (nextChar == '\b') {
180                         endToken(state);
181                         i += 2;
182                         state = STATE_BOLD;
183                         sb.append(c);
184                         length++;
185                         continue;
186                     } else {
187                         endToken(state);
188                         state = STATE_NEUTRAL;
189                         if (c == '\t') {
190                             for (int j = tabWidth - length % tabWidth; j-- > 0; length++)
191                                 sb.append(" ");
192                         } else {
193                             sb.append(c);
194                             length++;
195                         }
196                     }
197                     break;
198             }
199         }
200         endToken(state);
201     }
202
203     public FormatTable getFormatTable()
204     {
205         if (formatTable == null) {
206             formatTable = new FormatTable("ManMode");
207             formatTable.addEntryFromPrefs(MAN_FORMAT_PLAIN, "text");
208             formatTable.addEntryFromPrefs(MAN_FORMAT_BOLD, "function");
209             formatTable.addEntryFromPrefs(MAN_FORMAT_UNDERLINE, "keyword");
210         }
211         return formatTable;
212     }
213 }
214
Popular Tags