1 19 20 package gnu.regexp.util; 21 import java.applet.*; 22 import java.awt.*; 23 import gnu.regexp.*; 24 25 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 [] names = new String [] { 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 67 public REApplet() { super(); } 68 69 70 public void init() { 71 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 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 c.gridx = 0; 102 c.gridy = 0; 103 l1 = new Label("Regular Expression"); 104 gbag2.setConstraints(l1,c); 105 p.add(l1); 106 107 c.gridx = 1; 109 tf = new TextField(getParameter("regexp"),30); 110 gbag2.setConstraints(tf,c); 111 p.add(tf); 112 113 c.gridx = 0; 115 c.gridy = 1; 116 l4 = new Label("Expression Syntax"); 117 gbag2.setConstraints(l4,c); 118 p.add(l4); 119 120 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 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 c.gridx = 1; 150 c.gridy = 2; 151 b = new Button("Match"); 152 gbag2.setConstraints(b,c); 153 p.add(b); 154 155 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 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 198 public boolean action(Event e, Object arg) { 199 Object target = e.target; 200 201 if (target == b) { 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 } 224 | Popular Tags |