1
2
3
4
5
6
7 package nl.mineleni.cbsviewer.util;
8
9 import java.io.File;
10 import java.net.URL;
11 import java.util.Collections;
12 import java.util.List;
13
14 import javax.xml.bind.JAXBContext;
15 import javax.xml.bind.JAXBElement;
16 import javax.xml.bind.JAXBException;
17 import javax.xml.bind.Unmarshaller;
18
19 import nl.mineleni.cbsviewer.util.xml.LayerDescriptor;
20 import nl.mineleni.cbsviewer.util.xml.LayersList;
21
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 import flexjson.JSONSerializer;
26 import flexjson.transformer.AbstractTransformer;
27
28
29
30
31
32
33
34
35
36
37
38 public class AvailableLayersBean {
39
40
41 private static final Logger LOGGER = LoggerFactory
42 .getLogger(AvailableLayersBean.class);
43
44
45
46
47 private List<LayerDescriptor> layers = null;
48
49
50
51
52 public AvailableLayersBean() {
53 try {
54 final JAXBContext jc = JAXBContext
55 .newInstance("nl.mineleni.cbsviewer.util.xml");
56 final Unmarshaller u = jc.createUnmarshaller();
57 final URL r = this.getClass().getClassLoader()
58 .getResource("AvailableLayers.xml");
59 if (r != null) {
60 final File f = new File(r.getFile());
61 @SuppressWarnings("unchecked")
62 final JAXBElement<LayersList> element = (JAXBElement<LayersList>) u
63 .unmarshal(f);
64 final LayersList layerslist = element.getValue();
65 this.layers = layerslist.getLayerdescriptor();
66 } else {
67 throw new JAXBException(
68 "Bestand 'AvailableLayers.xml' niet gevonden");
69 }
70 } catch (final JAXBException e) {
71 LOGGER.error(
72 "Er is een fout opgetreden bij het inlezen van de layers.",
73 e);
74 }
75 }
76
77
78
79
80
81
82 public List<LayerDescriptor> getLayers() {
83 return Collections.unmodifiableList(this.layers);
84 }
85
86
87
88
89
90
91
92 public String asJSON() {
93 return this.asJSON(true);
94 }
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109 public String asJSON(final boolean asVar) {
110 final JSONSerializer serializer = new JSONSerializer();
111 final String json = serializer.transform(new AbstractTransformer() {
112 @Override
113 public Boolean isInline() {
114 return true;
115 }
116
117 @Override
118 public void transform(final Object object) {
119
120 return;
121 }
122 }, void.class).exclude("class", "aliases", "attributes")
123 .prettyPrint(LOGGER.isDebugEnabled()).serialize(this.layers);
124 if (asVar) {
125 return "\n/* <![CDATA[ */ var _layers=" + json + ";/* ]]> */";
126 }
127 return json;
128 }
129
130
131
132
133
134
135
136
137
138 public LayerDescriptor getLayerByID(final String id) {
139 for (final LayerDescriptor desc : this.layers) {
140 if (desc.getId().equalsIgnoreCase(id)) {
141 return desc;
142 }
143 }
144 return null;
145 }
146
147
148
149
150
151
152
153
154
155
156
157
158 public LayerDescriptor getLayerByLayers(final String queryLyrName,
159 final String lyrUrl) {
160 for (final LayerDescriptor desc : this.layers) {
161 if ((desc.getLayers().replaceAll("\\s", ""))
162 .equalsIgnoreCase(queryLyrName.replaceAll("\\s", ""))) {
163 if (desc.getUrl().equalsIgnoreCase(lyrUrl)) {
164 LOGGER.debug("Gevonden layer-id is: " + desc.getId());
165 return desc;
166 }
167 }
168 }
169 return null;
170 }
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185 public LayerDescriptor getLayerByLayers(final String queryLyrName,
186 final String lyrUrl, final String styles) {
187 for (final LayerDescriptor desc : this.layers) {
188 if ((desc.getLayers().replaceAll("\\s", ""))
189 .equalsIgnoreCase(queryLyrName.replaceAll("\\s", ""))) {
190 if (desc.getUrl().equalsIgnoreCase(lyrUrl)) {
191 if (desc.getStyles().replaceAll("\\s", "")
192 .equalsIgnoreCase(styles)) {
193 LOGGER.debug("Gevonden layer-id is: " + desc.getId());
194 return desc;
195 }
196 }
197 }
198 }
199 return null;
200 }
201
202
203
204
205
206
207
208
209
210 public LayerDescriptor getLayerByName(final String name) {
211 for (final LayerDescriptor desc : this.layers) {
212 if (desc.getName().equalsIgnoreCase(name)) {
213 LOGGER.debug("Gevonden layer-id is: " + desc.getId());
214 return desc;
215 }
216 }
217 return null;
218 }
219 }