001/*
002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and others.
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 *
016 * Contributors:
017 *     Anahide Tchertchian
018 */
019package org.nuxeo.ecm.web.resources.api;
020
021import org.apache.commons.lang3.StringUtils;
022
023/**
024 * @since 7.3
025 */
026public enum ResourceType {
027
028    any, unknown, css, js, bundle, html, jsfjs, jsfcss, xhtml, xhtmlfirst;
029
030    public String getSuffix() {
031        return "." + name();
032    }
033
034    /**
035     * @since 7.4
036     */
037    public final boolean equals(String type) {
038        return name().equalsIgnoreCase(type);
039    }
040
041    public final boolean matches(Resource r) {
042        if (any == this) {
043            return true;
044        }
045        if (r == null || r.getType() == null) {
046            return true;
047        }
048        return equals(r.getType());
049    }
050
051    public static final ResourceType parse(String type) {
052        for (ResourceType item : values()) {
053            if (item.equals(type)) {
054                return item;
055            }
056        }
057        return unknown;
058    }
059
060    public static final boolean matches(String type, Resource r) {
061        if (StringUtils.isBlank(type) || any.equals(type)) {
062            return true;
063        }
064        String rt = r.getType();
065        if (StringUtils.isBlank(rt)) {
066            return true;
067        }
068        return type.equalsIgnoreCase(rt);
069    }
070
071}