Click here to Skip to main content
15,892,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have this Class as a Javascript Minimizer
using System.IO;
using System.Net;
using System.Text;
using System.Xml;
    /// <summary>
    /// A C# wrapper around the Google Closure Compiler web service.
    /// </summary>
    public class GoogleClosure
    {
        private static string PostData = "js_code={0}&output_format=xml&output_info=compiled_code&compilation_level=SIMPLE_OPTIMIZATIONS";
        //private static string PostData = "js_code={0}&output_format=xml&output_info=compiled_code&compilation_level=ADVANCED_OPTIMIZATIONS";
        private static string ApiEndpoint = "http://closure-compiler.appspot.com/compile";

        /// <summary>
        /// Compresses the specified file using Google's Closure Compiler algorithm.
        /// <remarks>
        /// The file to compress must be smaller than 200 kilobytes.
        /// </remarks>
        /// </summary>
        /// <param name="file">The absolute file path to the javascript file to compress.</param>
        /// <returns>A compressed version of the specified JavaScript file.</returns>
        public static string CompressFile(string file)
        {
            string source = File.ReadAllText(file);
            return Compress(source);
        }
        
        public static string Compress(string source)
        {
            XmlDocument xml = CallApi(source);
            return xml.SelectSingleNode("//compiledCode").InnerText;
        }

        /// <summary>
        /// Calls the API with the source file as post data.
        /// </summary>
        /// <param name="source">The content of the source file.</param>
        /// <returns>The Xml response from the Google API.</returns>
        private static XmlDocument CallApi(string source)
        {
            using (WebClient client = new WebClient())
            {
                client.Headers.Add("content-type", "application/x-www-form-urlencoded");
                string data = string.Format(PostData, UrlEncoder.UrlEncode(source));
                string result = client.UploadString(ApiEndpoint, data);
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(result);
                return doc;
            }
        }
    }

The UrlEncoder Class has UrlEncode(string) and UrlDecode(string) (plus other needed private functions) and it is ripped from HttpUtility in System.Web using Reflector (dont exactly know if i did something illegal but i was pissed from having to switch to normal .net 4 from Client Profile for no reason :mad:).

Then i have the following text in javascript through Google Closure Minimizer...

h="\u0392\u03bb\u03ad\u03c0\u03c9 "+c+"\u03b5\u03b9\u03ba\u03cc\u03bd\u03b5\u03c2",b=true,a;document.getElementById("track2").onclick=d;


What would you suggest as a way for me to reformat this into utf-8 again?
i have tried UrlDecode(text.Replace("\\u0","%u0"));
but it leaves the "+" symbols blank.

What i want is to replace the \u0392(6 chars) with the corresponding utf8 char (and not only (\u0392) obviously).
Posted
Updated 28-Sep-10 14:24pm
v2

See http://msdn.microsoft.com/en-us/library/system.text.encoding.unicode.aspx[^] for how to convert between encodings.

Cheers

Andi
 
Share this answer
 
Comments
qyte64 29-Sep-10 5:20am    
The thing is that i currently have a string that contains js code.
More so i have a string representation of the char '?', i.e "\u03b9"
How can i use the later to get the first?
qyte64 29-Sep-10 5:23am    
*** char is '\u03b9' and string is "\\u03b9" ***
Andreas Gieriet 29-Sep-10 5:55am    
I assumed you want to convert unicode to utf8.
If you want to convert the above mentioned js string (including the ...+c+...) into plain text and then create an url from that, then you will fail, since the +c+ has a dynamic value which you most likely don't have at hand.
So, the real problem is that the data is dynamically evaluated in *js* and there is no point in converting this statically by any program into an url (no matter what encoding).
Or do I miss the point?
qyte64 29-Sep-10 6:14am    
No you are right in what you think i want to do.
I just cant find a proper and fast way to do it.
The link you provided assumes that you have an array of chars like '\uFFFF' but in my case i have a string "\\uFFFF" and i dont know how can i replace those 6chars inside the string with 1.
2nd i would like a way to do that without messing with the js code. (The main issue is that the "minimized" code is alot bigger in size than the actual code if it contains unicode characters that are returned as ?).
Second attempt:

C# characters are always unicode, not utf8.
You can convert the unicode string into an byte array which holds the utf8 bytes, and then store it by a binary stream.

The conversion is simlpe:

C#
byte[] utf8 = Encoding.UTF8.GetBytes("\u0392\u03bb\u03ad\u03c0\u03c9 Hello This is my text...");


But it is still not clear to me, what you want to achieve by utilizing the UrlDecode method for converting unicode characters. An url has constrinats that need to be encoded to form a legal pattern. That's what the url methods are for. If you want to reduce the size of some text by converting from unicode to utf8, then you must go over byte arrays as shown above.

Cheers

Andi
 
Share this answer
 
Comments
qyte64 29-Sep-10 10:35am    
the thing is that i dont have a string like
byte[] utf8 = Encoding.UTF8.GetBytes("???p? Hello This is my text...");

i currently kinda have:
byte[] utf8 = Encoding.UTF8.GetBytes("u0392 u03bb u03ad u03c0 u03c9 Hello This is my text...");
i.e the whole STRING ? and not the character.
Something like:
 string result = @"h="???p? "+c+"e????e?",b=true,a;document.getElementById("track2").onclick=d;";
the thing is that i dont have a string like
byte[] utf8 = Encoding.UTF8.GetBytes("\u0392\u03bb\u03ad\u03c0\u03c9 Hello This is my text...");

i currently kinda have:byte[] utf8 = Encoding.UTF8.GetBytes("u0392 u03bb u03ad u03c0 u03c9 Hello This is my text..."); i.e the whole STRING ? and not the character.
Something like:
string result = @"h=""\u0392\u03bb\u03ad\u03c0\u03c9 ""+c+""\u03b5\u03b9\u03ba\u03cc\u03bd\u03b5\u03c2"",b=true,a;document.getElementById(""track2"").onclick=d;";
notice on @
 
Share this answer
 
Comments
Andreas Gieriet 29-Sep-10 10:56am    
If I understand you correctly, you have a unicode sting that you want to convert to utf8. Take your string and pass it through the above mentioned method. UTF8 is a super set of ASCII-7bit. All your "readable" ASCII-7bit characters remain unchanged.
But: what you get as a result is a byte array that represents the utf8 encoding, not a string anymore (a string is an array of *unicode* characters in C#).
But I'm confused about your real problem at hand. Maybe someone else han help solving the issue.

Cheers

Andi

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900