/*******************************************************************************
 * Copyright (c) 2011 Bolton University, UK.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the License
 * which accompanies this distribution in the file LICENSE.txt
 *******************************************************************************/
package uk.ac.bolton.archimate.editor.utils;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.regex.Pattern;

import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.browser.IWebBrowser;
import org.eclipse.ui.browser.IWorkbenchBrowserSupport;


/**
 * HTML Utils
 * 
 * @author Phillip Beauvoir
 */
public class HTMLUtils {
    
    // Previous versions
    // "(http|https|ftp)://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?"    // Original
    // "(http|https|ftp)://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%~&=]*)?"   // Added ~
    // "(http|https|ftp)://([\\w-]+\\.)+[\\w-]+(/[\\w-./?%~&=]*)?"    // Removed space
    
    /**
     * The reg expression for HTML links
     */
    public static final String HTML_LINK_REGEX = "(http|https|ftp)://([\\w-]+\\.)+[\\w-]+(/[\\w-./?%~&=\\(\\)]*)?";  // added \\( and \\)
    
    /**
     * The compiled pattern to match HTML links
     */
    public static final Pattern HTML_LINK_PATTERN = Pattern.compile(HTML_LINK_REGEX);
    
    /**
     * The reg expression for HTML tags
     */
    public static final String HTML_TAG_REGEX = "<[^>]+>"; //$NON-NLS-1$

    /** 
     * The compiled pattern to match HTML tags
     */
    public static final Pattern HTML_TAG_REGEX_PATTERN = Pattern.compile(HTML_TAG_REGEX);
    
    /**
     * Strip tags out of a String
     * @param str
     * @return
     */
    public static String stripTags(String str) {
        if (str == null || str.indexOf('<') == -1 || str.indexOf('>') == -1) {
            return str;
        }
        
        str = HTML_TAG_REGEX_PATTERN.matcher(str).replaceAll(""); //$NON-NLS-1$
        return str;
    }

    /**
     * Open a link in a Browser
     * @param href
     */
    public static void openLinkInBrowser(String href) {
        // format the href for an html file (file:///<filename.html>
        // required for Mac only.
        if(href.startsWith("file:")) {
            href = href.substring(5);
            while(href.startsWith("/")) {
                href = href.substring(1);
            }
            href = "file:///" + href;
        }
        
        IWorkbenchBrowserSupport support = PlatformUI.getWorkbench().getBrowserSupport();
        try {
            IWebBrowser browser = support.getExternalBrowser();
            browser.openURL(new URL(urlEncodeForSpaces(href.toCharArray())));
        }
        catch(MalformedURLException ex) {
            ex.printStackTrace();
        }
        catch(PartInitException ex) {
            ex.printStackTrace();
        }
    }

    private static String urlEncodeForSpaces(char[] input) {
        StringBuffer retu = new StringBuffer(input.length);
        for(int i = 0; i < input.length; i++) {
            if(input[i] == ' ') {
                retu.append("%20"); //$NON-NLS-1$
            }
            else {
                retu.append(input[i]);
            }
        }
        return retu.toString();
    }
    
}

@@ -897,24 +897,33 @@
  ./?%25&=%5D*)?%22
+         
     // Origi
@@ -994,16 +994,25 @@
 ~&=%5D*)?%22
+         
    // Ad
@@ -1080,24 +1080,33 @@
 ./?%25~&=%5D*)?%22
+         
     // Remov
@@ -1115,16 +1115,117 @@
  space%0D%0A
+    // %22(http%7Chttps%7Cftp)://(%5B%5C%5Cw-%5D+%5C%5C.)+%5B%5C%5Cw-%5D+(/%5B%5C%5Cw-./?%25~#!&=%5C%5C(%5C%5C)%5D*)?%22;    // Added %5C%5C( and %5C%5C)%0D%0A
     %0D%0A  
@@ -1383,16 +1383,18 @@
 %5Cw-./?%25~
+#!
 &=%5C%5C(%5C%5C)
@@ -1408,25 +1408,21 @@
  // 
-a
+A
 dded 
-%5C%5C(
+#
  and 
-%5C%5C)
+!
 %0D%0A  

Add # and ! to HTML Regex allowed characters in URL