Changeset 10575


Ignore:
Timestamp:
2005-08-28T13:45:40+12:00 (19 years ago)
Author:
mdewsnip
Message:

Made URLField far simpler and more accurate. Removed the crazy regular expressions and now just attempts to create a URL object to validate the string.

Location:
trunk/gli/src/org/greenstone/gatherer
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/gli/src/org/greenstone/gatherer/Gatherer.java

    r10543 r10575  
    696696        dialog = new WarningDialog("warning.MissingEXEC", "MissingEXEC.Title", Dictionary.get("MissingEXEC.Message"), "general.library_url", false);
    697697    }
    698     dialog.setValueField(new URLField(Configuration.getColor("coloring.editable_foreground", false), Configuration.getColor("coloring.editable_background", false), Configuration.getColor("coloring.error_foreground", false), Configuration.getColor("coloring.error_background", false)));
     698    dialog.setValueField(new URLField(Configuration.getColor("coloring.editable_foreground", false), Configuration.getColor("coloring.editable_background", false)));
    699699    dialog.display();
    700700    dialog.dispose();
     
    716716    {
    717717    WarningDialog dialog = new WarningDialog("warning.MissingGLIServer", "MissingGLIServer.Title", Dictionary.get("MissingGLIServer.Message"), "general.gliserver_url", false);
    718     dialog.setValueField(new URLField(Configuration.getColor("coloring.editable_foreground", false), Configuration.getColor("coloring.editable_background", false), Configuration.getColor("coloring.error_foreground", false), Configuration.getColor("coloring.error_background", false)));
     718    dialog.setValueField(new URLField(Configuration.getColor("coloring.editable_foreground", false), Configuration.getColor("coloring.editable_background", false)));
    719719    dialog.display();
    720720    dialog.dispose();
  • trunk/gli/src/org/greenstone/gatherer/gui/URLField.java

    r9312 r10575  
    22
    33import java.awt.*;
    4 import java.util.regex.*;
     4import java.net.*;
    55import javax.swing.*;
    6 import javax.swing.event.*;
    7 import javax.swing.text.*;
    8 import org.greenstone.gatherer.DebugStream;
    96
    107
    118public class URLField
    12     extends JTextField
    13     implements DocumentListener {
    14 
    15     static final public Pattern URL_PATTERN = Pattern.compile("(h|ht|htt|http|http:|http:/|http://|http://)(([a-zA-Z]([a-zA-Z]|[0-9]|[\\$\\-_\\&\\+]|[!\\*\"\'\\(\\)]|(%[0-9A-Fa-f][0-9A-Fa-f]))*(\\.[a-zA-Z]([a-zA-Z]|[0-9]|[\\$\\-_\\&\\+]|[!\\*\"\'\\(\\)]|(%[0-9A-Fa-f][0-9A-Fa-f]))*)*)|(([0-9])+\\.([0-9])+\\.([0-9])+\\.([0-9])+))(:([0-9])+)?(/([a-zA-Z]|[0-9]|[\\$\\-_\\.\\&\\+]|[!\\*\"\'\\(\\),]|(%[0-9A-Fa-f][0-9A-Fa-f]))*)*");
    16 
    17     // Crap RegEx that matches everything dammit
    18     //static final public Pattern URL_PATTERN = Pattern.compile("((.*?)://)?(([^:]*):([^@]*)@)?(([^/:]*)(:[^/]*))?([^\\?#]*/?)?(\\?([^?#]*))?(#(.*))?");
    19 
    20     // Crap RegEx that demands URL starts with www
    21     //static final public Pattern URL_PATTERN = Pattern.compile("(h|ht|htt|http|http:|http:/|http://|http://)?[\\w]+(\\.[\\w]+)([\\w\\-\\.,@?^=%&:/~\\+#]*[\\w\\-\\@?^=%&/~\\+#])?");
    22 
    23     private Color background;
    24     private Color foreground;
    25     private Color invalid_background;
    26     private Color invalid_foreground;
    27 
    28     public URLField(Color foreground, Color background, Color invalid_foreground, Color invalid_background) {
     9    extends JTextField
     10{
     11    public URLField(Color foreground, Color background)
     12    {
    2913    super();
    30     this.background = background;
    31     this.foreground = foreground;
    32     this.invalid_background = invalid_background;
    33     this.invalid_foreground = invalid_foreground;
    3414    setBackground(background);
    3515    setForeground(foreground);
    36     //getDocument().addDocumentListener(this);
    3716    }
    3817
    39     public URLField(String email, Color foreground, Color background, Color invalid_foreground, Color invalid_background) {
    40     super(email);
    41     this.background = background;
    42     this.foreground = foreground;
    43     this.invalid_background = invalid_background;
    44     this.invalid_foreground = invalid_foreground;
    45     setBackground(background);
    46     //getDocument().addDocumentListener(this);
    47     }
    4818
    49     /** Gives notification that an attribute or set of attributes changed.
    50      * @param e
    51      */
    52     public void changedUpdate(DocumentEvent e) {
    53     validateURL();
    54     }
    55          
    56     /** Gives notification that there was an insert into the document.
    57      * @param e
    58      */
    59     public void insertUpdate(DocumentEvent e) {
    60     validateURL();
    61     }
     19    public boolean validateURL()
     20    {
     21    String url_string = getText();
     22    if (!url_string.equals("")) {
     23        // Check the URL string is valid by trying to create a URL object from it
     24        try {
     25        new URL(url_string);
     26        }
     27        catch (MalformedURLException exception) {
     28        // URL string is invalid
     29        return false;
     30        }
     31    }
    6232
    63     public boolean isEmpty() {
    64     return (this.getText().length() == 0);
    65     }
    66          
    67     /** Gives notification that a portion of the document has been removed.
    68      * @param e
    69      */
    70     public void removeUpdate(DocumentEvent e) {
    71     validateURL();
    72     }
    73 
    74     public boolean validateURL() {
    75     boolean result;
    76     String text = getText();
    77     Matcher m = URL_PATTERN.matcher(text);
    78     if(text.length() == 0 || m.matches()) {
    79         setBackground(background);
    80         setForeground(foreground);
    81         result = true;
    82     }
    83     else {
    84         setBackground(invalid_background);
    85         setForeground(invalid_foreground);
    86         result = false;
    87     }
    88     m = null;
    89     DebugStream.println("Validating: " + text + " -> " + (result ? "GOOD" : "BAD"));
    90     text = null;
    91     return result;
     33    // URL string is valid
     34    return true;
    9235    }
    9336}
Note: See TracChangeset for help on using the changeset viewer.