Statistics
| Revision:

gvsig-sldtools / org.gvsig.sld / org.gvsig.sldsupport / org.gvsig.sldsupport.lib / org.gvsig.sldsupport.lib.impl / src / main / java / org / gvsig / sldsupport / impl / util / SLDUtils.java @ 46

History | View | Annotate | Download (6.48 KB)

1
/*******************************************************************************
2
 *
3
 *   gvSIG. Desktop Geographic Information System.
4
 *  
5
 *   Copyright (C) 2007-2013 gvSIG Association.
6
 *  
7
 *   This program is free software; you can redistribute it and/or
8
 *   modify it under the terms of the GNU General Public License
9
 *   as published by the Free Software Foundation; either version 3
10
 *   of the License, or (at your option) any later version.
11
 *  
12
 *   This program is distributed in the hope that it will be useful,
13
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 *   GNU General Public License for more details.
16
 *  
17
 *   You should have received a copy of the GNU General Public License
18
 *   along with this program; if not, write to the Free Software
19
 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20
 *   MA  02110-1301, USA.
21
 *  
22
 *   For any additional information, do not hesitate to contact us
23
 *   at info AT gvsig.com, or visit our website www.gvsig.com.
24
 *   
25
 *******************************************************************************/
26
package org.gvsig.sldsupport.impl.util;
27

    
28
import java.awt.geom.Point2D;
29
import java.io.File;
30
import java.io.FileInputStream;
31
import java.io.IOException;
32
import java.text.DecimalFormat;
33
import java.text.DecimalFormatSymbols;
34
import java.util.ArrayList;
35
import java.util.List;
36

    
37
import org.slf4j.Logger;
38
import org.slf4j.LoggerFactory;
39
import org.xmlpull.v1.XmlPullParser;
40
import org.xmlpull.v1.XmlPullParserException;
41

    
42
import org.gvsig.sldsupport.exception.SLDReadException;
43
import org.gvsig.sldsupport.impl.parser.ExtendedKXmlParser;
44
import org.gvsig.sldsupport.sld.SLDTags;
45

    
46
/**
47
 * 
48
 * Basic symbology utility methods for SLD version 1.0.0
49
 * 
50
 * @author jldominguez
51
 * 
52
 */
53
public class SLDUtils {
54

    
55
    private static Logger logger = LoggerFactory.getLogger(SLDUtils.class);
56
    public static final String DEFAULT_ENCODING = "utf-8";
57

    
58
    public static final String VERSION_1_0_0 = "1.0.0";
59
    public static final String VERSION_1_1_0 = "1.1.0";
60
    public static final String DEFAULT_VERSION = VERSION_1_0_0;
61

    
62
    public static DecimalFormat df = null;
63

    
64
    static {
65
        DecimalFormatSymbols dformater_rules = new DecimalFormatSymbols();
66
        dformater_rules.setDecimalSeparator('.');
67
        df = new DecimalFormat("##########.0##########", dformater_rules);
68
    }
69

    
70
    public static String detectVersion(File infile)
71
        throws XmlPullParserException, IOException, SLDReadException {
72

    
73
        XmlPullParser parser = new ExtendedKXmlParser();
74
        FileInputStream fis = new FileInputStream(infile);
75
        // xmlSchemaParser = new XMLSchemaParser();
76
        parser.setInput(fis, "utf-8");
77

    
78
        int tag = parser.getEventType();
79
        if (tag == XmlPullParser.START_DOCUMENT) {
80
            parser.nextTag();
81
        } else {
82
            if (tag != XmlPullParser.START_TAG) {
83
                throw new IOException(
84
                    "Misplaced input stream (it's not before opening tag)");
85
            }
86
        }
87
        String value = parser.getAttributeValue(null, SLDTags.VERSION_ATTR);
88
        fis.close();
89
        if (value == null) {
90
            throw new SLDReadException("Version attribute not found");
91
        } else {
92
            return value;
93
        }
94
    }
95

    
96
    public static String getTextUntilTag(XmlPullParser parser)
97
        throws XmlPullParserException, IOException {
98

    
99
        String resp = "";
100
        /*
101
         * This allows entering the iteratioin even if current tag is not TEXT
102
         */
103
        int t = XmlPullParser.TEXT;
104
        while ((t != XmlPullParser.START_TAG) && (t != XmlPullParser.END_TAG)
105
            && (t != XmlPullParser.END_DOCUMENT)) {
106

    
107
            t = parser.getEventType();
108
            if (t == XmlPullParser.TEXT) {
109
                resp = resp + parser.getText();
110
            }
111
            t = parser.next();
112
        }
113
        return resp;
114
    }
115

    
116
    public static boolean isStr(String a, String b) {
117

    
118
        if ((a == null) && (b == null)) {
119
            return true;
120
        } else {
121
            if ((a == null) || (b == null)) {
122
                return false;
123
            } else {
124
                return a.compareToIgnoreCase(b) == 0;
125
            }
126
        }
127

    
128
    }
129

    
130
    /**
131
     * Returns -1 if not parseable
132
     */
133
    public static int parseInteger(String str) {
134

    
135
        int resp = -1;
136
        try {
137
            resp = Integer.parseInt(str);
138
        } catch (Exception ex) {
139
            logger.info("Unable to parse integer: " + str + ", returned -1");
140
        }
141
        return resp;
142
    }
143

    
144
    public static Point2D parsePair(String str) {
145

    
146
        Point2D resp = null;
147
        try {
148
            String[] xy = str.split(" ");
149
            double x = Double.parseDouble(xy[0]);
150
            double y = Double.parseDouble(xy[1]);
151
            resp = new Point2D.Double(x, y);
152
        } catch (Exception ex) {
153
            logger.info("Unable to parse doubles: " + str + ", returned 0,0");
154
            resp = new Point2D.Double(0, 0);
155

    
156
        }
157
        return resp;
158
    }
159

    
160
    /**
161
     * Returns -1 if not parseable
162
     */
163
    public static double parseDouble(String str) {
164

    
165
        double resp = -1;
166
        try {
167
            resp = Double.parseDouble(str);
168
        } catch (Exception ex) {
169
            logger.info("Unable to parse double: " + str + ", returned -1");
170
        }
171
        return resp;
172
    }
173

    
174
    public static String getAsString(List<Float> list, String sep) {
175

    
176
        if ((list == null) || (list.size() == 0)) {
177
            return "";
178
        }
179
        String resp = df.format(list.get(0).floatValue());
180
        for (int i = 1; i < list.size(); i++) {
181
            resp = resp + sep + df.format(list.get(i).floatValue());
182
        }
183
        return resp;
184
    }
185

    
186
    public static List<Float> getAsFloats(String value, String sep) {
187

    
188
        List<Float> resp = new ArrayList<Float>();
189
        if (value != null) {
190
            String[] pp = value.split(sep);
191
            float fi = 0;
192
            for (int i = 0; i < pp.length; i++) {
193
                try {
194
                    fi = Float.parseFloat(pp[i]);
195
                    resp.add(new Float(fi));
196
                } catch (Exception ex) {
197
                    logger.info("Unable to parse float in flaot array: "
198
                        + pp[i]);
199
                }
200
            }
201
        }
202
        return resp;
203
    }
204

    
205
}