Project

General

Profile

Download (3.79 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
* Copyright (C) 2007 EDIT
4
* European Distributed Institute of Taxonomy 
5
* http://www.e-taxonomy.eu
6
* 
7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8
* See LICENSE.TXT at the top of this package for the full license terms.
9
*/
10

    
11
package eu.etaxonomy.cdm.remote.json;
12

    
13
import java.util.ArrayList;
14
import java.util.HashMap;
15
import java.util.List;
16
import java.util.Map;
17

    
18
import net.sf.json.JsonConfig;
19
import net.sf.json.processors.JsonBeanProcessor;
20
import net.sf.json.processors.JsonBeanProcessorMatcher;
21
import net.sf.json.processors.JsonValueProcessor;
22
import net.sf.json.processors.JsonValueProcessorMatcher;
23
import net.sf.json.util.CycleDetectionStrategy;
24
import net.sf.json.util.PropertyFilter;
25

    
26
import org.springframework.beans.factory.FactoryBean;
27

    
28
/**
29
 * 
30
 * @author ben.clark
31
 * @author a.kohlbecker
32
 */
33
public class JsonConfigFactoryBean implements FactoryBean {
34

    
35
	private JsonConfig jsonConfig = null;
36
	
37
	private CycleDetectionStrategy cycleDetectionStrategy = CycleDetectionStrategy.LENIENT;
38
	
39
	private boolean ignoreJPATransient = false;
40
	
41
	private Map<Class,JsonBeanProcessor> jsonBeanProcessors = new HashMap<Class,JsonBeanProcessor>();
42
	private List<PropertyFilter> jsonPropertyFilters = new ArrayList<PropertyFilter>();
43
	private Map<Class,JsonValueProcessor> jsonValueProcessors = new HashMap<Class,JsonValueProcessor>();
44
	private JsonBeanProcessorMatcher jsonBeanProcessorMatcher = JsonBeanProcessorMatcher.DEFAULT;
45
	private JsonValueProcessorMatcher jsonValueProcessorMatcher = JsonValueProcessorMatcher.DEFAULT;
46
	private List<String> excludes = new ArrayList<String>();
47
	
48
	public void setCycleDetectionStrategy(CycleDetectionStrategy cycleDetectionStrategy) {
49
		this.cycleDetectionStrategy = cycleDetectionStrategy;
50
	}
51
	
52
	public void setIgnoreJPATransient(boolean ignoreJPATransient) {
53
		this.ignoreJPATransient = ignoreJPATransient;
54
	}
55

    
56
	public void setJsonBeanProcessors(Map<Class, JsonBeanProcessor> jsonBeanProcessors) {
57
		this.jsonBeanProcessors = jsonBeanProcessors;
58
	}
59

    
60
	public void setJsonPropertyFilters(List<PropertyFilter> jsonPropertyFilters) {
61
		this.jsonPropertyFilters = jsonPropertyFilters;
62
	}
63

    
64
	public void setJsonBeanProcessorMatcher(JsonBeanProcessorMatcher jsonBeanProcessorMatcher) {
65
		this.jsonBeanProcessorMatcher = jsonBeanProcessorMatcher;
66
	}
67
	
68
	public void setJsonValueProcessorMatcher(JsonValueProcessorMatcher jsonValueProcessorMatcher ) {
69
		this.jsonValueProcessorMatcher = jsonValueProcessorMatcher;
70
	}
71

    
72
	public void setJsonValueProcessors(Map<Class, JsonValueProcessor> jsonValueProcessors) {
73
		this.jsonValueProcessors = jsonValueProcessors;
74
	}
75
	
76
	public void setExcludes(List<String> excludes) {
77
		this.excludes = excludes;
78
	}
79

    
80
	public void init() {
81
		jsonConfig = new JsonConfig();
82
		
83
		jsonConfig.setCycleDetectionStrategy(cycleDetectionStrategy);
84
		
85
		jsonConfig.setIgnoreJPATransient(ignoreJPATransient);
86
		
87
		jsonConfig.setJsonValueProcessorMatcher(jsonValueProcessorMatcher);
88

    
89
		jsonConfig.setJsonBeanProcessorMatcher(jsonBeanProcessorMatcher);
90
		
91
		jsonConfig.setExcludes(excludes.toArray(new String[]{}));
92
		
93
		for(Class clazz : jsonBeanProcessors.keySet()) {
94
			jsonConfig.registerJsonBeanProcessor(clazz, jsonBeanProcessors.get(clazz));
95
		}
96
		
97
		for(PropertyFilter propertyFilter : jsonPropertyFilters) {
98
		    jsonConfig.setJsonPropertyFilter(propertyFilter);
99
		}
100

    
101
		
102
		for(Class clazz : jsonValueProcessors.keySet()) {
103
		    jsonConfig.registerJsonValueProcessor(clazz, jsonValueProcessors.get(clazz)); 
104
		}
105
		
106
		
107
	}
108
	
109
	
110
	public Object getObject() throws Exception {
111
		if(jsonConfig == null) {
112
			init();
113
		}
114
		return jsonConfig;
115
	}
116

    
117
    public Class getObjectType() {
118
		return JsonConfig.class;
119
	}
120

    
121
	public boolean isSingleton() {
122
		return true;
123
	}
124

    
125
}
    (1-1/1)