Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By wsaryada

Thread: Custom String and Number validator classes using Resource Bundle mechanism

  1. #1
    mcdhappy80 is offline Member
    Join Date
    Nov 2010
    Posts
    24
    Rep Power
    0

    Question Custom String and Number validator classes using Resource Bundle mechanism

    I'me reading, for quite some time now, about Resource Bundle mechanism in Java.
    It seems to me the more I read the more I'm distant from the solution.
    The number of articles I've found online talk that resource bundle is used for localization of application (which I don't need at this point).
    My problem is next:
    I need to create two separate validator classes. One class would be used for validating text and the other will be used for validating number data type.
    The text data type can hold everything except control (non-printable characters) while number data type can hold only numbers.
    The validators for each data type should be fetched in run-time using config file and Resource Bundle mechanism.
    Can someone point me in the right direction (or proper reading) of how should the bundle document be structured and how to create this in general?
    Thank You.

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    757
    Rep Power
    7

    Default Re: Custom String and Number validator classes using Resource Bundle mechanism

    If you not use the resource bundle specifically for supporting localization or internationalization of your application you can think of it as a keys and values for your application configuration. For example in your case you might store some regular expression in the resource bundle. You will have two regexs in there for validating text data type and number data type, each of the regex will have their own key in the resource bundle.

    All you need on how to use the resource bundle is here: Lesson: Isolating Locale-Specific Data (The Java™ Tutorials > Internationalization)
    mcdhappy80 likes this.

  3. #3
    mcdhappy80 is offline Member
    Join Date
    Nov 2010
    Posts
    24
    Rep Power
    0

    Question Custom String validator class - everything except non printable charaters

    I've solved the problem with the bundle. In the bundle I've entered the names of my validator classes, and I call them dynamically in the run time (depending if the type is TEXT or NUMBER).
    I have one more question regarding the string validator class.
    Here's the class:
    Java Code:
    package fileparserandvalidator;
    
    public class ValidateText {
        
        private boolean rezultatValidacije;
        
        public ValidateText(String pZaValidaciju)
        {
            this.setRezultatValidacije(pZaValidaciju.matches("[a-zA-Z0-9 ]*"));
            //System.out.println("Štampam: " + pZaValidaciju + " = " + this.getRezultatValidacije());
        }
    
        public boolean getRezultatValidacije() {
            return rezultatValidacije;
        }
    
        public void setRezultatValidacije(boolean rezultatValidacije) {
            this.rezultatValidacije = rezultatValidacije;
        }
        
        @Override
        public String toString()
        {
            if(this.getRezultatValidacije() == true)
            {
                return "validation_result=VALID";
            }
            else
            {
                return "validation_result=INVALID";
            }
        }
    }
    As You can see I'm using, not so complicated, regular expression to validate the string.
    One thing is not clear to me here when I run the program.
    If I enter some of the symbols ($,^,&,;,etc.) in the value part of my text file, my program reports that the file structure is not correct (which may be wrong because my assignment says Text is EVERYTHING except non-printable characters (which I've found to be on this link)).

    So my new question would be, how to validate (include) these characters too without breaking the given type=<VALUE_TYPE>;value=<VALUE>; file structure?
    Here's my current constructor with regular expression:
    Java Code:
    public Podatak(String pLinija)
        {
            
            if(pLinija.matches(Pattern.compile("type=[A-Z]*" + Util.RAZDELNIK + "value=[a-zA-Z0-9 ]*" + 
                    Util.RAZDELNIK).toString()) == true)
            {
                     // Do what is supposed to
            }
            else
            {
                     // File structure is not valid
             }
         }
    Util.RAZDELNIK is the (;) symbol which I define in the Util interface and my class implements it so it can be used.

    Thank You
    Last edited by mcdhappy80; 12-29-2012 at 02:27 PM. Reason: Updated info

Similar Threads

  1. When to Use Resource Bundle?
    By Arun_N in forum New To Java
    Replies: 7
    Last Post: 11-04-2011, 04:09 PM
  2. JSP, JSF: Cannot find the resource bundle
    By sunnyjassal in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 02-01-2011, 08:37 PM
  3. Reading a properties file using resource bundle.
    By rudrakanth in forum New To Java
    Replies: 1
    Last Post: 02-27-2009, 12:23 PM
  4. tomcat resource bundle tab not appearing??
    By freddieMaize in forum NetBeans
    Replies: 2
    Last Post: 05-25-2008, 09:15 AM
  5. Setting the label of radio buttons from Resource bundle
    By rajeeshankar in forum JavaServer Faces (JSF)
    Replies: 0
    Last Post: 12-17-2007, 09:23 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •