1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.settings4j.connector;
21
22 import java.io.File;
23 import java.io.IOException;
24 import java.io.UnsupportedEncodingException;
25 import java.nio.charset.Charset;
26
27 import org.apache.commons.io.Charsets;
28 import org.settings4j.ContentResolver;
29 import org.settings4j.contentresolver.FSContentResolver;
30 import org.settings4j.contentresolver.UnionContentResolver;
31
32
33
34
35
36
37 public class FSConnector extends AbstractConnector {
38
39 private final FSContentResolver fsContentResolver;
40 private UnionContentResolver unionContentResolver;
41 private Charset charset = Charsets.UTF_8;
42
43
44 public FSConnector() {
45 this(new FSContentResolver());
46 }
47
48
49
50
51
52
53
54 protected FSConnector(final FSContentResolver fsContentResolver) {
55 super();
56 this.fsContentResolver = fsContentResolver;
57 this.unionContentResolver = new UnionContentResolver(this.fsContentResolver);
58 }
59
60 @Override
61 public byte[] getContent(final String key) {
62 return this.fsContentResolver.getContent(key);
63 }
64
65 @Override
66 public Object getObject(final String key) {
67 if (getObjectResolver() != null) {
68 return getObjectResolver().getObject(key, this.unionContentResolver);
69 }
70
71 return null;
72 }
73
74 @Override
75 public String getString(final String key) {
76 final byte[] content = getContent(key);
77 if (content != null) {
78 return new String(this.fsContentResolver.getContent(key), this.charset);
79 }
80
81 return null;
82 }
83
84
85
86
87
88
89 public void setContent(final String key, final byte[] value) throws IOException {
90 this.fsContentResolver.setContent(key, value);
91 }
92
93
94
95
96
97
98 public void setString(final String key, final String value) throws IOException {
99 try {
100 setContent(key, value.getBytes(this.charset));
101 } catch (final UnsupportedEncodingException e) {
102 throw new UnsupportedOperationException(String.format("Charset not found: %s", this.charset), e);
103 }
104 }
105
106 public String getCharset() {
107 return this.charset.name();
108 }
109
110
111
112
113
114
115 public void setCharset(final String charset) {
116 this.charset = Charset.forName(charset);
117 }
118
119
120
121
122
123
124 public void setRootFolderPath(final String rootFolderPath) {
125 this.fsContentResolver.setRootFolderPath(rootFolderPath);
126 }
127
128 @Override
129 public void setContentResolver(final ContentResolver contentResolver) {
130 this.unionContentResolver = new UnionContentResolver(this.fsContentResolver);
131 this.unionContentResolver.addContentResolver(contentResolver);
132 }
133
134 @Override
135 protected ContentResolver getContentResolver() {
136 if (hasCustomContentResolver()) {
137 return this.unionContentResolver.getContentResolvers()[1];
138 }
139 return null;
140 }
141
142 private boolean hasCustomContentResolver() {
143 return this.unionContentResolver.getContentResolvers().length > 1;
144 }
145
146
147
148
149
150
151
152
153 public File getRootFolder() {
154 return this.fsContentResolver.getRootFolder();
155 }
156 }