1 /*
2  * Copyright (c) 2005, 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 com.sun.tools.javac.util;
27 
28 import java.util.regex.Pattern;
29 import javax.lang.model.SourceVersion;
30 
31 /**Utilities to convert an import-like string to a regexp.
32  *
33  *  <p><b>This is NOT part of any supported API.
34  *  If you write code that depends on this, you do so at your own risk.
35  *  This code and its internal interfaces are subject to change or
36  *  deletion without notice.</b>
37  */
38 public class MatchingUtils {
39 
40     private static final String allMatchesString = ".*";
41     private static final Pattern allMatches = Pattern.compile(allMatchesString);
42 
43     /**
44      * Return true if the argument string is a valid import-style
45      * string specifying claimed annotations; return false otherwise.
46      */
isValidImportString(String s)47     public static boolean isValidImportString(String s) {
48         if (s.equals("*"))
49             return true;
50 
51         boolean valid = true;
52         String t = s;
53         int index = t.indexOf('*');
54 
55         if (index != -1) {
56             // '*' must be last character...
57             if (index == t.length() -1) {
58                 // ... any and preceding character must be '.'
59                 if ( index-1 >= 0 ) {
60                     valid = t.charAt(index-1) == '.';
61                     // Strip off ".*$" for identifier checks
62                     t = t.substring(0, t.length()-2);
63                 }
64             } else
65                 return false;
66         }
67 
68         // Verify string is off the form (javaId \.)+ or javaId
69         if (valid) {
70             String[] javaIds = t.split("\\.", t.length()+2);
71             for(String javaId: javaIds)
72                 valid &= SourceVersion.isIdentifier(javaId);
73         }
74         return valid;
75     }
76 
validImportStringToPatternString(String s)77     public static String validImportStringToPatternString(String s) {
78         if (s.equals("*")) {
79             return allMatchesString;
80         } else {
81             String s_prime = s.replace(".", "\\.");
82 
83             if (s_prime.endsWith("*")) {
84                 s_prime =  s_prime.substring(0, s_prime.length() - 1) + ".+";
85             }
86 
87             return s_prime;
88         }
89     }
90 
validImportStringToPattern(String s)91     public static Pattern validImportStringToPattern(String s) {
92         String pattern = validImportStringToPatternString(s);
93 
94         if (pattern == allMatchesString) {
95             return allMatches;
96         } else {
97             return Pattern.compile(pattern);
98         }
99     }
100 
101 }
102