View Javadoc

1   /*
2    * Copyright 2013 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package de.smartics.properties.config.transfer.filesystem;
17  
18  import java.util.Locale;
19  
20  import org.apache.commons.io.FilenameUtils;
21  
22  /**
23   * An enumeration of valid properties formats.
24   */
25  public enum PropertiesFormat
26  {
27    // ***************************** Enumeration ******************************
28  
29    // ******************************** Fields ********************************
30  
31    // --- constants ----------------------------------------------------------
32  
33    /**
34     * The native properties format.
35     */
36    NATIVE("properties"),
37  
38    /**
39     * The XML format defined by the {@link java.util.Properties} implementation.
40     */
41    XML("xml");
42  
43    // --- members ------------------------------------------------------------
44  
45    /**
46     * The suffix of a file name. This is the concatenation of a dot ('
47     * <code>.</code>') with the type's specific extension.
48     */
49    private final String fileNameSuffix;
50  
51    // ***************************** Constructors *****************************
52  
53    private PropertiesFormat(final String fileNameExtension)
54    {
55      this.fileNameSuffix = '.' + fileNameExtension;
56    }
57  
58    // ******************************** Methods *******************************
59  
60    // --- init ---------------------------------------------------------------
61  
62    // --- get&set ------------------------------------------------------------
63  
64    // --- business -----------------------------------------------------------
65  
66    /**
67     * Adjusts the file name in this way that it contains the correct extension
68     * for the given type.
69     *
70     * @param path the path to adjust if needed.
71     * @return the path with an extension that reflects the type.
72     */
73    public String adjustPath(final String path)
74    {
75      if (path.endsWith(fileNameSuffix))
76      {
77        return path;
78      }
79      else
80      {
81        final String newPath =
82            FilenameUtils.removeExtension(path) + fileNameSuffix;
83        return newPath;
84      }
85    }
86  
87    /**
88     * Returns the properties format identifier by the case-insensitive identifier
89     * of the enumeration element.
90     *
91     * @param type the name of the enumeration element. The value is case
92     *          insensitive.
93     * @return the enumeration element that corresponds with the given type name.
94     */
95    public static PropertiesFormat fromString(final String type)
96    {
97      final String elementName = type.toUpperCase(Locale.ENGLISH);
98      return Enum.valueOf(PropertiesFormat.class, elementName);
99    }
100 
101   // --- object basics ------------------------------------------------------
102 
103 }