Skip to content

Commit 13204d4

Browse files
committed
Add PositionInfo interface.
Fix #26
1 parent 782feb7 commit 13204d4

File tree

3 files changed

+196
-0
lines changed

3 files changed

+196
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* Copyright 2015-2020 Kaitai Project: MIT license
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining
5+
* a copy of this software and associated documentation files (the
6+
* "Software"), to deal in the Software without restriction, including
7+
* without limitation the rights to use, copy, modify, merge, publish,
8+
* distribute, sublicense, and/or sell copies of the Software, and to
9+
* permit persons to whom the Software is furnished to do so, subject to
10+
* the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be
13+
* included in all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19+
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20+
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21+
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22+
*/
23+
24+
package io.kaitai.struct;
25+
26+
import java.util.ArrayList;
27+
import java.util.List;
28+
29+
/**
30+
* Region that represents positional information of array field and each of it
31+
* elements.
32+
*
33+
* @since 0.9
34+
*/
35+
public class ArrayRegion extends Region {
36+
/** Individual regions of each item in the array. */
37+
public final List<Region> items;
38+
39+
public ArrayRegion(KaitaiStream io) {
40+
super(io);
41+
items = new ArrayList<Region>();
42+
}
43+
44+
public ArrayRegion(KaitaiStream io, int size) {
45+
super(io);
46+
items = new ArrayList<Region>(size);
47+
}
48+
49+
public Region addRegion(KaitaiStream io) {
50+
final Region region = new Region(io);
51+
items.add(region);
52+
return region;
53+
}
54+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* Copyright 2015-2020 Kaitai Project: MIT license
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining
5+
* a copy of this software and associated documentation files (the
6+
* "Software"), to deal in the Software without restriction, including
7+
* without limitation the rights to use, copy, modify, merge, publish,
8+
* distribute, sublicense, and/or sell copies of the Software, and to
9+
* permit persons to whom the Software is furnished to do so, subject to
10+
* the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be
13+
* included in all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19+
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20+
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21+
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22+
*/
23+
24+
package io.kaitai.struct;
25+
26+
import java.util.Map;
27+
28+
/**
29+
* This interface is implemented by each {@link KaitaiStruct} successor, if
30+
* class was generated with positional information.
31+
* <p>
32+
* If you want to work with generated structures in the generic way, you can use
33+
* following code snipped to deal with positions information:
34+
* <code>
35+
* final KaitaiStruct struct = ...;
36+
* // Generator generates classes, that implements this interface,
37+
* // if debug mode/positions-info is enabled
38+
* if (struct instanceof PositionInfo) {
39+
* final PositionInfo info = (PositionInfo)struct;
40+
* //...
41+
* }
42+
* </code>
43+
*
44+
* @since 0.9
45+
*/
46+
public interface PositionInfo {
47+
/**
48+
* Information about each struct field. If field is array, than corresponding
49+
* {@code Region} will be of {@link ArrayRegion} instance. Field names equals
50+
* to names of java methods/fields in generated class.
51+
*
52+
* @return the map from field name to region information.
53+
*/
54+
Map<String, Region> _regions();
55+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/**
2+
* Copyright 2015-2020 Kaitai Project: MIT license
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining
5+
* a copy of this software and associated documentation files (the
6+
* "Software"), to deal in the Software without restriction, including
7+
* without limitation the rights to use, copy, modify, merge, publish,
8+
* distribute, sublicense, and/or sell copies of the Software, and to
9+
* permit persons to whom the Software is furnished to do so, subject to
10+
* the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be
13+
* included in all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19+
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20+
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21+
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22+
*/
23+
24+
package io.kaitai.struct;
25+
26+
/**
27+
* Information about positions of parsed value in the streams.
28+
*
29+
* @since 0.9
30+
*/
31+
public class Region {
32+
/** Offset from begin of root stream, for which that region was created. */
33+
public final long offset;
34+
/**
35+
* Offset from begin of stream, from which value was parsed. This is relative
36+
* position, to get absolute position (relative to root stream) use
37+
* {@link #absoluteStart()}. That offset always non-negative.
38+
*/
39+
public final long start;
40+
/**
41+
* Offset from begin of stream, from which value was parsed. This is relative
42+
* position, to get absolute position (relative to root stream) use
43+
* {@link #absoluteEnd()}.
44+
* <p>
45+
* If that offset is negative, then value wasn't parsed yet or exception was
46+
* thrown while parsing value.
47+
*/
48+
public long end = -1;
49+
50+
/**
51+
* Creates region that starts at current stream offset and ends at unknown position.
52+
*
53+
* @param io the stream to get positional information
54+
*/
55+
public Region(KaitaiStream io) {
56+
this(io.offset(), io.pos());
57+
}
58+
private Region(long offset, long start) {
59+
this.offset = offset;
60+
this.start = start;
61+
}
62+
63+
/**
64+
* Offset to the start of this region relative to the root stream.
65+
*
66+
* @return start offset from the root stream
67+
*/
68+
public long absoluteStart() { return offset + start; }
69+
/**
70+
* Offset to the end of this region relative to the root stream.
71+
* <p>
72+
* If that offset is negative, then value wasn't parsed yet or exception was
73+
* thrown while parsing value.
74+
*
75+
* @return start offset from the root stream or negative value if value not yet parsed
76+
*/
77+
public long absoluteEnd() { return end < 0 ? -1 : offset + end; }
78+
/**
79+
* Size of this region in bytes.
80+
* <p>
81+
* If size is negative, then value wasn't parsed yet or exception was
82+
* thrown while parsing value.
83+
*
84+
* @return size of the region
85+
*/
86+
public long size() { return end < 0 ? -1 : end - start; }
87+
}

0 commit comments

Comments
 (0)