From 33613a85afc4b1481367fbe92a17ee59c240250b Mon Sep 17 00:00:00 2001 From: Sven Eisenhauer Date: Fri, 10 Nov 2023 15:11:48 +0100 Subject: add new repo --- .../TestApp/src/Parser/UrlAnalyzer.java | 161 +++++++++++++++++++++ 1 file changed, 161 insertions(+) create mode 100644 Master/Agile Software Development/TestApp/src/Parser/UrlAnalyzer.java (limited to 'Master/Agile Software Development/TestApp/src/Parser/UrlAnalyzer.java') diff --git a/Master/Agile Software Development/TestApp/src/Parser/UrlAnalyzer.java b/Master/Agile Software Development/TestApp/src/Parser/UrlAnalyzer.java new file mode 100644 index 0000000..c543849 --- /dev/null +++ b/Master/Agile Software Development/TestApp/src/Parser/UrlAnalyzer.java @@ -0,0 +1,161 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package Parser; + +import java.util.HashMap; +import javax.swing.JOptionPane; + +/** + * + * @author sven + */ +public class UrlAnalyzer { + public static final String PROTOCOL_HTTP = "http"; + public static final String PROTOCOL_HTTPS = "https"; + public static final String PROTOCOL_FTP = "ftp"; + public static final String PROTOCOL_FILE = "file"; + public static final String NO_PROTOCOL = "NO_PROTOCOL"; + public static final String NO_HOST = "NO_HOST"; + public static final String NO_VALID_PATH = "NO_VALID_PATH"; + public static final String NO_VALID_FILE = "NO_VALID_FILE"; + public static final String PROTOCOL_SEPARATOR = "://"; + public static final String PATH_SEPARATOR = "/"; + public static final String PARAMS_INDICATOR = "?"; + public static final String ANCHOR = "#"; + public static final String PARAMS_SEPARATOR = "&"; + public static final String PARAMS_ASSIGN = "="; + public static final String[] VALID_PROTOCOLS = + {PROTOCOL_HTTP,PROTOCOL_HTTPS,PROTOCOL_FTP,PROTOCOL_FILE}; + public static final String NO_ANCHOR = "NO_ANCHOR"; + + public static HashMap getParams(String url) { + HashMap result = new HashMap(); + + if(url!=null) { + String paramStr = url.substring(url.indexOf(PARAMS_INDICATOR)+1); + String[] params = paramStr.split(PARAMS_SEPARATOR); + for(String param : params) { + String[] paramKeyValue = param.split(PARAMS_ASSIGN); + if (paramKeyValue.length > 1) { + result.put(paramKeyValue[0], paramKeyValue[1]); + } else { + result.put(paramKeyValue[0],null); + } + } + } + return result; + } + + public static final String getProtocol(final String url) { + if(url==null) + return NO_PROTOCOL; + + for (String proto : VALID_PROTOCOLS) { + if (url.startsWith(proto)) { + return proto; + } + } + return new String(NO_PROTOCOL); + } + public static final String getHostname(final String url) { + if(url==null) + return NO_HOST; + + if (!url.contains(PROTOCOL_SEPARATOR)) { + return NO_HOST; + } + int hostStart = url.indexOf(PROTOCOL_SEPARATOR)+PROTOCOL_SEPARATOR.length(); + int hostEnd = url.indexOf(PATH_SEPARATOR, hostStart); + if ( hostEnd <= 1 ) { + return NO_HOST; + } + return url.substring(hostStart, hostEnd); + } + public static final String getFilename(final String url) { + if(url==null) + return ""; + + String rest = url; + if (url.contains(PROTOCOL_SEPARATOR)) { + int protoEnd = url.indexOf(PROTOCOL_SEPARATOR)+PROTOCOL_SEPARATOR.length(); + rest = url.substring(protoEnd); + if ( (rest.indexOf(PATH_SEPARATOR)) == -1) { + return NO_VALID_FILE; + } + rest = url.substring(protoEnd); + } + int fileStart = rest.lastIndexOf(PATH_SEPARATOR); + String res = (fileStart != -1) ? rest.substring(fileStart+1) : rest; + if (hasParams(res)) { + res = res.substring(0, res.indexOf(PARAMS_INDICATOR)); + } + if (hasAnchor(res)) { + res = res.substring(0, res.indexOf(ANCHOR)); + } + return res; + } + public static final String getPath(String url) { + if(url==null) + return NO_VALID_PATH; + + int pathStart = 0; + int pathEnd = url.lastIndexOf(PATH_SEPARATOR); + + if (pathEnd < 1) { + // item at root level + // e. g. foobar.html oder cool.gifv + return PATH_SEPARATOR; + } + if (url.contains(PROTOCOL_SEPARATOR)) { + // we have a full url + // e.g. http://foo.bar.com/dir/subdir/index.html + pathStart = url.indexOf(PATH_SEPARATOR,url.indexOf(PROTOCOL_SEPARATOR)+PROTOCOL_SEPARATOR.length()); + if (pathStart < 0) { + return NO_VALID_PATH; + } + } else { + // we have a relative path on site + // e. g.: img/coolimg.jpg + // or res/img/anotherimg.gif + pathStart = 0; + pathEnd = url.lastIndexOf(PATH_SEPARATOR); + } + return url.substring(pathStart,pathEnd)+PATH_SEPARATOR; + } + public static final String getAnchor(String url) { + if(url==null) + return NO_ANCHOR; + + return (hasAnchor(url)) ? url.substring(url.indexOf(ANCHOR)+1) : null; + } + public static final boolean hasParams(String url) { + if(url==null) + return false; + + return url.contains(PARAMS_INDICATOR); + } + public static final boolean hasAnchor(String url) { + if(url==null) + return false; + + return url.contains(ANCHOR); + } + public static final String stripProtocol(String url) { + return url.substring(url.indexOf(PROTOCOL_SEPARATOR)+PROTOCOL_SEPARATOR.length()); + } + + public static final boolean validateURL(String url) + { + if( url.contains(PROTOCOL_HTTP) || url.contains(PROTOCOL_HTTPS) || url.contains(PROTOCOL_FTP) || url.contains(PROTOCOL_FILE) ) + if(url.contains(PROTOCOL_SEPARATOR)) + if(url.contains(".")) + return true; + + JOptionPane.showMessageDialog(null, "Falsche Url. Bitte geben Sie eine korrekte Url ein!"); + + return false; + } +} -- cgit v1.2.3