Pages

How to create a separate folder for resources | Netbeans

Well, there are different IDE's out there in the internet and all of them have different features. They provide some unique awesome features whereas even the best of them could be missing some basic stuffs.

Lets get to the point. When you create a Java Application Project in Netbeans, it creates the SourcePackages folder where you are supposed to create your packages and java classes. Now what if you require other stuffs like images, bitmaps, configuration files, xml's etc. you could create a package and put these stuffs there, but I personally don't like it. You could also put your resources in a different part of the disk than your project folder and reference it, but it will only work until the program is in your computer and it won't be packaged in the jar file. Now I researched on this problem and found a solution. It still may not be the best solution but I like it better than I have found yet.

I will point out the steps.

First create a project

Creating a project is easy and so I am not going to bother with the details. The folder structure that the netbeans create is like this.
I have deleted the default package, and created another one called tutorials, as you could see in the above picture.

Now add a Class

Click on the package tutorial, and add a class called TreeTest. Add the below code.

package tutorials;

/**
 *
 * @author Rajan Prasad Upadhyay
 */
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;

/**
 *
 * @author Rajan Prasad Upadhyay
 */
public class TreeTest {

    String name = "name";

    public static void main(String[] args) {
        javax.swing.JFrame topFrame = new JFrame();
        javax.swing.JPanel topPanel = new JPanel();
        javax.swing.JScrollPane leftScrollPane = new JScrollPane();
        javax.swing.JTree leftTree = new JTree();
        
        //for the cell renderer
        leftTree.setCellRenderer(new CustomCellRenderer());

        topFrame.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        //layouts
        topFrame.setLayout(new BorderLayout());
        topPanel.setLayout(new BorderLayout());

        // addition of components
//        topPanel.setMaximumSize(new Dimension(Integer.MAX_VALUE,Integer.MAX_VALUE));
        topPanel.add(leftScrollPane);

        leftScrollPane.getViewport().add(leftTree);

        addExperimentEvents(leftTree);

        topFrame.setSize(600, 500);
        topFrame.setContentPane(topPanel);
        topFrame.setVisible(true);
    }

