info.devbug.core.UnixCifsUrlConverter.java Source code

Java tutorial

Introduction

Here is the source code for info.devbug.core.UnixCifsUrlConverter.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package info.devbug.core;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang3.StringUtils;

/**
 *
 * @author Aliaksei Bahdanau
 */
public class UnixCifsUrlConverter implements CifsUrlconverter {

    private static final String CIFS_URL_PATTERN = "([\\\\]+[A-Za-z0-9]+)+(\\\\*[A-Za-z0-9\\s]*)*(\\\\*[A-Za-z0-9\\s]*.[A-Za-z0-9\\s])*";

    @Override
    public String convert(String windowsCifsUrl, boolean ignoreSpaces) {
        Pattern pattern = Pattern.compile(CIFS_URL_PATTERN);
        Matcher matcher = pattern.matcher(windowsCifsUrl);

        boolean isMatch = matcher.matches();

        if (isMatch) {
            String cifsPrefix = "smb://";

            // Windows cifs url starts from right slashes "//". We should remove them
            String url = StringUtils.remove(windowsCifsUrl, "\\\\");
            String unixUrl = StringUtils.replace(url, "\\", "/");

            if (!ignoreSpaces) {
                unixUrl = convertWithSpaces(unixUrl);
            }

            String finalUnixUrl = cifsPrefix + unixUrl;

            return finalUnixUrl;
        }
        return "Incorrect CIFS url";
    }

    private String convertWithSpaces(String cifsUrl) {
        return StringUtils.replace(cifsUrl, " ", "\\ ");
    }
}