2 * Copyright (C) 2014, United States Government, as represented by the
3 * Administrator of the National Aeronautics and Space Administration.
6 * The Java Pathfinder core (jpf-core) platform is licensed under the
7 * Apache License, Version 2.0 (the "License"); you may not use this file except
8 * in compliance with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0.
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package gov.nasa.jpf.util;
21 import java.util.ArrayList;
22 import java.util.regex.Matcher;
23 import java.util.regex.Pattern;
26 * utility to perform pathname expansion
27 * the following patterns are supported so far:
29 * (1) brace expansion ala bash: foo{Boo,Shoo} => fooBoo, fooShoo
30 * (this doesn't check for existence, its simply lexical)
32 * (2) '*' wildcard pathname expansion ala bash: "*.java" | "*\Main*.java"
33 * (supports wildcards in mutiple path elements and within file/dir name)
35 * (3) recursive dir expansion ala Ant: "**\*.jar"
38 public class PathnameExpander {
40 public String[] expandPath (String s) {
41 if (s == null || s.length() == 0) {
45 boolean hasWildcards = (s.indexOf('*') >= 0);
47 int i = s.indexOf('{');
49 ArrayList<String> list = new ArrayList<String>();
51 int j=0, jLast = s.length();
52 for (; (i = s.indexOf('{', j)) >= 0;) {
53 if ((j = s.indexOf('}', i)) > 0) {
54 String[] choices = s.substring(i + 1, j).split(",");
57 String prefix = s.substring(0, i);
58 for (String c : choices) {
62 String prefix = s.substring(jLast, i);
63 ArrayList<String> newList = new ArrayList<String>();
64 for (String e : list) {
65 for (String c : choices) {
66 newList.add(e + prefix + c);
73 throw new IllegalArgumentException("illegal path spec (missing '}'): " + s);
77 if (jLast < s.length()) {
78 String postfix = s.substring(jLast);
79 ArrayList<String> newList = new ArrayList<String>();
80 for (String e : list) {
81 newList.add(e + postfix);
87 ArrayList<String> newList = new ArrayList<String>();
88 for (String p : list) {
89 for (String c : expandWildcards(p)) {
96 return list.toArray(new String[list.size()]);
98 } else { // no bracket expansion required
101 return expandWildcards(s);
103 } else { // nothing to expand at all
104 return (new String[] {s});
109 protected String[] expandWildcards (String s){
110 int i = s.indexOf('*');
112 if (i >= 0){ // Ok, we have at least one wildcard
113 String[] a = s.split("\\/");
114 ArrayList<File> list = new ArrayList<File>();
116 int j= initializeMatchList(list, a[0]);
117 for (; j<a.length; j++){
118 ArrayList<File> newList = new ArrayList<File>();
121 if (e.indexOf('*') >= 0){
123 if (e.equals("**")){ // matches all subdirs recursively
124 collectDirs(list, newList);
126 } else { // file/dir name match
127 collectMatchingNames(list, newList, getPattern(e));
130 } else { // no wildcard
131 collectExistingFile(list, newList, e);
134 if (newList.isEmpty()){ // shortcut, nothing more to match
135 return new String[0];
140 return getPaths(list);
142 } else { // no wildcards, nothing to expand
143 return new String[] {s};
147 private int initializeMatchList (ArrayList<File> list, String path){
148 if (path.isEmpty()){ // absolute pathname (ignoring drive letters for now)
149 list.add(new File(File.separator));
151 } else if (path.equals("..") || path.equals(".")){
152 list.add(new File(path));
155 list.add(new File("."));
160 private void collectMatchingNames(ArrayList<File> list, ArrayList<File> newList, Pattern pattern){
161 for (File dir : list) {
162 if (dir.isDirectory()){
163 for (String c : dir.list()){
164 Matcher m = pattern.matcher(c);
166 newList.add(new File(dir,c));
173 private void collectExistingFile(ArrayList<File> list, ArrayList<File> newList, String fname) {
174 for (File dir : list) {
175 if (dir.isDirectory()){
176 File nf = new File(dir, fname);
184 private void collectDirs(ArrayList<File> list, ArrayList<File> newList){
185 for (File dir : list) {
186 if (dir.isDirectory()){
187 newList.add(dir); // this includes the dir itself!
188 collectSubdirs(newList,dir);
192 private void collectSubdirs(ArrayList<File> newList, File dir) {
193 for (File f : dir.listFiles()){
194 if (f.isDirectory()){
196 collectSubdirs(newList, f);
201 protected String[] getPaths(ArrayList<File> list) {
202 String[] result = new String[list.size()];
205 String p = f.getPath();
206 if ((p.length() > 1) && (p.charAt(0) == '.')){ // remove leading "./"
207 char c = p.charAt(1);
208 if (c == '\\' || c == '/'){
217 protected Pattern getPattern(String s){
220 StringBuilder sb = new StringBuilder();
222 int len = s.length();
223 for (int j=0; j<len; j++){
224 char c = s.charAt(j);
226 case '.' : sb.append("\\."); break;
227 case '$' : sb.append("\\$"); break;
228 case '[' : sb.append("\\["); break;
229 case ']' : sb.append("\\]"); break;
230 case '*' : sb.append(".*"); break;
231 // <2do> and probably more..
232 default: sb.append(c);
236 p = Pattern.compile(sb.toString());