viernes, 20 de febrero de 2009

Mysql UTF8 Encoding, and how to save Chinese/Japanese characters.

One of the requirements of the current application that I’m developing is to be able to save Unicode characters, but when we save the data to the db, the unicode characters were converted to a sequence of question symbols  (like these ??????) and that was really annoying.

Well we do some googling and found the answer here:

http://dev.mysql.com/doc/refman/5.0/en/charset-connection.html

Well to make the story short, if you want to store unicode characters be sure that the my.ini (mysql.cnf in linux) has the following lines in it.

collation_server=utf8_unicode_ci
character_set_server=utf8
character_set_client=utf8

I don’t know why, but It seems than the Mysql Connector for .Net was sending the queries statements  as non unicode, so, when the data arrives to the db it arrives with the wrong encoding and it stores the question symbols instead of the unicode characters.

If you don’t have control of the mysql Server, maybe you could run the equivalent sql statements before the queries, something like this :

SET NAMES UTF-8

I don’t know how to do it when you use NHibernate, (because the sql statements are sent by the NHibernate layer), but I read some posts that said it works.

Hopefully it can helps someone else.

Important note: those lines should be added after the default-character-set to work properly

miércoles, 28 de enero de 2009

Steps to deploy an ASP.NET 2.0 web application in Apache 2 using mod_mono 2.2 (In OpenSuse 11)

this is a quick list of the steps required to deploy .Net applications under Apache 2 and mod_mono in OpenSuse 11

  1. Compile your web application. You can do it on Linux using MonoDevelop or using Visual Studio 2008, we usually compile it on Linux just to be sure the website will no have problems to start.
  2. Create a folder for your application.

    In our case we created a folder named "WebApp " under this directory:

    /usr/share/mono/asp.net/apps/

    just because there were other demo applications that come with mono.

    Be sure to give this folder the right permissions. open a Console and log in as the root user (sudo -s) and move to the folder. Use the chmod command to change the permissions if necessary (chmod 777 ./ will enable all the permissions under this folder, you can try adding only the necessary access level, because 777 enable all of them)

  3. copy the contents of the compiled web application to the new folder created.
  4. navigate to the folder "/etc/apache2/conf.d" and create a text file named "WebApp.conf"
  5. copy this code inside the file
        Alias /WebApp /usr/share/mono/asp.net/apps/WebApp
        MonoApplicationsConfigFile WebApp /etc/xsp/2.0/applications-available/WebApp.webapp
        MonoServerPath WebApp "/usr/bin/mod-mono-server2"
        MonoSetEnv WebApp MONO_IOMAP=all
        <Location /WebApp>
         Allow from all
         Order allow,deny
         SetHandler mono
         MonoSetServerAlias WebApp
        </Location>
    
  6. As you can see in the line 2 we point to a WebApp file that should be located in the /etc/xsp/2.0/applications-available/, so navigate to that folder an create a WebApp.webapp text fileand put this code inside this file:
    <?xml version="1.0" ?>
       <web-application>
        <name>WebApp</name>
        <vpath>/WebApp</vpath>
        <path>/usr/share/mono/asp.net/apps/WebApp</path>
        <enabled>true</enabled>
       </web-application>
    
  7. Now is time to restart apache, so run this command: "apache2ctl restart".
  8. Now open your browser and navigate to http://localhost/WebApp/Default.aspx (if Default.aspx exist or maybe other page that you have in the site)
  9. If all went without errors now you can enjoy your new website running under Apache 2 using mod_mono 2.2

Notes: There a lot of things that could go, wrong, maybe you will need to first Install Apache 2, if you don't already have it in your Linux distro, or maybe the mod_mono 2.2. Those tasks are out of the scope of this post. The present list of steps were written here just to never forget them.if you need help to install apache 2 or mod_mono, or even MONO 2.2, then you will need to do some googling by yourself. sorry :(

martes, 27 de enero de 2009

