1 package org.apache.regexp; 2 3 59 60 import java.applet.*; 61 import java.awt.*; 62 import java.awt.event.*; 63 import java.io.*; 64 65 70 public class REDemo extends Applet implements TextListener 71 { 72 75 RE r = new RE(); 76 REDebugCompiler compiler = new REDebugCompiler(); 77 78 81 TextField fieldRE; TextField fieldMatch; TextArea outRE; TextArea outMatch; 86 89 public void init() 90 { 91 GridBagLayout gb = new GridBagLayout(); 93 setLayout(gb); 94 GridBagConstraints c = new GridBagConstraints(); 95 c.insets = new Insets(5, 5, 5, 5); 96 c.anchor = c.EAST; 97 gb.setConstraints(add(new Label("Regular expression:", Label.RIGHT)), c); 98 c.gridy = 0; 99 c.anchor = c.WEST; 100 gb.setConstraints(add(fieldRE = new TextField("\\[([:javastart:][:javapart:]*)\\]", 40)), c); 101 c.gridx = 0; 102 c.gridy = c.RELATIVE; 103 c.anchor = c.EAST; 104 gb.setConstraints(add(new Label("String:", Label.RIGHT)), c); 105 c.gridy = 1; 106 c.gridx = c.RELATIVE; 107 c.anchor = c.WEST; 108 gb.setConstraints(add(fieldMatch = new TextField("aaa([foo])aaa", 40)), c); 109 c.gridy = 2; 110 c.gridx = c.RELATIVE; 111 c.fill = c.BOTH; 112 c.weighty = 1.0; 113 c.weightx = 1.0; 114 gb.setConstraints(add(outRE = new TextArea()), c); 115 c.gridy = 2; 116 c.gridx = c.RELATIVE; 117 gb.setConstraints(add(outMatch = new TextArea()), c); 118 119 fieldRE.addTextListener(this); 121 fieldMatch.addTextListener(this); 122 123 textValueChanged(null); 125 } 126 127 131 void sayRE(String s) 132 { 133 outRE.setText(s); 134 } 135 136 140 void sayMatch(String s) 141 { 142 outMatch.setText(s); 143 } 144 145 149 String throwableToString(Throwable t) 150 { 151 String s = t.getClass().getName(); 152 String m; 153 if ((m = t.getMessage()) != null) 154 { 155 s += "\n" + m; 156 } 157 return s; 158 } 159 160 164 void updateRE(String expr) 165 { 166 try 167 { 168 r.setProgram(compiler.compile(expr)); 170 171 CharArrayWriter w = new CharArrayWriter(); 173 compiler.dumpProgram(new PrintWriter(w)); 174 sayRE(w.toString()); 175 System.out.println(w); 176 } 177 catch (Exception e) 178 { 179 r.setProgram(null); 180 sayRE(throwableToString(e)); 181 } 182 catch (Throwable t) 183 { 184 r.setProgram(null); 185 sayRE(throwableToString(t)); 186 } 187 } 188 189 194 void updateMatch(String match) 195 { 196 try 197 { 198 if (r.match(match)) 200 { 201 String out = "Matches.\n\n"; 203 204 for (int i = 0; i < r.getParenCount(); i++) 206 { 207 out += "$" + i + " = " + r.getParen(i) + "\n"; 208 } 209 sayMatch(out); 210 } 211 else 212 { 213 sayMatch("Does not match"); 215 } 216 } 217 catch (Throwable t) 218 { 219 sayMatch(throwableToString(t)); 220 } 221 } 222 223 227 public void textValueChanged(TextEvent e) 228 { 229 if (e == null || e.getSource() == fieldRE) 231 { 232 updateRE(fieldRE.getText()); 234 } 235 236 updateMatch(fieldMatch.getText()); 238 } 239 240 244 static public void main(String [] arg) 245 { 246 Frame f = new Frame("RE Demo"); 247 f.addWindowListener(new WindowAdapter() 249 { 250 public void windowClosing(WindowEvent e) 251 { 252 System.exit(0); 253 } 254 }); 255 REDemo demo = new REDemo(); 256 f.add(demo); 257 demo.init(); 258 f.pack(); 259 f.setVisible(true); 260 } 261 } 262 | Popular Tags |