1 /*
   2  * Copyright (c) 2012, 2014, Oracle and/or its affiliates.
   3  * All rights reserved. Use is subject to license terms.
   4  *
   5  * This file is available and licensed under the following license:
   6  *
   7  * Redistribution and use in source and binary forms, with or without
   8  * modification, are permitted provided that the following conditions
   9  * are met:
  10  *
  11  *  - Redistributions of source code must retain the above copyright
  12  *    notice, this list of conditions and the following disclaimer.
  13  *  - Redistributions in binary form must reproduce the above copyright
  14  *    notice, this list of conditions and the following disclaimer in
  15  *    the documentation and/or other materials provided with the distribution.
  16  *  - Neither the name of Oracle Corporation nor the names of its
  17  *    contributors may be used to endorse or promote products derived
  18  *    from this software without specific prior written permission.
  19  *
  20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31  */
  32 package com.oracle.javafx.scenebuilder.kit.util;
  33 
  34 import java.util.Collections;
  35 import java.util.HashSet;
  36 import java.util.Set;
  37 
  38 /**
  39  *
  40  */
  41 public class JavaLanguage {
  42     
  43     /**
  44      * Returns true if value is a valid identifier (as specified 
  45      * in Java Language Specification, section 3.8).
  46      * 
  47      * @param value string to test (can be null or empty)
  48      * @return true if value is a valid java identifier.
  49      */
  50     public static boolean isIdentifier(String value) {
  51         /*
  52          * See Java JavaLanguage Specification, section 3.8: Identifiers
  53          * https://docs.oracle.com/javase/specs/jls/se7/html/jls-3.html#jls-3.8
  54          */
  55         return isIdentifierChars(value)
  56                 && ! isKeyword(value)
  57                 && ! isBooleanLiteral(value)
  58                 && ! isNullLiteral(value);
  59     }
  60     
  61     
  62     /**
  63      * Returns true if value is a valid class name (fully qualified or not).
  64      * 
  65      * @param value string to test (can be null or empty)
  66      * @return  true if value is a valid class name
  67      */
  68     public static boolean isClassName(String value) {
  69         boolean result;
  70         
  71         if (value == null) {
  72             result = false;
  73         } else {
  74             result = true;
  75             for (String item : value.split("\\.")) { //NOI18N
  76                 if (isIdentifier(item) == false) {
  77                     result = false;
  78                     break;
  79                 }
  80             }
  81         }
  82         
  83         return result;
  84     }
  85     
  86     /*
  87      * Private
  88      */
  89     
  90     private static boolean isIdentifierChars(String value) {
  91         if (value == null || value.isEmpty()
  92                 || !Character.isJavaIdentifierStart(value.codePointAt(0))) {
  93             return false;
  94         }
  95         for (int i = 0; i < value.length();) {
  96             int codePoint = value.codePointAt(i);
  97             if (!Character.isJavaIdentifierPart(codePoint)) {
  98                 return false;
  99             }
 100             i += Character.charCount(codePoint);
 101         }
 102         return true;
 103     }
 104     
 105     private static Set<String> keywords;
 106     private static synchronized boolean isKeyword(String value) {
 107         if (keywords == null) {
 108             keywords = new HashSet<>();
 109             Collections.addAll(
 110                     keywords,
 111                     "abstract", "continue", "for", "new", "switch", //NOI18N
 112                     "assert", "default", "if", "package", "synchronized", //NOI18N
 113                     "boolean", "do", "goto", "private", "this", //NOI18N
 114                     "break", "double", "implements", "protected", "throw", //NOI18N
 115                     "byte", "else", "import", "public", "throws", //NOI18N
 116                     "case", "enum", "instanceof", "return", "transient", //NOI18N
 117                     "catch", "extends", "int", "short", "try", //NOI18N
 118                     "char", "final", "interface", "static", "void", //NOI18N
 119                     "class", "finally", "long", "strictfp", "volatile",  //NOI18N
 120                     "const", "float", "native", "super", "while"); //NOI18N
 121         }
 122         return keywords.contains(value);
 123     }
 124     
 125     private static boolean isBooleanLiteral(String value) {
 126         return value.equals("true") || value.equals("false"); //NOI18N
 127     }
 128     
 129     private static boolean isNullLiteral(String value) {
 130         return value.equals("null"); //NOI18N
 131     }
 132 }