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

View differences:

DxfGroup.java
1 1
/*
2 2
 * Cresques Mapping Suite. Graphic Library for constructing mapping applications.
3
 * 
4
 * Copyright (C) 2004-5. 
5 3
 *
4
 * Copyright (C) 2004-5.
5
 *
6 6
 * This program is free software; you can redistribute it and/or
7 7
 * modify it under the terms of the GNU General Public License
8 8
 * as published by the Free Software Foundation; either version 2
......
18 18
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
19 19
 *
20 20
 * For more information, contact:
21
 * 
21
 *
22 22
 * cresques@gmail.com
23 23
 */
24 24
package org.cresques.io;
25 25

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

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

  
30 32
import java.util.Locale;
31 33

  
34

  
32 35
/**
33 36
 * Grupo Dxf (code, data). Auxiliar para leer ficheros dxf
34
 * 
37
 *
35 38
 * @author "Luis W. Sevilla" <sevilla_lui@gva.es>
36 39
 * @author "Michel Michaud" (code from)
37 40
 */
38

  
39 41
public class DxfGroup {
40
	int code;
41
	Object data;
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;
42 80

  
43
	public DxfGroup() {
44
		code = -1;
45
		data = null;
46
	}
47
	
48
	public DxfGroup(int code, String data) {
49
		this.code = code;
50
		this.data = data;
51
	}
81
    public DxfGroup() {
82
        code = -1;
83
        data = null;
84
    }
52 85

  
53
	public static DxfGroup read(BufferedReader fi) throws NumberFormatException, IOException {
54
		DxfGroup grp = null;
55
		String txt = fi.readLine();
56
		if (txt != null) {
57
			if (!txt.equals("")) {
58
				grp = new DxfGroup();
59
				grp.code = Integer.parseInt(txt.trim());
60
				grp.readData(fi);				
61
			} else {
62
				// Se trata de una linea en blanco y no se hace nada.
63
			}
64
		}
65
		return grp;
66
	}
67
	
68
	public int getCode() { return code; }
69
	public Object getData() { return data; }
70
	
71
	private void readData(BufferedReader fi) throws IOException {
72
		String txt = fi.readLine().trim();
73
		if (0 <= code && code <= 9) {
74
			data = txt;	//_dfun = string_data
75
		} else if (10 <= code && code <= 59) {
76
			data = new Double(Double.parseDouble(txt)); //_dfun = float_data
77
		} else if (60 <= code && code <= 79) {
78
			try {
79
				data = new Integer(Integer.parseInt(txt)); //_dfun = int_data // 16-bit int
80
			} catch (java.lang.NumberFormatException e) {
81
				data = new Integer((int) Double.parseDouble(txt));
82
			}
83
		} else if (90 <= code && code <= 99) {
84
			data = new Integer(Integer.parseInt(txt)); //_dfun = int_data // 32-bit int
85
		} else if (code == 100) {
86
			data = txt; //_dfun = unicode_data
87
		} else if (code == 102) {
88
			System.err.println("Dxf: codigo "+code+" no implementado."); //_dfun = unicode_data
89
		} else if (code == 105) {
90
			data = txt; ; //_dfun = handle_data
91
		} else if (110 <= code && code <= 139) {
92
			data = new Double(Double.parseDouble(txt)); //_dfun = float_data // not in dxf spec
93
		} else if (140 <= code && code <= 149) { // says 147 in dxf spec
94
			data = new Double(Double.parseDouble(txt)); //_dfun = float_data
95
		} else if (170 <= code && code <= 179) { // says 175 in dxf spec
96
			data = new Integer(Integer.parseInt(txt)); //_dfun = int_data // 16-bit int
97
		} else if (210 <= code && code <= 239) {
98
			data = new Double(Double.parseDouble(txt)); //_dfun = float_data // del TEXT procendente de exportacion de microstation 
99
		} else if (270 <= code && code <= 279) {
100
			data = new Integer(Integer.parseInt(txt)); //_dfun = int_data // not in dxf spec
101
		} else if (280 <= code && code <= 289) {
102
			data = new Integer(Integer.parseInt(txt)); //_dfun = int_data // 8-bit int
103
		} else if (290 <= code && code <= 299) {
104
			data = new Boolean(Boolean.getBoolean(txt)); //_dfun = bool_data
105
		} else if (300 <= code && code <= 309) {
106
			data = txt;	//_dfun = string_data
107
		} else if (310 <= code && code <= 319) {
108
		    //_dfun = bin_data
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
109 157
            //throw new IOException("Dxf: codigo "+code+" no implementado.");
110
		} else if (320 <= code && code <= 329) {
111
		    //_dfun = handle_data
158
        } else if ((320 <= code) && (code <= 329)) {
159
            //_dfun = handle_data
112 160
            //throw new IOException("Dxf: codigo "+code+" no implementado.");
113
		} else if (330 <= code && code <= 369) {
114
			System.err.println("Dxf: codigo "+code+" no implementado."); //_dfun = hex_data
115
		} else if (370 <= code && code <= 379) {
116
			data = new Integer(Integer.parseInt(txt)); //_dfun = int_data // 8-bit int
117
		} else if (380 <= code && code <= 389) {
118
			data = new Integer(Integer.parseInt(txt)); //_dfun = int_data // 8-bit int
119
		} else if (390 <= code && code <= 399) {
120
			data = txt; //_dfun = handle_data
121
		} else if (400 <= code && code <= 409) {
122
			data = new Integer(Integer.parseInt(txt)); //_dfun = int_data // 16-bit int
123
		} else if (410 <= code && code <= 419) {
124
			data = txt;	//_dfun = string_data
125
		} else if (code == 999) {
126
			data = txt;	//_dfun = string_data // comment
127
		} else if (1000 <= code && code <= 1009) {
128
			data = txt;	//_dfun = string_data
129
		} else if (1010 <= code && code <= 1059) {
130
			data = new Double(Double.parseDouble(txt)); //_dfun = float_data
131
		} else if (1060 <= code && code <= 1070) {
132
			data = new Integer(Integer.parseInt(txt)); //_dfun = int_data // 16-bit int
133
		} else if (code == 1071) {
134
			data = new Integer(Integer.parseInt(txt)); //_dfun = int_data # 32-bit int
135
		} else {
136
			throw new IOException("DxfReader: c?digo "+code+" desconocido.");
137
			//raise ValueError, "Unexpected code: %d" % code
138
		}
139
		//return _dfun
140
	}
141
	
142
	public boolean equals(int c, String s) {
143
		if (c == code && s.compareTo((String) data) == 0 ) return true;
144
		return false;
145
	}
146
	/*
147
	def get_group(handle):
148
	_code = int(handle.readline())
149
	_dfun = get_data_type(_code)
150
	_data = _dfun(handle.readline())
151
	return (_code, _data)
152
	 */
153
	 
154
	private static final DecimalFormatSymbols dfs = new DecimalFormatSymbols(Locale.US);
155
	private static final DecimalFormat[] decimalFormats = new DecimalFormat[]{
156
						  new DecimalFormat("#0", dfs),
157
						  new DecimalFormat("#0.0", dfs),
158
						  new DecimalFormat("#0.00", dfs),
159
						  new DecimalFormat("#0.000", dfs),
160
						  new DecimalFormat("#0.0000", dfs),
161
						  new DecimalFormat("#0.00000", dfs),
162
						  new DecimalFormat("#0.000000", dfs),
163
						  new DecimalFormat("#0.0000000", dfs),
164
						  new DecimalFormat("#0.00000000", dfs),
165
						  new DecimalFormat("#0.000000000", dfs),
166
						  new DecimalFormat("#0.0000000000", dfs),
167
						  new DecimalFormat("#0.00000000000", dfs),
168
						  new DecimalFormat("#0.000000000000", dfs)};
169
						  
170
	public static String int34car(int code) {
171
		if (code<10) return "  " + Integer.toString(code);
172
		else if (code<100) return " " + Integer.toString(code);
173
		else return Integer.toString(code);
174
	}
175
    
176
	public static String int6car(int value) {
177
		String s = "     " + Integer.toString(value);
178
		return s.substring(s.length()-6, s.length());
179
	}
180
	public static String toString(int code, String value) {
181
	  return int34car(code) + "\r\n" + value + "\r\n";
182
	}
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.");
183 186

  
184
	public static String toString(int code, int value) {
185
	  return int34car(code) + "\r\n" + int6car(value) + "\r\n";
186
	}
187
            //raise ValueError, "Unexpected code: %d" % code
188
        }
