/**
 * Copyright 2014 by Andre Beckus
 * 
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at

 *   http://www.apache.org/licenses/LICENSE-2.0

 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package com.abcodeworks.webshortcututil.write;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;

import com.abcodeworks.webshortcututil.read.ShortcutReadException;

/**
 * Writes URL shortcuts (Windows shortcuts with a .url extension).
 * 
 */
public class UrlShortcutWriter extends ShortcutWriter {
    public String defaultExtension() {
        return "url";
    }
    
    @Override
    public void write(OutputStream stream, String name, String url)
            throws ShortcutWriteException {
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(stream));
        try {
            // We need to check if the URL only contains ASCII characters.  If it does not we need to encode it using UTF7.
            // See http://stackoverflow.com/questions/3585053/in-java-is-it-possible-to-check-if-a-string-is-only-ascii
            CharsetEncoder asciiEncoder = Charset.forName("US-ASCII").newEncoder();
            if(asciiEncoder.canEncode(url)) {
                writer.write("[InternetShortcut]\r\n");
                writer.write("URL=");
                writer.write(url);
                writer.write("\r\n");
            } else {
                writer.write("[InternetShortcut.W]\r\n");
                writer.write("URL=");

                byte[] asciiBytes = url.getBytes("UTF-7");

                // Note that the jutf7 library (http://jutf7.sourceforge.net/)
                // needs to be added as a Maven dependency (or I think can be just
                // added to the classpath) in order to add UTF-7 support.
                String utf7Url = new String(asciiBytes, "US-ASCII");

                writer.write(utf7Url);
                writer.write("\r\n");
            }

            writer.close();
        } catch (IOException e) {
            throw new ShortcutWriteException();
        } finally {
            try {
                if(writer != null) {
                    writer.close();
                }
            } catch (IOException e) {
                throw new ShortcutWriteException(e);
            }
        }
        
    }

}

@@ -1080,16 +1080,80 @@
 Encoder;
+%0Aimport java.util.regex.Matcher;%0Aimport java.util.regex.Pattern;
 %0A%0Aimport
@@ -1417,24 +1417,325 @@
 %0A    %7D%0A    %0A
+    protected void writeSection(BufferedWriter writer, String header, String url)%0A            throws IOException %7B%0A        writer.write(%22%5B%22);%0A        writer.write(header);%0A        writer.write(%22%5D%5Cr%5Cn%22);%0A        writer.write(%22URL=%22);%0A        writer.write(url);%0A        writer.write(%22%5Cr%5Cn%22);%0A    %7D%0A    %0A
     @Overrid
@@ -2337,34 +2337,41 @@
        write
-r.
+Section(
 write
-(%22%5B
+r, %22
 InternetShor
@@ -2374,22 +2374,22 @@
 Shortcut
-%5D%5Cr%5Cn%22
+%22, url
 );%0A     
@@ -2399,127 +2399,305 @@
     
-    writer.write(%22URL=%22);%0A                writer.write(url);%0A                writer.write(%22%5Cr%5Cn%22);%0A            %7D else %7B
+%7D else %7B%0A                // We still need the ASCII sections, or Windows gives an error.%0A                Pattern unicodeCharPattern = Pattern.compile(%22%5B%5Cu0080-%5CuFFFF%5D%22);%0A                Matcher matcher = unicodeCharPattern.matcher(name);%0A                String ascii_url = matcher.replaceAll(%22_%22);
 %0A   
@@ -2714,26 +2714,33 @@
    write
-r.
+Section(
 write
-(%22%5B
+r, %22
 Internet
@@ -2751,16 +2751,20 @@
 tcut
-.W%5D%5Cr%5Cn%22
+%22, ascii_url
 );%0A 
@@ -2787,82 +2787,55 @@
 rite
-r.write(%22URL=%22);%0A%0A                byte%5B%5D asciiBytes = url.getBytes(%22UTF-7%22
+Section(writer, %22InternetShortcut.A%22, ascii_url
 );%0A%0A
@@ -3070,16 +3070,75 @@
 upport.%0A
+                byte%5B%5D asciiBytes = url.getBytes(%22UTF-7%22);%0A
         
@@ -3198,17 +3198,16 @@
 SCII%22);%0A
-%0A
         
@@ -3223,61 +3223,53 @@
 rite
-r.write(utf7Url);%0A                writer.write(%22%5Cr%5Cn%22
+Section(writer, %22InternetShortcut.W%22, utf7Url
 );%0A 

Fix problem generating .url files with unicode URLs. Fixes issue #2.