01: package org.geotools.catalog.styling;
02:
03: import java.io.File;
04: import java.io.IOException;
05: import java.net.URI;
06: import java.net.URISyntaxException;
07: import java.util.logging.Level;
08:
09: import org.geotools.catalog.AbstractFileGeoResource;
10: import org.geotools.catalog.AbstractFileService;
11: import org.geotools.catalog.GeoResourceInfo;
12: import org.geotools.catalog.defaults.DefaultGeoResourceInfo;
13: import org.geotools.styling.SLDParser;
14: import org.geotools.styling.SLDTransformer;
15: import org.geotools.styling.Style;
16: import org.geotools.styling.StyleFactory;
17: import org.geotools.styling.StyleFactoryFinder;
18: import org.geotools.styling.StyledLayerDescriptor;
19: import org.geotools.util.ProgressListener;
20:
21: public class SLDGeoResource extends AbstractFileGeoResource {
22:
23: /**
24: * Parsed style object.
25: */
26: StyledLayerDescriptor sld;
27:
28: public SLDGeoResource(AbstractFileService service, File file) {
29: super (service, file);
30: }
31:
32: public boolean canResolve(Class adaptee) {
33: if (adaptee == null)
34: return false;
35:
36: if (adaptee.isAssignableFrom(StyledLayerDescriptor.class))
37: return true;
38:
39: return super .canResolve(adaptee);
40: }
41:
42: public Object resolve(Class adaptee, ProgressListener monitor)
43: throws IOException {
44: if (adaptee == null)
45: return null;
46:
47: if (adaptee.isAssignableFrom(StyledLayerDescriptor.class)) {
48: return style(monitor);
49: }
50:
51: return super .resolve(adaptee, monitor);
52:
53: }
54:
55: public GeoResourceInfo getInfo(ProgressListener monitor)
56: throws IOException {
57: StyledLayerDescriptor sld = style(monitor);
58:
59: URI schema = null;
60: try {
61: schema = new URI(SLDServiceFactory.SLD_NAMESPACE);
62: } catch (URISyntaxException e) {
63: }
64:
65: String[] keywords = new String[] { "sld",
66: "Styled Layer Descriptor" };
67:
68: return new DefaultGeoResourceInfo(sld.getTitle(),
69: sld.getName(), sld.getAbstract(), schema, null, null,
70: keywords, null);
71: }
72:
73: protected StyledLayerDescriptor style(ProgressListener monitor)
74: throws IOException {
75: if (sld == null) {
76: synchronized (this ) {
77: if (sld == null) {
78: StyleFactory styleFactory = StyleFactoryFinder
79: .createStyleFactory();
80:
81: try {
82: SLDParser parser = new SLDParser(styleFactory,
83: getFile());
84: sld = parser.parseSLD();
85: setMessage(null);
86: } catch (Throwable t) {
87: String msg = "Error parsing sld: " + getFile();
88: logger.log(Level.WARNING, msg, t);
89: setMessage(t);
90: }
91:
92: }
93: }
94: }
95:
96: return sld;
97: }
98: }
|