<commit_before>/****************************************************************************
 * Copyright (C) 2018 ecsec GmbH.
 * All rights reserved.
 * Contact: ecsec GmbH (<EMAIL>)
 *
 * This file may be used in accordance with the terms and conditions
 * contained in a signed written agreement between you and ecsec GmbH.
 *
 ***************************************************************************/

package org.openecard.gui.swing.common;

import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import org.openecard.common.util.SysUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 *
 * @author <NAME>
 */
public class SwingUtils {

    private static final Logger LOG = LoggerFactory.getLogger(SwingUtils.class);

    /**
     * Opens a URL in the OS configured default program.
     * Currently only file and http/https and mailto URLs are tested.
     * The URL may contain system properties which get expanded when the expand flag is set.
     *
     * @param uri URL to open.
     * @param expandSysProps If {@code true} expand system properties in the URL, if {@code false} just use the URL as
     *   it is.
     */
    public static void openUrl(URI uri, boolean expandSysProps) {
	try {
	    if (expandSysProps) {
		String urlStr = uri.toString();
		urlStr = SysUtils.expandSysProps(urlStr);
		uri = new URI(urlStr);
	    }

	    boolean browserOpened = false;
	    if (Desktop.isDesktopSupported()) {
		if ("file".equals(uri.getScheme()) && Desktop.getDesktop().isSupported(Desktop.Action.OPEN)) {
		    try {
			Desktop.getDesktop().open(new File(uri));
			browserOpened = true;
		    } catch (IOException ex) {
			// failed to open browser
			LOG.debug(ex.getMessage(), ex);
		    }
		} else if (Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) {
		    try {
			Desktop.getDesktop().browse(uri);
			browserOpened = true;
		    } catch (IOException ex) {
			// failed to open browser
			LOG.debug(ex.getMessage(), ex);
		    }
		}
	    }
	    if (! browserOpened) {
		String openTool;
		if (SysUtils.isUnix()) {
		    openTool = "xdg-open";
		} else if (SysUtils.isWin()) {
		    openTool = "start";
		} else {
		    openTool = "open";
		}
		ProcessBuilder pb = new ProcessBuilder(openTool, uri.toString());
		try {
		    pb.start();
		} catch (IOException ex) {
		    // failed to execute command
		    LOG.debug(ex.getMessage(), ex);
		}
	    }
	} catch (URISyntaxException ex) {
	    // wrong syntax
	    LOG.debug(ex.getMessage(), ex);
	}
    }

}
<commit_msg>fix: don`t use Java Swing Desktop browse under unix anymore, as it fails under ubuntu; fall back to xdg-open<commit_after>/****************************************************************************
 * Copyright (C) 2018 ecsec GmbH.
 * All rights reserved.
 * Contact: ecsec GmbH (<EMAIL>)
 *
 * This file may be used in accordance with the terms and conditions
 * contained in a signed written agreement between you and ecsec GmbH.
 *
 ***************************************************************************/

package org.openecard.gui.swing.common;

import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import org.openecard.common.util.SysUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 *
 * @author <NAME>
 */
public class SwingUtils {

    private static final Logger LOG = LoggerFactory.getLogger(SwingUtils.class);

    /**
     * Opens a URL in the OS configured default program.
     * Currently only file and http/https and mailto URLs are tested.
     * The URL may contain system properties which get expanded when the expand flag is set.
     *
     * @param uri URL to open.
     * @param expandSysProps If {@code true} expand system properties in the URL, if {@code false} just use the URL as
     *   it is.
     */
    public static void openUrl(URI uri, boolean expandSysProps) {
	try {
	    if (expandSysProps) {
		String urlStr = uri.toString();
		urlStr = SysUtils.expandSysProps(urlStr);
		uri = new URI(urlStr);
	    }

	    boolean browserOpened = false;
	    if (Desktop.isDesktopSupported() && !SysUtils.isUnix()) {
		if ("file".equals(uri.getScheme()) && Desktop.getDesktop().isSupported(Desktop.Action.OPEN)) {
		    try {
			Desktop.getDesktop().open(new File(uri));
			browserOpened = true;
		    } catch (IOException ex) {
			// failed to open browser
			LOG.debug(ex.getMessage(), ex);
		    }
		} else if (Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) {
		    try {
			Desktop.getDesktop().browse(uri);
			browserOpened = true;
		    } catch (IOException ex) {
			// failed to open browser
			LOG.debug(ex.getMessage(), ex);
		    }
		}
	    }
	    if (! browserOpened) {
		String openTool;
		if (SysUtils.isUnix()) {
		    openTool = "xdg-open";
		} else if (SysUtils.isWin()) {
		    openTool = "start";
		} else {
		    openTool = "open";
		}
		ProcessBuilder pb = new ProcessBuilder(openTool, uri.toString());
		try {
		    pb.start();
		} catch (IOException ex) {
		    // failed to execute command
		    LOG.debug(ex.getMessage(), ex);
		}
	    }
	} catch (URISyntaxException ex) {
	    // wrong syntax
	    LOG.debug(ex.getMessage(), ex);
	}
    }

}

{'fraction_non_alphanumeric': 0.17047385312441005, 'fraction_numerical': 0.0022654332641117614, 'mean_word_length': 3.181531176006314, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7591122', 'n_tokens_mistral': 1757, 'n_tokens_neox': 1555, 'n_words': 459}