MONO Formview/ObjectDataSourceView Bug?. Why some of the sections of my website Dissapear?

UPDATE : MONO Has Fixed this Bug in the 2.4 release, at least it seems to be fixed looking at the source code

But it seems to fail when the result is null because it returns a new Object collection with a null element wich seems to be wrong, so this patch could still apply under some circunstances

Well some days ago, while we’re developing our current project release using MONO 1.9.1, MONO 2.0 were released. It was including a lot of bugfixes and new features. So now we were able to compile the application in Linux without make any modification. We were really happy.

But our happiness was futile. Suddenly we realize that some portions of the Website were  not been rendered. We review the rendered code and realized that the formviews were not been rendered properly.

The Gridviews and other ASP.NET controls were rendering good, but, not the formviews. As we had to continue with the project release, we decide to turn back to MONO 1.9.1. At the end of the past Sprint, we decide to rertun and investigate why the the Formviews were not rendering.

First we were suspecting that it could be maybe a Viewstate Issue, (When we turned off the viewstate for the formview controls in MONO 1.9.1 they were not rendered also, turning the viewstate on solved the issue). But turning on or off the viewstate in MONO 2.0.x had no effect. The formview were still not rendered.

So, after some time of digging into the code, creating our custom Formview controls that inherited the ASP.NET Formview, overriding some methods and using the Response as Console for debugging (God! Mono needs a Debugger!) we found the issue.

So, here is:

The problem is located at the ExecuteSelect Method of the ObjectDataSourceView Class that is located in this path inside the MONO Source directory:

mono-2.2\mcs\class\System.Web\System.Web.UI.WebControls\ObjectDataSourceView.cs

(The MONO sources can be obtained from this location)

the method has this lines of code that could be wrong:

if (result is IEnumerable)
return (IEnumerable) result;
else
return new object[] {};

As you can see when you have methods that return only a single instance of an entity, and those methods are used as the SelectMethods of the ObjectDataSource Controls, the ExecuteSelect will consider they’re not IEnumerable instances so it will return an Empty collection, causing that the Formview Controls render nothing at the Aspx pages.

the correct code should be:

if (result is IEnumerable)
return (IEnumerable) result;
return (result != null)? new object [] { result } : new object[] {};

as you can see, now only if the result is null will be returned an empty collection otherwise a collection with exactly one element is returned.

So doing that modification, and recompiling the MONO Sources you could make your Formview Controls work properly again.

I reported the bug, but I’m not so sure if that is really a bug, (because maybe it is intended to work like that in the specs…) or if it will be solved soon by the MONO team.

Well, here is some useful information.

Details of the Bug, and some comments about it:

http://go-mono.com/forums/#nabble-td21435835

To re compile MONO Sources, after do the code change:

http://blog.palehorse.net/2008/11/06/my-adventures-installing-mono-20-on-centos-4-to-work-with-apache-via-mod_mono/

Good Luck, Hopely this could help someone else!

martes, 25 de noviembre de 2008

OpenSuse 11.0 plugin qt_pkg error

Sometime ago I downloaded the VM ware image of OPEN SUSE 11.0 that comes with MONO 2.0, but there were some annoying bugs with this version of MONO that was taking me crazy so as soon as the 2.0.1 version of MONO appeared, I wanted to upgrade to that version.

I launch the YAST2 application just to find that this application was not able to be started because it was missing the qt_pkg plugin. It was so annoying!!!!

After some research in google I finally found the solution in an Italian forum:

http://www.suseitalia.org/modules/newbb/viewtopic.php?topic_id=18739&forum=1&post_id=107616

So, basically to make the story short if you are having this problem too, try this, it worked for me!

  1. open a shell terminal
  2. type : sudo zypper in yast2-qt-pkg yast2-qt
  3. Answer yes to the download question.
  4. Wait and lauch Yast2! Now it should work normally!

Really thanks to “Anubis” for the solution to this really annoying error!

jueves, 2 de octubre de 2008

