001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.betwixt.versioning;
018:
019: import org.apache.commons.betwixt.AttributeDescriptor;
020: import org.apache.commons.betwixt.ElementDescriptor;
021: import org.apache.commons.betwixt.Options;
022: import org.apache.commons.betwixt.strategy.AttributeSuppressionStrategy;
023: import org.apache.commons.betwixt.strategy.ElementSuppressionStrategy;
024: import org.apache.commons.logging.Log;
025: import org.apache.commons.logging.LogFactory;
026:
027: public class VersioningStrategy implements ElementSuppressionStrategy,
028: AttributeSuppressionStrategy {
029: public static Log log = LogFactory.getLog(VersioningStrategy.class);
030:
031: public final static String VERSION_FROM = "version-from";
032:
033: public final static String VERSION_UNTIL = "version-until";
034:
035: private String version;
036:
037: public String getVersion() {
038: return version;
039: }
040:
041: public void setVersion(String version) {
042: this .version = version;
043: }
044:
045: public boolean suppress(ElementDescriptor descr) {
046: log.info("Checking element " + descr.getLocalName() + " ("
047: + descr + ")");
048:
049: if (false == checkVersionFrom(descr.getOptions())) {
050: log.info("Suppressing element (invalid version/from)");
051: return true;
052: }
053:
054: if (false == checkVersionUntil(descr.getOptions())) {
055: log.info("Suppressing element (invalid version/until)");
056: return true;
057: }
058:
059: log.info("Showing element");
060: return false;
061: }
062:
063: public boolean suppress(final AttributeDescriptor descr) {
064: log.info("Checking attribute " + descr.getLocalName() + " ("
065: + descr + ")");
066:
067: if (false == checkVersionFrom(descr.getOptions())) {
068: log.info("Suppressing attribute (invalid version/from)");
069: return true;
070: }
071:
072: if (false == checkVersionUntil(descr.getOptions())) {
073: log.info("Suppressing attribute (invalid version/until)");
074: return true;
075: }
076:
077: log.info("Showing attribute");
078: return false;
079: }
080:
081: private boolean checkVersionFrom(final Options options) {
082: log.info("Checking version/from");
083:
084: if (options == null) {
085: log.info("No options");
086: return true;
087: }
088:
089: final String value = options.getValue(VERSION_FROM);
090:
091: log.info("value=" + value);
092: log.info("version=" + version);
093: debugOptions(options);
094:
095: if (value == null || value.trim().length() == 0) {
096: log.info("No attribute \"Version from\"");
097: return true;
098: }
099:
100: final boolean versionOk = value.compareTo(version) <= 0;
101: log.info("versionOk=" + versionOk);
102:
103: return versionOk;
104: }
105:
106: private boolean checkVersionUntil(final Options options) {
107: log.info("Checking version/until");
108:
109: if (options == null) {
110: log.info("No options");
111: return true;
112: }
113:
114: final String value = options.getValue(VERSION_UNTIL);
115:
116: log.info("value=" + value);
117: log.info("version=" + version);
118: debugOptions(options);
119:
120: if (value == null || value.trim().length() == 0) {
121: log.info("No attribute \"Version until\"");
122: return true;
123: }
124:
125: final boolean versionOk = value.compareTo(version) >= 0;
126: log.info("versionOk=" + versionOk);
127:
128: return versionOk;
129: }
130:
131: public VersioningStrategy() {
132: super ();
133: }
134:
135: public VersioningStrategy(final String version) {
136: super ();
137: setVersion(version);
138: }
139:
140: private final void debugOptions(final Options options) {
141: final String[] names = options.getNames();
142:
143: log.info("Names:");
144:
145: for (int ii = 0; ii < names.length; ii++) {
146: final String name = names[ii];
147:
148: log.info(" " + ii + ": " + name + "="
149: + options.getValue(name));
150: }
151: }
152: }
|