001/*
002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Thomas Roger <troger@nuxeo.com>
016 */
017
018package org.nuxeo.common.utils;
019
020import java.util.regex.Pattern;
021
022/**
023 * Helper class to detect Html5 Dnd compliant browsers based on the User Agent string
024 *
025 * @author Tiry (tdelprat@nuxeo.com)
026 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a>
027 */
028public class UserAgentMatcher {
029
030    private static final Pattern UA_FIREFOX_3 = Pattern.compile("^[Mm]ozilla.*[Ff]irefox(/|\\s)?(3\\.[6789].*)");
031
032    private static final Pattern UA_FIREFOX_FROM_4 = Pattern.compile("^[Mm]ozilla.*[Ff]irefox(/|\\s)?(([456789].*)|([1-9][0123456789].*))");
033
034    private static final Pattern UA_SAFARI_FROM_5 = Pattern.compile("^Mozilla.*AppleWebKit.*Version/[5-9].*");
035
036    private static final Pattern UA_CHROME = Pattern.compile("^Mozilla.*AppleWebKit.*Chrom(e|ium)/([1-9][0123456789].([0-9.])*)(?: Safari/([0-9.])*)?");
037
038    private static final Pattern UA_MSIE_67 = Pattern.compile("^Mozilla/4.0 \\(compatible; MSIE [67].[0-9]((?!Trident).)*$");
039
040    private static final Pattern UA_MSIE_FROM_10 = Pattern.compile("^Mozilla.*[Tt]rident/[6-9]\\..*");
041
042    private static final Pattern UA_MSEDGE = Pattern.compile("^Mozilla.*Edge/.*");
043
044    private UserAgentMatcher() {
045        // Helper class
046    }
047
048    public static boolean isFirefox3(String UA) {
049        return UA_FIREFOX_3.matcher(UA).matches();
050    }
051
052    public static boolean isFirefox4OrMore(String UA) {
053        return UA_FIREFOX_FROM_4.matcher(UA).matches();
054    }
055
056    public static boolean isSafari5(String UA) {
057        return UA_SAFARI_FROM_5.matcher(UA).matches();
058    }
059
060    public static boolean isChrome(String UA) {
061        return UA_CHROME.matcher(UA).matches();
062    }
063
064    public static boolean html5DndIsSupported(String UA) {
065        return isFirefox3(UA) || isFirefox4OrMore(UA) || isSafari5(UA) || isChrome(UA) || isMSIE10OrMore(UA) || isMSEdge(UA);
066    }
067
068    public static boolean isMSIE6or7(String UA) {
069        return UA_MSIE_67.matcher(UA).matches();
070    }
071
072    /**
073     * @since 5.9.5
074     */
075    public static boolean isMSIE10OrMore(String UA) {
076        return UA_MSIE_FROM_10.matcher(UA).matches();
077    }
078
079    /**
080     * @since 7.4
081     */
082    public static boolean isMSEdge(String UA) {
083        return UA_MSEDGE.matcher(UA).matches();
084    }
085
086    public static boolean isHistoryPushStateSupported(String UA) {
087        return isFirefox4OrMore(UA) || isSafari5(UA) || isChrome(UA) || isMSIE10OrMore(UA);
088    }
089}