Blog

How to force one or more metacharacters to be treated as ordinary characters in a Java Regular Expression (RegEx)

When using RegEx in Java you might face the need of treating one or more metacharacters as ordinary characters. As a reminder the metacharacters in a Java RegEx are:

([{^$|)?*+.

If you want to treat them as ordinary characters you have two options:

  1. Escape the metacharacter with a backslash,
  2. Enclose the whole string that contains metacharacters within Q and E

Q means: “quotes all characters until E”, while E ends the quotes.

Read more...

Creating an instance of the object used to make AJAX calls

Nowadays, AJAX is a ubiquitous technology in the IT world. When you need to create the object used to send asynchronous requests to a server, you might face the browser-difference problem. Here is a JavaScript function you could use to overcome this problem:

  
// The following function creates an XMLHttpRequest object
function createHttpRequest() {
  if (typeof XMLHttpRequest != "undefined") //NOT IE {
    return new XMLHttpRequest();
  }
  else if (window.ActiveXObject) // IE {
    var sVersions = [ "MSXML2.XMLHttp.5.0",
    "MSXML2.XMLHttp.4.0","MSXML2.XMLHttp.3.0",
    "MSXML2.XMLHttp","Microsoft.XMLHttp"
    ];

    //try to get an instance of the newer version.
    //If it is not available go down till the oldest one
    for (var i = 0; i < sVersions.length; i++) {
      try {
        var ret = new ActiveXObject(sVersions[i]);
        return ret;
      }
      catch (oException) {
        //Do nothing. Just go on trying with the older versions
      }
    }
  }
  //if it gets here then no version is available
  alert("XMLHttpRequest object could not be created.");
}

As you can see this function creates the correct instance of the XMLHttpRequest object. If the browser is not Internet Explorer then it just instantiates the XMLHttpRequest object, otherwise it tries to create the correct ActiveX object used by IE to represent XMLHttpRequest. In this case it tries to instatiates the object from the newer version going down till the oldest one.

Read more...

Using proguard obfuscator through the Wireless Toolkit

When you develop an application you might want to protect your code. A good way to accomplish this is using obfuscation. Proguard is a good open-source tool you can use for this purpose. To use it through the Wireless Toolkit (WTK), after downloading Proguard, you need to tell the WTK where it can find the obfuscator. You can do that by editing the file ktools.properties that you can find under %WTK%wtklibWindows, where %WTK% is the root directory of the Wireless Toolkit. Basically, you just need to add the two following lines to the aforementioned file:

Read more...

The Function class in JavaScript

Most programmers know how to define and use a function in JavaScript. For example the following function displays an alert containing the string passed in as a parameter:

 
function displayAlert(sText) {
  alert(sText);
}
// Then you call it this way
displayAlert("Hello World!");

How many developers, however, know that JavaScript functions are actually objects? Indeed, you can define the previous function using the Function class:

 
var displayAlert = new Function("sText", "alert(sText);");
//...and you call it the same way
displayAlert("Hello World!");

For those who didn’t figure it out, the syntax of the Function class is the following:

Read more...