Statistics
| Revision:

svn-gvsig-desktop / tags / v1_1_Build_1008 / libraries / libCq CMS for java.old / src / org / cresques / io / DxfGroup.java @ 12520

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.cresques.io;
25

    
26
import java.io.BufferedReader;
27
import java.io.IOException;
28

    
29
import java.text.DecimalFormat;
30
import java.text.DecimalFormatSymbols;
31

    
32
import java.util.Locale;
33

    
34

    
35
/**
36
 * Grupo Dxf (code, data). Auxiliar para leer ficheros dxf
37
 *
38
 * @author "Luis W. Sevilla" <sevilla_lui@gva.es>
39
 * @author "Michel Michaud" (code from)
40
 */
41
public class DxfGroup {
42
    /*
43
    def get_group(handle):
44
    _code = int(handle.readline())
45
    _dfun = get_data_type(_code)
46
    _data = _dfun(handle.readline())
47
    return (_code, _data)
48
     */
49
    private static final DecimalFormatSymbols dfs = new DecimalFormatSymbols(Locale.US);
50
    private static final DecimalFormat[] decimalFormats = new DecimalFormat[] {
51
                                                              new DecimalFormat("#0",
52
                                                                                dfs),
53
                                                              new DecimalFormat("#0.0",
54
                                                                                dfs),
55
                                                              new DecimalFormat("#0.00",
56
                                                                                dfs),
57
                                                              new DecimalFormat("#0.000",
58
                                                                                dfs),
59
                                                              new DecimalFormat("#0.0000",
60
                                                                                dfs),
61
                                                              new DecimalFormat("#0.00000",
62
                                                                                dfs),
63
                                                              new DecimalFormat("#0.000000",
64
                                                                                dfs),
65
                                                              new DecimalFormat("#0.0000000",
66
                                                                                dfs),
67
                                                              new DecimalFormat("#0.00000000",
68
                                                                                dfs),
69
                                                              new DecimalFormat("#0.000000000",
70
                                                                                dfs),
71
                                                              new DecimalFormat("#0.0000000000",
72
                                                                                dfs),
73
                                                              new DecimalFormat("#0.00000000000",
74
                                                                                dfs),
75
                                                              new DecimalFormat("#0.000000000000",
76
                                                                                dfs)
77
                                                          };
78
    int code;
79
    Object data;
80
    
81
    /**
82
     * Constructor por defecto.
83
     */
84
    public DxfGroup() {
85
        code = -1;
86
        data = null;
87
    }
88
    
89
    /**
90
     * Constructor habitual. Representa una entidad individual dentro del DXF
91
     * @param code, ?ndice del dato dentro del DxfGroup
92
     * @param data, el propio dato que queda almacenado en el DxfGroup
93
     */
94
    public DxfGroup(int code, String data) {
95
        this.code = code;
96
        this.data = data;
97
    }
98
    
99
    /**
100
     * Lee una entidad del DXF y la empaqueta en un DxfGroup.
101
     * @param fi, BufferedReader mediante el cual accedemos al DXF
102
     * @return DxfGroup con la informaci?n procedente del DXF
103
     * @throws NumberFormatException
104
     * @throws IOException
105
     */
106
    public static DxfGroup read(BufferedReader fi)
107
                         throws NumberFormatException, IOException {
108
        DxfGroup grp = null;
109
        String txt = fi.readLine();
110

    
111
        if (txt != null) {
112
            if (!txt.equals("")) {
113
                grp = new DxfGroup();
114
                grp.code = Integer.parseInt(txt.trim());
115
                grp.readData(fi);
116
            } else {
117
                // Se trata de una linea en blanco y no se hace nada.
118
            }
119
        }
120

    
121
        return grp;
122
    }
123
    
124
    /**
125
     * Devuelve el code
126
     * @return
127
     */
128
    public int getCode() {
129
        return code;
130
    }
131
    
132
    /**
133
     * Devuelve data
134
     * @return
135
     */
136
    public Object getData() {
137
        return data;
138
    }
139
    
140
    /**
141
     * Lee un dato individual dentro de un DxfGroup
142
     * @param fi, BufferedReader
143
     * @throws IOException
144
     */
145
    private void readData(BufferedReader fi) throws IOException {
146
        String txt = fi.readLine().trim();
147

    
148
        if ((0 <= code) && (code <= 9)) {
149
            data = txt; //_dfun = string_data
150
        } else if ((10 <= code) && (code <= 59)) {
151
            data = new Double(Double.parseDouble(txt)); //_dfun = float_data
152
        } else if ((60 <= code) && (code <= 79)) {
153
            try {
154
                data = new Integer(Integer.parseInt(txt)); //_dfun = int_data // 16-bit int
155
            } catch (java.lang.NumberFormatException e) {
156
                data = new Integer((int) Double.parseDouble(txt));
157
            }
158
        } else if ((90 <= code) && (code <= 99)) {
159
            data = new Integer(Integer.parseInt(txt)); //_dfun = int_data // 32-bit int
160
        } else if (code == 100) {
161
            data = txt; //_dfun = unicode_data
162
        } else if (code == 102) {
163
            // Fran: Comentado para ganar velocidad.
164
            //System.err.println("Dxf: codigo " + code + " no implementado."); //_dfun = unicode_data
165
        } else if (code == 105) {
166
            data = txt;
167
            ; //_dfun = handle_data
168
        } else if ((110 <= code) && (code <= 139)) {
169
            data = new Double(Double.parseDouble(txt)); //_dfun = float_data // not in dxf spec
170
        } else if ((140 <= code) && (code <= 149)) { // says 147 in dxf spec
171
            data = new Double(Double.parseDouble(txt)); //_dfun = float_data
172
        } else if ((170 <= code) && (code <= 179)) { // says 175 in dxf spec
173
            data = new Integer(Integer.parseInt(txt)); //_dfun = int_data // 16-bit int
174
        } else if ((210 <= code) && (code <= 239)) {
175
            data = new Double(Double.parseDouble(txt)); //_dfun = float_data // del TEXT procendente de exportacion de microstation 
176
        } else if ((270 <= code) && (code <= 279)) {
177
            data = new Integer(Integer.parseInt(txt)); //_dfun = int_data // not in dxf spec
178
        } else if ((280 <= code) && (code <= 289)) {
179
            data = new Integer(Integer.parseInt(txt)); //_dfun = int_data // 8-bit int
180
        } else if ((290 <= code) && (code <= 299)) {
181
            data = new Boolean(Boolean.getBoolean(txt)); //_dfun = bool_data
182
        } else if ((300 <= code) && (code <= 309)) {
183
            data = txt; //_dfun = string_data
184
        } else if ((310 <= code) && (code <= 319)) {
185
            //_dfun = bin_data
186
            //throw new IOException("Dxf: codigo "+code+" no implementado.");
187
        } else if ((320 <= code) && (code <= 329)) {
188
            //_dfun = handle_data
189
            //throw new IOException("Dxf: codigo "+code+" no implementado.");
190
        } else if ((330 <= code) && (code <= 369)) {
191
            // Fran: Comentado para ganar velocidad.
192
            //System.err.println("Dxf: codigo " + code + " no implementado."); //_dfun = hex_data
193
        } else if ((370 <= code) && (code <= 379)) {
194
            data = new Integer(Integer.parseInt(txt)); //_dfun = int_data // 8-bit int
195
        } else if ((380 <= code) && (code <= 389)) {
196
            data = new Integer(Integer.parseInt(txt)); //_dfun = int_data // 8-bit int
197
        } else if ((390 <= code) && (code <= 399)) {
198
            data = txt; //_dfun = handle_data
199
        } else if ((400 <= code) && (code <= 409)) {
200
            data = new Integer(Integer.parseInt(txt)); //_dfun = int_data // 16-bit int
201
        } else if ((410 <= code) && (code <= 419)) {
202
            data = txt; //_dfun = string_data
203
        } else if (code == 999) {
204
            data = txt; //_dfun = string_data // comment
205
        } else if ((1000 <= code) && (code <= 1009)) {
206
            data = txt; //_dfun = string_data
207
        } else if ((1010 <= code) && (code <= 1059)) {
208
            data = new Double(Double.parseDouble(txt)); //_dfun = float_data
209
        } else if ((1060 <= code) && (code <= 1070)) {
210
            data = new Integer(Integer.parseInt(txt)); //_dfun = int_data // 16-bit int
211
        } else if (code == 1071) {
212
            data = new Integer(Integer.parseInt(txt)); //_dfun = int_data # 32-bit int
213
        } else {
214
            throw new IOException("DxfReader: c?digo " + code +
215
                                  " desconocido.");
216

    
217
            //raise ValueError, "Unexpected code: %d" % code
218
        }
219

    
220
        //return _dfun
221
    }
222
    
223
    /**
224
     * Permite comparar dos objetos de la clase DxfGroup
225
     * @param c, code
226
     * @param s, data
227
     * @return boolean
228
     */
229
    public boolean equals(int c, String s) {
230
        if ((c == code) && (s.compareTo((String) data) == 0)) {
231
            return true;
232
        }
233

    
234
        return false;
235
    }
236
    
237
    /**
238
     * Devuelve un dato concreto en forma de String tabulado
239
     * @param code
240
     * @return String
241
     */
242
    public static String int34car(int code) {
243
        if (code < 10) {
244
            return "  " + Integer.toString(code);
245
        } else if (code < 100) {
246
            return " " + Integer.toString(code);
247
        } else {
248
            return Integer.toString(code);
249
        }
250
    }
251
    
252
    /**
253
     * Devuelve un dato concreto en forma de String tabulado
254
     * @param value
255
     * @return String
256
     */
257
    public static String int6car(int value) {
258
        String s = "     " + Integer.toString(value);
259

    
260
        return s.substring(s.length() - 6, s.length());
261
    }
262
    
263
    /**
264
     * Convierte a String un dato del DxfGroup
265
     * @param code
266
     * @param value
267
     * @return String
268
     */
269
    public static String toString(int code, String value) {
270
        return int34car(code) + "\r\n" + value + "\r\n";
271
    }
272
    
273
    /**
274
     * Convierte a String un dato del DxfGroup
275
     * @param code
276
     * @param value
277
     * @return String
278
     */
279
    public static String toString(int code, int value) {
280
        return int34car(code) + "\r\n" + int6car(value) + "\r\n";
281
    }
282
    
283
    /**
284
     * Convierte a String un dato del DxfGroup
285
     * @param code
286
     * @param value
287
     * @param decimalPartLength
288
     * @return String
289
     */
290
    public static String toString(int code, float value, int decimalPartLength) {
291
        return int34car(code) + "\r\n" +
292
               decimalFormats[decimalPartLength].format((double) value) +
293
               "\r\n";
294
    }
295
    
296
    /**
297
     * Convierte a String un dato del DxfGroup
298
     * @param code
299
     * @param value
300
     * @param decimalPartLength
301
     * @return String
302
     */
303
    public static String toString(int code, double value, int decimalPartLength) {
304
        return int34car(code) + "\r\n" +
305
               decimalFormats[decimalPartLength].format(value) + "\r\n";
306
    }
307
    
308
    /**
309
     * Convierte a String un dato del DxfGroup
310
     * @param code
311
     * @param value
312
     * @return String
313
     */
314
    public static String toString(int code, Object value) {
315
        if (value instanceof String) {
316
            return toString(code, (String) value);
317
        } else if (value instanceof Integer) {
318
            return toString(code, ((Integer) value).intValue());
319
        } else if (value instanceof Double) {
320
            return toString(code, ((Double) value).floatValue(), 3);
321
        } else if (value instanceof Double) {
322
            return toString(code, ((Double) value).doubleValue(), 6);
323
        } else {
324
            return toString(code, value.toString());
325
        }
326
    }
327
    
328
    /**
329
     * Convierte a String un dato del DxfGroup
330
     */
331
    public String toString() {
332
        return toString(code, data);
333
    }
334

    
335
    /**
336
     * jmorell: Permite rellenar los datos. ?til en la escritura de DXFs.
337
     * @param data The data to set.
338
     */
339
    public void setData(Object data) {
340
        this.data = data;
341
    }
342

    
343
    /**
344
     * jmorell: Permite rellenar los c?digos. ?til en la escritura de DXFs.
345
     * @param code The code to set.
346
     */
347
    public void setCode(int code) {
348
        this.code = code;
349
    }
350
}