KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * ShellFormatter.java
3  *
4  * Copyright (C) 1998-2002 Peter Graves
5  * $Id: ShellFormatter.java,v 1.5 2003/12/04 15:17:06 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.REMatch;
26
27 public final class ShellFormatter extends Formatter
28 {
29     // Formats.
30
private static final byte SHELL_FORMAT_TEXT = 0;
31     private static final byte SHELL_FORMAT_PROMPT = 1;
32     private static final byte SHELL_FORMAT_INPUT = 2;
33
34     private final RE promptRE;
35
36     public ShellFormatter(Buffer buffer)
37     {
38         this.buffer = buffer;
39         promptRE = ((Shell)buffer).getPromptRE();
40     }
41
42     public LineSegmentList formatLine(Line line)
43     {
44         clearSegmentList();
45         if (line == null) {
46             addSegment("", SHELL_FORMAT_TEXT);
47             return segmentList;
48         }
49         final String JavaDoc text = getDetabbedText(line);
50         if (line.flags() == STATE_PROMPT) {
51             REMatch match = promptRE.getMatch(text);
52             if (match != null) {
53                 final int end = match.getEndIndex();
54                 addSegment(text, 0, end, SHELL_FORMAT_PROMPT);
55                 addSegment(text, end, SHELL_FORMAT_INPUT);
56             } else
57                 addSegment(text, SHELL_FORMAT_PROMPT);
58             return segmentList;
59         }
60         if (line.flags() == STATE_PASSWORD_PROMPT) {
61             addSegment(text, SHELL_FORMAT_TEXT);
62             return segmentList;
63         }
64         if (line.flags() == STATE_OUTPUT) {
65             addSegment(text, SHELL_FORMAT_TEXT);
66             return segmentList;
67         }
68         if (promptRE != null) {
69             if (line.flags() == STATE_INPUT) {
70                 REMatch match = promptRE.getMatch(text);
71                 if (match != null) {
72                     final int end = match.getEndIndex();
73                     addSegment(text, 0, end, SHELL_FORMAT_PROMPT);
74                     addSegment(text, end, SHELL_FORMAT_INPUT);
75                 } else {
76                     // No prompt text.
77
addSegment(text, SHELL_FORMAT_INPUT);
78                 }
79                 return segmentList;
80             }
81             Line next = line.next();
82             if (next == null) {
83                 // Last line of buffer. Check for prompt.
84
REMatch match = promptRE.getMatch(text);
85                 if (match != null) {
86                     line.setFlags(STATE_PROMPT);
87                     final int end = match.getEndIndex();
88                     addSegment(text, 0, end, SHELL_FORMAT_PROMPT);
89                     addSegment(text, end, SHELL_FORMAT_INPUT);
90                 } else
91                     addSegment(text, SHELL_FORMAT_INPUT);
92                 return segmentList;
93             }
94         }
95         addSegment(text, SHELL_FORMAT_TEXT);
96         return segmentList;
97     }
98
99     public FormatTable getFormatTable()
100     {
101         if (formatTable == null) {
102             formatTable = new FormatTable("ShellMode");
103             formatTable.addEntryFromPrefs(SHELL_FORMAT_TEXT, "text");
104             formatTable.addEntryFromPrefs(SHELL_FORMAT_PROMPT, "prompt");
105             formatTable.addEntryFromPrefs(SHELL_FORMAT_INPUT, "input" );
106         }
107         return formatTable;
108     }
109 }
110
Popular Tags