001: /*
002: * $Id: MatcherDemoApplet.java,v 1.5 2003/11/07 20:16:23 dfs Exp $
003: *
004: * ====================================================================
005: * The Apache Software License, Version 1.1
006: *
007: * Copyright (c) 2000 The Apache Software Foundation. All rights
008: * reserved.
009: *
010: * Redistribution and use in source and binary forms, with or without
011: * modification, are permitted provided that the following conditions
012: * are met:
013: *
014: * 1. Redistributions of source code must retain the above copyright
015: * notice, this list of conditions and the following disclaimer.
016: *
017: * 2. Redistributions in binary form must reproduce the above copyright
018: * notice, this list of conditions and the following disclaimer in
019: * the documentation and/or other materials provided with the
020: * distribution.
021: *
022: * 3. The end-user documentation included with the redistribution,
023: * if any, must include the following acknowledgment:
024: * "This product includes software developed by the
025: * Apache Software Foundation (http://www.apache.org/)."
026: * Alternately, this acknowledgment may appear in the software itself,
027: * if and wherever such third-party acknowledgments normally appear.
028: *
029: * 4. The names "Apache" and "Apache Software Foundation", "Jakarta-Oro"
030: * must not be used to endorse or promote products derived from this
031: * software without prior written permission. For written
032: * permission, please contact apache@apache.org.
033: *
034: * 5. Products derived from this software may not be called "Apache"
035: * or "Jakarta-Oro", nor may "Apache" or "Jakarta-Oro" appear in their
036: * name, without prior written permission of the Apache Software Foundation.
037: *
038: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
039: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
040: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
041: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
042: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
043: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
044: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
045: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
046: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
047: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
048: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
049: * SUCH DAMAGE.
050: * ====================================================================
051: *
052: * This software consists of voluntary contributions made by many
053: * individuals on behalf of the Apache Software Foundation. For more
054: * information on the Apache Software Foundation, please see
055: * <http://www.apache.org/>.
056: */
057:
058: package examples;
059:
060: import java.applet.*;
061: import java.awt.*;
062: import java.io.*;
063: import java.net.*;
064:
065: import org.apache.oro.text.*;
066: import org.apache.oro.text.awk.*;
067: import org.apache.oro.text.regex.*;
068:
069: /**
070: * This is a quickly hacked together demo of regular expression
071: * matching with three different regular expression syntaxes.
072: * It was originally written in JDK 1.0.2 days and hasn't changed
073: * much. It should be refactored into classes for a general purpose
074: * interactive testing interface that can be run as a standalone
075: * AWT application or embedded in an applet.
076: *
077: * @version @version@
078: */
079: public final class MatcherDemoApplet extends Applet {
080: static int CONTAINS_SEARCH = 0, MATCHES_SEARCH = 1;
081: static int CASE_SENSITIVE = 0, CASE_INSENSITIVE = 1;
082:
083: static int PERL5_EXPRESSION = 0;
084: static int AWK_EXPRESSION = 1;
085: static int GLOB_EXPRESSION = 2;
086:
087: static String[] expressionType = { "Perl5 Expression:",
088: "AWK Expression:", "Glob Expression:" };
089:
090: static int[] CASE_MASK[] = {
091: { Perl5Compiler.DEFAULT_MASK,
092: Perl5Compiler.CASE_INSENSITIVE_MASK },
093: { AwkCompiler.DEFAULT_MASK,
094: AwkCompiler.CASE_INSENSITIVE_MASK },
095: { GlobCompiler.DEFAULT_MASK,
096: GlobCompiler.CASE_INSENSITIVE_MASK } };
097:
098: TextField expressionField;
099: Label resultLabel, inputLabel;
100: TextArea resultArea, inputArea;
101: Choice expressionChoice, searchChoice, caseChoice;
102: Button searchButton, resetButton;
103: PatternCompiler compiler[];
104: PatternMatcher matcher[];
105:
106: public MatcherDemoApplet() {
107: setFont(new Font("Helvetica", Font.PLAIN, 14));
108: setBackground(new Color(210, 180, 140));
109:
110: expressionChoice = new Choice();
111:
112: for (int i = 0; i < expressionType.length; ++i)
113: expressionChoice.addItem(expressionType[i]);
114:
115: compiler = new PatternCompiler[expressionType.length];
116: matcher = new PatternMatcher[expressionType.length];
117:
118: compiler[PERL5_EXPRESSION] = new Perl5Compiler();
119: matcher[PERL5_EXPRESSION] = new Perl5Matcher();
120:
121: compiler[AWK_EXPRESSION] = new AwkCompiler();
122: matcher[AWK_EXPRESSION] = new AwkMatcher();
123:
124: compiler[GLOB_EXPRESSION] = new GlobCompiler();
125: matcher[GLOB_EXPRESSION] = matcher[PERL5_EXPRESSION];
126:
127: expressionField = new TextField(10);
128:
129: searchChoice = new Choice();
130: searchChoice.addItem("contains()");
131: searchChoice.addItem("matches()");
132: caseChoice = new Choice();
133: caseChoice.addItem("Case Sensitive");
134: caseChoice.addItem("Case Insensitive");
135: searchButton = new Button("Search");
136: resetButton = new Button("Reset");
137:
138: resultArea = new TextArea(20, 80);
139: inputArea = new TextArea(5, 80);
140: inputLabel = new Label("Search Input", Label.CENTER);
141: resultLabel = new Label("Search Results", Label.CENTER);
142: resultArea.setEditable(false);
143: }
144:
145: public void init() {
146: String param;
147: GridBagLayout layout;
148: GridBagConstraints constraints;
149:
150: if ((param = getParameter("background")) != null) {
151: try {
152: setBackground(new Color(Integer.parseInt(param, 16)));
153: } catch (NumberFormatException e) {
154: // do nothing, don't set color
155: }
156: }
157:
158: if ((param = getParameter("fontSize")) != null) {
159: Font font;
160:
161: font = getFont();
162:
163: try {
164: setFont(new Font(font.getFamily(), font.getStyle(),
165: Integer.parseInt(param)));
166: } catch (NumberFormatException e) {
167: // do nothing, don't set font size
168: }
169: }
170:
171: setLayout(layout = new GridBagLayout());
172:
173: constraints = new GridBagConstraints();
174: constraints.fill = GridBagConstraints.HORIZONTAL;
175: constraints.anchor = GridBagConstraints.EAST;
176: layout.setConstraints(expressionChoice, constraints);
177: add(expressionChoice);
178:
179: constraints.weightx = 1.0;
180: constraints.anchor = GridBagConstraints.WEST;
181: constraints.gridwidth = GridBagConstraints.REMAINDER;
182: layout.setConstraints(expressionField, constraints);
183: add(expressionField);
184:
185: constraints.gridwidth = 1;
186: layout.setConstraints(searchChoice, constraints);
187: add(searchChoice);
188:
189: layout.setConstraints(caseChoice, constraints);
190: add(caseChoice);
191:
192: layout.setConstraints(searchButton, constraints);
193: add(searchButton);
194:
195: constraints.gridwidth = GridBagConstraints.REMAINDER;
196: layout.setConstraints(resetButton, constraints);
197: add(resetButton);
198:
199: constraints.gridwidth = GridBagConstraints.REMAINDER;
200: layout.setConstraints(inputLabel, constraints);
201: add(inputLabel);
202:
203: constraints.gridwidth = GridBagConstraints.REMAINDER;
204: constraints.fill = GridBagConstraints.BOTH;
205: constraints.weighty = 0.25;
206: layout.setConstraints(inputArea, constraints);
207: add(inputArea);
208:
209: constraints.weighty = 0.0;
210: constraints.fill = GridBagConstraints.HORIZONTAL;
211: layout.setConstraints(resultLabel, constraints);
212: add(resultLabel);
213:
214: constraints.weighty = 1.0;
215: constraints.fill = GridBagConstraints.BOTH;
216: constraints.gridheight = GridBagConstraints.REMAINDER;
217: layout.setConstraints(resultArea, constraints);
218: add(resultArea);
219: }
220:
221: public void search() {
222: int matchNum, group, caseMask, exprChoice, search;
223: String text;
224: MatchResult result;
225: Pattern pattern;
226: PatternMatcherInput input;
227:
228: resultArea.setText("");
229: text = expressionField.getText();
230: exprChoice = expressionChoice.getSelectedIndex();
231: caseMask = CASE_MASK[exprChoice][caseChoice.getSelectedIndex()];
232:
233: resultArea.appendText("Compiling regular expression.\n");
234:
235: try {
236: pattern = compiler[exprChoice].compile(text, caseMask);
237: } catch (MalformedPatternException e) {
238: resultArea.appendText("\nMalformed Regular Expression:\n"
239: + e.getMessage());
240: return;
241: }
242:
243: search = searchChoice.getSelectedIndex();
244: text = inputArea.getText();
245: matchNum = 0;
246:
247: resultArea.appendText("\nSearching\n\n");
248:
249: if (search == MATCHES_SEARCH) {
250: if (matcher[exprChoice].matches(text, pattern))
251: resultArea.appendText("The input IS an EXACT match.\n");
252: else
253: resultArea
254: .appendText("The input IS NOT an EXACT match.\n");
255: } else {
256: input = new PatternMatcherInput(text);
257:
258: while (matcher[exprChoice].contains(input, pattern)) {
259: int groups;
260:
261: result = matcher[exprChoice].getMatch();
262: ++matchNum;
263:
264: resultArea.appendText("Match " + matchNum + ": "
265: + result.group(0) + "\n");
266: groups = result.groups();
267:
268: if (groups > 1) {
269: resultArea.appendText(" Subgroups:\n");
270: for (group = 1; group < groups; group++) {
271: resultArea.appendText(" " + group + ": "
272: + result.group(group) + "\n");
273: }
274: }
275: }
276:
277: resultArea.appendText("\nThe input contained " + matchNum
278: + " matches.");
279: }
280:
281: }
282:
283: public boolean action(Event event, Object arg) {
284: if (event.target == searchButton) {
285: search();
286: return true;
287: } else if (event.target == resetButton) {
288: resultArea.setText("");
289: inputArea.setText("");
290: expressionField.setText("");
291: return true;
292: }
293:
294: return false;
295: }
296: }
|