svn-gvsig-desktop / branches / v02_desarrollo / libraries / sld / temp / org.gvsig.sldsupport.lib.impl / src / main / java / org / gvsig / sldsupport / impl / util / SLDUtils.java @ 40799
History | View | Annotate | Download (6.33 KB)
1 | 40784 | jldominguez | package org.gvsig.sldsupport.impl.util; |
---|---|---|---|
2 | |||
3 | import java.io.File; |
||
4 | 40799 | jldominguez | import java.io.FileInputStream; |
5 | 40786 | jldominguez | import java.io.FileReader; |
6 | 40784 | jldominguez | import java.io.IOException; |
7 | 40794 | jldominguez | import java.util.ArrayList; |
8 | import java.util.List; |
||
9 | 40784 | jldominguez | |
10 | import org.gvsig.sldsupport.exception.SLDReadException; |
||
11 | 40799 | jldominguez | import org.gvsig.sldsupport.impl.parser.ExtendedKXmlParser; |
12 | import org.gvsig.sldsupport.sld.SLDRoot; |
||
13 | 40786 | jldominguez | import org.gvsig.sldsupport.sld.SLDTags; |
14 | 40799 | jldominguez | import org.gvsig.sldsupport.sld.layer.SLDLayer; |
15 | 40786 | jldominguez | import org.kxml2.io.KXmlParser; |
16 | 40784 | jldominguez | import org.slf4j.Logger; |
17 | import org.slf4j.LoggerFactory; |
||
18 | 40786 | jldominguez | import org.xmlpull.v1.XmlPullParser; |
19 | import org.xmlpull.v1.XmlPullParserException; |
||
20 | 40784 | jldominguez | |
21 | /**
|
||
22 | *
|
||
23 | * Basic symbology utility methods for SLD version 1.0.0
|
||
24 | *
|
||
25 | * @author jldominguez
|
||
26 | *
|
||
27 | */
|
||
28 | public class SLDUtils { |
||
29 | |||
30 | private static Logger logger = LoggerFactory.getLogger(SLDUtils.class); |
||
31 | 40786 | jldominguez | public static final String FILE_EXTENSION = "sld"; |
32 | public static final String DEFAULT_ENCODING = "ISO-8859-1"; |
||
33 | 40795 | jldominguez | |
34 | public static final String VERSION_1_0_0 = "1.0.0"; |
||
35 | public static final String VERSION_1_1_0 = "1.1.0"; |
||
36 | 40784 | jldominguez | |
37 | 40786 | jldominguez | // public static void writeSLDLayer(
|
38 | // ISLDLayer sld_layer, File out_file, String version)
|
||
39 | // throws IOException, SLDWriteException {
|
||
40 | //
|
||
41 | // DecimalFormatSymbols dformater_rules = new DecimalFormatSymbols ();
|
||
42 | // dformater_rules.setDecimalSeparator ('.');
|
||
43 | // DecimalFormat df = new DecimalFormat("##########.0##########",dformater_rules);
|
||
44 | //
|
||
45 | // HashMap<String, String> attributes = new HashMap<String, String>();
|
||
46 | // attributes.put("xsi:schemaLocation","http://www.opengis.net/sld StyledLayerDescriptor.xsd");
|
||
47 | // attributes.put("xmlns","http://www.opengis.net/sld");
|
||
48 | // attributes.put("xmlns:ogc","http://www.opengis.net/ogc");
|
||
49 | // attributes.put("xmlns:xlink","http://www.w3.org/1999/xlink");
|
||
50 | // attributes.put("xmlns:xsi","http://www.w3.org/2001/XMLSchema-instance");
|
||
51 | // attributes.put(SLDTags.VERSION_ATTR, version);
|
||
52 | //
|
||
53 | // XmlBuilder xmlBuilder = new XmlBuilder();
|
||
54 | // xmlBuilder.setEncoding(DEFAULT_ENCODING);
|
||
55 | // xmlBuilder.writeHeader();
|
||
56 | // xmlBuilder.openTag(SLDTags.SLD_ROOT, attributes);
|
||
57 | // xmlBuilder.openTag(SLDTags.NAMEDLAYER);
|
||
58 | // xmlBuilder.writeTag(SLDTags.NAME, sld_layer.getName());
|
||
59 | // xmlBuilder.openTag(SLDTags.USERSTYLE);
|
||
60 | // xmlBuilder.openTag(SLDTags.FEATURETYPESTYLE);
|
||
61 | //
|
||
62 | // xmlBuilder.writeTag(SLDTags.FEATURETYPENAME, "Feature");
|
||
63 | //
|
||
64 | // ArrayList<AbstractSLDStyle> stys = sld_layer.getLayerStyles();
|
||
65 | // if (stys == null || stys.size() == 0) {
|
||
66 | // throw new SLDWriteException(new Exception("No styles found."));
|
||
67 | // }
|
||
68 | //
|
||
69 | // AbstractSLDStyle asty = stys.get(0);
|
||
70 | //
|
||
71 | // if (!(asty instanceof SLDUserStyle)) {
|
||
72 | // throw new SLDWriteException(new Exception("Style not supported: " + asty.getName()));
|
||
73 | // }
|
||
74 | //
|
||
75 | // SLDUserStyle usty = (SLDUserStyle) asty;
|
||
76 | // ArrayList<SLDFeatureTypeStyle> ftstys = usty.getFeatureTypeStyle();
|
||
77 | //
|
||
78 | // if (ftstys == null || ftstys.size() == 0) {
|
||
79 | // throw new SLDWriteException(new Exception("No feature styles found."));
|
||
80 | // }
|
||
81 | //
|
||
82 | // SLDFeatureTypeStyle ftsty = ftstys.get(0);
|
||
83 | // ArrayList<SLDRule> rules = ftsty.getRules();
|
||
84 | //
|
||
85 | // if (rules == null || rules.size() == 0) {
|
||
86 | // throw new SLDWriteException(new Exception("No rules found."));
|
||
87 | // }
|
||
88 | //
|
||
89 | // SLDRule rule = null;
|
||
90 | // for (int i=0; i<rules.size(); i++) {
|
||
91 | // rule = rules.get(i);
|
||
92 | // xmlBuilder.writeRaw(rule.toXML());
|
||
93 | // }
|
||
94 | //
|
||
95 | // // ================
|
||
96 | //
|
||
97 | // xmlBuilder.closeTag();
|
||
98 | // xmlBuilder.closeTag();
|
||
99 | // xmlBuilder.closeTag();
|
||
100 | // xmlBuilder.closeTag();
|
||
101 | //
|
||
102 | // FileOutputStream fos = new FileOutputStream(out_file);
|
||
103 | // OutputStreamWriter writer = new OutputStreamWriter(fos, DEFAULT_ENCODING);
|
||
104 | //// FileWriter writer = new FileWriter(file.getAbsolutePath());
|
||
105 | // writer.write( xmlBuilder.getXML());
|
||
106 | // writer.close();
|
||
107 | // fos.close();
|
||
108 | // }
|
||
109 | |||
110 | 40799 | jldominguez | public static String detectVersion(File infile) |
111 | 40786 | jldominguez | throws XmlPullParserException, IOException, SLDReadException { |
112 | 40784 | jldominguez | |
113 | 40799 | jldominguez | XmlPullParser parser = new ExtendedKXmlParser();
|
114 | FileInputStream fis = new FileInputStream(infile); |
||
115 | 40786 | jldominguez | // xmlSchemaParser = new XMLSchemaParser();
|
116 | 40799 | jldominguez | parser.setInput(fis, "ISO-8859-1");
|
117 | 40784 | jldominguez | |
118 | 40799 | jldominguez | int tag = parser.getEventType();
|
119 | if (tag == XmlPullParser.START_DOCUMENT) {
|
||
120 | parser.nextTag(); |
||
121 | } else {
|
||
122 | if (tag != XmlPullParser.START_TAG) {
|
||
123 | throw new IOException( |
||
124 | "Misplaced input stream (it's not before opening tag)");
|
||
125 | } |
||
126 | } |
||
127 | String value = parser.getAttributeValue(null, SLDTags.VERSION_ATTR); |
||
128 | fis.close(); |
||
129 | if (value == null) { |
||
130 | throw new SLDReadException("Version attribute not found"); |
||
131 | } else {
|
||
132 | 40786 | jldominguez | return value;
|
133 | 40799 | jldominguez | } |
134 | 40789 | jldominguez | } |
135 | 40784 | jldominguez | |
136 | 40799 | jldominguez | |
137 | |||
138 | |||
139 | 40796 | jldominguez | public static String getTextUntilTag(XmlPullParser parser) |
140 | throws XmlPullParserException, IOException { |
||
141 | |||
142 | String resp = ""; |
||
143 | /*
|
||
144 | * This allows entering the iteratioin even if current tag is not TEXT
|
||
145 | */
|
||
146 | int t = XmlPullParser.TEXT;
|
||
147 | while (t != XmlPullParser.START_TAG
|
||
148 | && t != XmlPullParser.END_TAG |
||
149 | && t != XmlPullParser.END_DOCUMENT) { |
||
150 | |||
151 | t = parser.getEventType(); |
||
152 | if (t == XmlPullParser.TEXT) {
|
||
153 | resp = resp + parser.getText(); |
||
154 | } |
||
155 | t = parser.next(); |
||
156 | } |
||
157 | return resp;
|
||
158 | } |
||
159 | |||
160 | 40789 | jldominguez | public static boolean isStr(String a, String b) { |
161 | |||
162 | if (a == null && b == null) { |
||
163 | return true; |
||
164 | } else {
|
||
165 | if (a == null || b == null) { |
||
166 | return false; |
||
167 | } else {
|
||
168 | return a.compareToIgnoreCase(b) == 0; |
||
169 | } |
||
170 | } |
||
171 | |||
172 | } |
||
173 | 40786 | jldominguez | |
174 | 40793 | jldominguez | /**
|
175 | * Returns -1 if not parseable
|
||
176 | */
|
||
177 | public static int parseInteger(String str) { |
||
178 | |||
179 | int resp = -1; |
||
180 | try {
|
||
181 | resp = Integer.parseInt(str);
|
||
182 | 40794 | jldominguez | } catch (Exception ex) { |
183 | logger.info("Unable to parse integer: " + str + ", returned -1"); |
||
184 | } |
||
185 | 40793 | jldominguez | return resp;
|
186 | } |
||
187 | 40786 | jldominguez | |
188 | 40793 | jldominguez | /**
|
189 | * Returns -1 if not parseable
|
||
190 | */
|
||
191 | public static double parseDouble(String str) { |
||
192 | |||
193 | double resp = -1; |
||
194 | try {
|
||
195 | resp = Double.parseDouble(str);
|
||
196 | 40794 | jldominguez | } catch (Exception ex) { |
197 | logger.info("Unable to parse double: " + str + ", returned -1"); |
||
198 | } |
||
199 | 40793 | jldominguez | return resp;
|
200 | } |
||
201 | 40794 | jldominguez | |
202 | public static List<Float> getAsFloats(String value, String sep) { |
||
203 | |||
204 | List<Float> resp = new ArrayList<Float>(); |
||
205 | if (value != null) { |
||
206 | String[] pp = value.split(sep); |
||
207 | float fi = 0; |
||
208 | for (int i=0; i<pp.length; i++) { |
||
209 | try {
|
||
210 | fi = Float.parseFloat(pp[i]);
|
||
211 | resp.add(new Float(fi)); |
||
212 | } catch (Exception ex) { |
||
213 | logger.info("Unable to parse float in flaot array: " + pp[i]);
|
||
214 | } |
||
215 | } |
||
216 | } |
||
217 | return resp;
|
||
218 | } |
||
219 | 40789 | jldominguez | |
220 | 40793 | jldominguez | |
221 | |||
222 | 40784 | jldominguez | } |