001/*
002 *   Copyright (C) 2005 Christian Schulte <cs@schulte.it>
003 *   All rights reserved.
004 *
005 *   Redistribution and use in source and binary forms, with or without
006 *   modification, are permitted provided that the following conditions
007 *   are met:
008 *
009 *     o Redistributions of source code must retain the above copyright
010 *       notice, this list of conditions and the following disclaimer.
011 *
012 *     o Redistributions in binary form must reproduce the above copyright
013 *       notice, this list of conditions and the following disclaimer in
014 *       the documentation and/or other materials provided with the
015 *       distribution.
016 *
017 *   THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
018 *   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
019 *   AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
020 *   THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT,
021 *   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
022 *   NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
023 *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
024 *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025 *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
026 *   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027 *
028 *   $JOMC: PropertiesResourceType.java 5135 2016-04-08 13:53:07Z schulte $
029 *
030 */
031package org.jomc.mojo;
032
033import java.util.Arrays;
034import java.util.Collections;
035import java.util.List;
036
037/**
038 * Datatype describing a properties resource.
039 *
040 * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
041 * @version $JOMC: PropertiesResourceType.java 5135 2016-04-08 13:53:07Z schulte $
042 * @since 1.2
043 */
044public class PropertiesResourceType extends ResourceType
045{
046
047    /**
048     * Constant for the plain properties file format.
049     */
050    public static final String PLAIN_FORMAT = "plain";
051
052    /**
053     * Constant for the XML properties file format.
054     */
055    public static final String XML_FORMAT = "xml";
056
057    /**
058     * Supported properties file format values.
059     */
060    private static final String[] FORMAT_VALUES =
061    {
062        PLAIN_FORMAT, XML_FORMAT
063    };
064
065    /**
066     * The format of the properties resource.
067     */
068    private String format;
069
070    /**
071     * Creates a new {@code PropertiesResourceType} instance.
072     */
073    public PropertiesResourceType()
074    {
075        super();
076    }
077
078    /**
079     * Gets the value of the {@code format} property.
080     *
081     * @return The value of the {@code format} property.
082     */
083    public final String getFormat()
084    {
085        if ( this.format == null )
086        {
087            this.format = PLAIN_FORMAT;
088        }
089
090        return this.format;
091    }
092
093    /**
094     * Sets the value of the {@code format} property.
095     *
096     * @param value The new value of the {@code format} property or {@code null}.
097     */
098    public final void setFormat( final String value )
099    {
100        this.format = value;
101    }
102
103    /**
104     * Gets a list holding supported format values.
105     *
106     * @return An unmodifiable list holding supported format values.
107     *
108     * @see #isFormatSupported(java.lang.String)
109     */
110    public static List<String> getSupportedFormats()
111    {
112        return Collections.unmodifiableList( Arrays.asList( FORMAT_VALUES ) );
113    }
114
115    /**
116     * Tests a given format value.
117     *
118     * @param value The format value to test.
119     *
120     * @return {@code true}, if the given format value is supported; {@code false}, if the given format value is not
121     * supported.
122     *
123     * @see #getSupportedFormats()
124     */
125    public static boolean isFormatSupported( final String value )
126    {
127        if ( value != null )
128        {
129            for ( int i = FORMAT_VALUES.length - 1; i >= 0; i-- )
130            {
131                if ( value.equalsIgnoreCase( FORMAT_VALUES[i] ) )
132                {
133                    return true;
134                }
135            }
136        }
137
138        return false;
139    }
140
141    /**
142     * Creates and returns a copy of this object.
143     *
144     * @return A copy of this object.
145     */
146    @Override
147    public PropertiesResourceType clone()
148    {
149        return (PropertiesResourceType) super.clone();
150    }
151
152}