summaryrefslogtreecommitdiffstats
path: root/Master/Agile Software Development/TestApp/src/Parser/UrlAnalyzer.java
blob: c543849e44792aff62550e844c1a2ce4d1ce1abe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
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<String, String> getParams(String url) {
        HashMap<String,String> result = new HashMap<String,String>();
        
        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;
    }
}