187 189

  
188
	public static String toString(int code, float value, int decimalPartLength) {
189
	  return int34car(code) + "\r\n" +
190
			  decimalFormats[decimalPartLength].format((double)value) + "\r\n";
191
	}
190
        //return _dfun
191
    }
192 192

  
193
	public static String toString(int code, double value, int decimalPartLength) {
194
	  return int34car(code) + "\r\n" +
195
			  decimalFormats[decimalPartLength].format(value) + "\r\n";
196
	}
193
    public boolean equals(int c, String s) {
194
        if ((c == code) && (s.compareTo((String) data) == 0)) {
195
            return true;
196
        }
197 197

  
198
	public static String toString(int code, Object value) {
199
		if (value instanceof String) {return toString(code, (String)value);}
200
		else if (value instanceof Integer) {return toString(code, ((Integer)value).intValue());}
201
		else if (value instanceof Double) {return toString(code, ((Double)value).floatValue(), 3);}
202
		else if (value instanceof Double) {return toString(code, ((Double)value).doubleValue(), 6);}
203
		else return toString(code, value.toString());
204
	}
198
        return false;
199
    }
205 200

  
206
	 
207
	 public String toString() {
208
		return toString(code, data);
209
	 }
210
	/**
211
	 * jmorell: Permite rellenar los datos. ?til en la escirtura de DXFs.
212
	 * @param data The data to set.
213
	 */
214
	public void setData(Object data) {
215
		this.data = data;
216
	}
217
	/**
218
	 * jmorell: Permite rellenar los c?digos. ?til en la escirtura de DXFs.
219
	 * @param code The code to set.
220
	 */
221
	public void setCode(int code) {
222
		this.code = code;
223
	}
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
    }
224 269
}

Also available in: Unified diff