KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > regexp > REDemo


1 package org.apache.regexp;
2
3 /*
4  * ====================================================================
5  *
6  * The Apache Software License, Version 1.1
7  *
8  * Copyright (c) 1999 The Apache Software Foundation. All rights
9  * reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  *
15  * 1. Redistributions of source code must retain the above copyright
16  * notice, this list of conditions and the following disclaimer.
17  *
18  * 2. Redistributions in binary form must reproduce the above copyright
19  * notice, this list of conditions and the following disclaimer in
20  * the documentation and/or other materials provided with the
21  * distribution.
22  *
23  * 3. The end-user documentation included with the redistribution, if
24  * any, must include the following acknowlegement:
25  * "This product includes software developed by the
26  * Apache Software Foundation (http://www.apache.org/)."
27  * Alternately, this acknowlegement may appear in the software itself,
28  * if and wherever such third-party acknowlegements normally appear.
29  *
30  * 4. The names "The Jakarta Project", "Jakarta-Regexp", and "Apache Software
31  * Foundation" must not be used to endorse or promote products derived
32  * from this software without prior written permission. For written
33  * permission, please contact apache@apache.org.
34  *
35  * 5. Products derived from this software may not be called "Apache"
36  * nor may "Apache" appear in their names without prior written
37  * permission of the Apache Group.
38  *
39  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
40  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
41  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
42  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
45  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
46  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
47  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
48  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
49  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE.
51  * ====================================================================
52  *
53  * This software consists of voluntary contributions made by many
54  * individuals on behalf of the Apache Software Foundation. For more
55  * information on the Apache Software Foundation, please see
56  * <http://www.apache.org/>.
57  *
58  */

59
60 import java.applet.*;
61 import java.awt.*;
62 import java.awt.event.*;
63 import java.io.*;
64
65 /**
66  * Interactive demonstration and testing harness for regular expressions classes.
67  * @author <a HREF="mailto:jonl@muppetlabs.com">Jonathan Locke</a>
68  * @version $Id: REDemo.java,v 1.2 2000/12/07 17:24:59 jon Exp $
69  */

70 public class REDemo extends Applet implements TextListener
71 {
72     /**
73      * Matcher and compiler objects
74      */

75     RE r = new RE();
76     REDebugCompiler compiler = new REDebugCompiler();
77
78     /**
79      * Components
80      */

81     TextField fieldRE; // Field for entering regexps
82
TextField fieldMatch; // Field for entering match strings
83
TextArea outRE; // Output of RE compiler
84
TextArea outMatch; // Results of matching operation
85

86     /**
87      * Add controls and init applet
88      */

89     public void init()
90     {
91         // Add components using the dreaded GridBagLayout
92
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         // Listen to text changes
120
fieldRE.addTextListener(this);
121         fieldMatch.addTextListener(this);
122
123         // Initial UI update
124
textValueChanged(null);
125     }
126
127     /**
128      * Say something into RE text area
129      * @param s What to say
130      */

131     void sayRE(String JavaDoc s)
132     {
133         outRE.setText(s);
134     }
135
136     /**
137      * Say something into match text area
138      * @param s What to say
139      */

140     void sayMatch(String JavaDoc s)
141     {
142         outMatch.setText(s);
143     }
144
145     /**
146      * Convert throwable to string
147      * @param t Throwable to convert to string
148      */

149     String JavaDoc throwableToString(Throwable JavaDoc t)
150     {
151         String JavaDoc s = t.getClass().getName();
152         String JavaDoc m;
153         if ((m = t.getMessage()) != null)
154         {
155             s += "\n" + m;
156         }
157         return s;
158     }
159
160     /**
161      * Change regular expression
162      * @param expr Expression to compile
163      */

164     void updateRE(String JavaDoc expr)
165     {
166         try
167         {
168             // Compile program
169
r.setProgram(compiler.compile(expr));
170
171             // Dump program into RE feedback area
172
CharArrayWriter w = new CharArrayWriter();
173             compiler.dumpProgram(new PrintWriter(w));
174             sayRE(w.toString());
175             System.out.println(w);
176         }
177         catch (Exception JavaDoc e)
178         {
179             r.setProgram(null);
180             sayRE(throwableToString(e));
181         }
182         catch (Throwable JavaDoc t)
183         {
184             r.setProgram(null);
185             sayRE(throwableToString(t));
186         }
187     }
188
189     /**
190      * Update matching info by matching the string against the current
191      * compiled regular expression.
192      * @param match String to match against
193      */

194     void updateMatch(String JavaDoc match)
195     {
196         try
197         {
198             // If the string matches the regexp
199
if (r.match(match))
200             {
201                 // Say that it matches
202
String JavaDoc out = "Matches.\n\n";
203
204                 // Show contents of parenthesized subexpressions
205
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                 // Didn't match!
214
sayMatch("Does not match");
215             }
216         }
217         catch (Throwable JavaDoc t)
218         {
219             sayMatch(throwableToString(t));
220         }
221     }
222
223     /**
224      * Called when text values change
225      * @param e TextEvent
226      */

227     public void textValueChanged(TextEvent e)
228     {
229         // If it's a generic update or the regexp changed...
230
if (e == null || e.getSource() == fieldRE)
231         {
232             // Update regexp
233
updateRE(fieldRE.getText());
234         }
235
236         // We always need to update the match results
237
updateMatch(fieldMatch.getText());
238     }
239
240     /**
241      * Main application entrypoint.
242      * @param arg Command line arguments
243      */

244     static public void main(String JavaDoc[] arg)
245     {
246         Frame f = new Frame("RE Demo");
247         // f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
248
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