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