View Javadoc

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