Sunday, December 16, 2012

Exploring CFX(Java) in ColdFusion-9(Part-2)

NOTE: Please take a look into this URL to start from beginning.

Before writing some real business logic in a CFX tag we need to know the working and structure of CFX.

If we look into the java code of “HelloColdFusion.java” file present in “C:\ColdFusion9\cfx\java\distrib\examples” for ColdFusion developer installation in Windows machine then we will find the following text.



The first line of the code contains “import com.allaire.cfx.* ”. This means we are using some features of one existing java package(com.allaire.cfx). The jar file for package is present in “cf_root/wwwroot/WEB-INF/lib” for server configuration and present in “cf_webapp_root/WEB-INF/lib” for J2EE configuration.

In second line code it is just creating one public class called “HelloColdFusion”, it will be the name of our class in CFX tag and this class imports/extends another java interface called “CustomTag” which is present in “com.allaire.cfx” package. We are extending/importing that interface to get the functionality of that interface in our defined class “HelloColdFusion”.

In the next step we are creating one overloaded java method “processRequest”. The interface “CustomTag” contains one method, processRequest, which is passed Request and Response objects that are then used to do the work of the tag.

So, for each custom tag we must write the method “processRequest” and which is the start point for CFX tag execution. When we will call the CFX tag we don’t need to bother about the Request and Response Object from the ColdFusion side. This two Object holds information about what are the parameters passed to the custom tags and their properties and what are the response parameters passed back to ColdFusion and their properties.

I have listed the functions what we can access by the Request Object and their details. For more details click on the function.
Request Object:

MethodDescription
attributeExistsChecks whether the attribute was passed to this tag.
debugChecks whether the tag contains the debug attribute.
getAttributeRetrieves the value of the passed attribute.
getAttributeListRetrieves a list of all attributes passed to the tag.
getIntAttributeRetrieves the value of the passed attribute as an integer.
getQueryRetrieves the query that was passed to this tag, if any.
getSettingRetrieves the value of a global custom tag setting.


Response Object:
Followings are the response Object methods. For details about each method click on that method.

MethodDescription
writeOutputs text to the calling page.
setVariableSets a variable in the calling page.
addQueryAdds a query to the calling page.
writeDebugOutputs text to the debug stream.


Back to our code “String strName = request.getAttribute( "NAME" ) ” in this line we are just getting the value of the attribute “NAME” which we will pass during executing the CFX tag. Then, in the next line “response.write( "Hello, " + strName ) ” we are just writing some text back to the client. It will be display in the browser if we will run the page which contains that CFX  through browser.

We can also return some variable to the calling page by writing the following code:
response.setVariable("Status", "true");

Now, you can compile your “.java” file to get “.class” file what we require.

In the next post we will see how we can implement some real business logic here and how we can pass one query object to custom tags and how we can return some query/Struct/variable from the custom tags.

Saturday, December 15, 2012

Google URL Shortening API by ColdFusion

I was trying to use Google URL shortening API by using ColdFusion. I have written two ColdFusion function which will take Short/Long URL as input parameter then it will return a structure which would have been returned by Google contains both Short/Long URL for the passed value in input parameter.


If you will  try with the following example:

Long URL: https://developers.google.com/url-shortener/
Short URL: http://goo.gl/pZbaF

We will get the following result:






Thursday, December 13, 2012

Add Space Before Capital Letter

In one of my recent project my requirement was to add a space before a capital letter like, if our word is "HiAmbika" then we have to convert it to "Hi Ambika".

Example: 
Input String: "RajaRamMohamRoy"
Output       :  "Raja Ram Mohan Roy"

I just used regular expression in ColdFusion to achieve this as follows.


We can also do other advance string matching functionality with back referencing. We will see that latter.


Wednesday, December 12, 2012

Extract Text from HTML Code in ColdFusion

Our Goal:
How we could extract text from HTML code using ColdFusion?

Description:
We will use some regular expression to achieve this.

 Use regular expression "<.*?>" to extract HTML text. This one will work fine until there is no JavaScript(JS) or CSS code is present inside the HTML. When any JS/CSS code is present in HTML then it can't omit those.Then use a second regular expression to remove JS and CSS code from HTML.

To replace JS and CSS code we have to use "<(script|style).*?</\1>".

So, if we will combine the two regular expression then we can get actual text from the HTML code which may contain some CSS and JS code.

The final regular expression will be "<(script|style).*?</\1>|<.*?>".

Example:
Our HTML code is:


So, the final ColdFusion code to extract text from above HTML would be follows:


After, all these steps we will get following text as the out put.

NOTE:
In the final regular expression "<(script|style).*?</\1>|<.*?>", we have used expression to remove any CSS/JS first then remove the HTML. As if we will change the order to "<.*?>|<(script|style).*?</\1>" then the CSS/JS code will be there in the final output. As the CSS/JS code will match with the first part and it will treat as normal HTML code.

Followers