Statistics
| Revision:

root / branches / CqCMSDvp / libraries / libCq CMS for java.old / src / org / cresques / io / DxfGroup.java @ 2669

History | View | Annotate | Download (11.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
    public DxfGroup() {
82
        code = -1;
83
        data = null;
84
    }
85

    
86
    public DxfGroup(int code, String data) {
87
        this.code = code;
88
        this.data = data;
89
    }
90

    
91
    public static DxfGroup read(BufferedReader fi)
92
                         throws NumberFormatException, IOException {
93
        DxfGroup grp = null;
94
        String txt = fi.readLine();
95

    
96
        if (txt != null) {
97
            if (!txt.equals("")) {
98
                grp = new DxfGroup();
99
                grp.code = Integer.parseInt(txt.trim());
100
                grp.readData(fi);
101
            } else {
102
                // Se trata de una linea en blanco y no se hace nada.
103
            }
104
        }
105

    
106
        return grp;
107
    }
108

    
109
    public int getCode() {
110
        return code;
111
    }
112

    
113
    public Object getData() {
114
        return data;
115
    }
116

    
117
    private void readData(BufferedReader fi) throws IOException {
118
        String txt = fi.readLine().trim();
119

    
120
        if ((0 <= code) && (code <= 9)) {
121
            data = txt; //_dfun = string_data
122
        } else if ((10 <= code) && (code <= 59)) {
123
            data = new Double(Double.parseDouble(txt)); //_dfun = float_data
124
        } else if ((60 <= code) && (code <= 79)) {
125
            try {
126
                data = new Integer(Integer.parseInt(txt)); //_dfun = int_data // 16-bit int
127
            } catch (java.lang.NumberFormatException e) {
128
                data = new Integer((int) Double.parseDouble(txt));
129
            }
130
        } else if ((90 <= code) && (code <= 99)) {
131
            data = new Integer(Integer.parseInt(txt)); //_dfun = int_data // 32-bit int
132
        } else if (code == 100) {
133
            data = txt; //_dfun = unicode_data
134
        } else if (code == 102) {
135
            System.err.println("Dxf: codigo " + code + " no implementado."); //_dfun = unicode_data
136
        } else if (code == 105) {
137
            data = txt;
138
            ; //_dfun = handle_data
139
        } else if ((110 <= code) && (code <= 139)) {
140
            data = new Double(Double.parseDouble(txt)); //_dfun = float_data // not in dxf spec
141
        } else if ((140 <= code) && (code <= 149)) { // says 147 in dxf spec
142
            data = new Double(Double.parseDouble(txt)); //_dfun = float_data
143
        } else if ((170 <= code) && (code <= 179)) { // says 175 in dxf spec
144
            data = new Integer(Integer.parseInt(txt)); //_dfun = int_data // 16-bit int
145
        } else if ((210 <= code) && (code <= 239)) {
146
            data = new Double(Double.parseDouble(txt)); //_dfun = float_data // del TEXT procendente de exportacion de microstation 
147
        } else if ((270 <= code) && (code <= 279)) {
148
            data = new Integer(Integer.parseInt(txt)); //_dfun = int_data // not in dxf spec
149
        } else if ((280 <= code) && (code <= 289)) {
150
            data = new Integer(Integer.parseInt(txt)); //_dfun = int_data // 8-bit int
151
        } else if ((290 <= code) && (code <= 299)) {
152
            data = new Boolean(Boolean.getBoolean(txt)); //_dfun = bool_data
153
        } else if ((300 <= code) && (code <= 309)) {
154
            data = txt; //_dfun = string_data
155
        } else if ((310 <= code) && (code <= 319)) {
156
            //_dfun = bin_data
157
            //throw new IOException("Dxf: codigo "+code+" no implementado.");
158
        } else if ((320 <= code) && (code <= 329)) {
159
            //_dfun = handle_data
160
            //throw new IOException("Dxf: codigo "+code+" no implementado.");
161
        } else if ((330 <= code) && (code <= 369)) {
162
            System.err.println("Dxf: codigo " + code + " no implementado."); //_dfun = hex_data
163
        } else if ((370 <= code) && (code <= 379)) {
164
            data = new Integer(Integer.parseInt(txt)); //_dfun = int_data // 8-bit int
165
        } else if ((380 <= code) && (code <= 389)) {
166
            data = new Integer(Integer.parseInt(txt)); //_dfun = int_data // 8-bit int
167
        } else if ((390 <= code) && (code <= 399)) {
168
            data = txt; //_dfun = handle_data
169
        } else if ((400 <= code) && (code <= 409)) {
170
            data = new Integer(Integer.parseInt(txt)); //_dfun = int_data // 16-bit int
171
        } else if ((410 <= code) && (code <= 419)) {
172
            data = txt; //_dfun = string_data
173
        } else if (code == 999) {
174
            data = txt; //_dfun = string_data // comment
175
        } else if ((1000 <= code) && (code <= 1009)) {
176
            data = txt; //_dfun = string_data
177
        } else if ((1010 <= code) && (code <= 1059)) {
178
            data = new Double(Double.parseDouble(txt)); //_dfun = float_data
179
        } else if ((1060 <= code) && (code <= 1070)) {
180
            data = new Integer(Integer.parseInt(txt)); //_dfun = int_data // 16-bit int
181
        } else if (code == 1071) {
182
            data = new Integer(Integer.parseInt(txt)); //_dfun = int_data # 32-bit int
183
        } else {
184
            throw new IOException("DxfReader: c?digo " + code +
185
                                  " desconocido.");
186

    
187
            //raise ValueError, "Unexpected code: %d" % code
188
        }
189

    
190
        //return _dfun
191
    }
192

    
193
    public boolean equals(int c, String s) {
194
        if ((c == code) && (s.compareTo((String) data) == 0)) {
195
            return true;
196
        }
197

    
198
        return false;
199
    }
200

    
201
    public static String int34car(int code) {
202
        if (code < 10) {
203
            return "  " + Integer.toString(code);
204
        } else if (code < 100) {
205
            return " " + Integer.toString(code);
206
        } else {
207
            return Integer.toString(code);
208
        }
209
    }
210

    
211
    public static String int6car(int value) {
212
        String s = "     " + Integer.toString(value);
213

    
214
        return s.substring(s.length() - 6, s.length());
215
    }
216

    
217
    public static String toString(int code, String value) {
218
        return int34car(code) + "\r\n" + value + "\r\n";
219
    }
220

    
221
    public static String toString(int code, int value) {
222
        return int34car(code) + "\r\n" + int6car(value) + "\r\n";
223
    }
224

    
225
    public static String toString(int code, float value, int decimalPartLength) {
226
        return int34car(code) + "\r\n" +
227
               decimalFormats[decimalPartLength].format((double) value) +
228
               "\r\n";
229
    }
230

    
231
    public static String toString(int code, double value, int decimalPartLength) {
232
        return int34car(code) + "\r\n" +
233
               decimalFormats[decimalPartLength].format(value) + "\r\n";
234
    }
235

    
236
    public static String toString(int code, Object value) {
237
        if (value instanceof String) {
238
            return toString(code, (String) value);
239
        } else if (value instanceof Integer) {
240
            return toString(code, ((Integer) value).intValue());
241
        } else if (value instanceof Double) {
242
            return toString(code, ((Double) value).floatValue(), 3);
243
        } else if (value instanceof Double) {
244
            return toString(code, ((Double) value).doubleValue(), 6);
245
        } else {
246
            return toString(code, value.toString());
247
        }
248
    }
249

    
250
    public String toString() {
251
        return toString(code, data);
252
    }
253

    
254
    /**
255
     * jmorell: Permite rellenar los datos. ?til en la escirtura de DXFs.
256
     * @param data The data to set.
257
     */
258
    public void setData(Object data) {
259
        this.data = data;
260
    }
261

    
262
    /**
263
     * jmorell: Permite rellenar los c?digos. ?til en la escirtura de DXFs.
264
     * @param code The code to set.
265
     */
266
    public void setCode(int code) {
267
        this.code = code;
268
    }
269
}