svn-gvsig-desktop / trunk / libraries / libCq CMS for java.old / src / org / cresques / io / DxfGroup.java @ 20
History | View | Annotate | Download (6.62 KB)
1 |
/*
|
---|---|
2 |
* Created on 04-may-2004
|
3 |
*/
|
4 |
package org.cresques.io; |
5 |
|
6 |
import java.io.BufferedReader; |
7 |
import java.io.IOException; |
8 |
import java.text.DecimalFormat; |
9 |
import java.text.DecimalFormatSymbols; |
10 |
import java.util.Locale; |
11 |
|
12 |
/**
|
13 |
* Grupo Dxf (code, data). Auxiliar para leer ficheros dxf
|
14 |
*
|
15 |
* @author "Luis W. Sevilla" <sevilla_lui@gva.es>
|
16 |
* @author "Michel Michaud" (code from)
|
17 |
*/
|
18 |
|
19 |
public class DxfGroup { |
20 |
int code;
|
21 |
Object data;
|
22 |
|
23 |
public DxfGroup() {
|
24 |
code = -1;
|
25 |
data = null;
|
26 |
} |
27 |
|
28 |
public DxfGroup(int code, String data) { |
29 |
this.code = code;
|
30 |
this.data = data;
|
31 |
} |
32 |
|
33 |
public static DxfGroup read(BufferedReader fi) throws NumberFormatException, IOException { |
34 |
DxfGroup grp = null;
|
35 |
String txt = fi.readLine();
|
36 |
if (txt != null) { |
37 |
grp = new DxfGroup();
|
38 |
grp.code = Integer.parseInt(txt.trim());
|
39 |
grp.readData(fi); |
40 |
} |
41 |
return grp;
|
42 |
} |
43 |
|
44 |
public int getCode() { return code; } |
45 |
public Object getData() { return data; } |
46 |
|
47 |
private void readData(BufferedReader fi) throws IOException { |
48 |
String txt = fi.readLine().trim();
|
49 |
if (0 <= code && code <= 9) { |
50 |
data = txt; //_dfun = string_data
|
51 |
} else if (10 <= code && code <= 59) { |
52 |
data = new Float(Float.parseFloat(txt)); //_dfun = float_data |
53 |
} else if (60 <= code && code <= 79) { |
54 |
data = new Integer(Integer.parseInt(txt)); //_dfun = int_data // 16-bit int |
55 |
} else if (90 <= code && code <= 99) { |
56 |
data = new Integer(Integer.parseInt(txt)); //_dfun = int_data // 32-bit int |
57 |
} else if (code == 100) { |
58 |
data = txt; //_dfun = unicode_data
|
59 |
} else if (code == 102) { |
60 |
System.err.println("Dxf: codigo "+code+" no implementado."); //_dfun = unicode_data |
61 |
} else if (code == 105) { |
62 |
data = txt; ; //_dfun = handle_data
|
63 |
} else if (110 <= code && code <= 139) { |
64 |
data = new Float(Float.parseFloat(txt)); //_dfun = float_data // not in dxf spec |
65 |
} else if (140 <= code && code <= 149) { // says 147 in dxf spec |
66 |
data = new Float(Float.parseFloat(txt)); //_dfun = float_data |
67 |
} else if (170 <= code && code <= 179) { // says 175 in dxf spec |
68 |
data = new Integer(Integer.parseInt(txt)); //_dfun = int_data // 16-bit int |
69 |
} else if (210 <= code && code <= 239) { |
70 |
data = new Float(Float.parseFloat(txt)); //_dfun = float_data // del TEXT procendente de exportacion de microstation |
71 |
} else if (270 <= code && code <= 279) { |
72 |
data = new Integer(Integer.parseInt(txt)); //_dfun = int_data // not in dxf spec |
73 |
} else if (280 <= code && code <= 289) { |
74 |
data = new Integer(Integer.parseInt(txt)); //_dfun = int_data // 8-bit int |
75 |
} else if (290 <= code && code <= 299) { |
76 |
data = new Boolean(Boolean.getBoolean(txt)); //_dfun = bool_data |
77 |
} else if (300 <= code && code <= 309) { |
78 |
data = txt; //_dfun = string_data
|
79 |
} else if (310 <= code && code <= 319) { |
80 |
throw new IOException("Dxf: codigo "+code+" no implementado."); //_dfun = bin_data |
81 |
} else if (320 <= code && code <= 329) { |
82 |
throw new IOException("Dxf: codigo "+code+" no implementado."); //_dfun = handle_data |
83 |
} else if (330 <= code && code <= 369) { |
84 |
System.err.println("Dxf: codigo "+code+" no implementado."); //_dfun = hex_data |
85 |
} else if (370 <= code && code <= 379) { |
86 |
data = new Integer(Integer.parseInt(txt)); //_dfun = int_data // 8-bit int |
87 |
} else if (380 <= code && code <= 389) { |
88 |
data = new Integer(Integer.parseInt(txt)); //_dfun = int_data // 8-bit int |
89 |
} else if (390 <= code && code <= 399) { |
90 |
data = txt; //_dfun = handle_data
|
91 |
} else if (400 <= code && code <= 409) { |
92 |
data = new Integer(Integer.parseInt(txt)); //_dfun = int_data // 16-bit int |
93 |
} else if (410 <= code && code <= 419) { |
94 |
data = txt; //_dfun = string_data
|
95 |
} else if (code == 999) { |
96 |
data = txt; //_dfun = string_data // comment
|
97 |
} else if (1000 <= code && code <= 1009) { |
98 |
data = txt; //_dfun = string_data
|
99 |
} else if (1010 <= code && code <= 1059) { |
100 |
data = new Float(Float.parseFloat(txt)); //_dfun = float_data |
101 |
} else if (1060 <= code && code <= 1070) { |
102 |
data = new Integer(Integer.parseInt(txt)); //_dfun = int_data // 16-bit int |
103 |
} else if (code == 1071) { |
104 |
data = new Integer(Integer.parseInt(txt)); //_dfun = int_data # 32-bit int |
105 |
} else {
|
106 |
throw new IOException("DxfReader: c?digo "+code+" desconocido."); |
107 |
//raise ValueError, "Unexpected code: %d" % code
|
108 |
} |
109 |
//return _dfun
|
110 |
} |
111 |
|
112 |
public boolean equals(int c, String s) { |
113 |
if (c == code && s.compareTo((String) data) == 0 ) return true; |
114 |
return false; |
115 |
} |
116 |
/*
|
117 |
def get_group(handle):
|
118 |
_code = int(handle.readline())
|
119 |
_dfun = get_data_type(_code)
|
120 |
_data = _dfun(handle.readline())
|
121 |
return (_code, _data)
|
122 |
*/
|
123 |
|
124 |
private static final DecimalFormatSymbols dfs = new DecimalFormatSymbols(Locale.US); |
125 |
private static final DecimalFormat[] decimalFormats = new DecimalFormat[]{ |
126 |
new DecimalFormat("#0", dfs), |
127 |
new DecimalFormat("#0.0", dfs), |
128 |
new DecimalFormat("#0.00", dfs), |
129 |
new DecimalFormat("#0.000", dfs), |
130 |
new DecimalFormat("#0.0000", dfs), |
131 |
new DecimalFormat("#0.00000", dfs), |
132 |
new DecimalFormat("#0.000000", dfs), |
133 |
new DecimalFormat("#0.0000000", dfs), |
134 |
new DecimalFormat("#0.00000000", dfs), |
135 |
new DecimalFormat("#0.000000000", dfs), |
136 |
new DecimalFormat("#0.0000000000", dfs), |
137 |
new DecimalFormat("#0.00000000000", dfs), |
138 |
new DecimalFormat("#0.000000000000", dfs)}; |
139 |
|
140 |
public static String int34car(int code) { |
141 |
if (code<10) return " " + Integer.toString(code); |
142 |
else if (code<100) return " " + Integer.toString(code); |
143 |
else return Integer.toString(code); |
144 |
} |
145 |
|
146 |
public static String int6car(int value) { |
147 |
String s = " " + Integer.toString(value); |
148 |
return s.substring(s.length()-6, s.length()); |
149 |
} |
150 |
public static String toString(int code, String value) { |
151 |
return int34car(code) + "\r\n" + value + "\r\n"; |
152 |
} |
153 |
|
154 |
public static String toString(int code, int value) { |
155 |
return int34car(code) + "\r\n" + int6car(value) + "\r\n"; |
156 |
} |
157 |
|
158 |
public static String toString(int code, float value, int decimalPartLength) { |
159 |
return int34car(code) + "\r\n" + |
160 |
decimalFormats[decimalPartLength].format((double)value) + "\r\n"; |
161 |
} |
162 |
|
163 |
public static String toString(int code, double value, int decimalPartLength) { |
164 |
return int34car(code) + "\r\n" + |
165 |
decimalFormats[decimalPartLength].format(value) + "\r\n";
|
166 |
} |
167 |
|
168 |
public static String toString(int code, Object value) { |
169 |
if (value instanceof String) {return toString(code, (String)value);} |
170 |
else if (value instanceof Integer) {return toString(code, ((Integer)value).intValue());} |
171 |
else if (value instanceof Float) {return toString(code, ((Float)value).floatValue(), 3);} |
172 |
else if (value instanceof Double) {return toString(code, ((Double)value).doubleValue(), 6);} |
173 |
else return toString(code, value.toString()); |
174 |
} |
175 |
|
176 |
|
177 |
public String toString() { |
178 |
return toString(code, data);
|
179 |
} |
180 |
} |
181 |
|