View Javadoc
1   /**
2    * Copyright 2010-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.common.util.metainf.model;
17  
18  import java.io.File;
19  import java.util.Collections;
20  import java.util.Comparator;
21  import java.util.List;
22  
23  import org.kuali.common.util.Assert;
24  import org.springframework.util.ResourceUtils;
25  
26  import com.google.common.base.Optional;
27  import com.google.common.collect.ImmutableList;
28  
29  public final class MetaInfContext {
30  
31  	private final File outputFile;
32  	private final File scanDir;
33  	private final String encoding;
34  	private final boolean sort;
35  	private final boolean includePropertiesFile;
36  	private final Optional<Comparator<MetaInfResource>> comparator;
37  	private final boolean includeFileSizes;
38  	private final boolean includeLineCounts;
39  	private final List<String> includes;
40  	private final List<String> excludes;
41  	private final boolean relativePaths;
42  	private final File relativeDir;
43  	private final String urlPrefix;
44  
45  	public File getOutputFile() {
46  		return outputFile;
47  	}
48  
49  	public File getScanDir() {
50  		return scanDir;
51  	}
52  
53  	public String getEncoding() {
54  		return encoding;
55  	}
56  
57  	public boolean isSort() {
58  		return sort;
59  	}
60  
61  	public boolean isIncludePropertiesFile() {
62  		return includePropertiesFile;
63  	}
64  
65  	public boolean isIncludeFileSizes() {
66  		return includeFileSizes;
67  	}
68  
69  	public boolean isIncludeLineCounts() {
70  		return includeLineCounts;
71  	}
72  
73  	public List<String> getIncludes() {
74  		return includes;
75  	}
76  
77  	public List<String> getExcludes() {
78  		return excludes;
79  	}
80  
81  	public boolean isRelativePaths() {
82  		return relativePaths;
83  	}
84  
85  	public File getRelativeDir() {
86  		return relativeDir;
87  	}
88  
89  	public String getUrlPrefix() {
90  		return urlPrefix;
91  	}
92  
93  	public Optional<Comparator<MetaInfResource>> getComparator() {
94  		return comparator;
95  	}
96  
97  	public static Builder builder(File outputFile, String encoding, File scanDir) {
98  		return new Builder(outputFile, encoding, scanDir);
99  	}
100 
101 	public static class Builder {
102 
103 		// Required
104 		private final File outputFile;
105 		private final File scanDir;
106 		private final String encoding; // Encoding is required both for reading line counts and for creating the .resources file
107 
108 		// Optional
109 		private boolean sort = true;
110 		private boolean includePropertiesFile = false;
111 		private boolean includeFileSizes = true;
112 		private boolean includeLineCounts = false; // Make them explicitly set this to true, since it can be quite expensive
113 		private List<String> includes = Collections.emptyList();
114 		private List<String> excludes = Collections.emptyList();
115 		private boolean relativePaths = true;
116 		private String urlPrefix = ResourceUtils.CLASSPATH_URL_PREFIX;
117 		private Optional<Comparator<MetaInfResource>> comparator = Optional.absent();
118 
119 		// Defaults to scanDir
120 		private File relativeDir;
121 
122 		public Builder(File outputFile, String encoding, File scanDir) {
123 			this.outputFile = outputFile;
124 			this.encoding = encoding;
125 			this.scanDir = scanDir;
126 			this.relativeDir = scanDir; // Paths are generated relative to the scanDir by default
127 		}
128 
129 		public Builder comparator(Comparator<MetaInfResource> comparator) {
130 			this.comparator = Optional.of(comparator);
131 			return this;
132 		}
133 
134 		public Builder sort(boolean sort) {
135 			this.sort = sort;
136 			return this;
137 		}
138 
139 		public Builder includes(List<String> includes) {
140 			this.includes = includes;
141 			return this;
142 		}
143 
144 		public Builder excludes(List<String> excludes) {
145 			this.excludes = excludes;
146 			return this;
147 		}
148 
149 		public Builder relativePaths(boolean relativePaths) {
150 			this.relativePaths = relativePaths;
151 			return this;
152 		}
153 
154 		public Builder includePropertiesFile(boolean includePropertiesFile) {
155 			this.includePropertiesFile = includePropertiesFile;
156 			return this;
157 		}
158 
159 		public Builder includeFileSizes(boolean includeFileSizes) {
160 			this.includeFileSizes = includeFileSizes;
161 			return this;
162 		}
163 
164 		public Builder includeLineCounts(boolean includeLineCounts) {
165 			this.includeLineCounts = includeLineCounts;
166 			return this;
167 		}
168 
169 		public MetaInfContext build() {
170 			Assert.noNulls(outputFile, scanDir, includes, excludes, relativeDir, comparator);
171 			Assert.noBlanks(encoding, urlPrefix);
172 			this.includes = ImmutableList.copyOf(includes);
173 			this.excludes = ImmutableList.copyOf(excludes);
174 			return new MetaInfContext(this);
175 		}
176 
177 	}
178 
179 	private MetaInfContext(Builder builder) {
180 		this.sort = builder.sort;
181 		this.encoding = builder.encoding;
182 		this.urlPrefix = builder.urlPrefix;
183 		this.includeFileSizes = builder.includeFileSizes;
184 		this.includeLineCounts = builder.includeLineCounts;
185 		this.includePropertiesFile = builder.includePropertiesFile;
186 		this.includes = builder.includes;
187 		this.excludes = builder.excludes;
188 		this.scanDir = builder.scanDir;
189 		this.relativeDir = builder.relativeDir;
190 		this.outputFile = builder.outputFile;
191 		this.relativePaths = builder.relativePaths;
192 		this.comparator = builder.comparator;
193 	}
194 
195 }