    public static void addExperimentEvents(JTree tree) {
        MouseAdapter ma = new MouseAdapter() {
            private void myPopupEvent(MouseEvent e) {
                final int x = e.getX();
                final int y = e.getY();
                final JTree tree = (JTree) e.getSource();
                TreePath path = tree.getPathForLocation(x, y);
                if (path == null) {
                    return;
                }

                tree.setSelectionPath(path);

                Object obj = (Object) path.getLastPathComponent();

                String label = "Add:";// + obj.toString();
                JPopupMenu popup = new JPopupMenu("Menu");

                JMenu sectionsMenu = new JMenu("Sections");
                JMenuItem menuItem1 = new JMenuItem("Item1");
                sectionsMenu.add(menuItem1);
                JMenuItem menuItem2 = new JMenuItem("Item2");
                sectionsMenu.add(menuItem2);

                JMenuItem item = new JMenuItem(label);
                item.addActionListener(new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent ae) {
                        System.out.println("Editing the tree");
                        DefaultMutableTreeNode selNode = (DefaultMutableTreeNode) tree
                                .getLastSelectedPathComponent();
                        TreeNode root = (TreeNode) tree.getModel().getRoot();
                        DefaultTreeModel model = (DefaultTreeModel) tree.getModel();//new DefaultTreeModel(root);

                        if (selNode != null) {
                            DefaultMutableTreeNode newNode = new DefaultMutableTreeNode("New Node" + (selNode.getChildCount() + 1));
                            model.insertNodeInto(newNode, selNode, selNode.getChildCount());//
                            //model.insertNodeInto(newNode, (MutableTreeNode) root, root.getChildCount());
                            //tree.expandPath(new TreePath(model.getPathToRoot(newNode.getParent())));

                            TreeNode[] nodes = model.getPathToRoot(newNode);
                            TreePath path = new TreePath(nodes);

                            System.out.println(selNode.getChildCount() + ", " + path);
                            tree.scrollPathToVisible(path);
                            tree.setSelectionPath(path);
                            tree.startEditingAtPath(path);
                            //model.reload((TreeNode) model.getRoot());
                        } else {
                            System.out.println("selNode is null");
                        }
                    }
                });
                //addHoverAction(item);//
                //item.add(new JMenuItem("page"));

                popup.add(item);
                JMenuItem delete = new JMenuItem("Delete");
                delete.addActionListener(new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent ae) {
                        
                        TreePath path = tree.getPathForLocation(x, y);
                        final DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
                        DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) node.getParent();

                        int childNodeIndex = parentNode.getIndex(node);

                        System.out.println("Index of node deleted, wrt its parent is : " + childNodeIndex);

                        DefaultTreeModel model = (DefaultTreeModel) tree.getModel();

                        if (node.getParent() != null) {
                            model.removeNodeFromParent(node);
                        }
                    }
                });
                popup.add(delete);
                popup.add(new JMenuItem("Move: "));
                popup.add(sectionsMenu);
                popup.show(tree, x, y);
            }

            public void mousePressed(MouseEvent e) {
                if (e.isPopupTrigger()) {
                    myPopupEvent(e);
                }
            }

            public void mouseReleased(MouseEvent e) {
                if (e.isPopupTrigger()) {
                    myPopupEvent(e);
                }
            }
        };
        tree.addMouseListener(ma);
    }

    // <editor-fold defaultstate="collapsed" desc="hoverAction">
    public static void addHoverAction(final JComponent com) {

        MouseAdapter adapter = new MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                System.out.println("mouse Entered: ");
                if (true) {
                    JPopupMenu pop = new JPopupMenu();
                    pop.add(new JMenuItem("Node"));

                    pop.show(com.getParent(), com.getWidth(), com.getHeight());
                }
            }
        };
        com.addMouseListener(adapter);
    }
    // </editor-fold>

    //static methods
    
        
    
}

class CustomCellRenderer extends DefaultTreeCellRenderer{
    Icon cup;
    Icon triangle;
    Icon page;
    
    public CustomCellRenderer(){
        cup = createImageIcon("/image/cup.GIF");
        triangle = createImageIcon("/image/middle.GIF");
        page = createImageIcon("/image/cup.GIF");
    }
    
    public Component getTreeCellRendererComponent(
                JTree tree,
                Object value,
                boolean sel,
                boolean expanded,
                boolean leaf,
                int row,
                boolean hasFocus) {

            super.getTreeCellRendererComponent(
                    tree, value, sel,
                    expanded, leaf, row,
                    hasFocus);
            
            if(value.toString().equalsIgnoreCase("colors")){
                setIcon(cup);
            }else if(value.toString().equalsIgnoreCase("sports")){
                setIcon(triangle);
            }else if (value.toString().equalsIgnoreCase("food")){
                setIcon(page);
            }

            return this;
        }
    protected static  ImageIcon createImageIcon(String path) {
        java.net.URL imgURL = TreeTest.class.getResource(path);
//        System.out.println(CustomCellRenderer.class.g);
        System.out.println("The image uri is : " + imgURL + " " + path);
        if (imgURL != null) {
            System.out.println("returning new image icon.");
            return new ImageIcon(imgURL);
        } else {
            System.err.println("Couldn't find file: " + path);
            return null;
        }
    }

}

Now you could see that in the method CustomCellRenderer(), the code is trying to load some image files. Until now It could not find it because we have not added it. Create a package called image and put two GIF files in it called cup.GIF and middle.GIF.

Create a resource folder in eclipse

Right click on the project and click Properties. On the section "Source Package Folders" click Add Folder and create a resources folder in the same location as the src folder is located. The resources will be seen along with src in the panel. Now click ok. Go to the projection location in the file explorer and create a physical folder named resources in the path. Also create a folder called image inside the resources folder and put two small GIF files inside it. You could just cut and paste the image file from your src directory to this directory. Now see the netbeans project explorer. The project must be looking like this.

