root / branches / v2_0_0_prep / libraries / libDXF / src / org / gvsig / dxf / io / DxfGroup.java @ 29630
History | View | Annotate | Download (13.1 KB)
1 |
/*
|
---|---|
2 |
* Cresques Mapping Suite. Graphic Library for constructing mapping applications.
|
3 |
*
|
4 |
* Copyright (C) 2004-5.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 2
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
19 |
*
|
20 |
* For more information, contact:
|
21 |
*
|
22 |
* cresques@gmail.com
|
23 |
*/
|
24 |
package org.gvsig.dxf.io; |
25 |
|
26 |
import java.io.BufferedReader; |
27 |
import java.io.IOException; |
28 |
import java.text.DecimalFormat; |
29 |
import java.text.DecimalFormatSymbols; |
30 |
import java.util.Locale; |
31 |
|
32 |
|
33 |
/**
|
34 |
* Grupo Dxf (code, data). Auxiliar para leer ficheros dxf
|
35 |
*
|
36 |
* @author "Luis W. Sevilla" <sevilla_lui@gva.es>
|
37 |
* @author "Michel Michaud" (code from)
|
38 |
*/
|
39 |
public class DxfGroup { |
40 |
/*
|
41 |
def get_group(handle):
|
42 |
_code = int(handle.readline())
|
43 |
_dfun = get_data_type(_code)
|
44 |
_data = _dfun(handle.readline())
|
45 |
return (_code, _data)
|
46 |
*/
|
47 |
private static final DecimalFormatSymbols dfs = new DecimalFormatSymbols(Locale.US); |
48 |
private static final DecimalFormat[] decimalFormats = new DecimalFormat[] { |
49 |
new DecimalFormat("#0", |
50 |
dfs), |
51 |
new DecimalFormat("#0.0", |
52 |
dfs), |
53 |
new DecimalFormat("#0.00", |
54 |
dfs), |
55 |
new DecimalFormat("#0.000", |
56 |
dfs), |
57 |
new DecimalFormat("#0.0000", |
58 |
dfs), |
59 |
new DecimalFormat("#0.00000", |
60 |
dfs), |
61 |
new DecimalFormat("#0.000000", |
62 |
dfs), |
63 |
new DecimalFormat("#0.0000000", |
64 |
dfs), |
65 |
new DecimalFormat("#0.00000000", |
66 |
dfs), |
67 |
new DecimalFormat("#0.000000000", |
68 |
dfs), |
69 |
new DecimalFormat("#0.0000000000", |
70 |
dfs), |
71 |
new DecimalFormat("#0.00000000000", |
72 |
dfs), |
73 |
new DecimalFormat("#0.000000000000", |
74 |
dfs) |
75 |
}; |
76 |
int code;
|
77 |
Object data;
|
78 |
|
79 |
/**
|
80 |
* Constructor por defecto.
|
81 |
*/
|
82 |
public DxfGroup() {
|
83 |
code = -1;
|
84 |
data = null;
|
85 |
} |
86 |
|
87 |
/**
|
88 |
* Constructor habitual. Representa una entidad individual dentro del DXF
|
89 |
* @param code, ?ndice del dato dentro del DxfGroup
|
90 |
* @param data, el propio dato que queda almacenado en el DxfGroup
|
91 |
*/
|
92 |
public DxfGroup(int code, String data) { |
93 |
this.code = code;
|
94 |
this.data = data;
|
95 |
} |
96 |
|
97 |
/**
|
98 |
* Lee una entidad del DXF y la empaqueta en un DxfGroup.
|
99 |
* @param fi, BufferedReader mediante el cual accedemos al DXF
|
100 |
* @return DxfGroup con la informaci?n procedente del DXF
|
101 |
* @throws NumberFormatException
|
102 |
* @throws IOException
|
103 |
*/
|
104 |
public static DxfGroup read(BufferedReader fi) |
105 |
throws NumberFormatException, IOException { |
106 |
DxfGroup grp = null;
|
107 |
String txt = fi.readLine();
|
108 |
|
109 |
if (txt != null) { |
110 |
if (!txt.equals("")) { |
111 |
grp = new DxfGroup();
|
112 |
grp.code = Integer.parseInt(txt.trim());
|
113 |
grp.readData(fi); |
114 |
} else {
|
115 |
// Se trata de una linea en blanco y no se hace nada.
|
116 |
} |
117 |
} |
118 |
|
119 |
return grp;
|
120 |
} |
121 |
|
122 |
/**
|
123 |
* Devuelve el code
|
124 |
* @return
|
125 |
*/
|
126 |
public int getCode() { |
127 |
return code;
|
128 |
} |
129 |
|
130 |
/**
|
131 |
* Devuelve data
|
132 |
* @return
|
133 |
*/
|
134 |
public Object getData() { |
135 |
return data;
|
136 |
} |
137 |
|
138 |
/**
|
139 |
* Lee un dato individual dentro de un DxfGroup
|
140 |
* @param fi, BufferedReader
|
141 |
* @throws IOException
|
142 |
*/
|
143 |
private void readData(BufferedReader fi) throws IOException { |
144 |
String txt = fi.readLine().trim();
|
145 |
|
146 |
if ((0 <= code) && (code <= 9)) { |
147 |
data = txt; //_dfun = string_data
|
148 |
} else if ((10 <= code) && (code <= 59)) { |
149 |
data = new Double(Double.parseDouble(txt)); //_dfun = float_data |
150 |
} else if ((60 <= code) && (code <= 79)) { |
151 |
try {
|
152 |
data = new Integer(Integer.parseInt(txt)); //_dfun = int_data // 16-bit int |
153 |
} catch (java.lang.NumberFormatException e) {
|
154 |
data = new Integer((int) Double.parseDouble(txt)); |
155 |
} |
156 |
} else if ((90 <= code) && (code <= 99)) { |
157 |
data = new Integer(Integer.parseInt(txt)); //_dfun = int_data // 32-bit int |
158 |
} else if (code == 100) { |
159 |
data = txt; //_dfun = unicode_data
|
160 |
} else if (code == 102) { |
161 |
// Fran: Comentado para ganar velocidad.
|
162 |
//System.err.println("Dxf: codigo " + code + " no implementado."); //_dfun = unicode_data
|
163 |
} else if (code == 105) { |
164 |
data = txt; |
165 |
; //_dfun = handle_data
|
166 |
} else if ((110 <= code) && (code <= 139)) { |
167 |
data = new Double(Double.parseDouble(txt)); //_dfun = float_data // not in dxf spec |
168 |
} else if ((140 <= code) && (code <= 149)) { // says 147 in dxf spec |
169 |
data = new Double(Double.parseDouble(txt)); //_dfun = float_data |
170 |
} else if ((170 <= code) && (code <= 179)) { // says 175 in dxf spec |
171 |
data = new Integer(Integer.parseInt(txt)); //_dfun = int_data // 16-bit int |
172 |
} else if ((210 <= code) && (code <= 239)) { |
173 |
data = new Double(Double.parseDouble(txt)); //_dfun = float_data // del TEXT procendente de exportacion de microstation |
174 |
} else if ((270 <= code) && (code <= 279)) { |
175 |
data = new Integer(Integer.parseInt(txt)); //_dfun = int_data // not in dxf spec |
176 |
} else if ((280 <= code) && (code <= 289)) { |
177 |
data = new Integer(Integer.parseInt(txt)); //_dfun = int_data // 8-bit int |
178 |
} else if ((290 <= code) && (code <= 299)) { |
179 |
data = new Boolean(Boolean.getBoolean(txt)); //_dfun = bool_data |
180 |
} else if ((300 <= code) && (code <= 309)) { |
181 |
data = txt; //_dfun = string_data
|
182 |
} else if ((310 <= code) && (code <= 319)) { |
183 |
//_dfun = bin_data
|
184 |
//throw new IOException("Dxf: codigo "+code+" no implementado.");
|
185 |
} else if ((320 <= code) && (code <= 329)) { |
186 |
//_dfun = handle_data
|
187 |
//throw new IOException("Dxf: codigo "+code+" no implementado.");
|
188 |
} else if ((330 <= code) && (code <= 369)) { |
189 |
// Fran: Comentado para ganar velocidad.
|
190 |
//System.err.println("Dxf: codigo " + code + " no implementado."); //_dfun = hex_data
|
191 |
} else if ((370 <= code) && (code <= 379)) { |
192 |
data = new Integer(Integer.parseInt(txt)); //_dfun = int_data // 8-bit int |
193 |
} else if ((380 <= code) && (code <= 389)) { |
194 |
data = new Integer(Integer.parseInt(txt)); //_dfun = int_data // 8-bit int |
195 |
} else if ((390 <= code) && (code <= 399)) { |
196 |
data = txt; //_dfun = handle_data
|
197 |
} else if ((400 <= code) && (code <= 409)) { |
198 |
data = new Integer(Integer.parseInt(txt)); //_dfun = int_data // 16-bit int |
199 |
} else if ((410 <= code) && (code <= 419)) { |
200 |
data = txt; //_dfun = string_data
|
201 |
} else if (code == 999) { |
202 |
data = txt; //_dfun = string_data // comment
|
203 |
} else if ((1000 <= code) && (code <= 1009)) { |
204 |
data = txt; //_dfun = string_data
|
205 |
} else if ((1010 <= code) && (code <= 1059)) { |
206 |
data = new Double(Double.parseDouble(txt)); //_dfun = float_data |
207 |
} else if ((1060 <= code) && (code <= 1070)) { |
208 |
data = new Integer(Integer.parseInt(txt)); //_dfun = int_data // 16-bit int |
209 |
} else if (code == 1071) { |
210 |
data = new Integer(Integer.parseInt(txt)); //_dfun = int_data # 32-bit int |
211 |
} else {
|
212 |
throw new IOException("DxfReader: c?digo " + code + |
213 |
" desconocido.");
|
214 |
|
215 |
//raise ValueError, "Unexpected code: %d" % code
|
216 |
} |
217 |
|
218 |
//return _dfun
|
219 |
} |
220 |
|
221 |
/**
|
222 |
* Permite comparar dos objetos de la clase DxfGroup
|
223 |
* @param c, code
|
224 |
* @param s, data
|
225 |
* @return boolean
|
226 |
*/
|
227 |
public boolean equals(int c, String s) { |
228 |
if ((c == code) && (s.compareTo((String) data) == 0)) { |
229 |
return true; |
230 |
} |
231 |
|
232 |
return false; |
233 |
} |
234 |
|
235 |
/**
|
236 |
* Devuelve un dato concreto en forma de String tabulado
|
237 |
* @param code
|
238 |
* @return String
|
239 |
*/
|
240 |
public static String int34car(int code) { |
241 |
if (code < 10) { |
242 |
return " " + Integer.toString(code); |
243 |
} else if (code < 100) { |
244 |
return " " + Integer.toString(code); |
245 |
} else {
|
246 |
return Integer.toString(code); |
247 |
} |
248 |
} |
249 |
|
250 |
/**
|
251 |
* Devuelve un dato concreto en forma de String tabulado
|
252 |
* @param value
|
253 |
* @return String
|
254 |
*/
|
255 |
public static String int6car(int value) { |
256 |
String s = " " + Integer.toString(value); |
257 |
|
258 |
return s.substring(s.length() - 6, s.length()); |
259 |
} |
260 |
|
261 |
/**
|
262 |
* Convierte a String un dato del DxfGroup
|
263 |
* @param code
|
264 |
* @param value
|
265 |
* @return String
|
266 |
*/
|
267 |
public static String toString(int code, String value) { |
268 |
return int34car(code) + "\r\n" + value + "\r\n"; |
269 |
} |
270 |
|
271 |
/**
|
272 |
* Convierte a String un dato del DxfGroup
|
273 |
* @param code
|
274 |
* @param value
|
275 |
* @return String
|
276 |
*/
|
277 |
public static String toString(int code, int value) { |
278 |
return int34car(code) + "\r\n" + int6car(value) + "\r\n"; |
279 |
} |
280 |
|
281 |
/**
|
282 |
* Convierte a String un dato del DxfGroup
|
283 |
* @param code
|
284 |
* @param value
|
285 |
* @param decimalPartLength
|
286 |
* @return String
|
287 |
*/
|
288 |
public static String toString(int code, float value, int decimalPartLength) { |
289 |
return int34car(code) + "\r\n" + |
290 |
decimalFormats[decimalPartLength].format((double) value) +
|
291 |
"\r\n";
|
292 |
} |
293 |
|
294 |
/**
|
295 |
* Convierte a String un dato del DxfGroup
|
296 |
* @param code
|
297 |
* @param value
|
298 |
* @param decimalPartLength
|
299 |
* @return String
|
300 |
*/
|
301 |
public static String toString(int code, double value, int decimalPartLength) { |
302 |
return int34car(code) + "\r\n" + |
303 |
decimalFormats[decimalPartLength].format(value) + "\r\n";
|
304 |
} |
305 |
|
306 |
/**
|
307 |
* Convierte a String un dato del DxfGroup
|
308 |
* @param code
|
309 |
* @param value
|
310 |
* @return String
|
311 |
*/
|
312 |
public static String toString(int code, Object value) { |
313 |
if (value instanceof String) { |
314 |
return toString(code, (String) value); |
315 |
} else if (value instanceof Integer) { |
316 |
return toString(code, ((Integer) value).intValue()); |
317 |
} else if (value instanceof Double) { |
318 |
return toString(code, ((Double) value).floatValue(), 3); |
319 |
} else if (value instanceof Double) { |
320 |
return toString(code, ((Double) value).doubleValue(), 6); |
321 |
} else {
|
322 |
return toString(code, value.toString());
|
323 |
} |
324 |
} |
325 |
|
326 |
/**
|
327 |
* Convierte a String un dato del DxfGroup
|
328 |
*/
|
329 |
public String toString() { |
330 |
return toString(code, data);
|
331 |
} |
332 |
|
333 |
/**
|
334 |
* jmorell: Permite rellenar los datos. ?til en la escritura de DXFs.
|
335 |
* @param data The data to set.
|
336 |
*/
|
337 |
public void setData(Object data) { |
338 |
this.data = data;
|
339 |
} |
340 |
|
341 |
/**
|
342 |
* jmorell: Permite rellenar los c?digos. ?til en la escritura de DXFs.
|
343 |
* @param code The code to set.
|
344 |
*/
|
345 |
public void setCode(int code) { |
346 |
this.code = code;
|
347 |
} |
348 |
} |