001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.config.types;
031:
032: import com.caucho.config.ConfigException;
033: import com.caucho.log.Log;
034: import com.caucho.util.L10N;
035: import com.caucho.vfs.Path;
036: import com.caucho.vfs.Vfs;
037:
038: import javax.annotation.PostConstruct;
039: import java.io.IOException;
040: import java.util.ArrayList;
041: import java.util.logging.Level;
042: import java.util.logging.Logger;
043:
044: /**
045: * Abstract type building a path pattern. The pattern follows ant.
046: */
047: public class FileSetType {
048: static final L10N L = new L10N(PathPatternType.class);
049: static final Logger log = Log.open(PathPatternType.class);
050:
051: private Path _dir;
052: private String _userPathPrefix = "";
053:
054: private ArrayList<PathPatternType> _includeList;
055:
056: private ArrayList<PathPatternType> _excludeList = new ArrayList<PathPatternType>();
057:
058: /**
059: * Sets the starting directory.
060: */
061: public void setDir(Path dir) {
062: _dir = dir;
063: }
064:
065: /**
066: * Gets the starting directory.
067: */
068: public Path getDir() {
069: return _dir;
070: }
071:
072: /**
073: * Adds an include pattern.
074: */
075: public void addInclude(PathPatternType pattern) {
076: if (_includeList == null)
077: _includeList = new ArrayList<PathPatternType>();
078:
079: _includeList.add(pattern);
080: }
081:
082: /**
083: * Adds an exclude pattern.
084: */
085: public void addExclude(PathPatternType pattern) {
086: _excludeList.add(pattern);
087: }
088:
089: /**
090: * Sets the user-path prefix for better error reporting.
091: */
092: public void setUserPathPrefix(String prefix) {
093: if (prefix != null && !prefix.equals("")
094: && !prefix.endsWith("/"))
095: _userPathPrefix = prefix + "/";
096: else
097: _userPathPrefix = prefix;
098: }
099:
100: /**
101: * Initialize the type.
102: */
103: @PostConstruct
104: public void init() throws ConfigException {
105: if (_dir == null)
106: _dir = Vfs.lookup();
107: }
108:
109: /**
110: * Returns the set of files.
111: */
112: public ArrayList<Path> getPaths() {
113: return getPaths(new ArrayList<Path>());
114: }
115:
116: /**
117: * Returns the set of files.
118: */
119: public ArrayList<Path> getPaths(ArrayList<Path> paths) {
120: String dirPath = _dir.getPath();
121:
122: if (!dirPath.endsWith("/"))
123: dirPath += "/";
124:
125: getPaths(paths, _dir, dirPath);
126:
127: return paths;
128: }
129:
130: /**
131: * Returns the set of files.
132: */
133: public void getPaths(ArrayList<Path> paths, Path path, String prefix) {
134: if (path.isDirectory()) {
135: try {
136: String[] list = path.list();
137:
138: for (int i = 0; i < list.length; i++)
139: getPaths(paths, path.lookup(list[i]), prefix);
140: } catch (IOException e) {
141: log.log(Level.WARNING, e.toString(), e);
142: }
143: } else if (path.canRead()) {
144: if (isMatch(path, prefix)) {
145: String suffix = "";
146: String fullPath = path.getPath();
147:
148: if (prefix.length() < fullPath.length())
149: suffix = path.getPath().substring(prefix.length());
150:
151: path.setUserPath(_userPathPrefix + suffix);
152:
153: paths.add(path);
154: }
155: }
156: }
157:
158: /**
159: * Returns the set of files.
160: */
161: public boolean isMatch(Path path, String prefix) {
162: String suffix = "";
163: String fullPath = path.getPath();
164:
165: if (prefix.length() < fullPath.length())
166: suffix = path.getPath().substring(prefix.length());
167:
168: for (int i = 0; i < _excludeList.size(); i++) {
169: PathPatternType pattern = _excludeList.get(i);
170:
171: if (pattern.isMatch(suffix))
172: return false;
173: }
174:
175: if (_includeList == null)
176: return true;
177:
178: for (int i = 0; i < _includeList.size(); i++) {
179: PathPatternType pattern = _includeList.get(i);
180:
181: if (pattern.isMatch(suffix))
182: return true;
183: }
184:
185: return false;
186: }
187:
188: public String toString() {
189: return "FileSetType[]";
190: }
191: }
|