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