1. /*
  2. * Copyright 2001-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. * Regular expression factory, which will create Regexp objects. The
  22. * actual implementation class depends on the System or Ant Property:
  23. * <code>ant.regexp.regexpimpl</code>.
  24. *
  25. * @version $Revision: 1.11.2.4 $
  26. */
  27. public class RegexpFactory extends RegexpMatcherFactory {
  28. public RegexpFactory() {
  29. }
  30. /***
  31. * Create a new regular expression matcher instance.
  32. */
  33. public Regexp newRegexp() throws BuildException {
  34. return (Regexp) newRegexp(null);
  35. }
  36. /***
  37. * Create a new regular expression matcher instance.
  38. *
  39. * @param p Project whose ant.regexp.regexpimpl property will be used.
  40. */
  41. public Regexp newRegexp(Project p) throws BuildException {
  42. String systemDefault = null;
  43. if (p == null) {
  44. systemDefault = System.getProperty("ant.regexp.regexpimpl");
  45. } else {
  46. systemDefault = p.getProperty("ant.regexp.regexpimpl");
  47. }
  48. if (systemDefault != null) {
  49. return createRegexpInstance(systemDefault);
  50. // XXX should we silently catch possible exceptions and try to
  51. // load a different implementation?
  52. }
  53. try {
  54. testAvailability("java.util.regex.Matcher");
  55. return createRegexpInstance("org.apache.tools.ant.util.regexp.Jdk14RegexpRegexp");
  56. } catch (BuildException be) {
  57. // ignore
  58. }
  59. try {
  60. testAvailability("org.apache.oro.text.regex.Pattern");
  61. return createRegexpInstance("org.apache.tools.ant.util.regexp.JakartaOroRegexp");
  62. } catch (BuildException be) {
  63. // ignore
  64. }
  65. try {
  66. testAvailability("org.apache.regexp.RE");
  67. return createRegexpInstance("org.apache.tools.ant.util.regexp.JakartaRegexpRegexp");
  68. } catch (BuildException be) {
  69. // ignore
  70. }
  71. throw new BuildException("No supported regular expression matcher found");
  72. }
  73. /**
  74. * Wrapper over RegexpMatcherFactory.createInstance that ensures that
  75. * we are dealing with a Regexp implementation.
  76. *
  77. * @since 1.3
  78. *
  79. * @see RegexpMatcherFactory#createInstance(String)
  80. */
  81. protected Regexp createRegexpInstance(String classname)
  82. throws BuildException {
  83. RegexpMatcher m = createInstance(classname);
  84. if (m instanceof Regexp) {
  85. return (Regexp) m;
  86. } else {
  87. throw new BuildException(classname + " doesn't implement the Regexp interface");
  88. }
  89. }
  90. }