RegExpFactory.java revision 1041:dee18a93b53f
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
26package jdk.nashorn.internal.runtime.regexp;
27
28import java.util.Collections;
29import java.util.Set;
30import java.util.WeakHashMap;
31import jdk.nashorn.internal.runtime.ParserException;
32import jdk.nashorn.internal.runtime.options.Options;
33
34/**
35 * Factory class for regular expressions. This class creates instances of {@link JdkRegExp}.
36 * An alternative factory can be installed using the {@code nashorn.regexp.impl} system property.
37 */
38public class RegExpFactory {
39
40    private final static RegExpFactory instance;
41
42    private final static String JDK  = "jdk";
43    private final static String JONI = "joni";
44
45    /** Weak cache of already validated regexps - when reparsing, we don't, for example
46     *  need to recompile (reverify) all regexps that have previously been parsed by this
47     *  RegExpFactory in a previous compilation. This saves significant time in e.g. avatar
48     *  startup */
49    private static final Set<String> VALID_CACHE_SET =
50            Collections.newSetFromMap(
51                    Collections.synchronizedMap(
52                            new WeakHashMap<String, Boolean>()));
53
54    static {
55        final String impl = Options.getStringProperty("nashorn.regexp.impl", JONI);
56        switch (impl) {
57            case JONI:
58                instance = new JoniRegExp.Factory();
59                break;
60            case JDK:
61                instance = new RegExpFactory();
62                break;
63            default:
64                instance = null;
65                throw new InternalError("Unsupported RegExp factory: " + impl);
66        }
67    }
68
69    /**
70     * Creates a Regular expression from the given {@code pattern} and {@code flags} strings.
71     *
72     * @param pattern RegExp pattern string
73     * @param flags   RegExp flags string
74     * @return new RegExp
75     * @throws ParserException if flags is invalid or pattern string has syntax error.
76     */
77    public RegExp compile(final String pattern, final String flags) throws ParserException {
78        return new JdkRegExp(pattern, flags);
79    }
80
81    /**
82     * Compile a regexp with the given {@code source} and {@code flags}.
83     *
84     * @param pattern RegExp pattern string
85     * @param flags   flag string
86     * @return new RegExp
87     * @throws ParserException if invalid source or flags
88     */
89    public static RegExp create(final String pattern, final String flags) {
90        return instance.compile(pattern,  flags);
91    }
92
93    /**
94     * Validate a regexp with the given {@code source} and {@code flags}.
95     *
96     * @param pattern RegExp pattern string
97     * @param flags  flag string
98     *
99     * @throws ParserException if invalid source or flags
100     */
101    // @SuppressWarnings({"unused"})
102    public static void validate(final String pattern, final String flags) throws ParserException {
103        if (VALID_CACHE_SET.add(pattern + flags)) {
104            instance.compile(pattern, flags);
105        }
106    }
107
108    /**
109     * Returns true if the instance uses the JDK's {@code java.util.regex} package.
110     *
111     * @return true if instance uses JDK regex package
112     */
113    public static boolean usesJavaUtilRegex() {
114        return instance != null && instance.getClass() == RegExpFactory.class;
115    }
116}
117