001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.loader;
029
030import java.util.Locale;
031
032/**
033 * Describes a MIME type configured in OpenCms.<p>
034 *
035 * @since 7.0.0
036 */
037public class CmsMimeType implements Comparable<CmsMimeType> {
038
039    /** Indicates if this a MIME type read from the OpenCms configuration. */
040    private boolean m_configured;
041
042    /** The MIME type file extension. */
043    private String m_extension;
044
045    /** The MIME type description. */
046    private String m_type;
047
048    /**
049     * Default constructor for MIME types.<p>
050     *
051     * If the extension does not start with a dot '.', then a dot is automatically added
052     * as a prefix.<p>
053     *
054     * @param extension the MIME type extension
055     * @param type the MIME type description
056     */
057    public CmsMimeType(String extension, String type) {
058
059        this(extension, type, true);
060    }
061
062    /**
063     * Special constructor for "marked" MIME types.<p>
064     *
065     * If the extension does not start with a dot '.', then a dot is automatically added
066     * as a prefix.<p>
067     *
068     * @param extension the MIME type extension
069     * @param type the MIME type description
070     * @param configured indicates if this a MIME type read from the OpenCms configuration
071     */
072    public CmsMimeType(String extension, String type, boolean configured) {
073
074        m_extension = String.valueOf(extension).toLowerCase(Locale.ENGLISH);
075        if (!(m_extension.charAt(0) == '.')) {
076            m_extension = "." + m_extension;
077        }
078        m_type = String.valueOf(type).toLowerCase(Locale.ENGLISH);
079        m_configured = configured;
080    }
081
082    /**
083     * MIME-types are compared according to the type first, and to the extension second.<p>
084     *
085     * @see java.lang.Comparable#compareTo(java.lang.Object)
086     */
087    public int compareTo(CmsMimeType obj) {
088
089        if (obj == this) {
090            return 0;
091        }
092        int result = m_type.compareTo(obj.m_type);
093        if (result == 0) {
094            result = m_extension.compareTo(obj.m_extension);
095        }
096        return result;
097    }
098
099    /**
100     * MIME-types are equal is the extension is equal.<p>
101     *
102     * @see java.lang.Object#equals(java.lang.Object)
103     */
104    @Override
105    public boolean equals(Object obj) {
106
107        if (obj == this) {
108            return true;
109        }
110        if (obj instanceof CmsMimeType) {
111            return ((CmsMimeType)obj).m_extension.equals(m_extension);
112        }
113        return false;
114    }
115
116    /**
117     * Returns the MIME type file extension.<p>
118     *
119     * @return the MIME type file extension
120     */
121    public String getExtension() {
122
123        return m_extension;
124    }
125
126    /**
127     * Returns the MIME type description.<p>
128     *
129     * @return the MIME type description
130     */
131    public String getType() {
132
133        return m_type;
134    }
135
136    /**
137     * The hash code of MIME types is build only from the extension.<p>
138     *
139     * @see java.lang.Object#hashCode()
140     */
141    @Override
142    public int hashCode() {
143
144        return m_extension.hashCode();
145    }
146
147    /**
148     * Returns <code>true</code> if this MIME type has been read from the OpenCms configuration.<p>
149     *
150     * @return <code>true</code> if this MIME type has been read from the OpenCms configuration
151     */
152    public boolean isConfigured() {
153
154        return m_configured;
155    }
156}