Search and Parse Keyword(1)JACKSON for JSON and Jsoup for URL Fetch
1. Using jackson to mapping the JAVA Object and JSON
package com.sillycat.analyzerjava.service.impl;
import java.io.IOException;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion;
import com.sillycat.analyzerjava.service.JSONMapperHero;
/** * JSON mapper based on jackson *
* @author sillycat
*/
public class JSONMapperHeroJacksonImpl<T> implements JSONMapperHero<T> {
private ObjectMapper jsonMapper;
/**
* will be called by spring framework
*/
@SuppressWarnings("unused")
private void init() {
jsonMapper = new ObjectMapper();
jsonMapper.setSerializationInclusion(Inclusion.NON_NULL);
}
/**
* mapping the object to JSON String
*/
public String toJSON(T objClass) {
try {
return jsonMapper.writeValueAsString(objClass);
} catch (JsonGenerationException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
It is configured in Spring, so we can adjust it when we want.
<?xmlversion="1.0"encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/
beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- my lovely spring, long time no see -->
<bean id="analyzerHero"
class="com.sillycat.analyzerjava.service.impl.AnalyzerHeroSingleRegexImpl"/> <bean id="resourceFetchHero"
class="com.sillycat.analyzerjava.service.impl.ResourceFetchHeroJsoupImpl" <bean id="jsonMapperHero"
class="com.sillycat.analyzerjava.service.impl.JSONMapperHeroJacksonImpl"
init-method="init"/>
</beans>
Dependencies in pom.xml
<properties>
<springframework.version>3.1.1.RELEASE</springframework.version>
<jackson.version>1.9.10</jackson.version>
</properties>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>${jackson.version}</version>
</dependency>
2. Jsoup to fetch URL Resource
package com.sillycat.analyzerjava.service.impl;
import java.io.IOException;
import org.jsoup.Jsoup;
import com.sillycat.analyzerjava.service.ResourceFetchHero;
import com.sillycat.analyzerjava.service.base.BaseHero;
public class ResourceFetchHeroJsoupImpl extends BaseHero implements ResourceFetchHero{
public String getTitlefromURL(String url) {
String result = null;
try {
log.debug("====================================");
log.debug("searching title for url:" + url);
result = Jsoup.connect(url).get().title();
log.debug("title:" + result);
log.debug("====================================");
} catch (IOException e) {
log.error("Exception:" + e);
}
returnresult;
}
}
Dependencies in pom.xml
<!-- jsoup HTML parser library @ http://jsoup.org/ -->
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.7.3</version>
</dependency>
The details are in project analyzerjava.
References:
http://www.cnblogs.com/chenssy/p/3751221.html
http://www.iteye.com/topic/336577
http://my.oschina.net/u/999578/blog/151791
http://blog.shilimin.com/298.htm
http://www.iteye.com/topic/1116520
http://gaojunwei.iteye.com/blog/1856753
http://www.zuidaima.com/share/1863323818724352.htm
Parse URL
http://dikar.iteye.com/blog/345486
http://pmghong.blog.51cto.com/3221425/1334086
PerfTest
http://www.ibm.com/developerworks/cn/java/j-cq11296.html
http://blog.csdn.net/emu/article/details/363405
http://www.bdqn.cn/news/201404/13763.shtml
http://sillycat.iteye.com/blog/736717
java yacc
http://byaccj.sourceforge.net/
http://tianya23.blog.51cto.com/1081650/633108
http://sillycat.iteye.com/blog/2076910
http://sillycat.iteye.com/blog/2101121
java analyzer
http://www.cnblogs.com/xuqiang/archive/2010/09/21/1953501.html
http://www.blogjava.net/qujinlong123/archive/2007/05/08/113773.html
guava cache
http://sillycat.iteye.com/blog/1889542
jsoup
http://www.mkyong.com/java/jsoup-html-parser-hello-world-examples/
http://jsoup.org/
json mapper
http://sillycat.iteye.com/blog/563970