Now run the project. The gif files you have added will appear in the icon of the tree nodes.
Thats all, thanks for watching my tutorial. Keep coding. Enjoy!!!

Demonstration of JTree component | java


After my last code example, I started digging deep into the JTree component of java. I really feel like writing this almost complete functional code of JTree for my further reference.

package tutorials;

/**
 *
 * @author Rajan Prasad Upadhyay
 */
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.MutableTreeNode;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;

/**
 *
 * @author Rajan Prasad Upadhyay
 */
public class TreeTest {

    String name = "name";

    public static void main(String[] args) {
        javax.swing.JFrame topFrame = new JFrame();
        javax.swing.JPanel topPanel = new JPanel();
        javax.swing.JScrollPane leftScrollPane = new JScrollPane();
        javax.swing.JTree leftTree = new JTree();

        topFrame.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        //layouts
        topFrame.setLayout(new BorderLayout());
        topPanel.setLayout(new BorderLayout());

        // addition of components
//        topPanel.setMaximumSize(new Dimension(Integer.MAX_VALUE,Integer.MAX_VALUE));
        topPanel.add(leftScrollPane);

        leftScrollPane.getViewport().add(leftTree);

        addExperimentEvents(leftTree);

        topFrame.setSize(600, 500);
        topFrame.setContentPane(topPanel);
        topFrame.setVisible(true);
    }

    public static void addExperimentEvents(JTree tree) {
        MouseAdapter ma = new MouseAdapter() {
            private void myPopupEvent(MouseEvent e) {
                final int x = e.getX();
                final int y = e.getY();
                final JTree tree = (JTree) e.getSource();
                TreePath path = tree.getPathForLocation(x, y);
                if (path == null) {
                    return;
                }

                tree.setSelectionPath(path);

                Object obj = (Object) path.getLastPathComponent();

                String label = "Add:";// + obj.toString();
                JPopupMenu popup = new JPopupMenu("Menu");

                JMenu sectionsMenu = new JMenu("Sections");
                JMenuItem menuItem1 = new JMenuItem("Item1");
                sectionsMenu.add(menuItem1);
                JMenuItem menuItem2 = new JMenuItem("Item2");
                sectionsMenu.add(menuItem2);

                JMenuItem item = new JMenuItem(label);
                item.addActionListener(new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent ae) {
                        System.out.println("Editing the tree");
                        DefaultMutableTreeNode selNode = (DefaultMutableTreeNode) tree
                                .getLastSelectedPathComponent();
                        TreeNode root = (TreeNode) tree.getModel().getRoot();
                        DefaultTreeModel model = (DefaultTreeModel) tree.getModel();//new DefaultTreeModel(root);

                        if (selNode != null) {
                            DefaultMutableTreeNode newNode = new DefaultMutableTreeNode("New Node" + (selNode.getChildCount() + 1));
                            model.insertNodeInto(newNode, selNode, selNode.getChildCount());//
                            //model.insertNodeInto(newNode, (MutableTreeNode) root, root.getChildCount());
                            //tree.expandPath(new TreePath(model.getPathToRoot(newNode.getParent())));

                            TreeNode[] nodes = model.getPathToRoot(newNode);
                            TreePath path = new TreePath(nodes);

                            System.out.println(selNode.getChildCount() + ", " + path);
                            tree.scrollPathToVisible(path);
                            tree.setSelectionPath(path);
                            tree.startEditingAtPath(path);
                            //model.reload((TreeNode) model.getRoot());
                        } else {
                            System.out.println("selNode is null");
                        }
                    }
                });
                //addHoverAction(item);//
                //item.add(new JMenuItem("page"));

                popup.add(item);
                JMenuItem delete = new JMenuItem("Delete");
                delete.addActionListener(new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent ae) {
                        
                        TreePath path = tree.getPathForLocation(x, y);
                        final DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
                        DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) node.getParent();

                        int childNodeIndex = parentNode.getIndex(node);

                        System.out.println("Index of node deleted, wrt its parent is : " + childNodeIndex);

                        DefaultTreeModel model = (DefaultTreeModel) tree.getModel();

                        if (node.getParent() != null) {
                            model.removeNodeFromParent(node);
                        }
                    }
                });
                popup.add(delete);
                popup.add(new JMenuItem("Move: "));
                popup.add(sectionsMenu);
                popup.show(tree, x, y);
            }

            public void mousePressed(MouseEvent e) {
                if (e.isPopupTrigger()) {
                    myPopupEvent(e);
                }
            }

            public void mouseReleased(MouseEvent e) {
                if (e.isPopupTrigger()) {
                    myPopupEvent(e);
                }
            }
        };
        tree.addMouseListener(ma);
    }

    // 
    public static void addHoverAction(final JComponent com) {

        MouseAdapter adapter = new MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                System.out.println("mouse Entered: ");
                if (true) {
                    JPopupMenu pop = new JPopupMenu();
                    pop.add(new JMenuItem("Node"));

                    pop.show(com.getParent(), com.getWidth(), com.getHeight());
                }
            }
        };
        com.addMouseListener(adapter);
    }
    // 
}

