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