Statistics
| Revision:

root / tmp / trunk / cliente / WorkSpace_WPSClient / extWPSCallejero / src / es / logex / gvsig / fmap / layers / LegendManager.java @ 26601

History | View | Annotate | Download (3.85 KB)

1
package es.logex.gvsig.fmap.layers;
2

    
3
import java.awt.Component;
4
import java.io.File;
5
import java.io.FileInputStream;
6
import java.io.FileNotFoundException;
7
import java.io.InputStreamReader;
8
import java.io.Reader;
9
import java.io.UnsupportedEncodingException;
10

    
11

    
12
import javax.swing.JOptionPane;
13

    
14
import org.exolab.castor.xml.MarshalException;
15
import org.exolab.castor.xml.ValidationException;
16

    
17

    
18
import com.hardcode.gdbms.engine.instruction.FieldNotFoundException;
19
import com.iver.andami.PluginServices;
20
import com.iver.cit.gvsig.fmap.DriverException;
21
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
22
import com.iver.cit.gvsig.fmap.layers.XMLException;
23
import com.iver.cit.gvsig.fmap.rendering.Legend;
24
import com.iver.cit.gvsig.fmap.rendering.LegendFactory;
25
import com.iver.cit.gvsig.fmap.rendering.VectorialLegend;
26
import com.iver.utiles.XMLEntity;
27
import com.iver.utiles.xml.XMLEncodingUtils;
28
import com.iver.utiles.xmlEntity.generate.XmlTag;
29

    
30
public class LegendManager {
31
        
32
        public void setLegend(FLyrVect layer, String path) throws FieldNotFoundException {
33
                
34
                //File xmlFile = new File(file.getAbsolutePath());
35
                File xmlFile = new File(path);
36
                try {
37
                        String encoding = XMLEncodingUtils.getEncoding(new FileInputStream(xmlFile));
38
                        InputStreamReader reader=null;
39
                        if (encoding!=null) {
40
                                try {
41
                                        reader = new InputStreamReader(new FileInputStream(xmlFile), encoding);
42
                                        layer.setLegend(readLegend(reader, true));
43
                                } catch (UnsupportedEncodingException e) {
44
                                        reader = new InputStreamReader(new FileInputStream(xmlFile),"ISO-8859-1");
45
                                        try {
46
                                                layer.setLegend(readLegend(reader, false));
47
                                        } catch (UnsupportedEncodingException e1) {
48
                                                JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this, e1.getLocalizedMessage()));
49
                                                return;
50
                                        }
51
                                }
52
                        }
53
                        else {
54
                                reader = new InputStreamReader(new FileInputStream(xmlFile),"UTF-8");
55
                                try {
56
                                        layer.setLegend(readLegend(reader, false));
57
                                } catch (UnsupportedEncodingException e1) {
58
                                        JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this, e1.getLocalizedMessage()));
59
                                        return;
60
                                }
61
                        }
62
                } catch (FileNotFoundException e) {
63
                        e.printStackTrace();
64
                        
65
                } catch (UnsupportedEncodingException e) {
66
                        e.printStackTrace();
67
                        
68
                } catch (DriverException de) {
69
                        de.printStackTrace();
70
                }
71
        }
72
        
73
        private  VectorialLegend readLegend(Reader reader, boolean encodingFollowed) throws UnsupportedEncodingException {
74
                Legend legend = null;
75

    
76
                try {
77
                        XmlTag tag = (XmlTag) XmlTag.unmarshal(reader);
78
                        XMLEntity xml=new XMLEntity(tag);
79
                        
80
                        if (encodingFollowed) {
81
                                if (xml.contains("followHeaderEncoding")) {
82
                                        boolean useEncoding = xml.getBooleanProperty("followHeaderEncoding");
83
                                        if (!useEncoding) {
84
                                                throw new UnsupportedEncodingException("the encoding specified in the xml header is not safe");
85
                                        }
86
                                }
87
                                else {
88
                                        // Old projects didn't contain followHeaderEncoding and they were
89
                                        // not correctly encoded. We throw an exception now, and we'll try
90
                                        // to reopen the project
91
                                        // using the default system encoding.
92
                                        throw new UnsupportedEncodingException("the encoding specified in the xml header is not safe");
93
                                }
94
                        }
95

    
96
                        legend = LegendFactory.createFromXML(xml);
97
                        
98
                }  catch (XMLException e) {
99
                        PluginServices.getLogger().error(PluginServices.getText(this, "formato_incorrecto"),e);
100
                        JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this, "formato_incorrecto"));
101
                        //NotificationManager.addError("Al leer el proyecto", e);
102
                } catch (MarshalException e) {
103
                        // TODO Auto-generated catch block
104
                        e.printStackTrace();
105
                } catch (ValidationException e) {
106
                        // TODO Auto-generated catch block
107
                        e.printStackTrace();
108
                } 
109
                return (VectorialLegend)legend;
110
        }
111

    
112
        public VectorialLegend getLegend() {
113
                // TODO Auto-generated method stub
114
                return null;
115
        }
116

    
117
}