View Javadoc
1   /**
2    * Copyright 2010-2014 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.property.processor;
17  
18  import java.util.Collections;
19  import java.util.List;
20  import java.util.Properties;
21  
22  import org.kuali.common.util.Mode;
23  import org.kuali.common.util.PropertyUtils;
24  import org.kuali.common.util.Version;
25  import org.kuali.common.util.VersionUtils;
26  import org.kuali.common.util.property.Constants;
27  
28  public class VersionProcessor implements PropertyProcessor {
29  
30  	public static final String SANITIZED_QUALIFIER = Constants.DEFAULT_QUALIFIER_VERSION_SUFFIX + ".sanitized";
31  	public static final String SANITIZED_VERSION_KEY = "project.version.sanitized";
32  
33  	String majorSuffix = Constants.DEFAULT_MAJOR_VERSION_SUFFIX;
34  	String minorSuffix = Constants.DEFAULT_MINOR_VERSION_SUFFIX;
35  	String incrementalSuffix = Constants.DEFAULT_INCREMENTAL_VERSION_SUFFIX;
36  	String qualifierSuffix = Constants.DEFAULT_QUALIFIER_VERSION_SUFFIX;
37  	String sanitizedQualifierSuffix = SANITIZED_QUALIFIER;
38  	String trimmedSuffix = Constants.DEFAULT_TRIMMED_VERSION_SUFFIX;
39  	String snapshotSuffix = Constants.DEFAULT_SNAPSHOT_VERSION_SUFFIX;
40  	boolean alwaysAddOrOverride;
41  
42  	List<String> includes;
43  	List<String> excludes;
44  	Mode propertyOverwriteMode = Constants.DEFAULT_PROPERTY_OVERWRITE_MODE;
45  
46  	public VersionProcessor() {
47  		this(Collections.<String> emptyList());
48  	}
49  
50  	public VersionProcessor(String include) {
51  		this(Collections.singletonList(include));
52  	}
53  
54  	public VersionProcessor(List<String> includes) {
55  		this(includes, false);
56  	}
57  
58  	public VersionProcessor(List<String> includes, boolean alwaysAddOrOverride) {
59  		this.includes = includes;
60  		this.alwaysAddOrOverride = alwaysAddOrOverride;
61  	}
62  
63  	@Override
64  	public void process(Properties properties) {
65  		List<String> keys = PropertyUtils.getSortedKeys(properties, includes, excludes);
66  		Properties versionProperties = new Properties();
67  		for (String key : keys) {
68  			String version = properties.getProperty(key);
69  			Version v = VersionUtils.getVersion(version);
70  			versionProperties.putAll(getVersionProperties(key, v));
71  		}
72  		List<String> versionKeys = PropertyUtils.getSortedKeys(versionProperties);
73  		for (String versionKey : versionKeys) {
74  			String versionValue = versionProperties.getProperty(versionKey);
75  			if (alwaysAddOrOverride) {
76  				properties.setProperty(versionKey, versionValue);
77  			} else {
78  				PropertyUtils.addOrOverrideProperty(properties, versionKey, versionValue, propertyOverwriteMode);
79  			}
80  		}
81  	}
82  
83  	public Properties getVersionProperties(String key, Version v) {
84  		Properties properties = new Properties();
85  		if (v.getMajor() != null) {
86  			String newKey = key + "." + majorSuffix;
87  			properties.setProperty(newKey, v.getMajor());
88  		}
89  		if (v.getMinor() != null) {
90  			String newKey = key + "." + minorSuffix;
91  			properties.setProperty(newKey, v.getMinor());
92  		}
93  		if (v.getIncremental() != null) {
94  			String newKey = key + "." + incrementalSuffix;
95  			properties.setProperty(newKey, v.getIncremental());
96  		}
97  		if (v.getQualifier() != null) {
98  			String newKey = key + "." + qualifierSuffix;
99  			properties.setProperty(newKey, v.getQualifier());
100 			String sanitizedKey = key + "." + sanitizedQualifierSuffix;
101 			String sanitizedValue = VersionUtils.getSanitizedQualifier(v.getQualifier());
102 			properties.setProperty(sanitizedKey, sanitizedValue);
103 		}
104 		if (v.getTrimmed() != null) {
105 			String newKey = key + "." + trimmedSuffix;
106 			properties.setProperty(newKey, v.getTrimmed());
107 		}
108 		String newKey = key + "." + snapshotSuffix;
109 		properties.setProperty(newKey, Boolean.toString(v.isSnapshot()));
110 		properties.setProperty(SANITIZED_VERSION_KEY, VersionUtils.asSanitizedString(v));
111 		return properties;
112 	}
113 
114 	public String getMajorSuffix() {
115 		return majorSuffix;
116 	}
117 
118 	public void setMajorSuffix(String majorSuffix) {
119 		this.majorSuffix = majorSuffix;
120 	}
121 
122 	public String getMinorSuffix() {
123 		return minorSuffix;
124 	}
125 
126 	public void setMinorSuffix(String minorSuffix) {
127 		this.minorSuffix = minorSuffix;
128 	}
129 
130 	public String getIncrementalSuffix() {
131 		return incrementalSuffix;
132 	}
133 
134 	public void setIncrementalSuffix(String incrementalSuffix) {
135 		this.incrementalSuffix = incrementalSuffix;
136 	}
137 
138 	public String getQualifierSuffix() {
139 		return qualifierSuffix;
140 	}
141 
142 	public void setQualifierSuffix(String qualifierSuffix) {
143 		this.qualifierSuffix = qualifierSuffix;
144 	}
145 
146 	public String getTrimmedSuffix() {
147 		return trimmedSuffix;
148 	}
149 
150 	public void setTrimmedSuffix(String trimmedSuffix) {
151 		this.trimmedSuffix = trimmedSuffix;
152 	}
153 
154 	public String getSnapshotSuffix() {
155 		return snapshotSuffix;
156 	}
157 
158 	public void setSnapshotSuffix(String snapshotSuffix) {
159 		this.snapshotSuffix = snapshotSuffix;
160 	}
161 
162 	public List<String> getIncludes() {
163 		return includes;
164 	}
165 
166 	public void setIncludes(List<String> includes) {
167 		this.includes = includes;
168 	}
169 
170 	public List<String> getExcludes() {
171 		return excludes;
172 	}
173 
174 	public void setExcludes(List<String> excludes) {
175 		this.excludes = excludes;
176 	}
177 
178 	public Mode getPropertyOverwriteMode() {
179 		return propertyOverwriteMode;
180 	}
181 
182 	public void setPropertyOverwriteMode(Mode propertyOverwriteMode) {
183 		this.propertyOverwriteMode = propertyOverwriteMode;
184 	}
185 
186 	public boolean isAlwaysAddOrOverride() {
187 		return alwaysAddOrOverride;
188 	}
189 
190 	public void setAlwaysAddOrOverride(boolean alwaysAddOrOverride) {
191 		this.alwaysAddOrOverride = alwaysAddOrOverride;
192 	}
193 
194 	public String getSanitizedQualifierSuffix() {
195 		return sanitizedQualifierSuffix;
196 	}
197 
198 	public void setSanitizedQualifierSuffix(String sanitizedQualifierSuffix) {
199 		this.sanitizedQualifierSuffix = sanitizedQualifierSuffix;
200 	}
201 
202 }