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.strategy;
018
019 import org.apache.commons.betwixt.AttributeDescriptor;
020 import org.apache.commons.betwixt.ElementDescriptor;
021
022 /**
023 * Determines whether the expression of an attribute with a values
024 * should be suppressed.
025 *
026 * @since 0.7
027 * @author <a href='http://commons.apache.org'>Apache Commons Team</a>, <a href='http://www.apache.org'>Apache Software Foundation</a>
028 */
029 public abstract class ValueSuppressionStrategy {
030
031 /**
032 * Strategy allows all values to be expressed for all attributes
033 */
034 public static final ValueSuppressionStrategy ALLOW_ALL_VALUES = new ValueSuppressionStrategy() {
035 public boolean suppressAttribute(AttributeDescriptor attributeDescriptor, String value) {
036 return false;
037 }
038 };
039
040 /**
041 * Suppresses all null values.
042 */
043 public static final ValueSuppressionStrategy SUPPRESS_EMPTY = new ValueSuppressionStrategy() {
044 public boolean suppressAttribute(AttributeDescriptor attributeDescriptor, String value) {
045 return "".equals(value);
046 }
047 };
048
049 /**
050 * Default strategy is {@link #SUPPRESS_EMPTY}.
051 */
052 public static final ValueSuppressionStrategy DEFAULT = SUPPRESS_EMPTY;
053
054
055 /**
056 * Should the given attribute value be suppressed?
057 * @param attributeDescriptor <code>AttributeDescriptor</code> describing the attribute, not null
058 * @param value <code>Object</code> value, possibly null
059 * @return true if the attribute should not be written for the given value
060 */
061 public abstract boolean suppressAttribute(AttributeDescriptor attributeDescriptor, String value);
062
063 /**
064 * <p>
065 * Should be given element value be suppressed?
066 * </p><p>
067 * <strong>Note:</strong> to preserve binary compatibility,
068 * this method contains an implementation that returns false.
069 * Subclasses should not rely upon this behaviour as (in future)
070 * this may be made abstract.
071 * </p>
072 * @param element <code>ElementDescriptor</code> describing the element, not null
073 * @param namespaceUri the namespace of the element to be written
074 * @param localName the local name of the element to be written
075 * @param qualifiedName the qualified name of the element to be written
076 * @param value <code>Object</code> value, possibly null
077 * @return true if the element should be suppressed (in other words, not written)
078 * for the given value
079 * @since 0.8
080 */
081 public boolean suppressElement(ElementDescriptor element, String namespaceUri, String localName, String qualifiedName, Object value) {
082 return false;
083 }
084 }