Find if a port is occupied in a server | windows

I have a problem in a server. I have to run a tomcat instant and I was not sure on which port should I configure it to listen, since most of the applications use 8080 as their default port. Now I researched on it and found it quiet easy.
The following command will give you whether the particular port is pre-occupied in that machine or not.
netstat -a -n -0 | find "8080"
Now the theoretical details.
Netstat is a command for gathering network statistics. It displays very detailed information about communication of your computer with other devices in the network.

-a is the switch for displaying active TCP connections as well as UDP connections.
-o is used for preventing the command to display host names etc.

For more details take reference of the following site.

Stupid bash codes

I was trying some simple linux codes , months ago. I was logged into my ubuntu and I found some of my old works. I thought, better post it on my blog as a reference than to keep it useless.

First example
 
#!/bin/bash
echo "This is my kind of first script on this computer"
echo "The number of arguments is $#"

Just a couple of hints
  • # in the beginning of the statement is used as a comment
  • $# is a expression which returns number of arguments

For Loop Example

#!/bin/bash
for planet in mercury venus earth mars jupiter saturn uranus neptune pluto
do
 echo  "$planet";
# echo "another planet"
done;

Now this one is a little advanced. Write below text in a file named file1.txt
123a45
my name is rajan
 The cat is actually a cat
And the actual bash script which is going to read the above file and give some output

#!/bin/bash

#getting the character count
a=`cat file1.txt | wc -c`

#delete the alphabets and count the remaining characters
#b=echo "$a" | tr -d [:alpha:] | wc -c
b=`cat file1.txt | tr -d [:alpha:] | wc -c`


if [[ $a -eq $b ]]
then
 echo " The file contains no alphabets"
else
 echo " The file contains alphabets"
fi


What the script does is, it counts the number of character in variable a. Then it deletes the alpha characters, count the remaining characters and assign the count to b. Now if originally there were no character, both a and b would be same in which case it would display "The file contains no alphabets" as you could see in the code.

The tr is a special text manipulation command. There are other powerful text manipulation commands like awk, sed etc.

Another simple example.
#!/bin/bash

#dont use space between variable_name and = and value
count=99
hundred=100


if [ $count -eq 100 ]
then
  echo "Count is 100"
else
  echo "Count is not 100"
fi

#use operator [[ rather than [
if [[ $count -lt $hundred ]]
then
 echo "count less than hundred"
else
 echo "count is greater than hundred"
fi

Really cool Gui | Java

In todays example, I am gonna make a simple but awesome gui in java. I will use the swing components.
I have worked in a few Desktop application, and I remember the first days of struggle. How to make things resizable, cool etc.
This tutorial is actually for my reference. It does not do anything, just provides a layout to begin. It is more easier to do GUI stuffs in java using the Netbeans IDE.

A snapshot of the GUI in Java.
The code that made it possible is here.

import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTree;

/**
 *
 * @author Rajan Prasad Upadhyay
 */
