Repository navigation
Expand file tree
/
Copy pathAttribute.java
More file actions
549 lines (512 loc) · 20.5 KB
/
Copy pathAttribute.java
File metadata and controls
549 lines (512 loc) · 20.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
/*****************************************************************************
* Copyright by The HDF Group. *
* Copyright by the Board of Trustees of the University of Illinois. *
* All rights reserved. *
* *
* This file is part of the HDF Java Products distribution. *
* The full copyright notice, including terms governing use, modification, *
* and redistribution, is contained in the files COPYING and Copyright.html. *
* COPYING can be found at the root of the source code distribution tree. *
* Or, see http://hdfgroup.org/products/hdf-java/doc/Copyright.html. *
* If you do not have access to either file, you may request a copy from *
* help@hdfgroup.org. *
****************************************************************************/
package ncsa.hdf.object;
import java.lang.reflect.Array;
import java.math.BigInteger;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
/**
* An attribute is a (name, value) pair of metadata attached to a primary data
* object such as dataset, group or named datatype.
* <p>
* Like a dataset, an attribute has a name, datatype and dataspace.
*
* <p>
* For more details on attibutes, see {@link <a
* href="http://hdfgroup.org/HDF5/doc/UG/index.html">HDF5 User's Guide</a>}
* <p>
*
* The following code is an example of an attribute with 1D integer array of two
* elements.
*
* <pre>
* // Example of creating a new attribute
* // The name of the new attribute
* String name = "Data range";
* // Creating an unsigned 1-byte integer datatype
* Datatype type = new Datatype(Datatype.CLASS_INTEGER, // class
* 1, // size in bytes
* Datatype.ORDER_LE, // byte order
* Datatype.SIGN_NONE); // signed or unsigned
* // 1-D array of size two
* long[] dims = {2};
* // The value of the attribute
* int[] value = {0, 255};
* // Create a new attribute
* Attribute dataRange = new Attribute(name, type, dims);
* // Set the attribute value
* dataRange.setValue(value);
* // See FileFormat.writeAttribute() for how to attach an attribute to an object,
* @see ncsa.hdf.object.FileFormat#writeAttribute(HObject, Attribute, boolean)
* </pre>
*
* @see ncsa.hdf.object.Datatype
*
* @version 1.1 9/4/2007
* @author Peter X. Cao
*/
public class Attribute implements Metadata {
private static final long serialVersionUID = 2072473407027648309L;
private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(Attribute.class);
/** The name of the attribute. */
private final String name;
/** The datatype of the attribute. */
private final Datatype type;
/** The rank of the data value of the attribute. */
private int rank;
/** The dimension sizes of the attribute. */
private long[] dims;
/** The value of the attribute. */
private Object value;
/** additional information and properties for the attribute */
private Map<String, Object> properties;
/** Flag to indicate if the datatype is an unsigned integer. */
private boolean isUnsigned;
/** flag to indicate if the dataset is a single scalar point */
protected boolean isScalar = false;
/**
* Create an attribute with specified name, data type and dimension sizes.
*
* For scalar attribute, the dimension size can be either an array of size
* one or null, and the rank can be either 1 or zero. Attribute is a general
* class and is independent of file format, e.g., the implementation of
* attribute applies to both HDF4 and HDF5.
* <p>
* The following example creates a string attribute with the name "CLASS"
* and value "IMAGE".
*
* <pre>
* long[] attrDims = { 1 };
* String attrName = "CLASS";
* String[] classValue = { "IMAGE" };
* Datatype attrType = new H5Datatype(Datatype.CLASS_STRING, classValue[0].length() + 1, -1, -1);
* Attribute attr = new Attribute(attrName, attrType, attrDims);
* attr.setValue(classValue);
* </pre>
*
* @param attrName
* the name of the attribute.
* @param attrType
* the datatype of the attribute.
* @param attrDims
* the dimension sizes of the attribute, null for scalar
* attribute
*
* @see ncsa.hdf.object.Datatype
*/
public Attribute(String attrName, Datatype attrType, long[] attrDims) {
this(attrName, attrType, attrDims, null);
}
/**
* Create an attribute with specific name and value.
*
* For scalar attribute, the dimension size can be either an array of size
* one or null, and the rank can be either 1 or zero. Attribute is a general
* class and is independent of file format, e.g., the implementation of
* attribute applies to both HDF4 and HDF5.
* <p>
* The following example creates a string attribute with the name "CLASS"
* and value "IMAGE".
*
* <pre>
* long[] attrDims = { 1 };
* String attrName = "CLASS";
* String[] classValue = { "IMAGE" };
* Datatype attrType = new H5Datatype(
* Datatype.CLASS_STRING,
* classValue[0]
* .length() + 1,
* -1, -1);
* Attribute attr = new Attribute(
* attrName,
* attrType,
* attrDims,
* classValue);
* </pre>
*
* @param attrName
* the name of the attribute.
* @param attrType
* the datatype of the attribute.
* @param attrDims
* the dimension sizes of the attribute, null for scalar
* attribute
* @param attrValue
* the value of the attribute, null if no value
*
* @see ncsa.hdf.object.Datatype
*/
public Attribute(String attrName, Datatype attrType, long[] attrDims, Object attrValue) {
name = attrName;
type = attrType;
dims = attrDims;
value = null;
properties = new HashMap();
rank = 0;
log.trace("Attribute: {}, attrValue={}", attrName, attrValue);
if (dims != null) {
rank = dims.length;
}
else {
isScalar = true;
rank = 1;
dims = new long[] { 1 };
}
if (attrValue != null) {
value = attrValue;
}
isUnsigned = (type.getDatatypeSign() == Datatype.SIGN_NONE);
log.trace("Attribute: finish");
}
/**
* Returns the value of the attribute. For atomic datatype, this will be an
* 1D array of integers, floats and strings. For compound datatype, it will
* be an 1D array of strings with field members separated by comma. For
* example, "{0, 10.5}, {255, 20.0}, {512, 30.0}" is a cmpound attribute of
* {int, float} of three data points.
*
* @return the value of the attribute, or null if failed to retrieve data
* from file.
*/
public Object getValue() {
return value;
}
/**
* set a property for the attribute.
*/
public void setProperty(String key, Object value)
{
properties.put(key, value);
}
/**
* get a property for a given key.
*/
public Object getProperty(String key)
{
return properties.get(key);
}
/**
* get all property keys.
*/
public Collection<String> getPropertyKeys()
{
return properties.keySet();
}
/**
* Sets the value of the attribute. It returns null if failed to retrieve
* the name from file.
*
* @param theValue
* The value of the attribute to set
*/
public void setValue(Object theValue) {
value = theValue;
}
/**
* Returns the name of the attribute.
*
* @return the name of the attribute.
*/
public String getName() {
return name;
}
/**
* Returns the rank (number of dimensions) of the attribute. It returns a
* negative number if failed to retrieve the dimension information from
* file.
*
* @return the number of dimensions of the attribute.
*/
public int getRank() {
return rank;
}
/**
* Returns the dimension sizes of the data value of the attribute. It
* returns null if failed to retrieve the dimension information from file.
*
* @return the dimension sizes of the attribute.
*/
public long[] getDataDims() {
return dims;
}
/**
* Returns the datatype of the attribute. It returns null if failed to
* retrieve the datatype information from file.
*
* @return the datatype of the attribute.
*/
public Datatype getType() {
return type;
}
/**
* @return true if the data is a single scalar point; otherwise, returns
* false.
*/
public boolean isScalar() {
return isScalar;
}
/**
* Checks if the data type of this attribute is an unsigned integer.
*
* @return true if the data type of the attribute is an unsigned integer;
* otherwise returns false.
*/
public boolean isUnsigned() {
return isUnsigned;
}
/**
* Return the name of the attribute.
*
* @see #toString(String delimiter)
*/
@Override
public String toString() {
return name;
}
/**
* Returns a string representation of the data value of the attribute. For
* example, "0, 255".
* <p>
* For compound datatype, it will be an 1D array of strings with field
* members separated by comma. For example,
* "{0, 10.5}, {255, 20.0}, {512, 30.0}" is a compound attribute of {int,
* float} of three data points.
* <p>
*
* @param delimiter
* The delimiter to separate individual data point. It can be
* comma, semicolon, tab or space. For example, to String(",")
* will separate data by comma.
*
* @return the string representation of the data values.
*/
public String toString(String delimiter) {
if (value == null) {
return null;
}
log.trace("toString: start");
Class<? extends Object> valClass = value.getClass();
if (!valClass.isArray()) {
return value.toString();
}
// attribute value is an array
StringBuffer sb = new StringBuffer();
int n = Array.getLength(value);
boolean is_unsigned = (this.getType().getDatatypeSign() == Datatype.SIGN_NONE);
boolean is_enum = (this.getType().getDatatypeClass() == Datatype.CLASS_ENUM);
log.trace("toString: is_enum={} is_unsigned={} Array.getLength={}", is_enum, is_unsigned, n);
if (is_unsigned) {
String cname = valClass.getName();
char dname = cname.charAt(cname.lastIndexOf("[") + 1);
log.trace("toString: is_unsigned with cname={} dname={}", cname, dname);
switch (dname) {
case 'B':
byte[] barray = (byte[]) value;
short sValue = barray[0];
if (sValue < 0) {
sValue += 256;
}
sb.append(sValue);
for (int i = 1; i < n; i++) {
sb.append(delimiter);
sValue = barray[i];
if (sValue < 0) {
sValue += 256;
}
sb.append(sValue);
}
break;
case 'S':
short[] sarray = (short[]) value;
int iValue = sarray[0];
if (iValue < 0) {
iValue += 65536;
}
sb.append(iValue);
for (int i = 1; i < n; i++) {
sb.append(delimiter);
iValue = sarray[i];
if (iValue < 0) {
iValue += 65536;
}
sb.append(iValue);
}
break;
case 'I':
int[] iarray = (int[]) value;
long lValue = iarray[0];
if (lValue < 0) {
lValue += 4294967296L;
}
sb.append(lValue);
for (int i = 1; i < n; i++) {
sb.append(delimiter);
lValue = iarray[i];
if (lValue < 0) {
lValue += 4294967296L;
}
sb.append(lValue);
}
break;
case 'J':
long[] larray = (long[]) value;
Long l = (Long) larray[0];
String theValue = Long.toString(l);
if (l < 0) {
l = (l << 1) >>> 1;
BigInteger big1 = new BigInteger("9223372036854775808"); // 2^65
BigInteger big2 = new BigInteger(l.toString());
BigInteger big = big1.add(big2);
theValue = big.toString();
}
sb.append(theValue);
for (int i = 1; i < n; i++) {
sb.append(delimiter);
l = (Long) larray[i];
theValue = Long.toString(l);
if (l < 0) {
l = (l << 1) >>> 1;
BigInteger big1 = new BigInteger("9223372036854775808"); // 2^65
BigInteger big2 = new BigInteger(l.toString());
BigInteger big = big1.add(big2);
theValue = big.toString();
}
sb.append(theValue);
}
break;
default:
sb.append(Array.get(value, 0));
for (int i = 1; i < n; i++) {
sb.append(delimiter);
sb.append(Array.get(value, i));
}
break;
}
}
else if(is_enum) {
String cname = valClass.getName();
char dname = cname.charAt(cname.lastIndexOf("[") + 1);
log.trace("toString: is_enum with cname={} dname={}", cname, dname);
String enum_members = this.getType().getEnumMembers();
log.trace("toString: is_enum enum_members={}", enum_members);
Map<String,String> map = new HashMap<String,String>();
String[] entries = enum_members.split(",");
for (String entry : entries) {
String[] keyValue = entry.split("=");
map.put(keyValue[1],keyValue[0]);
log.trace("toString: is_enum value={} name={}", keyValue[1],keyValue[0]);
}
String theValue = null;
switch (dname) {
case 'B':
byte[] barray = (byte[]) value;
short sValue = barray[0];
theValue = String.valueOf(sValue);
if (map.containsKey(theValue)) {
sb.append(map.get(theValue));
}
else
sb.append(sValue);
for (int i = 1; i < n; i++) {
sb.append(delimiter);
sValue = barray[i];
theValue = String.valueOf(sValue);
if (map.containsKey(theValue)) {
sb.append(map.get(theValue));
}
else
sb.append(sValue);
}
break;
case 'S':
short[] sarray = (short[]) value;
int iValue = sarray[0];
theValue = String.valueOf(iValue);
if (map.containsKey(theValue)) {
sb.append(map.get(theValue));
}
else
sb.append(iValue);
for (int i = 1; i < n; i++) {
sb.append(delimiter);
iValue = sarray[i];
theValue = String.valueOf(iValue);
if (map.containsKey(theValue)) {
sb.append(map.get(theValue));
}
else
sb.append(iValue);
}
break;
case 'I':
int[] iarray = (int[]) value;
long lValue = iarray[0];
theValue = String.valueOf(lValue);
if (map.containsKey(theValue)) {
sb.append(map.get(theValue));
}
else
sb.append(lValue);
for (int i = 1; i < n; i++) {
sb.append(delimiter);
lValue = iarray[i];
theValue = String.valueOf(lValue);
if (map.containsKey(theValue)) {
sb.append(map.get(theValue));
}
else
sb.append(lValue);
}
break;
case 'J':
long[] larray = (long[]) value;
Long l = (Long) larray[0];
theValue = Long.toString(l);
if (map.containsKey(theValue)) {
sb.append(map.get(theValue));
}
else
sb.append(theValue);
for (int i = 1; i < n; i++) {
sb.append(delimiter);
l = (Long) larray[i];
theValue = Long.toString(l);
if (map.containsKey(theValue)) {
sb.append(map.get(theValue));
}
else
sb.append(theValue);
}
break;
default:
sb.append(Array.get(value, 0));
for (int i = 1; i < n; i++) {
sb.append(delimiter);
sb.append(Array.get(value, i));
}
break;
}
}
else {
sb.append(Array.get(value, 0));
for (int i = 1; i < n; i++) {
sb.append(delimiter);
sb.append(Array.get(value, i));
}
}
log.trace("toString: finish");
return sb.toString();
}
}