Converting any String to Camel Case


Hey guys, this is the code for Converting any String to Camel Case String. I found this code extremely useful.







 
/**
  * Accepts any String & converts in into Camel Case String
  * 
  * @param text
  * @return
  */
 public String convertToCamelCase(String text) {
  String result = "", result1 = "";
  for (int i = 0; i < text.length(); i++) {
   String next = text.substring(i, i + 1);
   if (i == 0) {
    result += next.toUpperCase();
   } else {
    result += next.toLowerCase();
   }
  }
  String[] splited = result.split("\\s+");
  String[] splited1 = new String[splited.length];

  for (int i = 0; i < splited.length; i++) {
   int l = splited[i].length();
   result1 = "";
   for (int j = 0; j < splited[i].length(); j++) {
    String next = splited[i].substring(j, j + 1);

    if (j == 0) {
     result1 += next.toUpperCase();
    } else {
     result1 += next.toLowerCase();
    }
   }
   splited1[i] = result1;
  }
  result = "";
  for (int i = 0; i < splited1.length; i++) {
   result += " " + splited1[i];
  }
  return result;
 }


Have fun... !!

"Knowledge without justice ought to be called cunning rather than wisdom" - Plato

Using Google Web Service for "Google Search"


Sometimes, I wondered if I could search Google without using a browser. Then I found that Google has a web service which we can use for this purpose. Here is the sample client for Google Search Web Service.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import org.json.JSONObject;

public class SampleSearch {
  public static void main(String[] args) throws Exception {
      //q=javacodingtutorial.blogspot.com - This is the string you want to search
      //userip=USERS-IP-ADDRESS - This gives the IP address of  the user 
 String temp = "https://ajax.googleapis.com/ajax/services/search/web?v=1.0&"
   + "q=javacodingtutorial.blogspot.com&userip=USERS-IP-ADDRESS";
  URL url = new URL(temp);
  URLConnection connection = url.openConnection();
  String line;
  StringBuilder builder = new StringBuilder();
  BufferedReader reader = new BufferedReader(new InputStreamReader(
    connection.getInputStream()));
  System.out.println(connection.getURL());
  System.out.println();
  while ((line = reader.readLine()) != null) {
   builder.append(line);
  }
  JSONObject json = new JSONObject(builder.toString());
  System.out.println(json);
   }
}

This code will return Google Search Results in JSON objects.
Here is the sample output Download
But, there is a problem with this code if you want for than 4 results. Because this Webservice

Converting a list to a Array of String

These two lines will convert a list<String> to array of Strings - 
List<String> myList = new ArrayList<String>(); 
String listToArray[] = myList.toArray(new String[myList .size()]);
Feel free to share!!

How to Create plug in that reads Any Console in Eclipse

Many times I feel , I wish I could save the entire console from Eclipse in to a file. For past few days I was working on CVS (file repository system) & what I had to after committing hundreds files was that – I had to find out the file names, versions & previous versions from Eclipse CVS log & create a excel file with those data & send to my supervisor.
Trust me  this is really is a fun to create a plug-in using which my efforts were reduced from 1 or 2 hours to ONE SINGLE CLICK!!

So, to be precise, to create this, our first step will be
   To create a plug-in which  -
       a.Searches for a particular console( e.g. CVS)
       b. Reads data from that console.

Note: If you are not aware how to create a Eclipse Plugin using Java, please refer to this article - Create Plug in for Eclipse

Nested Class

Nested Class:
Java allows creating a class inside another class. These kinds of classes are called nested Class.
The simplest example for a nested class is:

 class OuterClass {  
 class ClassInside  
     {  
       //do something  
     }  
 }  

Open default mail application in your machine using Java

We can open default mail app in  our machine using Java Desktop.
Use the following code for that.
1:  import java.awt.Desktop;  
2:  import java.io.IOException;  
3:  import java.net.URI;  
4:  import java.net.URISyntaxException;  
5:  public class Email {  
6:       /**  
7:        * @param args  
8:        */  
9:         public static void main(String[] args) {  
10:            //// TODO Auto-generated method stub  
11:            Desktop desktop = Desktop.getDesktop();  
12:            String url = "";  
13:            URI mailTo;  
14:            try {  
15:                 url = "mailTo:test@gmail.com" + "?subject=" + "TEST%20SUBJECT" 
16:                           + "&body=" + "TEST%20BODY";  
17:                 mailTo = new URI(url);  
18:                 desktop.mail(mailTo);  
19:            } catch (URISyntaxException e) {  
20:                 e.printStackTrace();  
21:            } catch (IOException e) {  
22:                 e.printStackTrace();  
23:            }  
24:       }  
25:  }  

