001: /*
002: * gnu/regexp/util/REApplet.java
003: * Copyright (C) 1998 Wes Biggs
004: *
005: * This program is free software; you can redistribute it and/or modify
006: * it under the terms of the GNU General Public License as published
007: * by the Free Software Foundation; either version 2 of the License, or
008: * (at your option) any later version.
009: *
010: * This program is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: * GNU General Public License for more details.
014: *
015: * You should have received a copy of the GNU General Public License
016: * along with this program; if not, write to the Free Software
017: * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
018: */
019:
020: package gnu.regexp.util;
021:
022: import java.applet.*;
023: import java.awt.*;
024: import gnu.regexp.*;
025:
026: /**
027: * This is a simple applet to demonstrate the capabilities of gnu.regexp.
028: * To run it, use appletviewer on the reapplet.html file included in the
029: * documentation directory.
030: *
031: * @author <A HREF="mailto:wes@cacas.org">Wes Biggs</A>
032: * @version 1.02
033: */
034: public class REApplet extends Applet {
035: private Label l1, l2, l3, l4;
036: private Button b;
037: private TextField tf;
038: private TextArea input, output;
039: private Checkbox insens;
040: private Choice syntax;
041: private static String[] names = new String[] { "awk", "ed",
042: "egrep", "emacs", "grep", "POSIX awk", "POSIX basic",
043: "POSIX egrep", "POSIX extended", "POSIX minimal basic",
044: "POSIX minimal extended", "sed", "perl 4",
045: "perl 4 (singe line)", "perl 5", "perl 5 (single line)" };
046:
047: private static RESyntax[] values = new RESyntax[] {
048: new RESyntax(RESyntax.RE_SYNTAX_AWK).setLineSeparator("\n"),
049: new RESyntax(RESyntax.RE_SYNTAX_ED).setLineSeparator("\n"),
050: new RESyntax(RESyntax.RE_SYNTAX_EGREP)
051: .setLineSeparator("\n"),
052: new RESyntax(RESyntax.RE_SYNTAX_EMACS)
053: .setLineSeparator("\n"),
054: new RESyntax(RESyntax.RE_SYNTAX_GREP)
055: .setLineSeparator("\n"),
056: new RESyntax(RESyntax.RE_SYNTAX_POSIX_AWK)
057: .setLineSeparator("\n"),
058: new RESyntax(RESyntax.RE_SYNTAX_POSIX_BASIC)
059: .setLineSeparator("\n"),
060: new RESyntax(RESyntax.RE_SYNTAX_POSIX_EGREP)
061: .setLineSeparator("\n"),
062: new RESyntax(RESyntax.RE_SYNTAX_POSIX_EXTENDED)
063: .setLineSeparator("\n"),
064: new RESyntax(RESyntax.RE_SYNTAX_POSIX_MINIMAL_BASIC)
065: .setLineSeparator("\n"),
066: new RESyntax(RESyntax.RE_SYNTAX_POSIX_MINIMAL_EXTENDED)
067: .setLineSeparator("\n"),
068: new RESyntax(RESyntax.RE_SYNTAX_SED).setLineSeparator("\n"),
069: new RESyntax(RESyntax.RE_SYNTAX_PERL4)
070: .setLineSeparator("\n"),
071: new RESyntax(RESyntax.RE_SYNTAX_PERL4_S)
072: .setLineSeparator("\n"),
073: new RESyntax(RESyntax.RE_SYNTAX_PERL5)
074: .setLineSeparator("\n"),
075: new RESyntax(RESyntax.RE_SYNTAX_PERL5_S)
076: .setLineSeparator("\n") };
077:
078: /** Creates an REApplet. */
079: public REApplet() {
080: super ();
081: }
082:
083: /** Initializes the applet and constructs GUI elements. */
084: public void init() {
085: // test run RE stuff to cache gnu.regexp.* classes.
086: try {
087: RE x = new RE("^.*(w[x])\1$");
088: REMatchEnumeration xx = x.getMatchEnumeration("wxwx");
089: while (xx.hasMoreMatches())
090: xx.nextMatch().toString();
091: } catch (REException arg) {
092: }
093:
094: setBackground(Color.lightGray);
095:
096: /*
097: Layout looks like this:
098:
099: [0,0:[0,0: Regular Expression] [1,0: Textbox]
100: [0,1: Expression Syntax] [1,1: [0,0: Choice] [1,0: Checkbox]]
101: [1,2: Button]]
102: [0,1: Input Text] [1,1: Match]
103: [0,2: Textarea] [1,2: Textarea]
104: */
105:
106: GridBagLayout gbag = new GridBagLayout();
107: setLayout(gbag);
108: GridBagConstraints c = new GridBagConstraints();
109: Panel p = new Panel();
110: GridBagLayout gbag2 = new GridBagLayout();
111: p.setLayout(gbag2);
112:
113: c.anchor = GridBagConstraints.WEST;
114: c.weightx = 1.0;
115:
116: // [0,0: Regular Expression]
117: c.gridx = 0;
118: c.gridy = 0;
119: l1 = new Label("Regular Expression");
120: gbag2.setConstraints(l1, c);
121: p.add(l1);
122:
123: // [1,0: TextField]
124: c.gridx = 1;
125: tf = new TextField(getParameter("regexp"), 30);
126: gbag2.setConstraints(tf, c);
127: p.add(tf);
128:
129: // [0,1: Expression Syntax]
130: c.gridx = 0;
131: c.gridy = 1;
132: l4 = new Label("Expression Syntax");
133: gbag2.setConstraints(l4, c);
134: p.add(l4);
135:
136: // [1,1: subpanel]
137: Panel p2 = new Panel();
138: GridBagLayout gbag3 = new GridBagLayout();
139: p2.setLayout(gbag3);
140: c.gridx = 1;
141: gbag2.setConstraints(p2, c);
142: p.add(p2);
143:
144: // Subpanel [0,0: Choice]
145: c.gridx = 0;
146: c.gridy = 0;
147: syntax = new Choice();
148: for (int i = 0; i < names.length; i++)
149: syntax.addItem(names[i]);
150: String zz = getParameter("syntax");
151: if (zz != null) {
152: try {
153: syntax.select(getParameter("syntax"));
154: } catch (IllegalArgumentException e) {
155: }
156: }
157:
158: gbag3.setConstraints(syntax, c);
159: p2.add(syntax);
160:
161: c.gridx = 1;
162: insens = new Checkbox("Ignore case", false);
163: gbag3.setConstraints(insens, c);
164: p2.add(insens);
165:
166: // Next Row
167: c.gridx = 1;
168: c.gridy = 2;
169: b = new Button("Match");
170: gbag2.setConstraints(b, c);
171: p.add(b);
172:
173: // Add the entire upper panel.
174: c.gridwidth = 2;
175: c.gridheight = 1;
176: c.gridx = 0;
177: c.gridy = 0;
178: c.anchor = GridBagConstraints.CENTER;
179: gbag.setConstraints(p, c);
180: add(p);
181:
182: c.gridwidth = 1;
183: c.gridheight = 1;
184:
185: // Main: [0,1]:
186: l2 = new Label("Input Text");
187: c.gridwidth = 1;
188: c.gridx = 0;
189: c.gridy = 1;
190: gbag.setConstraints(l2, c);
191: add(l2);
192:
193: l3 = new Label("Matches Found");
194: c.gridx = 1;
195: gbag.setConstraints(l3, c);
196: add(l3);
197:
198: input = new TextArea(getParameter("input"), 5, 30);
199: c.gridx = 0;
200: c.gridy = 2;
201: gbag.setConstraints(input, c);
202: add(input);
203:
204: c.gridx = 1;
205: output = new TextArea(5, 30);
206: output.setEditable(false);
207: c.gridwidth = GridBagConstraints.REMAINDER;
208: gbag.setConstraints(output, c);
209: add(output);
210: }
211:
212: /**
213: * Handles events in the applet. Returns true if the indicated event
214: * was handled, false for all other events.
215: */
216: public boolean action(Event e, Object arg) {
217: Object target = e.target;
218:
219: if (target == b) { // match
220: try {
221: String expr = tf.getText();
222: RE reg = null;
223: RESyntax res = values[syntax.getSelectedIndex()];
224: reg = new RE(expr, insens.getState() ? RE.REG_ICASE
225: | RE.REG_MULTILINE : RE.REG_MULTILINE, res);
226: REMatchEnumeration en = reg.getMatchEnumeration(input
227: .getText());
228: StringBuffer sb = new StringBuffer();
229: int matchNum = 0;
230: while (en.hasMoreMatches()) {
231: sb.append(String.valueOf(++matchNum));
232: sb.append(". ");
233: sb.append(en.nextMatch().toString());
234: sb.append('\n');
235: }
236: output.setText(sb.toString());
237: } catch (REException err) {
238: output.setText("Expression compilation error: "
239: + err.getMessage());
240: }
241: return true;
242: } else
243: return false;
244: }
245: }
|