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 and read about

Example usecase:

Gson gson = new Gson();
String strNotary = "{id:72170
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)
	{ = 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 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);"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 and put to project.

Log4j can be configured in 2 ways: with 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="">

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

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

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

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

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

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

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


And here is class Bar:


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
		// Test"Hello World!");

After executing we’ll see the output: - 19 01 2012 12:56:42,848 [INFO] main - Hello World! - 19 01 2012 12:56:42,848 [INFO] main - Hello World!
root - 19-01-2012 12:56:42,848 [INFO] main - [Line:27] - Hello World!

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


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:

  • INFO
  • WARN

Pattern layout see here

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.


Escape/Unescape HTML in Javascript

Very useful function for working with HTML.

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

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

Git branching (ref)

Simply great article about Git –
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