001 /*
002 GRANITE DATA SERVICES
003 Copyright (C) 2007-2010 ADEQUATE SYSTEMS SARL
004
005 This file is part of Granite Data Services.
006
007 Granite Data Services is free software; you can redistribute it and/or modify
008 it under the terms of the GNU Library General Public License as published by
009 the Free Software Foundation; either version 2 of the License, or (at your
010 option) any later version.
011
012 Granite Data Services is distributed in the hope that it will be useful, but
013 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
015 for more details.
016
017 You should have received a copy of the GNU Library General Public License
018 along with this library; if not, see <http://www.gnu.org/licenses/>.
019 */
020
021 package org.granite.builder.properties;
022
023 import java.util.ArrayList;
024 import java.util.List;
025 import java.util.regex.Pattern;
026
027 import org.granite.builder.util.StringUtil;
028
029 import com.thoughtworks.xstream.annotations.XStreamAlias;
030 import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
031
032 /**
033 * @author Franck WOLFF
034 */
035 @XStreamAlias(value="source")
036 public class Gas3Source implements Validable, Comparable<Gas3Source> {
037
038 @XStreamAsAttribute
039 private String path;
040
041 @XStreamAsAttribute
042 private String includes;
043
044 @XStreamAsAttribute
045 private String excludes;
046
047 @XStreamAsAttribute
048 private String output;
049
050 private transient List<Pattern> includePatterns;
051 private transient List<Pattern> excludePatterns;
052
053 public Gas3Source(String path, String includes, String excludes, String output) {
054 this.path = path;
055 setIncludes(includes);
056 setExcludes(excludes);
057 this.output = output;
058 }
059
060 public boolean match(String path, String file) {
061 if (!this.path.equals(path))
062 return false;
063
064 compilePatterns();
065
066 for (Pattern pattern : excludePatterns) {
067 if (pattern.matcher(file).matches())
068 return false;
069 }
070
071 for (Pattern pattern : includePatterns) {
072 if (pattern.matcher(file).matches())
073 return true;
074 }
075
076 return includePatterns.isEmpty();
077 }
078
079 public String getPath() {
080 return path;
081 }
082 public void setPath(String path) {
083 this.path = path;
084 }
085
086 public String getIncludes() {
087 return includes;
088 }
089 public void setIncludes(String includes) {
090 this.includes = includes;
091 this.includePatterns = null;
092 }
093
094 public String getExcludes() {
095 return excludes;
096 }
097 public void setExcludes(String excludes) {
098 this.excludes = excludes;
099 this.excludePatterns = null;
100 }
101
102 public String getOutput() {
103 return output;
104 }
105 public void setOutput(String output) {
106 this.output = output;
107 }
108
109 public String getOutputDir() {
110 if (output == null)
111 return "";
112 String[] dirs = StringUtil.split(output, ';');
113 if (dirs.length > 0)
114 return dirs[0];
115 return "";
116 }
117
118 public String getBaseOutputDir() {
119 return getBaseOutputDir(false);
120 }
121
122 public String getBaseOutputDir(boolean fallback) {
123 if (output == null)
124 return "";
125 String[] dirs = StringUtil.split(output, ';');
126 if (dirs.length > 1 && dirs[1].length() > 0)
127 return dirs[1];
128 return (fallback && dirs.length > 0 ? dirs[0] : "");
129 }
130
131 public void validate(ValidationResults results) {
132 if (path == null || output == null)
133 results.getErrors().add("source: path and output cannot be null");
134 }
135
136 @Override
137 public boolean equals(Object obj) {
138 if (obj == this)
139 return true;
140 if (!(obj instanceof Gas3Source))
141 return false;
142 Gas3Source g3s = (Gas3Source)obj;
143 return (path == null ? g3s.path == null : path.equals(g3s.path));
144 }
145
146 @Override
147 public int hashCode() {
148 return path != null ? path.hashCode() : 0;
149 }
150
151 public int compareTo(Gas3Source o) {
152 if (path == null)
153 return -1;
154 if (o.path == null)
155 return 1;
156 return path.compareTo(o.path);
157 }
158
159 private void compilePatterns() {
160 if (includePatterns == null) {
161 if (includePatterns == null)
162 includePatterns = new ArrayList<Pattern>();
163 else
164 includePatterns.clear();
165 if (includes != null) {
166 for (String include : StringUtil.split(includes, ';')) {
167 if (include.length() > 0)
168 includePatterns.add(Pattern.compile(StringUtil.regexifyPathPattern(include)));
169 }
170 }
171 }
172
173 if (excludePatterns == null) {
174 if (excludePatterns == null)
175 excludePatterns = new ArrayList<Pattern>();
176 else
177 excludePatterns.clear();
178 if (excludes != null) {
179 for (String exclude : StringUtil.split(excludes, ';')) {
180 if (exclude.length() > 0)
181 excludePatterns.add(Pattern.compile(StringUtil.regexifyPathPattern(exclude)));
182 }
183 }
184 }
185 }
186 }