Monday, September 22, 2008

How to validate e-mail?

A simple regex that can be used to validate e-mail address - ^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,6}$. This can be readily used with client side JavaScript. Following is an example:

function validateEmail(email) {
return (email.match(/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,6}$/ig) != null) ? true : false;
}

javax.mail.internet.InternetAddress can also be used to validate e-mail address. Following is an example:

try {
InteretAddress address = new InternetAddress(emailstring);
address.validate();
return true;
}
catch(AddressException e) {
return false;
}

Wednesday, September 17, 2008

Outlook: Saving Embedded Images

Downloading embedded attachments from an email in Outlook is not that easy. If we try to download them manually using "right click -> save as" method all images will be downloaded in BMP format with huge size regardless of the original format of the embedded image. This becomes a pain for downloading large number of images from an email.
 
Simple solution is a VBA macro. The complete code and method to integrate it with Outlook is available at Save Embedded Pictures in Their Original Format. In addition, we need to sign the macro to run it without any security warning - i.e., in case the security level is set to high. The related page Signing your own macros with SelfCert.exe describes how to sign the macro.

Monday, September 01, 2008

Façade Pattern

  • ­          A facade hides all the complexity of one or more classes
  • ­          Similar to Adapter pattern, it alters an interface, but for a different reason – to simplify the interface
  • ­          Façades and adapters may wrap multiple classes, but a façade's intent is to simplify, while an adapter's is to convert the interface to something different
  • ­          A façade not only simplifies an interface, it decouples a client from a subsystem of components
  • ­          It provides a simplified interface while still exposing the full functionality of the system to those who may need it
  • ­          Façades don't encapsulate the subsystem classes – they merely provide a simplified interface to their functionality
  • ­          The subsystem classes still remain available for direct use by clients that need to use more specific interfaces
  • ­          The façade can also add additional functionality in addition to making use of the subsystem
  • ­          It is not necessary that each subsystem should have only one façade, any number of façades can be created for a given subsystem
  • ­          Following is an example of a façade for a home theater system

public class HomeTheaterFacade {

      Amplifier amp;

      DvdPlayer dvd;

      Projector projector;

      Screen screen;

      Tuner tuner;

 

      public HomeTheaterFacade(Amplifier amp,

                              DvdPlayer dvd,

                              Projector projector,

                              Screen screen,

                              Tuner tuner) {

            this.amp = amp;

            this.dvd = dvd;

            this.projector = projector;

            this.screen = screen;

            this.tuner = tuner;

      }

 

      public void watchMovie() {

      }

 

      public void endMovie() {

      }

}

­           

  • ­          The Façade Pattern provides a unified interface to a set of interfaces in a subsystem
  • ­          Façade defines a higher-level interface that makes the subsystem easier to user
  • ­          To use the façade pattern, we create a class that simplifies and unifies a set of more complex classes that belong to some subsystem
  • ­          It allows us to avoid tight coupling between clients and subsystems­