public class FlexibleGui {

    public static void main(String[] args) {
        javax.swing.JFrame topFrame = new JFrame();
        javax.swing.JPanel topPanel = new JPanel();
        javax.swing.JSplitPane splitPane = new JSplitPane();
        javax.swing.JPanel  leftPanel   = new JPanel();
        javax.swing.JPanel  rightPanel   = new JPanel();
        javax.swing.JScrollPane leftScrollPane = new JScrollPane();
        javax.swing.JScrollPane rightScrollPane = new JScrollPane();
        javax.swing.JTree  leftTree = new JTree();
        
        //layouts
        // with border layout, components take maximum size they could get.
        topFrame.setLayout(new BorderLayout());
        topPanel.setLayout(new BorderLayout());
        leftPanel.setLayout(new BorderLayout());
        rightPanel.setLayout(new BorderLayout());
        
        // addition of components
//        topPanel.setMaximumSize(new Dimension(Integer.MAX_VALUE,Integer.MAX_VALUE));
        topPanel.add(splitPane);
        
        splitPane.setRightComponent(rightPanel);
        splitPane.setLeftComponent(leftPanel);
        //setting the default location of the middle border
        splitPane.setDividerLocation(200);
        
        //add scroll panes
        rightPanel.add(rightScrollPane);
        leftPanel.add(leftScrollPane);
        
        leftScrollPane.getViewport().add(leftTree);
        
        topFrame.setSize(600, 500);
        topFrame.setContentPane(topPanel);
        topFrame.setVisible(true);
    }
}

In later posts, I may be posting about the user interaction, events handeling, changing the icons etc. Depends on my mood.

Java Serialize and DeSerialize | Json

I was trying to persist a complex object in java and re-use it later after re-running the program. Java supports traditional Serializable interface which is also cool. But it was rather looking for a better way to do it and I found this awesone API called json-io. You could also download the jar file from its maven repository, include it in the classpath and use it. I am writing a simple code to demonstrate it for my reference but you could always go to its site and learn more.

Well, I should mention this. I was trying to implement a desktop application which has some settings. The application should save its settings and perform according to them. The settings should persist, the next time the application is started. I could simply create a map, for the options, update the maps in run time and save them in a file on exit and reload from the same file.

import com.cedarsoftware.util.io.JsonReader;
import com.cedarsoftware.util.io.JsonWriter;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Rajan Prasad Upadhyay
 */
public class Dog {
    Barks bark = new Barks();
    String name ;

    public Barks getBark() {
        return bark;
    }

