1 /*
   2  * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.nashorn.internal.runtime.regexp;
  27 
  28 import jdk.nashorn.internal.parser.Lexer;
  29 import jdk.nashorn.internal.runtime.ParserException;
  30 import jdk.nashorn.internal.runtime.options.Options;
  31 
  32 /**
  33  * Factory class for regular expressions. This class creates instances of {@link DefaultRegExp}.
  34  * An alternative factory can be installed using the {@code nashorn.regexp.impl} system property.
  35  */
  36 public class RegExpFactory {
  37 
  38 
  39     private final static RegExpFactory instance;
  40 
  41     private final static String JDK  = "jdk";
  42     private final static String JONI = "joni";
  43 
  44     static {
  45         final String impl = Options.getStringProperty("nashorn.regexp.impl", JDK);
  46         switch (impl) {
  47             case JONI:
  48                 instance = new JoniRegExp.Factory();
  49                 break;
  50             case JDK:
  51                 instance = new RegExpFactory();
  52                 break;
  53             default:
  54                 instance = null;
  55                 throw new InternalError("Unsupported RegExp factory: " + impl);
  56         }
  57     }
  58 
  59     /**
  60      * Creates a Regular expression from the given {@code pattern} and {@code flags} strings.
  61      *
  62      * @param pattern RegExp pattern string
  63      * @param flags RegExp flags string
  64      * @throws ParserException if flags is invalid or pattern string has syntax error.
  65      */
  66     protected RegExp compile(final String pattern, final String flags) throws ParserException {
  67         return new DefaultRegExp(pattern, flags);
  68     }
  69 
  70     /**
  71      * Replace a regexp token as suitable for regexp instances created by this factory.
  72      *
  73      * @param str a regular expression token
  74      * @return the replacement token
  75      */
  76     protected String replaceToken(final String str) {
  77         switch (str) {
  78             case "\\s":
  79                 return "[" + Lexer.getWhitespaceRegExp() + "]";
  80             case "\\S":
  81                 return "[^" + Lexer.getWhitespaceRegExp() + "]";
  82             case "[^]":
  83                 return "[\\s\\S]";
  84             default:
  85                 return str;
  86         }
  87     }
  88 
  89     /**
  90      * Compile a regexp with the given {@code source} and {@code flags}.
  91      *
  92      * @param pattern RegExp pattern string
  93      * @param flags  flag string
  94      *
  95      * @throws ParserException if invalid source or flags
  96      */
  97     public static RegExp create(final String pattern, final String flags) {
  98         return instance.compile(pattern,  flags);
  99     }
 100 
 101     /**
 102      * Replace a regexp token as needed by the currently installed factory instance.
 103      *
 104      * @param token a regexp token
 105      * @return the replacement token
 106      */
 107     public static String replace(final String token) {
 108         return instance.replaceToken(token);
 109     }
 110 
 111     /**
 112      * Validate a regexp with the given {@code source} and {@code flags}.
 113      *
 114      * @param pattern RegExp pattern string
 115      * @param flags  flag string
 116      *
 117      * @throws ParserException if invalid source or flags
 118      */
 119     // @SuppressWarnings({"unused"})
 120     public static void validate(final String pattern, final String flags) throws ParserException {
 121         instance.compile(pattern, flags);
 122     }
 123 }
--- EOF ---