To Validate or not to Validate, XHTML, Dojo and Custom Attributes

Well, I must admit it. Now I really confused, I was validating the XHTML output of the pages of our current project, and because we used some custom attributes to create some widgets using a combination of Javascript and HTML (a custom attribute is an attribute that is not part of the HTML or XHTML specifications, like in <div id=”dialog” custom_type=”dialog”>, the custom attribute is “custom_type”), I realize the pages will fail the validation process, and as I was suspecting they did, they fail. But I remember that a big and popular Javascript toolkit uses custom attributes too, so, I think, mmm, “let’s look how they did to make their html output be valid XHTML”, and what I found has confused me a lot.

Dojo does not produce valid XHTML, they remove the Dojo namespace from their markup, so their pages don’t validate anymore (http://dojotoolkit.org/book/dojo-porting-guide-0-4-x-0-9/widgets/general). Why? I don’t really understand why?, I really want to know it.

Well, I think they’re tired of the war: “The Parser” posts this:

[...]

There's a big religious war about whether pages validating or not is important and I don't want to beat that dead horse anymore.

[...]

Lucky me, Dojo was not my favorite Javascript toolkit, and in my current project I am not using it, I am using JQuery (Dojo fans, sorry, if you need to make your code valid, as far as I know, you will have to make changes to the parser!!).

But, what can I do now? Do I must forget about Validation issues?

Well I was looking deeper and I found a really nice post from Jonh Resig http://ejohn.org/blog/html-5-data-attributes/

It seems that HTML 5 (http://www.w3.org/html/wg/html5) includes some cool features!, like the data-attribute to declare custom attributes, so in the future, I will not have to deal with custom DTDs or custom XHTML Namespaces! (Namespaces are not used to validate XHTML, Thanks for the comment zcorpan!).

But, now I’m more confused! What should I do?.

Should I still try to make the pages validate or not?

Well I think I could use XHTML Namespaces to make it validate.(I will use the approach of the data-attribute in order to keep compatible with the HTML5 specification) So I will have to do some changes now. A little bit more of work, but at the end of the day I will feel better with myself. But seriously I’m already confused, because the future is not as clear as I was thinking... Ok, don’t you believe me? Take a look to these good articles:

http://xhtml.com/en/future/x-html-5-versus-xhtml-2/#x5

http://www.robertnyman.com/2007/02/05/html-5-or-xhtml-2/

Goodbye.

---

Take a look to this post too:

http://marcgrabanski.com/article/5-tips-for-better-jquery-code

miércoles, 1 de octubre de 2008

Regular Expresions

RegularExpressionLogo

Hi, again…

Regular expressions are extremely useful, you can think on them like “wildcards on steroids” (I like that phrase I found it here: http://www.regular-expressions.info/index.html), they save you a lot of time when you do a search inside a string and want to return the matches based on a pattern or when you want to do replacements based on them. I don’t really remember when I started to use Regular Expressions but now I use them quite often. So here are some tips that I’ve found so far, in Javascript:

(If you already know a lot about Regular Expressions, please go directly to my seven tip, maybe you can find it useful).

1. You can match a specific character using its hexadecimal index in the character set: e.g.: \xA9 matches the copyright symbol in the Latin-1 character set.

This one is really useful when you’re working with Unicode, there are several considerations if you plan to work with Unicode, take in mind that Unicode may encode a single grapheme as two different characters, e.g.: “à” could be “a” and “`” so many regular expressions could fail mysteriously, and it could be hard to find why.

2. Use shorthand characters when possible, e.g. : use \d instead of [0-9] and \w to match any word character (alphanumeric characters plus underscore). The regular expression will be more readable.

3. Take in mind that “.” matches (almost) any character. Well this will work different in IE and in Firefox, in IE the “.” Matches even line breaks, and in Firefox line breaks won’t be matched. This difference is really important to remember.

4. Use capture groups if you plan to do something with the results, you can define a group using “(” and “)” like in (q)(u) (this will match q followed by and u and you will have 2 capturing group, to access each group use $1 and $2 for each group. Please note that the groups index begin in “1”, the group 0 contains the entire match not a specific group.

5. Lazy Repetition feature is useful but it can be expensive, like in <.+?> that matches any html tag (invalid tags inclusive!), if you now the exactly set of characters expected is better to use <[^<>]+>, it will perform better that the “.” Character.

6. You can create a regular expression in Javascript in two ways:

a. Using literal notation: /(q)(?=u)(u)/g

b. Using: var regx = new RegExp(“(q)(?=u)(u)”,g);

Take in mind that for the second way you need to be careful when you use the special characters like “\”.

E.g.:

/((ht|f)tp(s?):\/\/)?((W){3}\.)?(\w)+((\.)(([a-zA-Z])+/?)?)+/g

Var reg = new RegExp(“((ht|f)tp(s?): / /)?((W){3}\\.)?(\w)+((\\.)(([a-zA-Z])+/?)?)+”,g)

Please note that the “\.” in the literal notation is translated to “\\.” when you use the constructor notation.

7. Make yourself a big favor (a really big favor, even if you’re a Regular Expression Guru like my friend Paolo, he is really good with Regular Expressions!), and consider use this free tool:

http://www.gskinner.com/RegExr/

gskinnertool

This online tool is as simply as impressive, it is really complete and the regular expressions created there work quite well in Javascript, you can construct your regular expressions and test them on the fly! (this tool comes to you as a courtesy of my good friend Hans who recommended me to use it, and now I simply can’t imagine work with Regular Expressions without it)

There is also a Desktop version located here: http://www.gskinner.com/RegExr/desktop/ (it needs Adobe Air installed)

or use this other tool:

http://osteele.com/tools/rework/

rework-tool

is really impresive too, and had a lot of features and it is programmed in javascript , but i don’t know why I prefer the first one :P, maybe because it looks nicer.

So that’s all so far.

miércoles, 3 de septiembre de 2008

Google Chrome – just another browser? Or "the" one?

chrome
Well, I must admit, it’s still a young project but it looks really nice. The project was keep in secret for more than one year. I cannot wait to try it, so I downloaded it and installed it. It’s a minimalist browser, it has tabs, and a clean UI.
It seems that it is based on Mozilla and Webkit, and it’s really fast when it comes to javascript performance, it is really fast.
They have created an entirely new browser from the scratch. Why? Well they have collected an entire set of reasons into this clear and funny comic style “googlebook”: http://www.google.com/googlebooks/chrome/
What’s new with this browser?
  1. It’s free, open source
  2. It has support for standards.
  3. It’s designed for the today’s demanding web applications
  4. It has a faster Javascript VM (Yes! a Virtual Machine, so it compiles Javascript instead of just interpret it!!, impressive). Developed by the V8 Team
  5. It provides each tab with his own isolated environment called the Sandbox, so the risk from attacks and from malware on the web are minimized (not completed eliminated, that’s really hard to accomplish even for google!!)
  6. Improved Garbage Collection. (No more memory leaks like in IE?, well, I have to test it!!!)
  7. Nice UI, simple and consistent.
  8. A simpler popup blocker.
  9. It has Google Gears included.
So what’s bad with this browser?
1. I can’t find easily my favorites. (well to be honest, my friend Paolo didn’t find them)
2. They say that each tab runs on an entirely separated process, so, having 10 tabs open, we have then 10 process with all the memory and the resources they need to be opened. Mmm, they claim that his memory management does a great job, and can cope with the challenge… but, let’s try it and look what happens.
3. It has a kind of firebug addon integrated, but, It’s not dockable like the firebug plugin for Mozilla, so it’s a little bit hard to work with it.
So, to resume all in just one sentence…
Google Chrome is not only a Web Browser. It’s an application platform to run Web Applications.
Nice, isn’t it? Well, IMHO, the competence is good for the Web and for us.