    public void setBark(Barks bark) {
        this.bark = bark;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    
    public static void main(String[] args) {
        
        String json = "";
        
        
        
        try {
            Dog dog = new Dog();
            dog.setName("Tommy");
            json = JsonWriter.objectToJson(dog);
            System.out.println(json);
        } catch (IOException ex) {
            Logger.getLogger(Dog.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        try{
            String sjon = json;
            Dog dg = (Dog) JsonReader.jsonToJava(sjon);
            System.out.println(dg.getName() + " sounds like "+ dg.getBark().getSound());
            
        }catch (Exception ex) {
            Logger.getLogger(Dog.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }
}

class Barks{
    String sound = "woo woo";

    public String getSound() {
        return sound;
    }

    public void setSound(String sound) {
        this.sound = sound;
    }
    
    
}


How to read text content from a url | java

I was involved in this project where the task was to grab the contents from a url, and then to do other stuffs on that content. Other things will be discussed later for this post is dedicated to grabbing the text content of the URL. Initially, we implemented our own logic to do this task, but later I found this amazing API, of which I am going to give an introduction.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Rajan Prasad Upadhyay
 */
public class URLReader {
    public static String getUrlContent(String url){
        StringBuilder builder = new StringBuilder();
        try {
            URL uri = new URL(url);
            BufferedReader in = new BufferedReader(new InputStreamReader(uri.openStream()));
            String line;
            while((line = in.readLine()) != null){
                builder.append(line);
                System.out.println(line);
            }
            in.close();
        } catch (MalformedURLException ex) {
            Logger.getLogger(URLReader.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(URLReader.class.getName()).log(Level.SEVERE, null, ex);
        }
        return builder.toString();
    }
    
    public static String htmlToTextFilter1(String rawHtml){
        
        String text = rawHtml.replaceAll("\\<.*?>","");
        
        return text;
    }
    
    public static void main(String[] args) {
        String rawHtml = getUrlContent("http://rajanpupa.blogspot.com/");
        
        System.out.println(htmlToTextFilter1(rawHtml));
    }
}


There are better ways to grab texts from the web. Rather then implementing your own code/logic, you could simply use standard API's specifically designed for these kind of purpose. I used opensource api JSoup for the same task and its performance and accuracy is absolutely amazing.

Download the JSoup library from here, and import it in your project.
Here is a snapshot of the code.
     try {
            Document doc = Jsoup.connect("http://rajanpupa.blogspot.com").get();
            String title = doc.title();
            System.out.println(title);
            System.out.println(doc.text());
        } catch (IOException ex) {
            Logger.getLogger(JSourURLReader.class.getName()).log(Level.SEVERE, null, ex);
        }

Efficient way to build strings | Java

In java, the string is a immutable data type. Immutable is something that could not be modified once it is created. There are immutable data objects in other languages also, eg tuples in python.

Now lets get to the point.
I used to do the straight way to concat strings in java with the += operator. Even though I have to build a string using long algorithms. I even used the same way for my document parser in the previous, which I may soon re-implement. The point is that, every time I add another character to a string, the original string is destroyed, and an entirely string is created with the character added in the original string. Now the overhead of object creation may not be huge for a few string concatenation but say if our algorithm is such that we are performing 50 or 100 or thousands of strings additions or subtractions, then it has a huge impact. The solution is to use the StringBuilder object.

What this class does is, it provide append, delete kind of methods. It does not create new string objects on every append or delete and thus the object creation overhead is removed. It creates an string object only when called the toString() method.

Here is an example.
        StringBuilder sb = new StringBuilder();
        sb.append("rajana");
        sb.deleteCharAt(sb.length()-1);
        sb.append(" upadhyay");
        System.out.println(sb.toString());

Text Analysis | Document parser

Long back, I was involved in a project which deals with Text Analysis. It was a preety interesting project which I realized later, when the project was actually completed. Anyways, recently I was surfing odesk to see if there is something I could make a few bucks easily. I found an assignment project where the basic requirement was to be able to parse a text file, and generate a report of number of unique words, their counts etc. I did not get hired for the job, buy I decided to post my work here.

Backthen, the problem was actually to group news article. There are many sources of news in the internet. Our idea was to channel these pieces of articles into our application, run some analyzing and scoring algorithms, make a cluster of related news, and to present the related news in a more suggestive way according to the users interest.

package testapplications;

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.StringTokenizer;
import java.util.Vector;

/**
 *
 * @author Rajan Prasad Upadhyay
 */
/*
 * This class holds one piece of text document. It splits the text into 
 * words along with their count, so that distinct words could be easily found 
 * out. used for indexing, and calculating similarity between documents in collection
 * class
 */
public class Document {

    /*
     * This class is made as a data-structure for holding the words along with their
     * counts in the text when the text is splitted into words in vector.
     */
    private class StringCounter {

        public String word = "";
        public int count = 1;

        public StringCounter(String str) {
            word = str;
        }

        public void increaseCount() {
            count++;
        }

    }

    String text;
    public Vector<String> words = new Vector();
    public Vector<StringCounter> count = new Vector();

    public void sentenceSplitter(String speech) {
        /*default found in java, not used here*/
        /*This will break the text into tokens, words*/
        //String speech = "My name is Rajan Prasad Upadhyay.I am twenty-two years old.";
        StringTokenizer st = new StringTokenizer(speech, ".");
        System.out.println(st.countTokens());
        //st.nextToken(".");
        while (st.hasMoreTokens()) {
            System.out.println(st.nextToken());
        }
    }

    public Document(List<String> lines) {
        text = "";
        for (String str : lines) {
            text += str;
        }
        parse();
        //System.out.println(text);
    }

    public Document(String text) {
        this.text = text;
        parse();
    }

    public Document() {
    }//never use like this
    /*internal operations methods not needed outside*/

    private boolean isValid(char c) {
        //
        if (c == '.' || c == '\'' || c == ',' || c == '?' || c == '!' || c == ':' || c == '\t' || c == '\n') {
            return false;
        } else {
            return true;
        }
    }

    private void parse() {
        /*split the text into words and store in vector*/
        String word = "";
        char c;
        int i = 0;
        int n = text.length();
        while (i < n) {
            c = text.charAt(i++);
            //System.out.print(c);
            if (c != ' ' && isValid(c)) {
                word += c;
                if (i == n) {
                    this.insertWord(word);
                    word = "";
                }
                continue;
            } else {
                if (word.length() > 0) {
                    //System.out.println(word);
                    this.insertWord(word);
                    word = "";
                } else {
                    //ie word.length=0
                    continue;
                }
            }//end else
        }//while

    }
    /*
     * These methods may be needed outside 
     */

    public void insertWord(String word) {
        /*
         * if word is not present in the vector then insert it into the vector
         * and allocate count 1 , otherwise increase the count associated with 
         * the word by 1.
         */
        word = word.trim().toLowerCase();
        //if you want case sensitive, comment the above line
        if (!words.contains(word)) {
            words.add(word);
            count.add(new StringCounter(word));
        } else {
            count.elementAt(words.indexOf(word)).increaseCount();
        }
    }

    public void printWeighedWords() {
        /*
         * This is just a debugging method
         */
        int max = getDistinctWordsCount();
        for (int i = 0; i < max; i++) {
            System.out.println(words.elementAt(i) + " :" + count.elementAt(i).count);
        }
    }

    public int getDistinctWordsCount() {
        /*
         * returns the total number of distinct words
         * ie. if the word "rain" is repeated many times
         * it returns as only one word
         */
        return words.size();
    }

    public int getTotalWordsCount() {
        /*
         * returns total number of words, counts
         * same word multiple times if it is repeated 
         * multiple times
         */
        int i = 0;
        for (int j = 0; j < count.size(); j++) {
            i += count.elementAt(j).count;
        }
        return i;
    }

    public int getCountOfTerm(String term) {
        /*
         * returns the count of term if it is present
         * otherwise returns zero
         */
        int ind = words.indexOf(term);
        if (ind > 0) {
            return count.elementAt(ind).count;
        } else {
            return 0;
        }
    }

    public String toString() {
        String doc = "[";
        int max = getDistinctWordsCount();
        for (int i = 0; i < max; i++) {
            doc += words.elementAt(i) + "(" + count.elementAt(i).count + ")";
            if (i < max - 1) {
                doc += ", ";
            }
        }
        doc += "]";
        return doc;

    }

    public static void main(String[] args) throws IOException {
       //Document doc = parseFile("assignment1.txt");

        Document doc = new Document("Hello World, My name is Rajan. and Hello world, "
                + "I enjoy doing this kind of works.");

        System.out.println("Total words count: " + doc.getTotalWordsCount());
        System.out.println("Distinct words count: " + doc.getDistinctWordsCount());
        System.out.println("Weighed words: \n");
        System.out.println(doc);

    }

    public static Document parseFile(String filepath) {
        // String filepath = "assignment1.txt";
        Charset ENCODING = StandardCharsets.UTF_8;

        Path path = Paths.get(filepath);

        //System.out.println(Files.readAllLines(path, ENCODING));
        Document doc = null;
        try {
            doc = new Document(Files.readAllLines(path, ENCODING));
            int count = doc.getDistinctWordsCount();
            System.out.println("Words count:  " + count);
            System.out.println("Words:  " + doc.words);
            doc.printWeighedWords();
        } catch (IOException e) {
            System.out.println("error");
            e.printStackTrace();
        }
        return doc;
    }
}