001/*
002 * Copyright 2012 Atteo.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.atteo.evo.filtering;
017
018/**
019 * Simple property resolver which provides the value for some name.
020 */
021public abstract class SimplePropertyResolver implements PropertyResolver {
022    protected boolean filterResult = true;
023
024    /**
025     * If true, will filter the value returned by {@link #getProperty(String)}.
026     */
027    public void setFilterResult(boolean filterResult) {
028        this.filterResult = filterResult;
029    }
030
031    @Override
032    public String resolveProperty(String name, PropertyResolver resolver) throws PropertyNotFoundException {
033        name = Filtering.filter(name, resolver);
034        String value = getProperty(name);
035        if (value == null) {
036            return null;
037        }
038        if (filterResult) {
039            return Filtering.filter(value, resolver);
040        } else {
041            return value;
042        }
043    }
044
045    /**
046     * Returns property value which will be optionally filtered for any properties.
047     * Otherwise should behave the same as {@link PropertyResolver#resolveProperty(String, PropertyResolver)}.
048     */
049    public abstract String getProperty(String name) throws PropertyNotFoundException;
050}