Commit 298ebc5d authored by Nico Mack's avatar Nico Mack

Added UriParser class

parent 53ddcc5f
/**
* Copyright Luxembourg Institute of Science and Technology, 2019. All rights reserved.
*
* This file is part of TULIP.
*
* TULIP is free software: you can redistribute it and/or modify it under the terms of the GNU
* Lesser General Public License as published by the Free Software Foundation, version 3 of the
* License.
*
* TULIP is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
* General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with TULIP. If
* not, see <http://www.gnu.org/licenses/lgpl-3.0.html>.
*/
package lu.list.itis.dkd.tui.utility;
import com.google.common.base.Strings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author mack
* @since [major].[minor]
* @version [major].[minor].[micro]
*/
// ***************************************************************************
// * Class Definition and Members *
// ***************************************************************************
public class UriParser {
private String scheme;
private String url;
private int port;
private boolean isValid;
// ***************************************************************************
// * Constants *
// ***************************************************************************
private static final int UNSPECIFIED_PORT = -1;
// inspired by https://snipplr.com/view/6889/regular-expressions-for-uri-validationparsing/
private static final Pattern AUTHORITY_PATTERN = Pattern.compile("([a-z][a-z0-9+.-]*)://(\\[[0-9A-F:.]{2,}\\]|(?:[a-z0-9-._~!$&'()*+,;=]|%[0-9A-F]{2})*)(?::(?=(\\d*)))", Pattern.CASE_INSENSITIVE); //$NON-NLS-1$
private static final Logger LOGGER = LoggerFactory.getLogger(UriParser.class.getSimpleName());
// ---------------------------------------------------------------------------
// ***************************************************************************
// * Constructor(s)
// ***************************************************************************
// ---------------------------------------------------------------------------
/**
* @param uri
*/
// ---------------------------------------------------------------------------
public UriParser(String uri) {
Matcher authority = AUTHORITY_PATTERN.matcher(uri);
if (authority.lookingAt()) {
this.isValid = true;
this.scheme = authority.group(1);
this.url = authority.group(2);
this.port = (Strings.isNullOrEmpty(authority.group(3))) ? UNSPECIFIED_PORT : StringUtils.getIntegerValue(authority.group(3));
} else {
this.isValid = false;
LOGGER.warn("Malformed URI. Can't parse {}!", uri); //$NON-NLS-1$
}
}
// ---------------------------------------------------------------------------
// ***************************************************************************
// * Class Body
// ***************************************************************************
// ---------------------------------------------------------------------------
/**
* @return
*/
// ---------------------------------------------------------------------------
public boolean isValid() {
return this.isValid;
}
// ---------------------------------------------------------------------------
/**
* @return
*/
// ---------------------------------------------------------------------------
public String getScheme() {
return this.scheme;
}
// ---------------------------------------------------------------------------
/**
* @return
*/
// ---------------------------------------------------------------------------
public String getUrl() {
return this.url;
}
// ---------------------------------------------------------------------------
/**
* @return
*/
// ---------------------------------------------------------------------------
public boolean hasSpecifiedPort() {
return (this.port != UNSPECIFIED_PORT);
}
// ---------------------------------------------------------------------------
/**
* @return
*/
// ---------------------------------------------------------------------------
public int getPort() {
return this.port;
}
// ---------------------------------------------------------------------------
// ***************************************************************************
// * End of Class
// ***************************************************************************
// ---------------------------------------------------------------------------
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment