FileDocCategorySizeDatePackage
REDemo.javaAPI DocJava SE 5 API8216Fri Aug 26 14:55:28 BST 2005com.sun.org.apache.regexp.internal

REDemo

public class REDemo extends Applet implements TextListener
Interactive demonstration and testing harness for regular expressions classes.
author
Jonathan Locke
version
$Id: REDemo.java,v 1.1 2000/04/27 01:22:33 jon Exp $

Fields Summary
RE
r
Matcher and compiler objects
REDebugCompiler
compiler
TextField
fieldRE
Components
TextField
fieldMatch
TextArea
outRE
TextArea
outMatch
Constructors Summary
Methods Summary
public static void_main(java.lang.String[] arg)
Main application entrypoint.

param
arg Command line arguments

        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 voidinit()
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);
    
voidsayMatch(java.lang.String s)
Say something into match text area

param
s What to say

        outMatch.setText(s);
    
voidsayRE(java.lang.String s)
Say something into RE text area

param
s What to say

        outRE.setText(s);
    
public voidtextValueChanged(java.awt.event.TextEvent e)
Called when text values change

param
e TextEvent

        // 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.StringthrowableToString(java.lang.Throwable t)
Convert throwable to string

param
t Throwable to convert to string

        String s = t.getClass().getName();
        String m;
        if ((m = t.getMessage()) != null)
        {
            s += "\n" + m;
        }
        return s;
    
voidupdateMatch(java.lang.String match)
Update matching info by matching the string against the current compiled regular expression.

param
match String to match against

        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));
        }
    
voidupdateRE(java.lang.String expr)
Change regular expression

param
expr Expression to compile

        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));
        }