import java.util.Scanner;
import java.util.regex.*;
import java.io.*;
import javax.swing.*;
/**
* A program that parses a user-supplied URL
*/
public class URLParser {
public static void main(String[] args) throws IOException {
Scanner scan = new Scanner(System.in);
PrintWriter out = new PrintWriter(new FileWriter("D:\\cs2200\\lab8\\URLs.txt"));
String input="";
// Everything works the way I want it to when I take out this while loop.
while (input != "x") {
System.out.print("Enter URL(type \"x\" to finish): ");
input = scan.nextLine();
parse(input);
if (input.charAt(0) != 'N')
out.println(input);
System.out.println(input);
}
out.close();
}
public static String parse(String theInput) {
boolean legality = false;
theInput.trim();
Pattern protocol = Pattern.compile("(http://)|(https://)|(ftp://)");
Matcher matchProt = protocol.matcher(theInput);
boolean foundProtMatch = matchProt.find();
Pattern hostname = Pattern.compile("(www.)|([a-zA-Z0-9]{1,}\\.)");
Matcher matchHost = hostname.matcher(theInput);
boolean foundHostMatch = matchHost.find();
Pattern path = Pattern.compile("[/\\.a-zA-Z0-9]*");
Matcher matchPath = path.matcher(theInput);
boolean foundPathMatch = matchPath.find();
if (theInput.charAt(0) == 'h' || theInput.charAt(0) == 'f') {
if ( foundProtMatch == true && foundHostMatch == true && foundPathMatch == true ) {
JOptionPane.showMessageDialog(null, "Yes, it's legal.");
legality = true; }
else {
JOptionPane.showMessageDialog(null, "No, it’s illegal.");
legality = false; }
}
else if (theInput.charAt(0) == 'w' && theInput.charAt(1) == 'w' && theInput.charAt(2) == 'w') {
if ( foundHostMatch == true && foundPathMatch == true ) {
JOptionPane.showMessageDialog(null, "Yes, it's legal.");
legality = true; }
else {
JOptionPane.showMessageDialog(null, "No, it’s illegal.");
legality = false; }
}
else {
JOptionPane.showMessageDialog(null, "No, it’s illegal.");
legality = false; }
if (legality == false)
theInput = "Not Legal: " + theInput;
// System.out.println(theInput);
return theInput;
}
private String trim(String theInput) {
theInput.trim();
return theInput;
}
}
ciarz3r 0 Newbie Poster
stephen84s 550 Nearly a Posting Virtuoso Featured Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.