KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > regexp > internal > REDemo


1 package com.sun.org.apache.regexp.internal;
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 import javax.swing.*;
65
66 /**
67  * Interactive demonstration and testing harness for regular expressions classes.
68  * @author <a HREF="mailto:jonl@muppetlabs.com">Jonathan Locke</a>
69  * @version $Id: REDemo.java,v 1.1 2000/04/27 01:22:33 jon Exp $
70  */

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

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

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

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

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

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

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

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

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

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

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

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