Thursday, March 22, 2012

Generate Javadoc Through Command Line


Here's a quick tip on generating Javadoc HTML files for your Java classes via command line.

javadoc  < MySourceFileName > .java -d <Destination Directory>

You need to make use of the 'javadoc' command.
You can specify multiple files separated by a white space or you can simply specify a directory name containing all your source files.

javadoc -sourcepath  < Source Directory >  -d  < Destination Directory>

Although I havent worked with eclipse, I believe it has an option where you can specify the javadoc command and then you would be able to easily generate documents via some menu option. Just FYI, if you work on windows, the javadoc command is located in the same directory as the java.exe command.

For more help and options on this command, check out the references

References

  1. http://www.mcs.csueastbay.edu/~billard/se/cs3340/ex7/javadoctutorial.html - Contains a list of other available options. Its short, sweet and cool.
  2. http://docs.oracle.com/javase/1.4.2/docs/tooldocs/windows/javadoc.html
  3. http://www.myeclipseide.com/PNphpBB2-viewtopic-t-5191.html



Happy Programming :)
Signing Off

Ryan

Wednesday, March 21, 2012

Get the sql query for a view in Oracle

Here is a tiny snippet to get the sql query for view in an Oracle database.



SELECT text

FROM all_views

WHERE view_name like '%VIEW_NAME%';




Happy Programming :)
Signing Off

Ryan

Monday, March 19, 2012

Finding the Primary Key and other constraints of a table in Oracle

Here's a quick query to help you find the columns involved as primary keys for a table in Oracle.

select * from user_cons_columns ucc, user_constraints uc

where uc.constraint_name=ucc.constraint_name

and uc.constraint_type='P'

and uc.table_name = 'EMP';


It seems that all the constraints for the tables in an oracle database are defined in the table user_constraints.

desc user_constraints;


select * from user_constraints;

To Find the distinct constraint types on a given table


select distinct constraint_type from user_constraints
where table_name='EMP';

When you execute the above query, you get a set of constraint codes that are used for the table in oracle.

R = Referential Constraint/Foreign Key constraint
C = Check Constraint
P = Primary Key
U = Unique Key

Now that you know what oracle constraints are defined on the table, you may also want to know the columns on which these constraints are actually defined. These come in handy mostly for determining the primary key, or the foreign key of a table.

For example, to determine all the columns in a table that have any constraints defined on them you can use


select * from user_cons_columns where table_name = 'EMP';


For additional information, you can take a look at the links mentioned in the references.

References

Happy Programming :)
Signing Off

Ryan

Sunday, March 18, 2012

Building Chrome Extensions From Scratch - Part 3

This is Part 3 of the series - Building Chrome Extensions From Scratch. In this part, we are gonna continue from where we left off in the previous part.

As a quick recap, in Part 2, we covered
  1. How to add a popup page to an extension.
  2. How to add a background page to an extension.
  3. How to communicate between extensions using the chrome extensions API.

In this part of the series, we will try to explore the other files that you are most likely to use when building a chrome extension.


Configuration
When you have an extension, you may not want it to be active for all the web pages. You may instead want it to be active only for a particular website, or a URL pattern to be precise. In order to achieve that, chrome extensions give you two options.

  1. Page Actions
  2. Browser Actions
You set up an extension as a page action, when you want the extension icon to be visible at the rightmost end of the address bar but only when it satisfies a particular condition. For instance, assume that you want the Mr0 icon to be visible only when we open a wikipedia page. A page action appears in the address bar of the browser instead of the toolbar. Lets try to make our extension a page action.

Here's what our new manifest file would look like

manifest.json
{
  "name": "Mr0",
  "version": "1.0",
  "background_page": "background.html",
  "permissions": ["tabs"], //We will talk about this later
  "page_action": {
    "default_icon": "images/icon19.jpg",
 "default_popup": "popup.html"
 }
}

