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 26 package com.sun.tools.javac.parser; 27 28 import java.nio.CharBuffer; 29 30 import com.sun.tools.javac.code.Source; 31 import com.sun.tools.javac.util.Context; 32 import com.sun.tools.javac.util.Log; 33 import 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 */ 44 public class ScannerFactory { 45 /** The context key for the scanner factory. */ 46 public static final Context.Key<ScannerFactory> scannerFactoryKey = 47 new Context.Key<ScannerFactory>(); 48 49 /** Get the Factory instance for this context. */ 50 public static ScannerFactory instance(Context context) { 51 ScannerFactory instance = context.get(scannerFactoryKey); 52 if (instance == null) 53 instance = new ScannerFactory(context); 54 return instance; 55 } 56 57 final Log log; 58 final Names names; 59 final Source source; 60 final Keywords keywords; 61 62 /** Create a new scanner factory. */ 63 protected ScannerFactory(Context context) { 64 context.put(scannerFactoryKey, this); 65 this.log = Log.instance(context); 66 this.names = Names.instance(context); 67 this.source = Source.instance(context); 68 this.keywords = Keywords.instance(context); 69 } 70 71 public Scanner newScanner(CharSequence input, boolean keepDocComments) { 72 if (input instanceof CharBuffer) { 73 CharBuffer buf = (CharBuffer) input; 74 if (keepDocComments) 75 return new DocCommentScanner(this, buf); 76 else 77 return new Scanner(this, buf); 78 } else { 79 char[] array = input.toString().toCharArray(); 80 return newScanner(array, array.length, keepDocComments); 81 } 82 } 83 84 public Scanner newScanner(char[] input, int inputLength, boolean keepDocComments) { 85 if (keepDocComments) 86 return new DocCommentScanner(this, input, inputLength); 87 else 88 return new Scanner(this, input, inputLength); 89 } 90 }