Changeset 546


Ignore:
Timestamp:
11/27/13 16:23:34 (10 years ago)
Author:
jts21
Message:

Move URL validation to navigate() method

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/expeditee/items/widgets/JfxBrowser.java

    r545 r546  
    284284        }
    285285
    286         public void navigate(final String url) {
    287                 System.out.println(url);
     286        public void navigate(String url) {
     287                final String actualURL;
     288                String urlLower = url.toLowerCase();
     289                // check if protocol is missing
     290                // TODO: can/should we support other protocols such as ftp ???
     291                if(!(urlLower.startsWith("http://") || url.startsWith("https://"))) {
     292                        // check if it's a search
     293                        int firstSpace = url.indexOf(" ");
     294                        int firstDot = url.indexOf(".");
     295                        int firstSlash = url.indexOf('/');
     296                        int firstQuestion = url.indexOf('?');
     297                        if(firstDot == -1 ||                                        // no '.' and no protocol -> search
     298                                (firstSpace != -1 && firstSpace <= firstDot + 1) || // ' ' before '.'         -> search
     299                                (firstSlash == -1 && firstQuestion == -1)) {        // no '/' or '?'          -> search
     300                                // make it a search
     301                                actualURL = UserSettings.searchEngine + url;
     302                        } else {
     303                                // add the missing protocol
     304                                actualURL = "http://" + url;
     305                        }
     306                } else {
     307                        actualURL = url;
     308                }
     309                System.out.println(actualURL);
    288310                try {
    289311                        PlatformRunLater.invoke(null, new Runnable() {
     
    291313                                public void run() {
    292314                                        try {
    293                                                 WebEngineLoad.invoke(WebViewGetEngine.invoke(JfxBrowser.this._browser.webview), url);
     315                                                WebEngineLoad.invoke(WebViewGetEngine.invoke(JfxBrowser.this._browser.webview), actualURL);
    294316                                        } catch (Exception e) {
    295317                                                e.printStackTrace();
     
    307329                if ((carried = FreeItems.getTextAttachedToCursor()) != null) {
    308330                        String text = carried.getText().trim();
    309                         String textLower = text.toLowerCase();
    310                         // check if protocol is missing
    311                         // TODO: can/should we support other protocols such as ftp ???
    312                         if(!(textLower.startsWith("http://") || text.startsWith("https://"))) {
    313                                 // check if it's a search
    314                                 int firstSpace = text.indexOf(" ");
    315                                 int firstDot = text.indexOf(".");
    316                                 int firstSlash = text.indexOf('/');
    317                                 int firstQuestion = text.indexOf('?');
    318                                 if(firstDot == -1 ||                                        // no '.' and no protocol -> search
    319                                         (firstSpace != -1 && firstSpace <= firstDot + 1) || // ' ' before '.'         -> search
    320                                         (firstSlash == -1 && firstQuestion == -1)) {        // no '/' or '?'          -> search
    321                                         // make it a search
    322                                         text = UserSettings.searchEngine + text;
    323                                 } else {
    324                                         // add the missing protocol
    325                                         text = "http://" + text;
    326                                 }
    327                         }
    328331                        this.navigate(text);
    329332                        FreeItems.getInstance().clear();
Note: See TracChangeset for help on using the changeset viewer.