1 package org.xmlfield.core;
2
3 import java.util.Map;
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 public class XmlFieldFactory {
21
22 private static Boolean staticGetterCache = null;
23 private static Map<String, String> staticParserConfiguration = null;
24
25
26
27
28 private static final ThreadLocal<XmlField> xmlFieldInstances = new ThreadLocal<XmlField>() {
29 @Override
30 protected XmlField initialValue() {
31 XmlField xf = new XmlField(staticParserConfiguration);
32 if (staticGetterCache != null) {
33 xf.setGetterCache(staticGetterCache);
34 }
35 return xf;
36 }
37 };
38
39 private Boolean getterCache = null;
40 private Map<String, String> parserConfiguration = null;
41
42 private final boolean useThreadLocal;
43
44 public XmlFieldFactory() {
45 this(false);
46 }
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66 public XmlFieldFactory(boolean useThreadLocal) {
67 this.useThreadLocal = useThreadLocal;
68 }
69
70
71
72
73 public void cleanThreadLocal() {
74 if (useThreadLocal) {
75 xmlFieldInstances.remove();
76 }
77 }
78
79
80
81
82
83
84
85 public XmlField getXmlField() {
86 if (useThreadLocal) {
87 return xmlFieldInstances.get();
88 }
89
90 XmlField xf = new XmlField(parserConfiguration);
91 if (getterCache != null) {
92 xf.setGetterCache(getterCache);
93 }
94
95 return xf;
96 }
97
98
99
100
101
102
103
104 public void setGetterCache(boolean enabled) {
105 if (useThreadLocal) {
106 staticGetterCache = enabled;
107 } else {
108 this.getterCache = enabled;
109 }
110 }
111
112
113
114
115
116
117
118 public void setParserConfiguration(Map<String, String> configuration) {
119 if (useThreadLocal) {
120 staticParserConfiguration = configuration;
121 } else {
122 parserConfiguration = configuration;
123 }
124 }
125
126 }