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 020package gnu.regexp.util; 021import java.applet.*; 022import java.awt.*; 023import gnu.regexp.*; 024 025/** 026 * This is a simple applet to demonstrate the capabilities of gnu.regexp. 027 * To run it, use appletviewer on the reapplet.html file included in the 028 * documentation directory. 029 * 030 * @author <A HREF="mailto:wes@cacas.org">Wes Biggs</A> 031 * @version 1.02 032 */ 033public class REApplet extends Applet { 034 private Label l1, l2, l3, l4; 035 private Button b; 036 private TextField tf; 037 private TextArea input, output; 038 private Checkbox insens; 039 private Choice syntax; 040 private static String[] names = new String[] { 041 "awk", "ed", "egrep", "emacs", "grep", "POSIX awk", "POSIX basic", 042 "POSIX egrep", "POSIX extended", "POSIX minimal basic", 043 "POSIX minimal extended", "sed", "perl 4", "perl 4 (singe line)", 044 "perl 5", "perl 5 (single line)" 045 }; 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).setLineSeparator("\n"), 051 new RESyntax(RESyntax.RE_SYNTAX_EMACS).setLineSeparator("\n"), 052 new RESyntax(RESyntax.RE_SYNTAX_GREP).setLineSeparator("\n"), 053 new RESyntax(RESyntax.RE_SYNTAX_POSIX_AWK).setLineSeparator("\n"), 054 new RESyntax(RESyntax.RE_SYNTAX_POSIX_BASIC).setLineSeparator("\n"), 055 new RESyntax(RESyntax.RE_SYNTAX_POSIX_EGREP).setLineSeparator("\n"), 056 new RESyntax(RESyntax.RE_SYNTAX_POSIX_EXTENDED).setLineSeparator("\n"), 057 new RESyntax(RESyntax.RE_SYNTAX_POSIX_MINIMAL_BASIC).setLineSeparator("\n"), 058 new RESyntax(RESyntax.RE_SYNTAX_POSIX_MINIMAL_EXTENDED).setLineSeparator("\n"), 059 new RESyntax(RESyntax.RE_SYNTAX_SED).setLineSeparator("\n"), 060 new RESyntax(RESyntax.RE_SYNTAX_PERL4).setLineSeparator("\n"), 061 new RESyntax(RESyntax.RE_SYNTAX_PERL4_S).setLineSeparator("\n"), 062 new RESyntax(RESyntax.RE_SYNTAX_PERL5).setLineSeparator("\n"), 063 new RESyntax(RESyntax.RE_SYNTAX_PERL5_S).setLineSeparator("\n") 064 }; 065 066 /** Creates an REApplet. */ 067 public REApplet() { super(); } 068 069 /** Initializes the applet and constructs GUI elements. */ 070 public void init() { 071 // test run RE stuff to cache gnu.regexp.* classes. 072 try { 073 RE x = new RE("^.*(w[x])\1$"); 074 REMatchEnumeration xx = x.getMatchEnumeration("wxwx"); 075 while (xx.hasMoreMatches()) xx.nextMatch().toString(); 076 } catch (REException arg) { } 077 078 setBackground(Color.lightGray); 079 080 /* 081 Layout looks like this: 082 083 [0,0:[0,0: Regular Expression] [1,0: Textbox] 084 [0,1: Expression Syntax] [1,1: [0,0: Choice] [1,0: Checkbox]] 085 [1,2: Button]] 086 [0,1: Input Text] [1,1: Match] 087 [0,2: Textarea] [1,2: Textarea] 088 */ 089 090 GridBagLayout gbag = new GridBagLayout(); 091 setLayout(gbag); 092 GridBagConstraints c = new GridBagConstraints(); 093 Panel p = new Panel(); 094 GridBagLayout gbag2 = new GridBagLayout(); 095 p.setLayout(gbag2); 096 097 c.anchor = GridBagConstraints.WEST; 098 c.weightx = 1.0; 099 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 zz = getParameter("syntax"); 134 if (zz != null) { 135 try { 136 syntax.select(getParameter("syntax")); 137 } catch (IllegalArgumentException 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 arg) { 199 Object target = e.target; 200 201 if (target == b) { // match 202 try { 203 String 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 sb = new StringBuffer(); 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}