package com.linuxzasve.mobile;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Skracuje url koristeci Google URL shortener (goo.gl)
 *
 */
public class GoogleUrlShortener {
	
	/**
	 * Funkcija skracuje url dugiUrl
	 *
	 * @return skraceni URL, oblika http://goo.gl/...
	 */
	public static String ShortenUrl(String dugiUrl) {
		
		// Saljem POST na https://www.googleapis.com/urlshortener/v1/url
		String postUrl = "https://www.googleapis.com/urlshortener/v1/url";
		
		// string koji treba skratiti se salje kao JSON, npr:
        // {"longUrl": "http://www.google.com/"}
        String jsonLongUrl = "{\"longUrl\":\"" + dugiUrl + "\"}";
        
        String skraceniUrl = "";

        try
        {
            URLConnection conn = new URL(postUrl).openConnection();
            conn.setDoOutput(true);
            
            // Content-Type: application/json
            conn.setRequestProperty("Content-Type", "application/json");
            OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
            wr.write(jsonLongUrl);
            wr.flush();

            BufferedReader rd =  new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line;

            while ((line = rd.readLine()) != null)
            {
            	// "id": "http://goo.gl/fbsS",
                Pattern p = Pattern.compile("id\": \"(.*)\",");
                Matcher m = p.matcher(line);
                if (m.find()) {
                	skraceniUrl = m.group(1);
                	break;
                }
            }

            wr.close();
            rd.close();
        }
        catch (MalformedURLException ex)
        {

        }
        catch (IOException ex)
        {

        }
        
        return skraceniUrl;
	}
}

@@ -562,18 +562,16 @@
 iUrl) %7B%0A
-%09%09
 %0A%09%09// Sa
@@ -758,24 +758,18 @@
 N, npr:%0A
-        
+%09%09
 // %7B%22lon
@@ -801,24 +801,18 @@
 .com/%22%7D%0A
-        
+%09%09
 String j
@@ -861,33 +861,19 @@
  %22%5C%22%7D%22;%0A
-        %0A        
+%0A%09%09
 String s
@@ -894,42 +894,21 @@
 %22;%0A%0A
-        try%0A        %7B%0A            
+%09%09try%0A%09%09%7B%0A%09%09%09
 URLC
@@ -959,28 +959,19 @@
 tion();%0A
-            
+%09%09%09
 conn.set
@@ -990,33 +990,12 @@
 e);%0A
-            %0A            
+%0A%09%09%09
 // C
@@ -1024,28 +1024,19 @@
 on/json%0A
-            
+%09%09%09
 conn.set
@@ -1088,28 +1088,19 @@
 json%22);%0A
-            
+%09%09%09
 OutputSt
@@ -1163,28 +1163,19 @@
 eam());%0A
-            
+%09%09%09
 wr.write
@@ -1189,28 +1189,19 @@
 ngUrl);%0A
-            
+%09%09%09
 wr.flush
@@ -1205,28 +1205,19 @@
 ush();%0A%0A
-            
+%09%09%09
 Buffered
@@ -1295,28 +1295,19 @@
 am()));%0A
-            
+%09%09%09
 String l
@@ -1312,28 +1312,19 @@
  line;%0A%0A
-            
+%09%09%09
 while ((
@@ -1357,35 +1357,14 @@
 ull)
-%0A            %7B%0A            
+ %7B%0A%09%09%09
 %09// 
@@ -1391,32 +1391,20 @@
 /fbsS%22,%0A
-                
+%09%09%09%09
 Pattern 
@@ -1443,32 +1443,20 @@
 )%5C%22,%22);%0A
-                
+%09%09%09%09
 Matcher 
@@ -1480,329 +1480,198 @@
 e);%0A
-                if (m.find()) %7B%0A                %09skraceniUrl = m.group(1);%0A                %09break;%0A                %7D%0A            %7D%0A%0A            wr.close();%0A            rd.close();%0A        %7D%0A        catch (MalformedURLException ex)%0A        %7B%0A%0A        %7D%0A        catch (IOException ex)%0A        %7B%0A%0A        %7D%0A        %0A        
+%09%09%09%09if (m.find()) %7B%0A%09%09%09%09%09skraceniUrl = m.group(1);%0A%09%09%09%09%09break;%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%0A%09%09%09wr.close();%0A%09%09%09rd.close();%0A%09%09%7D%0A%09%09catch (MalformedURLException ex)%0A%09%09%7B%0A%0A%09%09%7D%0A%09%09catch (IOException ex)%0A%09%09%7B%0A%0A%09%09%7D%0A%0A%09%09
 retu

Indent fix