001    /**
002     *
003     * Copyright 2004 James Strachan
004     *
005     * Licensed under the Apache License, Version 2.0 (the "License");
006     * you may not use this file except in compliance with the License.
007     * You may obtain a copy of the License at
008     *
009     * http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     *
017     **/
018    package org.codehaus.groovy.control;
019    
020    import org.codehaus.groovy.antlr.AntlrParserPluginFactory;
021    
022    /**
023     * A factory of parser plugin instances
024     *
025     * @version $Revision: 1.8 $
026     */
027    public abstract class ParserPluginFactory {
028        public static ParserPluginFactory newInstance(boolean useNewParser) {
029            if (useNewParser) {
030                Class type = null;
031                String name = "org.codehaus.groovy.antlr.AntlrParserPluginFactory";
032                try {
033                    type = Class.forName(name);
034                }
035                catch (ClassNotFoundException e) {
036                    try {
037                        type = ParserPluginFactory.class.getClassLoader().loadClass(name);
038                    }
039                    catch (ClassNotFoundException e1) {
040                        ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
041                        if (contextClassLoader != null) {
042                            try {
043                                type = contextClassLoader.loadClass(name);
044                            }
045                            catch (ClassNotFoundException e2) {
046                                // ignore
047                            }
048                        }
049                    }
050                }
051    
052                if (type != null) {
053                    try {
054                        return (ParserPluginFactory) type.newInstance();
055                    }
056                    catch (Exception e) {
057                        throw new RuntimeException("Could not create AntlrParserPluginFactory: " + e, e);
058                    }
059                }
060                // can't find Antlr parser, so lets use the Classic one
061            }
062            return new AntlrParserPluginFactory();
063        }
064    
065        public abstract ParserPlugin createParserPlugin();
066    }