KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > config > types > FileSetType


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.config.types;
31
32 import com.caucho.config.ConfigException;
33 import com.caucho.log.Log;
34 import com.caucho.util.L10N;
35 import com.caucho.vfs.Path;
36 import com.caucho.vfs.Vfs;
37
38 import javax.annotation.PostConstruct;
39 import java.io.IOException JavaDoc;
40 import java.util.ArrayList JavaDoc;
41 import java.util.logging.Level JavaDoc;
42 import java.util.logging.Logger JavaDoc;
43
44 /**
45  * Abstract type building a path pattern. The pattern follows ant.
46  */

47 public class FileSetType {
48   static final L10N L = new L10N(PathPatternType.class);
49   static final Logger JavaDoc log = Log.open(PathPatternType.class);
50
51   private Path _dir;
52   private String JavaDoc _userPathPrefix = "";
53   
54   private ArrayList JavaDoc<PathPatternType> _includeList;
55   
56   private ArrayList JavaDoc<PathPatternType> _excludeList =
57     new ArrayList JavaDoc<PathPatternType>();
58
59   /**
60    * Sets the starting directory.
61    */

62   public void setDir(Path dir)
63   {
64     _dir = dir;
65   }
66
67   /**
68    * Gets the starting directory.
69    */

70   public Path getDir()
71   {
72     return _dir;
73   }
74
75   /**
76    * Adds an include pattern.
77    */

78   public void addInclude(PathPatternType pattern)
79   {
80     if (_includeList == null)
81       _includeList = new ArrayList JavaDoc<PathPatternType>();
82     
83     _includeList.add(pattern);
84   }
85
86   /**
87    * Adds an exclude pattern.
88    */

89   public void addExclude(PathPatternType pattern)
90   {
91     _excludeList.add(pattern);
92   }
93
94   /**
95    * Sets the user-path prefix for better error reporting.
96    */

97   public void setUserPathPrefix(String JavaDoc prefix)
98   {
99     if (prefix != null && ! prefix.equals("") && ! prefix.endsWith("/"))
100       _userPathPrefix = prefix + "/";
101     else
102       _userPathPrefix = prefix;
103   }
104
105   /**
106    * Initialize the type.
107    */

108   @PostConstruct
109   public void init()
110     throws ConfigException
111   {
112     if (_dir == null)
113       _dir = Vfs.lookup();
114   }
115
116   /**
117    * Returns the set of files.
118    */

119   public ArrayList JavaDoc<Path> getPaths()
120   {
121     return getPaths(new ArrayList JavaDoc<Path>());
122   }
123
124   /**
125    * Returns the set of files.
126    */

127   public ArrayList JavaDoc<Path> getPaths(ArrayList JavaDoc<Path> paths)
128   {
129     String JavaDoc dirPath = _dir.getPath();
130     
131     if (! dirPath.endsWith("/"))
132       dirPath += "/";
133
134     getPaths(paths, _dir, dirPath);
135
136     return paths;
137   }
138
139   /**
140    * Returns the set of files.
141    */

142   public void getPaths(ArrayList JavaDoc<Path> paths, Path path, String JavaDoc prefix)
143   {
144     if (path.isDirectory()) {
145       try {
146     String JavaDoc []list = path.list();
147
148     for (int i = 0; i < list.length; i++)
149       getPaths(paths, path.lookup(list[i]), prefix);
150       } catch (IOException JavaDoc e) {
151     log.log(Level.WARNING, e.toString(), e);
152       }
153     }
154     else if (path.canRead()) {
155       if (isMatch(path, prefix)) {
156     String JavaDoc suffix = "";
157     String JavaDoc fullPath = path.getPath();
158
159     if (prefix.length() < fullPath.length())
160       suffix = path.getPath().substring(prefix.length());
161
162     path.setUserPath(_userPathPrefix + suffix);
163
164     paths.add(path);
165       }
166     }
167   }
168
169   /**
170    * Returns the set of files.
171    */

172   public boolean isMatch(Path path, String JavaDoc prefix)
173   {
174     String JavaDoc suffix = "";
175     String JavaDoc fullPath = path.getPath();
176
177     if (prefix.length() < fullPath.length())
178       suffix = path.getPath().substring(prefix.length());
179
180     for (int i = 0; i < _excludeList.size(); i++) {
181       PathPatternType pattern = _excludeList.get(i);
182
183       if (pattern.isMatch(suffix))
184     return false;
185     }
186
187     if (_includeList == null)
188       return true;
189     
190     for (int i = 0; i < _includeList.size(); i++) {
191       PathPatternType pattern = _includeList.get(i);
192
193       if (pattern.isMatch(suffix))
194     return true;
195     }
196
197     return false;
198   }
199 }
200
Popular Tags