001package ball.util.ant.taskdefs;
002/*-
003 * ##########################################################################
004 * Utilities
005 * $Id: PropertySetterAntTask.html 5431 2020-02-12 19:03:17Z ball $
006 * $HeadURL: svn+ssh://svn.hcf.dev/var/spool/scm/repository.svn/hcf-dev/blog/2019-07-08-fivethirtyeight-best-scrabble-string/src/main/resources/javadoc/src-html/ball/util/ant/taskdefs/PropertySetterAntTask.html $
007 * %%
008 * Copyright (C) 2008 - 2020 Allen D. Ball
009 * %%
010 * Licensed under the Apache License, Version 2.0 (the "License");
011 * you may not use this file except in compliance with the License.
012 * You may obtain a copy of the License at
013 *
014 *      http://www.apache.org/licenses/LICENSE-2.0
015 *
016 * Unless required by applicable law or agreed to in writing, software
017 * distributed under the License is distributed on an "AS IS" BASIS,
018 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
019 * See the License for the specific language governing permissions and
020 * limitations under the License.
021 * ##########################################################################
022 */
023import org.apache.tools.ant.BuildException;
024import org.apache.tools.ant.Task;
025
026/**
027 * Interface to provide common default methods for {@link Task}s that may
028 * assign property values.
029 *
030 * @author {@link.uri mailto:ball@hcf.dev Allen D. Ball}
031 * @version $Revision: 5431 $
032 */
033public interface PropertySetterAntTask extends AntTaskMixIn {
034    String getProperty();
035    void setProperty(String property);
036
037    /**
038     * Method to get the value to assign to the property.
039     *
040     * @return  The property value.
041     *
042     * @throws  Exception       As specified by implementing subclass.
043     */
044    Object getPropertyValue() throws Exception;
045
046    default void execute() throws BuildException {
047        try {
048            Task task = (Task) this;
049            String key = getProperty();
050            Object value = getPropertyValue();
051
052            if (key != null) {
053                if (value != null) {
054                    task.getProject().setNewProperty(key, value.toString());
055                }
056            } else {
057                task.log(String.valueOf(value));
058            }
059        } catch (BuildException exception) {
060            throw exception;
061        } catch (RuntimeException exception) {
062            throw exception;
063        } catch (Exception exception) {
064            throw new BuildException(exception);
065        }
066    }
067}