1 /*
   2  * Copyright (c) 2009, 2015, 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 jdk.internal.module;
  27 
  28 public final class Checks {
  29 
  30     private Checks() { }
  31 
  32     private static void fail(String what, String id, int i) {
  33         throw new IllegalArgumentException(id
  34                                            + ": Invalid " + what + ": "
  35                                            + " Illegal character"
  36                                            + " at index " + i);
  37     }
  38 
  39     /**
  40      * Returns {@code true} if the given identifier is a legal Java identifier.
  41      */
  42     public static boolean isJavaIdentifier(String id) {
  43         int n = id.length();
  44         if (n == 0)
  45             return false;
  46         if (!Character.isJavaIdentifierStart(id.codePointAt(0)))
  47             return false;
  48         int cp = id.codePointAt(0);
  49         int i = Character.charCount(cp);
  50         for (; i < n; i += Character.charCount(cp)) {
  51             cp = id.codePointAt(i);
  52             if (!Character.isJavaIdentifierPart(cp) && id.charAt(i) != '.')
  53                 return false;
  54         }
  55         if (cp == '.')
  56             return false;
  57 
  58         return true;
  59     }
  60 
  61     /**
  62      * Checks if a given identifier is a legal Java identifier.
  63      */
  64     public static String requireJavaIdentifier(String what, String id) {
  65         if (id == null)
  66             throw new IllegalArgumentException("Null " + what);
  67         int n = id.length();
  68         if (n == 0)
  69             throw new IllegalArgumentException("Empty " + what);
  70         if (!Character.isJavaIdentifierStart(id.codePointAt(0)))
  71             fail(what, id, 0);
  72         int cp = id.codePointAt(0);
  73         int i = Character.charCount(cp);
  74         int last = 0;
  75         for (; i < n; i += Character.charCount(cp)) {
  76             cp = id.codePointAt(i);
  77             if (!Character.isJavaIdentifierPart(cp) && id.charAt(i) != '.')
  78                 fail(what, id, i);
  79             last = i;
  80         }
  81         if (cp == '.')
  82             fail(what, id, last);
  83 
  84         return id;
  85     }
  86 
  87     public static String requireModuleName(String id) {
  88         return requireJavaIdentifier("module name", id);
  89     }
  90 
  91     public static String requirePackageName(String id) {
  92         return requireJavaIdentifier("package name", id);
  93     }
  94 
  95     public static String requireServiceTypeName(String id) {
  96         return requireJavaIdentifier("service type name", id);
  97     }
  98 
  99     public static String requireServiceProviderName(String id) {
 100         return requireJavaIdentifier("service provider name", id);
 101     }
 102 
 103 }