1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.betwixt.versioning;
18
19 import org.apache.commons.betwixt.AttributeDescriptor;
20 import org.apache.commons.betwixt.ElementDescriptor;
21 import org.apache.commons.betwixt.Options;
22 import org.apache.commons.betwixt.strategy.AttributeSuppressionStrategy;
23 import org.apache.commons.betwixt.strategy.ElementSuppressionStrategy;
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26
27 public class VersioningStrategy implements ElementSuppressionStrategy,
28 AttributeSuppressionStrategy {
29 public static Log log = LogFactory.getLog(VersioningStrategy.class);
30
31 public final static String VERSION_FROM = "version-from";
32
33 public final static String VERSION_UNTIL = "version-until";
34
35 private String version;
36
37 public String getVersion() {
38 return version;
39 }
40
41 public void setVersion(String version) {
42 this.version = version;
43 }
44
45 public boolean suppress(ElementDescriptor descr) {
46 log.info("Checking element " + descr.getLocalName() + " (" + descr + ")");
47
48 if (false == checkVersionFrom(descr.getOptions())) {
49 log.info("Suppressing element (invalid version/from)");
50 return true;
51 }
52
53 if (false == checkVersionUntil(descr.getOptions())) {
54 log.info("Suppressing element (invalid version/until)");
55 return true;
56 }
57
58 log.info("Showing element");
59 return false;
60 }
61
62 public boolean suppress(final AttributeDescriptor descr) {
63 log.info("Checking attribute " + descr.getLocalName() + " (" + descr + ")");
64
65 if (false == checkVersionFrom(descr.getOptions())) {
66 log.info("Suppressing attribute (invalid version/from)");
67 return true;
68 }
69
70 if (false == checkVersionUntil(descr.getOptions())) {
71 log.info("Suppressing attribute (invalid version/until)");
72 return true;
73 }
74
75 log.info("Showing attribute");
76 return false;
77 }
78
79 private boolean checkVersionFrom(final Options options) {
80 log.info("Checking version/from");
81
82 if (options == null) {
83 log.info("No options");
84 return true;
85 }
86
87 final String value = options.getValue(VERSION_FROM);
88
89 log.info("value=" + value);
90 log.info("version=" + version);
91 debugOptions(options);
92
93 if (value == null || value.trim().length() == 0) {
94 log.info("No attribute \"Version from\"");
95 return true;
96 }
97
98 final boolean versionOk = value.compareTo(version) <= 0;
99 log.info("versionOk=" + versionOk);
100
101 return versionOk;
102 }
103
104 private boolean checkVersionUntil(final Options options) {
105 log.info("Checking version/until");
106
107 if (options == null) {
108 log.info("No options");
109 return true;
110 }
111
112 final String value = options.getValue(VERSION_UNTIL);
113
114 log.info("value=" + value);
115 log.info("version=" + version);
116 debugOptions(options);
117
118 if (value == null || value.trim().length() == 0) {
119 log.info("No attribute \"Version until\"");
120 return true;
121 }
122
123 final boolean versionOk = value.compareTo(version) >= 0;
124 log.info("versionOk=" + versionOk);
125
126 return versionOk;
127 }
128
129 public VersioningStrategy() {
130 super();
131 }
132
133 public VersioningStrategy(final String version) {
134 super();
135 setVersion(version);
136 }
137
138 private final void debugOptions(final Options options) {
139 final String[] names = options.getNames();
140
141 log.info("Names:");
142
143 for (int ii = 0; ii < names.length; ii++) {
144 final String name = names[ii];
145
146 log.info(" " + ii + ": " + name + "=" + options.getValue(name));
147 }
148 }
149 }