KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > regexp > util > REApplet


1 /*
2  * gnu/regexp/util/REApplet.java
3  * Copyright (C) 1998 Wes Biggs
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published
7  * by the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19
20 package gnu.regexp.util;
21 import java.applet.*;
22 import java.awt.*;
23 import gnu.regexp.*;
24
25 /**
26  * This is a simple applet to demonstrate the capabilities of gnu.regexp.
27  * To run it, use appletviewer on the reapplet.html file included in the
28  * documentation directory.
29  *
30  * @author <A HREF="mailto:wes@cacas.org">Wes Biggs</A>
31  * @version 1.02
32  */

33 public class REApplet extends Applet {
34     private Label l1, l2, l3, l4;
35     private Button b;
36     private TextField tf;
37     private TextArea input, output;
38     private Checkbox insens;
39     private Choice syntax;
40     private static String JavaDoc[] names = new String JavaDoc[] {
41     "awk", "ed", "egrep", "emacs", "grep", "POSIX awk", "POSIX basic",
42     "POSIX egrep", "POSIX extended", "POSIX minimal basic",
43     "POSIX minimal extended", "sed", "perl 4", "perl 4 (singe line)",
44     "perl 5", "perl 5 (single line)"
45     };
46
47     private static RESyntax[] values = new RESyntax[] {
48     new RESyntax(RESyntax.RE_SYNTAX_AWK).setLineSeparator("\n"),
49     new RESyntax(RESyntax.RE_SYNTAX_ED).setLineSeparator("\n"),
50     new RESyntax(RESyntax.RE_SYNTAX_EGREP).setLineSeparator("\n"),
51     new RESyntax(RESyntax.RE_SYNTAX_EMACS).setLineSeparator("\n"),
52     new RESyntax(RESyntax.RE_SYNTAX_GREP).setLineSeparator("\n"),
53     new RESyntax(RESyntax.RE_SYNTAX_POSIX_AWK).setLineSeparator("\n"),
54     new RESyntax(RESyntax.RE_SYNTAX_POSIX_BASIC).setLineSeparator("\n"),
55     new RESyntax(RESyntax.RE_SYNTAX_POSIX_EGREP).setLineSeparator("\n"),
56     new RESyntax(RESyntax.RE_SYNTAX_POSIX_EXTENDED).setLineSeparator("\n"),
57     new RESyntax(RESyntax.RE_SYNTAX_POSIX_MINIMAL_BASIC).setLineSeparator("\n"),
58     new RESyntax(RESyntax.RE_SYNTAX_POSIX_MINIMAL_EXTENDED).setLineSeparator("\n"),
59     new RESyntax(RESyntax.RE_SYNTAX_SED).setLineSeparator("\n"),
60     new RESyntax(RESyntax.RE_SYNTAX_PERL4).setLineSeparator("\n"),
61     new RESyntax(RESyntax.RE_SYNTAX_PERL4_S).setLineSeparator("\n"),
62     new RESyntax(RESyntax.RE_SYNTAX_PERL5).setLineSeparator("\n"),
63     new RESyntax(RESyntax.RE_SYNTAX_PERL5_S).setLineSeparator("\n")
64     };
65
66     /** Creates an REApplet. */
67     public REApplet() { super(); }
68     
69     /** Initializes the applet and constructs GUI elements. */
70     public void init() {
71     // test run RE stuff to cache gnu.regexp.* classes.
72
try {
73         RE x = new RE("^.*(w[x])\1$");
74         REMatchEnumeration xx = x.getMatchEnumeration("wxwx");
75         while (xx.hasMoreMatches()) xx.nextMatch().toString();
76     } catch (REException arg) { }
77     
78     setBackground(Color.lightGray);
79     
80     /*
81       Layout looks like this:
82       
83       [0,0:[0,0: Regular Expression] [1,0: Textbox]
84       [0,1: Expression Syntax] [1,1: [0,0: Choice] [1,0: Checkbox]]
85       [1,2: Button]]
86       [0,1: Input Text] [1,1: Match]
87       [0,2: Textarea] [1,2: Textarea]
88     */

89     
90     GridBagLayout gbag = new GridBagLayout();
91     setLayout(gbag);
92     GridBagConstraints c = new GridBagConstraints();
93     Panel p = new Panel();
94     GridBagLayout gbag2 = new GridBagLayout();
95     p.setLayout(gbag2);
96     
97     c.anchor = GridBagConstraints.WEST;
98     c.weightx = 1.0;
99     
100     // [0,0: Regular Expression]
101
c.gridx = 0;
102     c.gridy = 0;
103     l1 = new Label("Regular Expression");
104     gbag2.setConstraints(l1,c);
105     p.add(l1);
106     
107     // [1,0: TextField]
108
c.gridx = 1;
109     tf = new TextField(getParameter("regexp"),30);
110     gbag2.setConstraints(tf,c);
111     p.add(tf);
112     
113     // [0,1: Expression Syntax]
114
c.gridx = 0;
115     c.gridy = 1;
116     l4 = new Label("Expression Syntax");
117     gbag2.setConstraints(l4,c);
118     p.add(l4);
119     
120     // [1,1: subpanel]
121
Panel p2 = new Panel();
122     GridBagLayout gbag3 = new GridBagLayout();
123     p2.setLayout(gbag3);
124     c.gridx = 1;
125     gbag2.setConstraints(p2,c);
126     p.add(p2);
127     
128     // Subpanel [0,0: Choice]
129
c.gridx = 0;
130     c.gridy = 0;
131     syntax = new Choice();
132     for (int i = 0; i < names.length; i++) syntax.addItem(names[i]);
133     String JavaDoc zz = getParameter("syntax");
134     if (zz != null) {
135         try {
136         syntax.select(getParameter("syntax"));
137         } catch (IllegalArgumentException JavaDoc e) { }
138     }
139
140     gbag3.setConstraints(syntax,c);
141     p2.add(syntax);
142         
143     c.gridx = 1;
144     insens = new Checkbox("Ignore case",false);
145     gbag3.setConstraints(insens,c);
146     p2.add(insens);
147     
148     // Next Row
149
c.gridx = 1;
150     c.gridy = 2;
151     b = new Button("Match");
152     gbag2.setConstraints(b,c);
153     p.add(b);
154     
155     // Add the entire upper panel.
156
c.gridwidth = 2;
157     c.gridheight = 1;
158     c.gridx = 0;
159     c.gridy = 0;
160     c.anchor = GridBagConstraints.CENTER;
161     gbag.setConstraints(p,c);
162     add(p);
163     
164     c.gridwidth = 1;
165     c.gridheight = 1;
166     
167     // Main: [0,1]:
168
l2 = new Label("Input Text");
169     c.gridwidth = 1;
170     c.gridx = 0;
171     c.gridy = 1;
172     gbag.setConstraints(l2,c);
173     add(l2);
174     
175     l3 = new Label("Matches Found");
176     c.gridx = 1;
177     gbag.setConstraints(l3,c);
178     add(l3);
179     
180     input = new TextArea(getParameter("input"),5,30);
181     c.gridx = 0;
182     c.gridy = 2;
183     gbag.setConstraints(input,c);
184     add(input);
185     
186     c.gridx = 1;
187     output = new TextArea(5,30);
188     output.setEditable(false);
189     c.gridwidth = GridBagConstraints.REMAINDER;
190     gbag.setConstraints(output,c);
191     add(output);
192     }
193     
194     /**
195      * Handles events in the applet. Returns true if the indicated event
196      * was handled, false for all other events.
197      */

198     public boolean action(Event e, Object JavaDoc arg) {
199     Object JavaDoc target = e.target;
200     
201     if (target == b) { // match
202
try {
203         String JavaDoc expr = tf.getText();
204         RE reg = null;
205         RESyntax res = values[syntax.getSelectedIndex()];
206         reg = new RE(expr,insens.getState() ? RE.REG_ICASE | RE.REG_MULTILINE : RE.REG_MULTILINE, res);
207         REMatchEnumeration en = reg.getMatchEnumeration(input.getText());
208         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
209         int matchNum = 0;
210         while (en.hasMoreMatches()) {
211             sb.append(String.valueOf(++matchNum));
212             sb.append(". ");
213             sb.append(en.nextMatch().toString());
214             sb.append('\n');
215         }
216         output.setText(sb.toString());
217         } catch (REException err) {
218         output.setText("Expression compilation error: " + err.getMessage());
219         }
220     return true;
221     } else return false;
222     }
223 }
224
Popular Tags