Save This Page
Home » jakarta-regexp-1.5 » org.apache » regexp » [javadoc | source]
    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *     http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    * Unless required by applicable law or agreed to in writing, software
   12    * distributed under the License is distributed on an "AS IS" BASIS,
   13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    * See the License for the specific language governing permissions and
   15    * limitations under the License.
   16    */
   17   
   18   package org.apache.regexp;
   19   
   20   import java.applet.Applet;
   21   import java.awt;
   22   import java.awt.event.TextEvent;
   23   import java.awt.event.TextListener;
   24   import java.awt.event.WindowAdapter;
   25   import java.awt.event.WindowEvent;
   26   import java.io.CharArrayWriter;
   27   import java.io.PrintWriter;
   28   
   29   /**
   30    * Interactive demonstration and testing harness for regular expressions classes.
   31    * @author <a href="mailto:jonl@muppetlabs.com">Jonathan Locke</a>
   32    * @version $Id: REDemo.java 518156 2007-03-14 14:31:26Z vgritsenko $
   33    */
   34   public class REDemo extends Applet implements TextListener
   35   {
   36       /**
   37        * Matcher and compiler objects
   38        */
   39       RE r = new RE();
   40       REDebugCompiler compiler = new REDebugCompiler();
   41   
   42       /**
   43        * Components
   44        */
   45       TextField fieldRE;          // Field for entering regexps
   46       TextField fieldMatch;       // Field for entering match strings
   47       TextArea outRE;             // Output of RE compiler
   48       TextArea outMatch;          // Results of matching operation
   49   
   50       /**
   51        * Add controls and init applet
   52        */
   53       public void init()
   54       {
   55           // Add components using the dreaded GridBagLayout
   56           GridBagLayout gb = new GridBagLayout();
   57           setLayout(gb);
   58           GridBagConstraints c = new GridBagConstraints();
   59           c.insets = new Insets(5, 5, 5, 5);
   60           c.anchor = GridBagConstraints.EAST;
   61           gb.setConstraints(add(new Label("Regular expression:", Label.RIGHT)), c);
   62           c.gridy = 0;
   63           c.anchor = GridBagConstraints.WEST;
   64           gb.setConstraints(add(fieldRE = new TextField("\\[([:javastart:][:javapart:]*)\\]", 40)), c);
   65           c.gridx = 0;
   66           c.gridy = GridBagConstraints.RELATIVE;
   67           c.anchor = GridBagConstraints.EAST;
   68           gb.setConstraints(add(new Label("String:", Label.RIGHT)), c);
   69           c.gridy = 1;
   70           c.gridx = GridBagConstraints.RELATIVE;
   71           c.anchor = GridBagConstraints.WEST;
   72           gb.setConstraints(add(fieldMatch = new TextField("aaa([foo])aaa", 40)), c);
   73           c.gridy = 2;
   74           c.gridx = GridBagConstraints.RELATIVE;
   75           c.fill = GridBagConstraints.BOTH;
   76           c.weighty = 1.0;
   77           c.weightx = 1.0;
   78           gb.setConstraints(add(outRE = new TextArea()), c);
   79           c.gridy = 2;
   80           c.gridx = GridBagConstraints.RELATIVE;
   81           gb.setConstraints(add(outMatch = new TextArea()), c);
   82   
   83           // Listen to text changes
   84           fieldRE.addTextListener(this);
   85           fieldMatch.addTextListener(this);
   86   
   87           // Initial UI update
   88           textValueChanged(null);
   89       }
   90   
   91       /**
   92        * Say something into RE text area
   93        * @param s What to say
   94        */
   95       void sayRE(String s)
   96       {
   97           outRE.setText(s);
   98       }
   99   
  100       /**
  101        * Say something into match text area
  102        * @param s What to say
  103        */
  104       void sayMatch(String s)
  105       {
  106           outMatch.setText(s);
  107       }
  108   
  109       /**
  110        * Convert throwable to string
  111        * @param t Throwable to convert to string
  112        */
  113       String throwableToString(Throwable t)
  114       {
  115           String s = t.getClass().getName();
  116           String m;
  117           if ((m = t.getMessage()) != null)
  118           {
  119               s += "\n" + m;
  120           }
  121           return s;
  122       }
  123   
  124       /**
  125        * Change regular expression
  126        * @param expr Expression to compile
  127        */
  128       void updateRE(String expr)
  129       {
  130           try
  131           {
  132               // Compile program
  133               r.setProgram(compiler.compile(expr));
  134   
  135               // Dump program into RE feedback area
  136               CharArrayWriter w = new CharArrayWriter();
  137               compiler.dumpProgram(new PrintWriter(w));
  138               sayRE(w.toString());
  139               System.out.println(w);
  140           }
  141           catch (Exception e)
  142           {
  143               r.setProgram(null);
  144               sayRE(throwableToString(e));
  145           }
  146           catch (Throwable t)
  147           {
  148               r.setProgram(null);
  149               sayRE(throwableToString(t));
  150           }
  151       }
  152   
  153       /**
  154        * Update matching info by matching the string against the current
  155        * compiled regular expression.
  156        * @param match String to match against
  157        */
  158       void updateMatch(String match)
  159       {
  160           try
  161           {
  162               // If the string matches the regexp
  163               if (r.match(match))
  164               {
  165                   // Say that it matches
  166                   String out = "Matches.\n\n";
  167   
  168                   // Show contents of parenthesized subexpressions
  169                   for (int i = 0; i < r.getParenCount(); i++)
  170                   {
  171                       out += "$" + i + " = " + r.getParen(i) + "\n";
  172                   }
  173                   sayMatch(out);
  174               }
  175               else
  176               {
  177                   // Didn't match!
  178                   sayMatch("Does not match");
  179               }
  180           }
  181           catch (Throwable t)
  182           {
  183               sayMatch(throwableToString(t));
  184           }
  185       }
  186   
  187       /**
  188        * Called when text values change
  189        * @param e TextEvent
  190        */
  191       public void textValueChanged(TextEvent e)
  192       {
  193           // If it's a generic update or the regexp changed...
  194           if (e == null || e.getSource() == fieldRE)
  195           {
  196               // Update regexp
  197               updateRE(fieldRE.getText());
  198           }
  199   
  200           // We always need to update the match results
  201           updateMatch(fieldMatch.getText());
  202       }
  203   
  204       /**
  205        * Main application entrypoint.
  206        * @param arg Command line arguments
  207        */
  208       static public void main(String[] arg)
  209       {
  210           Frame f = new Frame("RE Demo");
  211           // f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  212           f.addWindowListener(new WindowAdapter()
  213           {
  214               public void windowClosing(WindowEvent e)
  215               {
  216                   System.exit(0);
  217               }
  218           });
  219           REDemo demo = new REDemo();
  220           f.add(demo);
  221           demo.init();
  222           f.pack();
  223           f.setVisible(true);
  224       }
  225   }

Save This Page
Home » jakarta-regexp-1.5 » org.apache » regexp » [javadoc | source]