Methods Summary |
---|
public static void | _main(java.lang.String[] arg)Main application entrypoint.
JFrame f = new JFrame("RE Demo");
// f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
Container c = f.getContentPane();
c.setLayout(new FlowLayout());
REDemo demo = new REDemo();
c.add(demo);
demo.init();
f.pack();
f.setVisible(true);
|
public void | init()Add controls and init applet // Results of matching operation
// Add components using the dreaded GridBagLayout
GridBagLayout gb = new GridBagLayout();
setLayout(gb);
GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(5, 5, 5, 5);
c.anchor = c.EAST;
gb.setConstraints(add(new Label("Regular expression:", Label.RIGHT)), c);
c.gridy = 0;
c.anchor = c.WEST;
gb.setConstraints(add(fieldRE = new TextField("\\[([:javastart:][:javapart:]*)\\]", 40)), c);
c.gridx = 0;
c.gridy = c.RELATIVE;
c.anchor = c.EAST;
gb.setConstraints(add(new Label("String:", Label.RIGHT)), c);
c.gridy = 1;
c.gridx = c.RELATIVE;
c.anchor = c.WEST;
gb.setConstraints(add(fieldMatch = new TextField("aaa([foo])aaa", 40)), c);
c.gridy = 2;
c.gridx = c.RELATIVE;
c.fill = c.BOTH;
c.weighty = 1.0;
c.weightx = 1.0;
gb.setConstraints(add(outRE = new TextArea()), c);
c.gridy = 2;
c.gridx = c.RELATIVE;
gb.setConstraints(add(outMatch = new TextArea()), c);
// Listen to text changes
fieldRE.addTextListener(this);
fieldMatch.addTextListener(this);
// Initial UI update
textValueChanged(null);
|
void | sayMatch(java.lang.String s)Say something into match text area
outMatch.setText(s);
|
void | sayRE(java.lang.String s)Say something into RE text area
outRE.setText(s);
|
public void | textValueChanged(java.awt.event.TextEvent e)Called when text values change
// If it's a generic update or the regexp changed...
if (e == null || e.getSource() == fieldRE)
{
// Update regexp
updateRE(fieldRE.getText());
}
// We always need to update the match results
updateMatch(fieldMatch.getText());
|
java.lang.String | throwableToString(java.lang.Throwable t)Convert throwable to string
String s = t.getClass().getName();
String m;
if ((m = t.getMessage()) != null)
{
s += "\n" + m;
}
return s;
|
void | updateMatch(java.lang.String match)Update matching info by matching the string against the current
compiled regular expression.
try
{
// If the string matches the regexp
if (r.match(match))
{
// Say that it matches
String out = "Matches.\n\n";
// Show contents of parenthesized subexpressions
for (int i = 0; i < r.getParenCount(); i++)
{
out += "$" + i + " = " + r.getParen(i) + "\n";
}
sayMatch(out);
}
else
{
// Didn't match!
sayMatch("Does not match");
}
}
catch (Throwable t)
{
sayMatch(throwableToString(t));
}
|
void | updateRE(java.lang.String expr)Change regular expression
try
{
// Compile program
r.setProgram(compiler.compile(expr));
// Dump program into RE feedback area
CharArrayWriter w = new CharArrayWriter();
compiler.dumpProgram(new PrintWriter(w));
sayRE(w.toString());
System.out.println(w);
}
catch (Exception e)
{
r.setProgram(null);
sayRE(throwableToString(e));
}
catch (Throwable t)
{
r.setProgram(null);
sayRE(throwableToString(t));
}
|