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