Creating a plug in for Eclipse

Creating a a simple plug in is one of the easiest tasks. Honestly before creating a plugin I, myself also thought that, Creating eclipse is a big deal!
So, here are the steps:
1. Make your eclipse plug in development friendly.
    a. Click on Eclipse help –> Install new software –> type - http://download.eclipse.org/releases/kepler.
    b. Install Eclipse Plug-in Development Environment

Interface vs. Abstract Class

 
With my personal experience I can say, this is question has always been a favourite question for the interviewers. But there are very few online resources where you can find a proper reply for this though I have already published this article before in another blog.
Let’s just dig into Interface & Abstract class first.
What is an Interface?
An interface is a named collection of method definitions without implementations.
Interface is actually a template in Java. It contains only the method declarations. The class that implements the interface contains the definitions for the abstract methods of the interface. Interface can also contain fields, but the fields should be static & final. On the other hand, the abstract methods declared in the interface are abstract & public.

Using parameters in URLs in Jersey Web Services

I have demonstrated a couple of web service examples in my previous posts. I hope you have noticed that the URLs I have used for calling web services is not practical. I mean, URLs now a days are not quite like them. Normally, we use URLs with parameters.
e.g. - http://localhost:8080/Test/resources/test/search?name=Tom
Well, in this case, we will have to change the server in the following way -
   1: // URL - http://localhost:8080/Test/resources/test/search?name=Tom

   2: @GET

   3: @Path("/search")

   4: public String getUserDetailsFromAddress(

   5:         @QueryParam("name") String name) {

   6:     return "Hello"+name;

   7: }
And, in the client side also, we need to do some changes as well:


JSONArray resultArray = new JSONArray(service
.path("resources/test/search").queryParam("name", "Tom")
.get(String.class));

Well, that should do the trick !!




Related post : Creating web service with maven jersey
Feel free to share !!!

“Knowledge which is acquired under compulsion obtains no hold on the mind.” - Plato

Installing Maven in Eclipse Juno or Higher

How to Install Maven in Eclipse

Click on Eclipse Help -> go to Eclipse Marketplace -> Search for Maven.
Install Maven Integration for Eclipse(Juno and newer) 1.4.



While working on Maven , recently I found a ridiculous issue. Maven was downloading my dependencies, they were on the class of the project. But, still the project was not using the dependencies from Maven & I was getting nasty ClassNotFoundException.

How to write a Web Service which returns JSON object & a client that reads it

We will need a couple of more jars in our classpath in order to use JSON.
They are - jackson-core-asl-1.8.0.jar, jackson-jaxrs-1.8.0.jar, jackson-mapper-asl-1.8.0.jar, jackson-xc-1.8.0.jar, jersey-json-1.8.jar, jersey-bundle-1.8.jar
Download them & add in the class path or add them in pom.xml as a dependency.

Parsing a XML in Java

What if a web service returns its result in form of XML? like this may be -


It's easy!!

This is the code snippet which is going to do the magic!!

A Jersey Web Service that returns XML

Now we are going to create a web service, which doesn't return String. It returns Objects or may be we can say XMLs(after this post, you will say that there is actually no difference in them)
Let's say, There is a Web service which accepts the user name in url (not as a parameter) & returns the details of the users with the same name in XML.
It is easily understandable that, we need to use a list as the number of users with the same name is not known.

A Simple Client for Webservice

Now I am gonna set up a client which will use the url  - http://localhost:8080/Test/resources/test/ABC & will receive the result String "Hello ABC".

For that, we will start with a standalone java project in Eclipse.

Creating RESTful Web Service with Maven, Jersey 1.8 and JBoss 7.1.1 Final in Eclipse

A few days back my Team Lead asked me to set up a demo web service with Jersey, Jboss 7.1.1 final and Maven in Eclipse Juno. I wasted 2 days flat to search the tutorial & set up only a hello world application in web service. So, I thought . may be this will be of some help who are going to use jersey with jboss in near future.
So, here is the tutorial with screenshots :
(I am assuming that everyone knows how to set up Maven in eclipse or may be you can refer to this tutorial - Installing Maven in Eclipse Juno or Higher