1/* 2 * Copyright (c) 1999, 2010, 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 com.sun.tools.javac.parser; 27 28import java.nio.CharBuffer; 29 30import com.sun.tools.javac.code.Source; 31import com.sun.tools.javac.util.Context; 32import com.sun.tools.javac.util.Log; 33import com.sun.tools.javac.util.Names; 34 35 36/** 37 * A factory for creating scanners. 38 * 39 * <p><b>This is NOT part of any supported API. 40 * If you write code that depends on this, you do so at your own 41 * risk. This code and its internal interfaces are subject to change 42 * or deletion without notice.</b> 43 */ 44public class ScannerFactory { 45 /** The context key for the scanner factory. */ 46 public static final Context.Key<ScannerFactory> scannerFactoryKey = new Context.Key<>(); 47 48 /** Get the Factory instance for this context. */ 49 public static ScannerFactory instance(Context context) { 50 ScannerFactory instance = context.get(scannerFactoryKey); 51 if (instance == null) 52 instance = new ScannerFactory(context); 53 return instance; 54 } 55 56 final Log log; 57 final Names names; 58 final Source source; 59 final Tokens tokens; 60 61 /** Create a new scanner factory. */ 62 protected ScannerFactory(Context context) { 63 context.put(scannerFactoryKey, this); 64 this.log = Log.instance(context); 65 this.names = Names.instance(context); 66 this.source = Source.instance(context); 67 this.tokens = Tokens.instance(context); 68 } 69 70 public Scanner newScanner(CharSequence input, boolean keepDocComments) { 71 if (input instanceof CharBuffer) { 72 CharBuffer buf = (CharBuffer) input; 73 if (keepDocComments) 74 return new Scanner(this, new JavadocTokenizer(this, buf)); 75 else 76 return new Scanner(this, buf); 77 } else { 78 char[] array = input.toString().toCharArray(); 79 return newScanner(array, array.length, keepDocComments); 80 } 81 } 82 83 public Scanner newScanner(char[] input, int inputLength, boolean keepDocComments) { 84 if (keepDocComments) 85 return new Scanner(this, new JavadocTokenizer(this, input, inputLength)); 86 else 87 return new Scanner(this, input, inputLength); 88 } 89} 90