1. package com.sun.org.apache.regexp.internal;
  2. /*
  3. * ====================================================================
  4. *
  5. * The Apache Software License, Version 1.1
  6. *
  7. * Copyright (c) 1999 The Apache Software Foundation. All rights
  8. * reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. *
  14. * 1. Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. *
  17. * 2. Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in
  19. * the documentation and/or other materials provided with the
  20. * distribution.
  21. *
  22. * 3. The end-user documentation included with the redistribution, if
  23. * any, must include the following acknowlegement:
  24. * "This product includes software developed by the
  25. * Apache Software Foundation (http://www.apache.org/)."
  26. * Alternately, this acknowlegement may appear in the software itself,
  27. * if and wherever such third-party acknowlegements normally appear.
  28. *
  29. * 4. The names "The Jakarta Project", "Jakarta-Regexp", and "Apache Software
  30. * Foundation" must not be used to endorse or promote products derived
  31. * from this software without prior written permission. For written
  32. * permission, please contact apache@apache.org.
  33. *
  34. * 5. Products derived from this software may not be called "Apache"
  35. * nor may "Apache" appear in their names without prior written
  36. * permission of the Apache Group.
  37. *
  38. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  39. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  40. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  41. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  42. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  43. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  44. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  45. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  46. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  47. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  48. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  49. * SUCH DAMAGE.
  50. * ====================================================================
  51. *
  52. * This software consists of voluntary contributions made by many
  53. * individuals on behalf of the Apache Software Foundation. For more
  54. * information on the Apache Software Foundation, please see
  55. * <http://www.apache.org/>.
  56. *
  57. */
  58. import java.applet.*;
  59. import java.awt.*;
  60. import java.awt.event.*;
  61. import java.io.*;
  62. import javax.swing.*;
  63. /**
  64. * Interactive demonstration and testing harness for regular expressions classes.
  65. * @author <a href="mailto:jonl@muppetlabs.com">Jonathan Locke</a>
  66. * @version $Id: REDemo.java,v 1.1 2000/04/27 01:22:33 jon Exp $
  67. */
  68. public class REDemo extends Applet implements TextListener
  69. {
  70. /**
  71. * Matcher and compiler objects
  72. */
  73. RE r = new RE();
  74. REDebugCompiler compiler = new REDebugCompiler();
  75. /**
  76. * Components
  77. */
  78. TextField fieldRE; // Field for entering regexps
  79. TextField fieldMatch; // Field for entering match strings
  80. TextArea outRE; // Output of RE compiler
  81. TextArea outMatch; // Results of matching operation
  82. /**
  83. * Add controls and init applet
  84. */
  85. public void init()
  86. {
  87. // Add components using the dreaded GridBagLayout
  88. GridBagLayout gb = new GridBagLayout();
  89. setLayout(gb);
  90. GridBagConstraints c = new GridBagConstraints();
  91. c.insets = new Insets(5, 5, 5, 5);
  92. c.anchor = c.EAST;
  93. gb.setConstraints(add(new Label("Regular expression:", Label.RIGHT)), c);
  94. c.gridy = 0;
  95. c.anchor = c.WEST;
  96. gb.setConstraints(add(fieldRE = new TextField("\\[([:javastart:][:javapart:]*)\\]", 40)), c);
  97. c.gridx = 0;
  98. c.gridy = c.RELATIVE;
  99. c.anchor = c.EAST;
  100. gb.setConstraints(add(new Label("String:", Label.RIGHT)), c);
  101. c.gridy = 1;
  102. c.gridx = c.RELATIVE;
  103. c.anchor = c.WEST;
  104. gb.setConstraints(add(fieldMatch = new TextField("aaa([foo])aaa", 40)), c);
  105. c.gridy = 2;
  106. c.gridx = c.RELATIVE;
  107. c.fill = c.BOTH;
  108. c.weighty = 1.0;
  109. c.weightx = 1.0;
  110. gb.setConstraints(add(outRE = new TextArea()), c);
  111. c.gridy = 2;
  112. c.gridx = c.RELATIVE;
  113. gb.setConstraints(add(outMatch = new TextArea()), c);
  114. // Listen to text changes
  115. fieldRE.addTextListener(this);
  116. fieldMatch.addTextListener(this);
  117. // Initial UI update
  118. textValueChanged(null);
  119. }
  120. /**
  121. * Say something into RE text area
  122. * @param s What to say
  123. */
  124. void sayRE(String s)
  125. {
  126. outRE.setText(s);
  127. }
  128. /**
  129. * Say something into match text area
  130. * @param s What to say
  131. */
  132. void sayMatch(String s)
  133. {
  134. outMatch.setText(s);
  135. }
  136. /**
  137. * Convert throwable to string
  138. * @param t Throwable to convert to string
  139. */
  140. String throwableToString(Throwable t)
  141. {
  142. String s = t.getClass().getName();
  143. String m;
  144. if ((m = t.getMessage()) != null)
  145. {
  146. s += "\n" + m;
  147. }
  148. return s;
  149. }
  150. /**
  151. * Change regular expression
  152. * @param expr Expression to compile
  153. */
  154. void updateRE(String expr)
  155. {
  156. try
  157. {
  158. // Compile program
  159. r.setProgram(compiler.compile(expr));
  160. // Dump program into RE feedback area
  161. CharArrayWriter w = new CharArrayWriter();
  162. compiler.dumpProgram(new PrintWriter(w));
  163. sayRE(w.toString());
  164. System.out.println(w);
  165. }
  166. catch (Exception e)
  167. {
  168. r.setProgram(null);
  169. sayRE(throwableToString(e));
  170. }
  171. catch (Throwable t)
  172. {
  173. r.setProgram(null);
  174. sayRE(throwableToString(t));
  175. }
  176. }
  177. /**
  178. * Update matching info by matching the string against the current
  179. * compiled regular expression.
  180. * @param match String to match against
  181. */
  182. void updateMatch(String match)
  183. {
  184. try
  185. {
  186. // If the string matches the regexp
  187. if (r.match(match))
  188. {
  189. // Say that it matches
  190. String out = "Matches.\n\n";
  191. // Show contents of parenthesized subexpressions
  192. for (int i = 0; i < r.getParenCount(); i++)
  193. {
  194. out += "$" + i + " = " + r.getParen(i) + "\n";
  195. }
  196. sayMatch(out);
  197. }
  198. else
  199. {
  200. // Didn't match!
  201. sayMatch("Does not match");
  202. }
  203. }
  204. catch (Throwable t)
  205. {
  206. sayMatch(throwableToString(t));
  207. }
  208. }
  209. /**
  210. * Called when text values change
  211. * @param e TextEvent
  212. */
  213. public void textValueChanged(TextEvent e)
  214. {
  215. // If it's a generic update or the regexp changed...
  216. if (e == null || e.getSource() == fieldRE)
  217. {
  218. // Update regexp
  219. updateRE(fieldRE.getText());
  220. }
  221. // We always need to update the match results
  222. updateMatch(fieldMatch.getText());
  223. }
  224. /**
  225. * Main application entrypoint.
  226. * @param arg Command line arguments
  227. */
  228. static public void _main(String[] arg)
  229. {
  230. JFrame f = new JFrame("RE Demo");
  231. // f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  232. f.addWindowListener(new WindowAdapter()
  233. {
  234. public void windowClosing(WindowEvent e)
  235. {
  236. System.exit(0);
  237. }
  238. });
  239. Container c = f.getContentPane();
  240. c.setLayout(new FlowLayout());
  241. REDemo demo = new REDemo();
  242. c.add(demo);
  243. demo.init();
  244. f.pack();
  245. f.setVisible(true);
  246. }
  247. }