Monthly Archives: January 2012

Gson java library for json


Gson library is the simplest way to convert Json string to java object. You could involve it here http://code.google.com/p/google-gson/ and read about http://sites.google.com/site/gson/gson-user-guide

Example usecase:

Gson gson = new Gson();
String strNotary = "{id:72170
           ,lat:null
           ,lng:null
           ,category:347
           ,name:Hello;
           ,address:"null"
           ,contacts:"NewYork"
           ,accuracy:0
           ,iteration:0
           ,ShowOnMap:"True"
           ,MaxZoomLevel:"12"
           }";
Notary notary = gson.fromJson(strNotary, Notary.class);

For convertion you need to describe Notary class

public class Notary
{
	private String name;
	private String contacts;

	public String getName()
	{
		return name;
	}
	public void setName(String name)
	{
		this.name = name;
	}
	public String getContacts()
	{
		return contacts;
	}
	public void setContacts(String contacts)
	{
		this.contacts = contacts;
	}
}

And to convert to json

Gson gson = new Gson();
String requestJson = gson.toJson(aData, HashMap.class);

Also, there is another lib http://json-lib.sourceforge.net/ you can use.

JSON tutorial

Using SLF4J (The Simple Logging Facade for Java)


SLF4J is useful tool for logging universally, i.e. independently on logger implementation. Awesome framework and simple to integrate.

To install just add files to build path slf4j-api-1.6.4.jar and logger adapter, for ex. slf4j-log4j12-1.6.4.jar
And that’s all!

Use it with:

Logger logger = LoggerFactory.getLogger(HelloWorld.class);
logger.info("Hello World");

Configuration and using Log4j


Log4j is a very simple and convenient logging service for your java application. To add Log4j you’ve to download it from http://logging.apache.org/log4j and put to project.

Log4j can be configured in 2 ways: with log4j.properties or log4j.xml file. IMHO, xml is more clear for understanding.

Settings initiation from Properties file can be made with org.apache.log4j.PropertyConfigurator class, but for Xml one you’ve to use org.apache.log4j.xml.DOMConfigurator class.

To test and learn Log4j you can use the following log4j.xml config:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

 <appender name="consoleAppender1">
 <param name="Threshold" value="INFO" />
 <layout>
 <param name="ConversionPattern"
 value="root - %d{dd-MM-yyyy HH:mm:ss,SSS} [%p] %t - %c [Line:%L] - %m%n" />
 </layout>
 </appender>

 <appender name="consoleAppender2">
 <param name="Threshold" value="INFO" />
 <layout>
 <param name="ConversionPattern"
 value="com.foo - %d{dd MM yyyy HH:mm:ss,SSS} [%p] %t %c - %m%n" />
 </layout>
 </appender>

 <appender name="consoleAppender3">
 <param name="Threshold" value="INFO" />
 <layout>
 <param name="ConversionPattern"
 value="com.foo.Bar - %d{dd-MM-yyyy HH:mm:ss,SSS} [%p] %t - %c [Line:%L] - %m%n" />
 </layout>
 </appender>

 <appender name="fileAppender">
 <param name="Threshold" value="INFO" />
 <param name="File" value="logs/common.log" />
 <layout>
 <param name="ConversionPattern" value="%d{dd-MM-yyyy HH:mm:ss,SSS} [%p] %t - %c [Line:%L] - %m%n" />
 </layout>
 </appender>

 <logger name="com.foo.Bar" additivity="true">
 <level value="INFO" />
 <appender-ref ref="consoleAppender3" />
 <appender-ref ref="fileAppender" />
 </logger>

 <logger name="com.foo" additivity="true">
 <level value="OFF" />
 <appender-ref ref="consoleAppender2" />
 <appender-ref ref="fileAppender" />
 </logger>

 <root>
 <level value="WARN" />
 <appender-ref ref="consoleAppender1" />
 </root>

</log4j:configuration>

And here is class Bar:

package com.foo;

import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;

public class Bar
{
	private static Logger logger = Logger.getLogger(Bar.class);

	public static void main(String[] args)
	{
		// Load settings
		DOMConfigurator.configure("log4j.xml");
		
		// Test
		logger.info("Hello World!");
	}
}

After executing we’ll see the output:

com.foo.Bar - 19 01 2012 12:56:42,848 [INFO] main com.foo.Bar - Hello World!
com.foo - 19 01 2012 12:56:42,848 [INFO] main com.foo.Bar - Hello World!
root - 19-01-2012 12:56:42,848 [INFO] main - com.foo.Bar [Line:27] - Hello World!

As you see, Log4j logger’s messages are going up from init class com.foo.Bar, through package logger com.foo to root logger, and because

additivity="true"

executes appenders in parent logger by hierarchy, regardless to log level of parent. But Threshold of appenders will check log level.

Here are logging levels by increasing priority:

  • TRACE
  • DEBUG
  • INFO
  • WARN
  • ERROR
  • FATAL

Pattern layout see here http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PatternLayout.html

Hope, it helps.

Eclipse IDE vs others


How many hollywars around development IDE better what. After reading, discussing and trying I’ve released my own opinion. For me, the best dev. env. is ECLIPSE. Cause, it’s free, fast and very convenient. Yes, there is one unpleasant moment: it may close unexpectedly. But you can find the reason in web-search.

Anyway, no better IDE ever.

Web

Escape/Unescape HTML in Javascript


Very useful function for working with HTML.

  function escapeHTML(str) {
    return str.replace(/&/g,'&')
      .replace(//g,'>');
  }

  function unescapeHTML(str) {
    return stripTags(str)
      .replace(/</g,'<')
      .replace(/>/g,'>')
      .replace(/&/g,'&');
  }
  
  function stripTags(str) {
    return str.replace(/<\w+(\s+("[^"]*"|'[^']*'|[^>])+)?>|<\/\w+>/gi, '');
  }

Git branching (ref)


Simply great article about Git – http://nvie.com/posts/a-successful-git-branching-model/
PDF: Git-branching-model

May be, it’s time to Git?

Inversion of Control (IoC)


So many thoughts about subject. But imho,

IoC is a setting realization to class properties