ScannerFactory.java revision 2571:10fc81ac75b4
1343171Sdim/* 2343171Sdim * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved. 3353358Sdim * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4353358Sdim * 5353358Sdim * This code is free software; you can redistribute it and/or modify it 6343171Sdim * under the terms of the GNU General Public License version 2 only, as 7343171Sdim * published by the Free Software Foundation. Oracle designates this 8343171Sdim * particular file as subject to the "Classpath" exception as provided 9343171Sdim * by Oracle in the LICENSE file that accompanied this code. 10343171Sdim * 11343171Sdim * This code is distributed in the hope that it will be useful, but WITHOUT 12343171Sdim * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13343171Sdim * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14343171Sdim * version 2 for more details (a copy is included in the LICENSE file that 15343171Sdim * accompanied this code). 16343171Sdim * 17343171Sdim * You should have received a copy of the GNU General Public License version 18360784Sdim * 2 along with this work; if not, write to the Free Software Foundation, 19343171Sdim * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20343171Sdim * 21343171Sdim * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22343171Sdim * or visit www.oracle.com if you need additional information or have any 23360784Sdim * questions. 24343171Sdim */ 25343171Sdim 26343171Sdimpackage com.sun.tools.javac.parser; 27343171Sdim 28343171Sdimimport java.nio.CharBuffer; 29360784Sdim 30343171Sdimimport com.sun.tools.javac.code.Source; 31343171Sdimimport com.sun.tools.javac.util.Context; 32343171Sdimimport com.sun.tools.javac.util.Log; 33343171Sdimimport com.sun.tools.javac.util.Names; 34343171Sdim 35353358Sdim 36343171Sdim/** 37343171Sdim * A factory for creating scanners. 38343171Sdim * 39343171Sdim * <p><b>This is NOT part of any supported API. 40343171Sdim * If you write code that depends on this, you do so at your own 41343171Sdim * risk. This code and its internal interfaces are subject to change 42343171Sdim * or deletion without notice.</b> 43343171Sdim */ 44343171Sdimpublic class ScannerFactory { 45343171Sdim /** The context key for the scanner factory. */ 46343171Sdim public static final Context.Key<ScannerFactory> scannerFactoryKey = new Context.Key<>(); 47360784Sdim 48343171Sdim /** Get the Factory instance for this context. */ 49343171Sdim public static ScannerFactory instance(Context context) { 50360784Sdim ScannerFactory instance = context.get(scannerFactoryKey); 51360784Sdim if (instance == null) 52360784Sdim instance = new ScannerFactory(context); 53353358Sdim return instance; 54353358Sdim } 55343171Sdim 56343171Sdim final Log log; 57343171Sdim final Names names; 58343171Sdim final Source source; 59343171Sdim final Tokens tokens; 60343171Sdim 61343171Sdim /** Create a new scanner factory. */ 62343171Sdim protected ScannerFactory(Context context) { 63343171Sdim context.put(scannerFactoryKey, this); 64343171Sdim this.log = Log.instance(context); 65343171Sdim this.names = Names.instance(context); 66343171Sdim this.source = Source.instance(context); 67343171Sdim this.tokens = Tokens.instance(context); 68343171Sdim } 69343171Sdim 70343171Sdim public Scanner newScanner(CharSequence input, boolean keepDocComments) { 71343171Sdim if (input instanceof CharBuffer) { 72343171Sdim CharBuffer buf = (CharBuffer) input; 73343171Sdim if (keepDocComments) 74343171Sdim return new Scanner(this, new JavadocTokenizer(this, buf)); 75343171Sdim else 76343171Sdim return new Scanner(this, buf); 77343171Sdim } else { 78343171Sdim char[] array = input.toString().toCharArray(); 79343171Sdim return newScanner(array, array.length, keepDocComments); 80343171Sdim } 81343171Sdim } 82343171Sdim 83343171Sdim public Scanner newScanner(char[] input, int inputLength, boolean keepDocComments) { 84343171Sdim if (keepDocComments) 85343171Sdim return new Scanner(this, new JavadocTokenizer(this, input, inputLength)); 86343171Sdim else 87343171Sdim return new Scanner(this, input, inputLength); 88343171Sdim } 89343171Sdim} 90343171Sdim