In order to change the visibility of the extension icon, we would need find a means to be notified when the url in the tabs change, i.e. when the user selects a different tab. The listener would have to be placed in the background page because it is the only page in the extension that is alive in the browser all througout. Here's what you could write in the extension page.

background.html


The code is pretty simple. As you might have noticed in the previous snippet, we added a new key in the manifest file - "permissions". This is necessary if you want to grant your extension access to the chrome tabs javascript extension API. If you don't do that, your javascript code in the extension page for registering an handler for the onUpdated event will result in an error.

Now if you reload your extension and you open a wiki page in the tab, you will observe that the extension icon appears in the address bar whenever you open a wikipedia page. If you open any other page, the extension icon will automatically disappear.



Browser Actions
'Browser actions' is what we have been using all along in all the previous parts of the series. When you set up an extension as a browser action, the extension icon will be visible in the toolbar, no matter what tab or what page you are on. All throughout the series, we have kept the Mr0 extension to have a browser action instead of a page action because I wanted to see the icon in the browser toolbar. However you may choose to do it differently depending upon your requirement.

There is however a small difference between a browser action and a page action. If you have a browser action, you can add a badge to the extension. You cannot do the same for a page action. Wondering what a badge is? Well, a badge is nothing but tiny piece of text that you can display over your icon in the toolbar. You can do this in the background page for the extension by simply adding the following line of code

chrome.browserAction.setBadgeText({text:'Mr0'});





Content Scripts
Content scripts are the scripts that you specify in the manifest file, which get included on all the pages for which the extension is configured to run. For example, if you create a simple content script in your extension that logs a message such as 'hi' on the console of the web page, you will be able to see the message printed on each web page that the extension is configured to run on. Lets try it out with our Mr0 example.

In order to do this, all that you need to do is to create a simple javascript file and maybe call it myScript.js and place it directly under the root directory of your extension. We are just gonna add a simple line to it.



    console.log("Bingo");



Now, in your manifest.mf file, you need to mention this script as a content script. Here's how our manifest.mf would look like after the change





{

  "name": "Mr0",

  "version": "1.0",

  "background_page": "background.html",

  "permissions": ["tabs"],

  "content_scripts": [

    {

   "matches": ["http://*.wikipedia.org/*"],

      "js": ["myScript.js"]

    }

  ],

  "page_action": {

    "default_icon": "images/icon19.jpg",

 "default_popup": "popup.html"

 }

}





If you take a close look, you would have noticed that I had to use another property - "matches"  inside the content_scripts array so that the content script will be injected only into the pages that match the given url pattern. There can be any match pattern and you can refer to the documentation here for more details.

Now when you open any wikipedia page, and you press F12(Shortcut to open the chrome developer tools frame), check out the console and you will observe a cool message that says "Bingo". You can open any number of wikipedia pages, and the same message will be printed on the console of each of these pages.

What this actually indicates is that you can write custom javascript and execute it on each page independently. I used the word independently because your custom script will be executed such that although it will have access to the DOM structure of your page, it will not be able to access any other javascript that is executing on that page. This allows you to write your custom scripts without worrying about the clash of variable names.

Using the same feature, you can also include external libraries into your chrome extension. For example, you can add the jquery framework file into your extension. The version of jQuery that you make use of in your extension can be different from the version of jQuery being used by the client, and there wont be any clashes whatsoever.

You can add multiple content scripts separated by commas in the 'js' object. For example, the following piece of code will allow you to add the jQuery library into your extension and make it available for use in all your custom scripts.



"content_scripts": [
    {
   "matches": ["http://*.wikipedia.org/*"],
   "js": ["jquery.js","myScript.js"]
    }
  ]



This code assumes that you have a file called 'jquery.js' directly under the root directory of your chrome extension.

Now that you have a javascript framework and the ability to access the DOM of a page via your content script, you can do pretty much anything that is possible with the DOM of your page with ease.

There are several other configuration parameters that will come in handy when you create your chrome extensions. However the examples in this series should be sufficient to get you up and running from scratch in no time.

Happy Programming :)
Signing Off

Ryan