001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.regexp;
019:
020: import java.applet.Applet;
021: import java.awt.*;
022: import java.awt.event.TextEvent;
023: import java.awt.event.TextListener;
024: import java.awt.event.WindowAdapter;
025: import java.awt.event.WindowEvent;
026: import java.io.CharArrayWriter;
027: import java.io.PrintWriter;
028:
029: /**
030: * Interactive demonstration and testing harness for regular expressions classes.
031: * @author <a href="mailto:jonl@muppetlabs.com">Jonathan Locke</a>
032: * @version $Id: REDemo.java 518156 2007-03-14 14:31:26Z vgritsenko $
033: */
034: public class REDemo extends Applet implements TextListener {
035: /**
036: * Matcher and compiler objects
037: */
038: RE r = new RE();
039: REDebugCompiler compiler = new REDebugCompiler();
040:
041: /**
042: * Components
043: */
044: TextField fieldRE; // Field for entering regexps
045: TextField fieldMatch; // Field for entering match strings
046: TextArea outRE; // Output of RE compiler
047: TextArea outMatch; // Results of matching operation
048:
049: /**
050: * Add controls and init applet
051: */
052: public void init() {
053: // Add components using the dreaded GridBagLayout
054: GridBagLayout gb = new GridBagLayout();
055: setLayout(gb);
056: GridBagConstraints c = new GridBagConstraints();
057: c.insets = new Insets(5, 5, 5, 5);
058: c.anchor = GridBagConstraints.EAST;
059: gb.setConstraints(add(new Label("Regular expression:",
060: Label.RIGHT)), c);
061: c.gridy = 0;
062: c.anchor = GridBagConstraints.WEST;
063: gb.setConstraints(add(fieldRE = new TextField(
064: "\\[([:javastart:][:javapart:]*)\\]", 40)), c);
065: c.gridx = 0;
066: c.gridy = GridBagConstraints.RELATIVE;
067: c.anchor = GridBagConstraints.EAST;
068: gb.setConstraints(add(new Label("String:", Label.RIGHT)), c);
069: c.gridy = 1;
070: c.gridx = GridBagConstraints.RELATIVE;
071: c.anchor = GridBagConstraints.WEST;
072: gb.setConstraints(add(fieldMatch = new TextField(
073: "aaa([foo])aaa", 40)), c);
074: c.gridy = 2;
075: c.gridx = GridBagConstraints.RELATIVE;
076: c.fill = GridBagConstraints.BOTH;
077: c.weighty = 1.0;
078: c.weightx = 1.0;
079: gb.setConstraints(add(outRE = new TextArea()), c);
080: c.gridy = 2;
081: c.gridx = GridBagConstraints.RELATIVE;
082: gb.setConstraints(add(outMatch = new TextArea()), c);
083:
084: // Listen to text changes
085: fieldRE.addTextListener(this );
086: fieldMatch.addTextListener(this );
087:
088: // Initial UI update
089: textValueChanged(null);
090: }
091:
092: /**
093: * Say something into RE text area
094: * @param s What to say
095: */
096: void sayRE(String s) {
097: outRE.setText(s);
098: }
099:
100: /**
101: * Say something into match text area
102: * @param s What to say
103: */
104: void sayMatch(String s) {
105: outMatch.setText(s);
106: }
107:
108: /**
109: * Convert throwable to string
110: * @param t Throwable to convert to string
111: */
112: String throwableToString(Throwable t) {
113: String s = t.getClass().getName();
114: String m;
115: if ((m = t.getMessage()) != null) {
116: s += "\n" + m;
117: }
118: return s;
119: }
120:
121: /**
122: * Change regular expression
123: * @param expr Expression to compile
124: */
125: void updateRE(String expr) {
126: try {
127: // Compile program
128: r.setProgram(compiler.compile(expr));
129:
130: // Dump program into RE feedback area
131: CharArrayWriter w = new CharArrayWriter();
132: compiler.dumpProgram(new PrintWriter(w));
133: sayRE(w.toString());
134: System.out.println(w);
135: } catch (Exception e) {
136: r.setProgram(null);
137: sayRE(throwableToString(e));
138: } catch (Throwable t) {
139: r.setProgram(null);
140: sayRE(throwableToString(t));
141: }
142: }
143:
144: /**
145: * Update matching info by matching the string against the current
146: * compiled regular expression.
147: * @param match String to match against
148: */
149: void updateMatch(String match) {
150: try {
151: // If the string matches the regexp
152: if (r.match(match)) {
153: // Say that it matches
154: String out = "Matches.\n\n";
155:
156: // Show contents of parenthesized subexpressions
157: for (int i = 0; i < r.getParenCount(); i++) {
158: out += "$" + i + " = " + r.getParen(i) + "\n";
159: }
160: sayMatch(out);
161: } else {
162: // Didn't match!
163: sayMatch("Does not match");
164: }
165: } catch (Throwable t) {
166: sayMatch(throwableToString(t));
167: }
168: }
169:
170: /**
171: * Called when text values change
172: * @param e TextEvent
173: */
174: public void textValueChanged(TextEvent e) {
175: // If it's a generic update or the regexp changed...
176: if (e == null || e.getSource() == fieldRE) {
177: // Update regexp
178: updateRE(fieldRE.getText());
179: }
180:
181: // We always need to update the match results
182: updateMatch(fieldMatch.getText());
183: }
184:
185: /**
186: * Main application entrypoint.
187: * @param arg Command line arguments
188: */
189: static public void main(String[] arg) {
190: Frame f = new Frame("RE Demo");
191: // f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
192: f.addWindowListener(new WindowAdapter() {
193: public void windowClosing(WindowEvent e) {
194: System.exit(0);
195: }
196: });
197: REDemo demo = new REDemo();
198: f.add(demo);
199: demo.init();
200: f.pack();
201: f.setVisible(true);
202: }
203: }
|