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