01: package net.refractions.udig.style.sld.editor;
02:
03: import java.io.File;
04: import java.io.FileWriter;
05: import java.io.IOException;
06:
07: import net.refractions.udig.project.internal.Layer;
08: import net.refractions.udig.style.sld.SLDPlugin;
09: import net.refractions.udig.style.sld.internal.Messages;
10:
11: import org.eclipse.core.runtime.IProgressMonitor;
12: import org.geotools.styling.SLDTransformer;
13: import org.geotools.styling.StyledLayerDescriptor;
14:
15: public class ExportSLD extends ExportTo {
16:
17: @Override
18: public boolean canExport(Object target) {
19: return super .canExport(target);
20: }
21:
22: @Override
23: public String defaultName(Object target) {
24: String filename = null;
25: if (target instanceof Layer) {
26: Layer layer = (Layer) target;
27: filename = layer.getName();
28: } else if (target instanceof StyledLayerDescriptor) {
29: StyledLayerDescriptor sld = (StyledLayerDescriptor) target;
30: filename = sld.getTitle();
31: if (filename == null)
32: filename = sld.getName();
33: }
34: if (filename == null)
35: filename = Messages.ImportExport_new;
36: filename = filename.replace(':', '_');
37: return filename + " " + Messages.ImportExport_selected; //$NON-NLS-1$
38: }
39:
40: @Override
41: public String prompt(Object target) {
42: return Messages.ExportSLD_export;
43: }
44:
45: @Override
46: public String[] getExtentions() {
47: return addTo(new String[] { "sld" }, super .getExtentions()); //$NON-NLS-1$
48: }
49:
50: @Override
51: public void exportTo(Object target, File file,
52: IProgressMonitor monitor) throws Exception {
53: SLDTransformer aTransformer = new SLDTransformer();
54: aTransformer.setIndentation(StyleEditor.INDENT);
55: String xml = ""; //$NON-NLS-1$
56: if (target instanceof StyledLayerDescriptor) {
57: StyledLayerDescriptor sld = (StyledLayerDescriptor) target;
58: xml = aTransformer.transform(sld);
59: } else {
60: SLDPlugin.log("StyledLayerDescriptor not found", null); //$NON-NLS-1$
61: throw (IOException) new IOException("SLD not found"); //$NON-NLS-1$
62: }
63: FileWriter w = new FileWriter(file);
64: w.write(xml);
65: w.close();
66: }
67:
68: @Override
69: public String[] getFilterNames() {
70: return addTo(
71: new String[] { Messages.ImportExportSLD_document },
72: super.getFilterNames());
73: }
74:
75: }
|