Statistics
| Revision:

svn-gvsig-desktop / branches / v02_desarrollo / libraries / sld / temp / org.gvsig.sldsupport.lib.impl / src / main / java / org / gvsig / sldsupport / impl / DefaultSLDSupportManager.java @ 40799

History | View | Annotate | Download (18.4 KB)

1 40784 jldominguez
package org.gvsig.sldsupport.impl;
2
3
import java.io.File;
4
import java.io.FileInputStream;
5
import java.io.FileOutputStream;
6
import java.io.IOException;
7
import java.io.InputStream;
8
import java.io.OutputStream;
9
import java.util.ArrayList;
10
import java.util.HashMap;
11
import java.util.HashSet;
12
import java.util.Iterator;
13
import java.util.List;
14
import java.util.Map;
15
import java.util.Set;
16
17
import org.gvsig.sldsupport.SLDSupportManager;
18
import org.gvsig.sldsupport.exception.InvalidSLDObjectException;
19
import org.gvsig.sldsupport.exception.SLDException;
20 40796 jldominguez
import org.gvsig.sldsupport.exception.SLDReadException;
21 40784 jldominguez
import org.gvsig.sldsupport.exception.SLDWriteException;
22
import org.gvsig.sldsupport.exception.UnsupportedSLDVersionException;
23 40799 jldominguez
import org.gvsig.sldsupport.impl.util.SLDUtils;
24 40784 jldominguez
import org.gvsig.sldsupport.reader.SLDReader;
25
import org.gvsig.sldsupport.reader.SLDReaderFactory;
26
import org.gvsig.sldsupport.sld.SLDObject;
27
import org.gvsig.sldsupport.writer.SLDWriter;
28
import org.gvsig.sldsupport.writer.SLDWriterFactory;
29
import org.slf4j.Logger;
30
import org.slf4j.LoggerFactory;
31 40799 jldominguez
import org.xmlpull.v1.XmlPullParserException;
32 40784 jldominguez
33
public class DefaultSLDSupportManager implements SLDSupportManager {
34
35
        private static Logger logger = LoggerFactory.getLogger(
36
                        DefaultSLDSupportManager.class);
37
38
        private Map<String, Set<SLDReaderFactory>> versionToReaders =
39
                        new HashMap<String, Set<SLDReaderFactory>>();
40
        private Map<String, Set<SLDWriterFactory>> versionToWriters =
41
                        new HashMap<String, Set<SLDWriterFactory>>();
42
43
        public void registerReader(SLDReaderFactory fact) {
44
45
                String v = fact.getVersion();
46
                Set<SLDReaderFactory> set = versionToReaders.get(v);
47
                if (set == null) {
48
                        set = new HashSet<SLDReaderFactory>();
49
                        set.add(fact);
50
                        versionToReaders.put(v, set);
51
                } else {
52
                        set.add(fact);
53
                }
54
        }
55
56
        public void registerWriter(SLDWriterFactory fact) {
57
58
                String v = fact.getVersion();
59
                Set<SLDWriterFactory> set = versionToWriters.get(v);
60
                if (set == null) {
61
                        set = new HashSet<SLDWriterFactory>();
62
                        set.add(fact);
63
                        versionToWriters.put(v, set);
64
                } else {
65
                        set.add(fact);
66
                }
67
68
        }
69
70
        public List<SLDWriter> createWriters(String version)
71
                        throws UnsupportedSLDVersionException {
72
73
                Set<SLDWriterFactory> set = versionToWriters.get(version);
74
                if (set == null || set.size() == 0) {
75
                        throw new UnsupportedSLDVersionException(version);
76
                }
77
78
                List<SLDWriter> resp = new ArrayList<SLDWriter>();
79
                Iterator<SLDWriterFactory> iter = set.iterator();
80
                SLDWriterFactory fact = null;
81
                while (iter.hasNext()) {
82
                        fact = iter.next();
83
                        try {
84
                                resp.add(fact.create());
85
                        } catch (Exception exc) {
86
                                logger.info("Error while creating writer (ignored)", exc);
87
                        }
88
                }
89
                return resp;
90
        }
91
92
        public List<SLDReader> createReaders(String version)
93
                        throws UnsupportedSLDVersionException {
94
95
                Set<SLDReaderFactory> set = versionToReaders.get(version);
96
                if (set == null || set.size() == 0) {
97
                        throw new UnsupportedSLDVersionException(version);
98
                }
99
100
                List<SLDReader> resp = new ArrayList<SLDReader>();
101
                Iterator<SLDReaderFactory> iter = set.iterator();
102
                SLDReaderFactory fact = null;
103
                while (iter.hasNext()) {
104
                        fact = iter.next();
105
                        try {
106
                                resp.add(fact.create());
107
                        } catch (Exception exc) {
108
                                logger.info("Error while creating reader (ignored)", exc);
109
                        }
110
                }
111
                return resp;
112
        }
113
114
        public void write(SLDObject obj, String version, File outfile)
115
                        throws IOException, SLDException {
116
117
                if (obj == null) {
118
                        throw new IOException("Input SLDObject is null");
119
                }
120
121
                Set<SLDWriterFactory> wset = this.versionToWriters.get(version);
122
                if (wset == null || wset.size() == 0) {
123
                        throw new UnsupportedSLDVersionException(version);
124
                }
125
126
                OutputStream outstream = null;
127
                Iterator<SLDWriterFactory> witer = wset.iterator();
128
                SLDWriterFactory fact_item = null;
129
                SLDWriter witem = null;
130
                /*
131
                 * Trying to write without forcing...
132
                 */
133
                while (witer.hasNext()) {
134
                        fact_item = witer.next();
135
                        witem = fact_item.create();
136
                        outstream = new FileOutputStream(outfile);
137
                        try {
138
                                witem.write(obj, outstream);
139
                                // It was possible to write without forcing
140
                                return;
141
                        } catch (Exception exc) {
142
                                logger.info("One of the writers was unable to write object: "
143
                                                + obj.getClass().getSimpleName());
144
                                // Go on...
145
                        } finally {
146
                                try { outstream.close(); } catch (Exception exc) { }
147
                        }
148
                }
149
                /*
150
                 * It was not possible to write without forcing.
151
                 * Now we'll try to write with forceWrite
152
                 */
153
                witer = wset.iterator();
154
                while (witer.hasNext()) {
155
                        fact_item = witer.next();
156
                        witem = fact_item.create();
157
                        outstream = new FileOutputStream(outfile);
158
                        try {
159
                                witem.forceWrite(obj, outstream);
160
                                // It was possible to write without forcing
161
                                return;
162
                        } catch (Exception exc) {
163
                                logger.info("One of the writers was unable to force-write object: "
164
                                                + obj.getClass().getSimpleName());
165
                                // Go on...
166
                        } finally {
167
                                try { outstream.close(); } catch (Exception exc) { }
168
                        }
169
                }
170
                /*
171
                 * It was not possible to write with forceWrite
172
                 */
173
                throw new SLDWriteException(
174
                                "Unable to write with available writers ("
175
                                + obj.getClass().getSimpleName() + ")");
176
        }
177
178
179
        public SLDObject read(File infile)
180 40796 jldominguez
                        throws SLDException, IOException {
181 40784 jldominguez
182
                if (infile == null || !infile.exists()) {
183
                        throw new IOException("Null or non-existing file: "
184
                                        + ((infile == null) ? "Null" : infile.getAbsolutePath()));
185
                }
186 40799 jldominguez
187
188
                String ve = null;
189
                try {
190
                        ve = SLDUtils.detectVersion(infile);
191
                } catch (XmlPullParserException e) {
192
                }
193
194
                System.out.println("Version = " + ve);
195
196 40784 jldominguez
                InputStream instream = null;
197
                Iterator<String> iter = versionToReaders.keySet().iterator();
198
                String vitem = null;
199
                Set<SLDReaderFactory> sitem = null;
200
                Iterator<SLDReaderFactory> siter = null;
201
                SLDReaderFactory fact_item = null;
202
                SLDReader ritem = null;
203
                SLDObject resp = null;
204 40799 jldominguez
                boolean found = false;
205 40784 jldominguez
                while (iter.hasNext()) {
206
                        vitem = iter.next();
207
                        sitem = versionToReaders.get(vitem);
208
                        siter = sitem.iterator();
209
                        while (siter.hasNext()) {
210 40799 jldominguez
211
                                found = true; // Used to decide message in exception
212 40784 jldominguez
                                fact_item = siter.next();
213
                                ritem = fact_item.create();
214
                                instream = new FileInputStream(infile);
215
                                try {
216
                                        resp = ritem.read(instream);
217
                                } catch (Exception exc) {
218 40799 jldominguez
                                        logger.info("One of the readers was unable to parse file: "        + infile.getAbsolutePath(), exc);
219 40784 jldominguez
                                }
220
                                try { instream.close(); } catch (Exception exc) { }
221
                                if (resp != null) {
222
                                        return resp;
223
                                }
224
                        }
225
                }
226 40799 jldominguez
                if (found) {
227
                        throw new SLDReadException(        "No available SLD reader was able to parse file: "
228
                                        + infile.getAbsolutePath());
229
                } else {
230
                        throw new SLDReadException("No SLD readers found.");
231
                }
232
233 40784 jldominguez
        }
234
235
//        public static String VERSION = "1.0.0";
236
//
237
//        private static Logger logger = LoggerFactory.getLogger(DefaultSLDSupportManager.class);
238
//
239
//        private Map<Class<? extends ILegend>, Set<SLDLegendReader>> legToReaders =
240
//        new HashMap<Class<? extends ILegend>, Set<SLDLegendReader>>();
241
//        // ================
242
//        private Map<Class<? extends ILegend>, Set<SLDLegendWriter>> legToWriters =
243
//        new HashMap<Class<? extends ILegend>, Set<SLDLegendWriter>>();
244
//
245
//        private SLDProtocolHandler handler = new SLDProtocolHandler1_0_0();
246
//
247
//        public DefaultSLDSupportManager() {
248
//
249
//        }
250
//
251
//        public void writeLegend(
252
//                        ILegend leg, String layer_name, File out_file,
253
//                        String version) throws IOException, SLDWriteException {
254
//
255
//                if (leg == null) {
256
//                        throw new SLDWriteException(new Exception("Legend is NULL"));
257
//                }
258
//                Set<SLDLegendWriter> set = getLegendWritersFor(leg);
259
//
260
//                if (set == null || set.size() == 0) {
261
//                        throw new SLDWriteException(new Exception(
262
//                                        "Unsupported legend: '" + leg.getClass().getName() + "'"));
263
//                }
264
//
265
//                Iterator<SLDLegendWriter> iter = set.iterator();
266
//                List<String> vv = null;
267
//                SLDLegendWriter item = null;
268
//                while (iter.hasNext()) {
269
//                        item = iter.next();
270
//                        vv = item.getSupportedVersions();
271
//                        if (vv.contains(version)) {
272
//                                ISLDLayer sld_layer = item.getSLDLayer(leg, layer_name, version);
273
//                                SLDUtils.writeSLDLayer(sld_layer, out_file, version);
274
//                                return;
275
//                        }
276
//                }
277
//                throw new SLDWriteException(new Exception(
278
//                                "Legend is supported: '" + leg.getClass().getName()
279
//                                                + "' but not in version '" + version + "'"));
280
//        }
281
//
282
//        // =========================================
283
//        // =========================================
284
//
285
//        public void registerLegendReader(SLDLegendReader reader) {
286
//
287
//                List<Class<? extends ILegend>> list = reader.getSupportedLegends();
288
//                if (list == null || list.size() == 0) {
289
//                        return;
290
//                }
291
//                Iterator<Class<? extends ILegend>> iter = list.iterator();
292
//
293
//                Class<? extends ILegend> clazz = null;
294
//                Set<SLDLegendReader> set = null;
295
//                while (iter.hasNext()) {
296
//                        clazz = iter.next();
297
//                        set = this.legToReaders.get(clazz);
298
//                        if (set == null) {
299
//                                set = new HashSet<SLDLegendReader>();
300
//                                set.add(reader);
301
//                                this.legToReaders.put(clazz, set);
302
//                        } else {
303
//                                set.add(reader);
304
//                        }
305
//                }
306
//        }
307
//
308
//        public void registerLegendWriter(SLDLegendWriter writer) {
309
//
310
//                List<Class<? extends ILegend>> list = writer.getSupportedLegends();
311
//                if (list == null || list.size() == 0) {
312
//                        return;
313
//                }
314
//                Iterator<Class<? extends ILegend>> iter = list.iterator();
315
//
316
//                Class<? extends ILegend> clazz = null;
317
//                Set<SLDLegendWriter> set = null;
318
//                while (iter.hasNext()) {
319
//                        clazz = iter.next();
320
//                        set = this.legToWriters.get(clazz);
321
//                        if (set == null) {
322
//                                set = new HashSet<SLDLegendWriter>();
323
//                                set.add(writer);
324
//                                this.legToWriters.put(clazz, set);
325
//                        } else {
326
//                                set.add(writer);
327
//                        }
328
//                }
329
//        }
330
//
331
//
332
//        // =============================
333
//
334
//        private Set<SLDLegendWriter> getLegendWritersFor(ILegend leg) {
335
//
336
//                Set<SLDLegendWriter> resp = new HashSet<SLDLegendWriter>();
337
//                if (leg == null) {
338
//                        return resp;
339
//                }
340
//                Class<? extends ILegend> target = leg.getClass();
341
//                Class<? extends ILegend> kitem = null;
342
//                Iterator<Class<? extends ILegend>> kiter = legToWriters.keySet().iterator();
343
//                while (kiter.hasNext()) {
344
//                        kitem = kiter.next();
345
//                        if (kitem.isAssignableFrom(target)) {
346
//                                /*
347
//                                 * kitem is same or superclass/superinterface of target
348
//                                 */
349
//                                resp.addAll(legToWriters.get(kitem));
350
//                        }
351
//                }
352
//                return resp;
353
//        }
354
//
355
//        public List<ISLDLayer> getSLDLayers(File f, String v) throws SLDReadException {
356
//
357
//                try {
358
//                        handler.parse(f);
359
//                } catch (Exception e) {
360
//                        throw new SLDReadException(e);
361
//                }
362
//
363
//                return handler.getLayers();
364
//        }
365
//
366
//        public ILegend getLegend(ISLDLayer sld_layer, VectorLayer vlayer, String v)
367
//        throws SLDReadException {
368
//
369
//                ILegend resp = null;
370
//                Collection<Set<SLDLegendReader>> all_readers = legToReaders.values();
371
//
372
//                Iterator<Set<SLDLegendReader>> iter_a = all_readers.iterator();
373
//                Set<SLDLegendReader> item_a = null;
374
//                Iterator<SLDLegendReader> iter_b = null;
375
//                SLDLegendReader item_b = null;
376
//                while (iter_a.hasNext()) {
377
//                        item_a = iter_a.next();
378
//                        iter_b = item_a.iterator();
379
//                        while (iter_b.hasNext()) {
380
//                                item_b = iter_b.next();
381
//                                try {
382
//                                        resp = item_b.getLegend(sld_layer, vlayer, v);
383
//                                } catch (SLDReadException exc) {
384
//                                        logger.info("While reading SLD file.", exc);
385
//                                        // This reader did not work
386
//                                } catch (Exception exc) {
387
//                                        logger.info("While reading SLD file.", exc);
388
//                                }
389
//                                if (resp != null) {
390
//                                        return resp;
391
//                                }
392
//                        }
393
//                }
394
//                throw new SLDReadException(new Exception(
395
//                                "Unable to read legend from SLD layer: " + sld_layer.getName()));
396
//        }
397
//
398
//        // ============================================================
399
//        // ============================================================
400
//        // ============================================================
401
//
402
//
403
//        public SLDExtent createSLDExtent(String version)
404
//                        throws UnsupportedSLDVersionException{
405
//
406
//                if (version.compareTo(VERSION) == 0) {
407
//                        return new SLDExtent1_0_0();
408
//                } else {
409
//                        throw new UnsupportedSLDVersionException(version);
410
//                }
411
//        }
412
//
413
//        public SLDFeatureTypeConstraint createSLDFeatureTypeConstraint(
414
//                        String version)  throws UnsupportedSLDVersionException{
415
//
416
//                if (version.compareTo(VERSION) == 0) {
417
//                        return new SLDFeatureTypeConstraint1_0_0();
418
//                } else {
419
//                        throw new UnsupportedSLDVersionException(version);
420
//                }
421
//        }
422
//
423
//        public SLDLayerFeatureConstraints createSLDLayerFeatureConstraints(
424
//                        String version)  throws UnsupportedSLDVersionException{
425
//
426
//                if (version.compareTo(VERSION) == 0) {
427
//                        return new SLDLayerFeatureConstraints1_0_0();
428
//                } else {
429
//                        throw new UnsupportedSLDVersionException(version);
430
//                }
431
//        }
432
//
433
//        public SLDExternalGraphic createSLDExternalGraphic(String version)
434
//                        throws UnsupportedSLDVersionException{
435
//
436
//                if (version.compareTo(VERSION) == 0) {
437
//                        return new SLDExternalGraphic1_0_0();
438
//                } else {
439
//                        throw new UnsupportedSLDVersionException(version);
440
//                }
441
//        }
442
//
443
//        public SLDFill createSLDFill(String version)  throws UnsupportedSLDVersionException{
444
//
445
//                if (version.compareTo(VERSION) == 0) {
446
//                        return new SLDFill1_0_0();
447
//                } else {
448
//                        throw new UnsupportedSLDVersionException(version);
449
//                }
450
//        }
451
//
452
//        public SLDGraphic createSLDGraphic(String version)
453
//                        throws UnsupportedSLDVersionException{
454
//                if (version.compareTo(VERSION) == 0) {
455
//                        return new SLDGraphic1_0_0();
456
//                } else {
457
//                        throw new UnsupportedSLDVersionException(version);
458
//                }
459
//        }
460
//
461
//        public SLDLegendGraphic createSLDLegendGraphic(String version)
462
//                        throws UnsupportedSLDVersionException{
463
//
464
//                if (version.compareTo(VERSION) == 0) {
465
//                        return new SLDLegendGraphic1_0_0();
466
//                } else {
467
//                        throw new UnsupportedSLDVersionException(version);
468
//                }
469
//        }
470
//
471
//        public SLDMark createSLDMark(String version)
472
//                        throws UnsupportedSLDVersionException{
473
//
474
//                if (version.compareTo(VERSION) == 0) {
475
//                        return new SLDMark1_0_0();
476
//                } else {
477
//                        throw new UnsupportedSLDVersionException(version);
478
//                }
479
//        }
480
//
481
//        public SLDStroke createSLDStroke(String version)
482
//                        throws UnsupportedSLDVersionException{
483
//
484
//                if (version.compareTo(VERSION) == 0) {
485
//                        return new SLDStroke1_0_0();
486
//                } else {
487
//                        throw new UnsupportedSLDVersionException(version);
488
//                }
489
//        }
490
//
491
//        public SLDNamedLayer createSLDNamedLayer(String version)
492
//                        throws UnsupportedSLDVersionException{
493
//
494
//                if (version.compareTo(VERSION) == 0) {
495
//                        return new SLDNamedLayer1_0_0();
496
//                } else {
497
//                        throw new UnsupportedSLDVersionException(version);
498
//                }
499
//        }
500
//
501
//        public SLDUserLayer createSLDUserLayer(String version)
502
//                        throws UnsupportedSLDVersionException{
503
//
504
//                if (version.compareTo(VERSION) == 0) {
505
//                        return new SLDUserLayer1_0_0();
506
//                } else {
507
//                        throw new UnsupportedSLDVersionException(version);
508
//                }
509
//        }
510
//
511
//        public SLDRule createSLDRule(String version)
512
//                        throws UnsupportedSLDVersionException{
513
//
514
//                if (version.compareTo(VERSION) == 0) {
515
//                        return new SLDRule1_0_0();
516
//                } else {
517
//                        throw new UnsupportedSLDVersionException(version);
518
//                }
519
//        }
520
//
521
//        public SLDFeatureTypeStyle createSLDFeatureTypeStyle(String version)
522
//                         throws UnsupportedSLDVersionException{
523
//
524
//                if (version.compareTo(VERSION) == 0) {
525
//                        return new SLDFeatureTypeStyle1_0_0();
526
//                } else {
527
//                        throw new UnsupportedSLDVersionException(version);
528
//                }
529
//        }
530
//
531
//        public SLDNamedStyle createSLDNamedStyle(String version)
532
//                        throws UnsupportedSLDVersionException {
533
//
534
//                if (version.compareTo(VERSION) == 0) {
535
//                        return new SLDNamedStyle1_0_0();
536
//                } else {
537
//                        throw new UnsupportedSLDVersionException(version);
538
//                }
539
//        }
540
//
541
//        public SLDUserStyle createSLDUserStyle(String version)
542
//                         throws UnsupportedSLDVersionException {
543
//
544
//                if (version.compareTo(VERSION) == 0) {
545
//                        return new SLDUserStyle1_0_0();
546
//                } else {
547
//                        throw new UnsupportedSLDVersionException(version);
548
//                }
549
//        }
550
//
551
//        public SLDPointSymbolizer createSLDPointSymbolizer(String version)
552
//                        throws UnsupportedSLDVersionException{
553
//
554
//                if (version.compareTo(VERSION) == 0) {
555
//                        return new SLDPointSymbolizer1_0_0();
556
//                } else {
557
//                        throw new UnsupportedSLDVersionException(version);
558
//                }
559
//        }
560
//
561
//        public SLDMultiPointSymbolizer createSLDMultiPointSymbolizer(String version)
562
//                        throws UnsupportedSLDVersionException{
563
//
564
//                if (version.compareTo(VERSION) == 0) {
565
//                        return new SLDMultiPointSymbolizer1_0_0();
566
//                } else {
567
//                        throw new UnsupportedSLDVersionException(version);
568
//                }
569
//        }
570
//
571
//        public SLDLineSymbolizer createSLDLineSymbolizer(String version)
572
//                        throws UnsupportedSLDVersionException {
573
//
574
//                if (version.compareTo(VERSION) == 0) {
575
//                        return new SLDLineSymbolizer1_0_0();
576
//                } else {
577
//                        throw new UnsupportedSLDVersionException(version);
578
//                }
579
//        }
580
//
581
//        public SLDMultiLineSymbolizer createSLDMultiLineSymbolizer(String version)
582
//                        throws UnsupportedSLDVersionException {
583
//
584
//                if (version.compareTo(VERSION) == 0) {
585
//                        return new SLDMultiLineSymbolizer1_0_0();
586
//                } else {
587
//                        throw new UnsupportedSLDVersionException(version);
588
//                }
589
//        }
590
//
591
//        public SLDPolygonSymbolizer createSLDPolygonSymbolizer(String version)
592
//                        throws UnsupportedSLDVersionException{
593
//
594
//                if (version.compareTo(VERSION) == 0) {
595
//                        return new SLDPolygonSymbolizer1_0_0();
596
//                } else {
597
//                        throw new UnsupportedSLDVersionException(version);
598
//                }
599
//        }
600
//
601
//        public SLDMultiPolygonSymbolizer createSLDMultiPolygonSymbolizer(
602
//                        String version)  throws UnsupportedSLDVersionException{
603
//
604
//                if (version.compareTo(VERSION) == 0) {
605
//                        return new SLDMultiPolygonSymbolizer1_0_0();
606
//                } else {
607
//                        throw new UnsupportedSLDVersionException(version);
608
//                }
609
//        }
610
//
611
//        public SLDMultiShapeSymbolizer createSLDMultiShapeSymbolizer(String version)
612
//                        throws UnsupportedSLDVersionException {
613
//
614
//                if (version.compareTo(VERSION) == 0) {
615
//                        return new SLDMultiShapeSymbolizer1_0_0();
616
//                } else {
617
//                        throw new UnsupportedSLDVersionException(version);
618
//                }
619
//        }
620
//
621
//        public List<String> getSupportedVersions() {
622
//
623
//                List<String> resp = new ArrayList<String>();
624
//                resp.add(VERSION);
625
//                return resp;
626
//        }
627
628
629
630
631
632
633
}