Leave a comment

Generating HTML

Recently I involved in a project which copies several documentation files in to many subfolder. In order to make the navigation process simple, we thought an index.html file containing the links to those files would be helpful.

A simple java program that read through all directories from the top and created an index for the documents.

Any better solution?

private String getIndexHtml(String folder, boolean dirBrowsingAllowed) {
        File root = new File(folder);
        System.out.println("Root Folder: "+root.toString());
        File file = root;
        StringBuffer sb = new StringBuffer();
        sb.append("<html>");
        sb.append("<body>");
        sb.append("<h1> Document index </h1>");
        listFiles(file, dirBrowsingAllowed, sb);
        sb.append("</body>");
        sb.append("</html>");
        return sb.toString();
    }

    private void listFiles(File file, boolean dirBrowsingAllowed, StringBuffer sb) {
        String[] list = file.list();
        sb.append("<ul>");
        for (String fileOrFolder : list) {
            File newFile = new File(file.toString()+File.separator+fileOrFolder);
            if(newFile.isHidden()) continue;
            if(newFile.isDirectory()){
                if(dirBrowsingAllowed){
                    sb.append("<li>");
                    sb.append("<a href=\"file:///");
                    sb.append(newFile.getAbsolutePath());
                    sb.append("\">");
                    sb.append(newFile.getPath());
                    sb.append("</a>");
                    sb.append("</li>");
                }
                listFiles(newFile, dirBrowsingAllowed, sb);
            } else {
                sb.append("<li>");
                sb.append("<a href=\"file:///");
                sb.append(newFile.getAbsolutePath());
                sb.append("\">");
                sb.append(newFile.getName());
                sb.append("</a>");
                sb.append("</li>");
            }
        }
        sb.append("</ul>");
    }
Leave a comment

Learning New programming languages

After having few chapters read from a outstanding book “The Passionate Programmer” by Chad fowler, I planned to learn few new languages. I started search of finding a language to learn. I got several languages. One of them is clojure.  The first thing which prevents me or an averion that the syntax of the language. It is very clumsy, hard to read and most importantly It would be a real challenge to debug.

Nowadays Software languages are moving towards spoken language. We should be able to read the code. Achronym is OK than symbols.

(defn fib
  ([] (concat [0 1] (fib 0 1)))
  ([a b] (lazy-cons (+ a b) (fib b (+ a b)))))

Sample Clojure program for generate fib numbers. It was hard for me to understand and debug. I’ll be learning Ruby and decided to write an applications for my personal use like perfios.com.

Cheers

Leave a comment

Rediscovering Myself

I was just out of some good practices/habits I followed. I just did work that come to me and not tried to focus on my interests. The growing son made me busy all these days. I realized this and I’m trying to back on track. Starting a day with great enthusiasm. Active all through the day.

Restarted the hobby of Photography. Booked a seat in local photography event. Planned to pen up blogs frequently.

Leave a comment

One reason to put braces in IF statement

Recently it happened to read adam-bien blog. I found the interesting explanation for why curly braces blocks are required for IF statements in Java.

Without curly brackets, you could accidentally write a semicolon after the IF-statements. The semicolon is a valid, empty statement, which will be “execute” instead of the actual (intended) one.

So either write the IF-statements with curly brackets blocks …or carefully search for semicolons after the IF-statement.

Powered by ScribeFire.

Leave a comment

Accessing protected URL in Java

Come through an elegant code yesterday. Somehow the content needs to be accessed from the database server which is password protected. A simple URL query would fetch the content. I’ve tried Apache commons. I could managed to access the content via plain java.net classes works for me pretty smoothly.

here is how

Authenticator.setDefault(new MyAuthenticator("user","password"));

URL url = new URL("http://localhost:5300/database?_query='for $a in collection('test')/types return $a'");
InputStream resultStream =url.openStream();

And the simple inner class

class MyAuthenticator extends Authenticator { 

 private String username, password;

  public MyAuthenticator(String username, String password) {
      this.username = username;      this.password = password;
  }

  // This method is called when a password-protected URL is accessed  
  protected PasswordAuthentication getPasswordAuthentication() {
       return new PasswordAuthentication(username, password.toCharArray()); 
  }
}

Are there any better way?

Follow

Get every new post delivered to your Inbox.