1. /*
  2. * Copyright 2000-2004 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. package org.apache.tools.ant.util.regexp;
  18. import org.apache.tools.ant.BuildException;
  19. import org.apache.tools.ant.Project;
  20. /**
  21. * Simple Factory Class that produces an implementation of
  22. * RegexpMatcher based on the system property
  23. * <code>ant.regexp.matcherimpl</code> and the classes
  24. * available.
  25. *
  26. * <p>In a more general framework this class would be abstract and
  27. * have a static newInstance method.</p>
  28. *
  29. */
  30. public class RegexpMatcherFactory {
  31. public RegexpMatcherFactory() {
  32. }
  33. /***
  34. * Create a new regular expression instance.
  35. */
  36. public RegexpMatcher newRegexpMatcher() throws BuildException {
  37. return newRegexpMatcher(null);
  38. }
  39. /***
  40. * Create a new regular expression instance.
  41. *
  42. * @param p Project whose ant.regexp.regexpimpl property will be used.
  43. */
  44. public RegexpMatcher newRegexpMatcher(Project p)
  45. throws BuildException {
  46. String systemDefault = null;
  47. if (p == null) {
  48. systemDefault = System.getProperty("ant.regexp.regexpimpl");
  49. } else {
  50. systemDefault = p.getProperty("ant.regexp.regexpimpl");
  51. }
  52. if (systemDefault != null) {
  53. return createInstance(systemDefault);
  54. // XXX should we silently catch possible exceptions and try to
  55. // load a different implementation?
  56. }
  57. try {
  58. testAvailability("java.util.regex.Matcher");
  59. return createInstance("org.apache.tools.ant.util.regexp.Jdk14RegexpMatcher");
  60. } catch (BuildException be) {
  61. // ignore
  62. }
  63. try {
  64. testAvailability("org.apache.oro.text.regex.Pattern");
  65. return createInstance("org.apache.tools.ant.util.regexp.JakartaOroMatcher");
  66. } catch (BuildException be) {
  67. // ignore
  68. }
  69. try {
  70. testAvailability("org.apache.regexp.RE");
  71. return createInstance("org.apache.tools.ant.util.regexp.JakartaRegexpMatcher");
  72. } catch (BuildException be) {
  73. // ignore
  74. }
  75. throw new BuildException("No supported regular expression matcher found");
  76. }
  77. protected RegexpMatcher createInstance(String className)
  78. throws BuildException {
  79. try {
  80. Class implClass = Class.forName(className);
  81. return (RegexpMatcher) implClass.newInstance();
  82. } catch (Throwable t) {
  83. throw new BuildException(t);
  84. }
  85. }
  86. protected void testAvailability(String className) throws BuildException {
  87. try {
  88. Class.forName(className);
  89. } catch (Throwable t) {
  90. throw new BuildException(t);
  91. }
  92. }
  93. }