Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java Open Source > Struts
|
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Struts section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 8th, 2006, 02:30 AM
Registered User
 
Join Date: Nov 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Help ! Struts Validation

I need to validate the URL using struts validator..
the URL should be RFC 1738, and RFC2396 compliant.
how can i do that..
plz help me on creating the Regex for URL validaion.

 
Old November 13th, 2006, 06:55 AM
Authorized User
 
Join Date: Aug 2006
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to sachin.tathod
Default

Hi,

 Following code will help u to get url validation.

Let me know any problem.

import java.util.regex.Matcher;
import java.util.regex.Pattern;



public class ValidatorUtil {

     */
    public static boolean validateURL(String url) {
        url = url.trim();

        String newUrl = "";
        String urlWithoutHeaderPart = "";
        String urlWithoutHFPart = "";
        String urlFilePart = "";
        String urlDomainPart = "";

        int fileIndex = -1;
        int domainIndex = -1;

        boolean isValidFilePartGlobal = true;
        boolean isValidDomainPartGlobal = true;
        boolean isValidMiddlePartGlobal = true;

        Pattern p = Pattern.compile("\\b(http(s?)\\:\\/\\/)");
        Matcher m = p.matcher(url.trim());
        boolean matchFound = m.find();

        if (matchFound) //if url already starts with http:// or https://
         {
            newUrl = url.trim();
        } else {
            newUrl = "http://".concat(url.trim()).trim();
        }

        Pattern p1 = Pattern.compile("\\b(http\\:\\/\\/)");
        Matcher m1 = p1.matcher(newUrl.trim());
        Pattern p2 = Pattern.compile("\\b(https\\:\\/\\/)");
        Matcher m2 = p2.matcher(newUrl.trim());
        boolean matchFound1 = m1.find();
        boolean matchFound2 = m2.find();

        if (matchFound2) {
            urlWithoutHeaderPart = newUrl.trim().substring(8);
        }

        if (matchFound1 && !matchFound2) {
            //if http://
            urlWithoutHeaderPart = newUrl.trim().substring(7);
        }

        //Check File Part
        fileIndex = urlWithoutHeaderPart.trim().indexOf("/");

        if (fileIndex == 0) {
            isValidFilePartGlobal = false;
            urlWithoutHFPart = urlWithoutHeaderPart;
        }

        if (fileIndex > 0) {
            urlFilePart = urlWithoutHeaderPart.trim().substring(fileIndex);

            Pattern filePartPattern = Pattern.compile("[-\\w\\./]+");
            Matcher macthFilePart = filePartPattern.matcher(urlFilePart.trim());
            boolean isValidFilePart = macthFilePart.matches();

            if (!isValidFilePart) {
                //isInvalidurl = true;
                isValidFilePartGlobal = false;
            }

            urlWithoutHFPart = urlWithoutHeaderPart.trim().substring(0,
                    fileIndex);
        } else {
            urlWithoutHFPart = urlWithoutHeaderPart;
        }

        //Check Domain part
        domainIndex = urlWithoutHFPart.trim().lastIndexOf(".");

        if (domainIndex <= 0) {
            isValidDomainPartGlobal = false;
        } else {
            urlDomainPart = urlWithoutHFPart.trim().substring(domainIndex + 1);

            boolean isValidDomain = (validateDomainPart(urlDomainPart));

            if (!isValidDomain) {
                //isInvalidurl = true;
                isValidDomainPartGlobal = false;
            }
        }

        //end
        //Check Middle Part
        Pattern middlePartPattern = Pattern.compile(
                "^([-\\w]+\\.){1,4}[a-zA-Z]{2,6}$");
        Matcher macthMiddlePart = middlePartPattern.matcher(urlWithoutHFPart.trim()) ;
        boolean isValidMiddlePart = macthMiddlePart.matches();

        if (!isValidMiddlePart) {
            isValidMiddlePartGlobal = false;
        }

        return ((isValidFilePartGlobal && isValidDomainPartGlobal &&
        isValidMiddlePartGlobal) ? true : false);
    }

    /**
     * @param String
     * This method returns true or false based on the input parameter String is a valid domain or not
     */
    private static boolean validateDomainPart(String s) {
        boolean isValidDomainPart = false;
        String[] domainArray = {
                "aero", "biz", "coop", "com", "edu", "gov", "info", "in", "mil",
                "museum", "name", "net", "org", "pro", "uk", "fr" , "ca"
            };

        for (int i = 0; i < domainArray.length; i++) {
            if (((domainArray[i].trim()).equalsIgnoreCase(s.trim()))) {
                isValidDomainPart = true;

                break;
            }
        }

        return isValidDomainPart;
    }
}


Thanks and Regards,
SACHIN S.TATHOD
Patni Computer System Ltd.
Magarpatta City, Cyber City,
Tower 3,Level I & II,
Hadapsar,Pune - 411028
Mobile No:- +91-9881239401
Email ID:- sachin.tathod@patni.com





Similar Threads
Thread Thread Starter Forum Replies Last Post
Standalone validation + web form validation morbo Struts 0 August 19th, 2008 04:02 AM
Problem in Struts Validation meetravig Struts 0 July 24th, 2007 01:32 AM
struts actions package: "struts", other packages? ojasrege Struts 1 July 21st, 2007 06:02 AM
How to get upgraded from Struts 1.1 to struts 1.2. rayalujakkaraju@yahoo.com Struts 0 January 3rd, 2006 04:29 AM
Struts Validation sridevi J2EE 2 June 20th, 2005 11:56 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.