Skip to content

Commit bac8e01

Browse files
committed
Add PositionInfo interface.
Fix #26
1 parent fcb7b2f commit bac8e01

File tree

3 files changed

+210
-0
lines changed

3 files changed

+210
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
* Span that represents positional information of array field and each of it
31+
* elements. Spans of items is available in the {@link #items} field.
32+
*
33+
* @since 0.10
34+
*/
35+
public class ArraySpan extends Span {
36+
/** Individual span of the each item in the array. */
37+
public final List<Span> items;
38+
39+
/**
40+
* Creates a span of array that starts at the current stream offset and
41+
* ends at the unknown position.
42+
*
43+
* @param io the stream to get positional information
44+
*/
45+
public ArraySpan(KaitaiStream io) {
46+
super(io);
47+
items = new ArrayList<Span>();
48+
}
49+
50+
public ArraySpan(KaitaiStream io, int size) {
51+
super(io);
52+
items = new ArrayList<Span>(size);
53+
}
54+
55+
/**
56+
* Appends a new span of array item from current stream position to the end-of-stream
57+
* to this span
58+
*
59+
* @param io Stream used to inquire current position
60+
* @return A new span, added to the internal list of item spans
61+
*/
62+
public Span addItem(KaitaiStream io) {
63+
final Span span = new Span(io);
64+
items.add(span);
65+
return span;
66+
}
67+
}
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.10
45+
*/
46+
public interface PositionInfo {
47+
/**
48+
* Information about each struct field. If field is an array, then corresponding
49+
* {@code Span} will be of {@link ArraySpan} instance. Map keys is equals to the
50+
* names of the java methods/fields in the generated class.
51+
*
52+
* @return the map from field name to field span information.
53+
*/
54+
Map<String, Span> _spans();
55+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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.10
30+
*/
31+
public class Span {
32+
/** Offset from begin of the root stream, for which that span was created. */
33+
public final long offset;
34+
/**
35+
* Offset from begin of the stream, from which value was parsed. This is relative
36+
* position, to get an absolute position (relative to the root stream) use
37+
* {@link #absoluteStart()}. That offset is always non-negative.
38+
*/
39+
public final long start;
40+
/**
41+
* Offset from begin of the stream, from which value was parsed. This is relative
42+
* position, to get an absolute position (relative to the 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 a span that starts at the current stream offset and ends at
52+
* the unknown position.
53+
*
54+
* @param io the stream to get the positional information
55+
*/
56+
public Span(KaitaiStream io) {
57+
this(io.offset(), io.pos());
58+
}
59+
private Span(long offset, long start) {
60+
this.offset = offset;
61+
this.start = start;
62+
}
63+
64+
/**
65+
* Offset to the start of this span relative to the root stream.
66+
*
67+
* @return start offset from the root stream
68+
*/
69+
public long absoluteStart() { return offset + start; }
70+
/**
71+
* Offset to the end of this span relative to the root stream.
72+
* <p>
73+
* If that offset is negative, then value wasn't parsed yet or exception was
74+
* thrown while parsing value.
75+
*
76+
* @return start offset from the root stream or negative value if value not yet parsed
77+
*/
78+
public long absoluteEnd() { return end < 0 ? -1 : offset + end; }
79+
/**
80+
* Size of this span in bytes.
81+
* <p>
82+
* If size is negative, then value wasn't parsed yet or exception was
83+
* thrown while parsing value.
84+
*
85+
* @return size of the span in bytes
86+
*/
87+
public long size() { return end < 0 ? -1 : end - start; }
88+
}

0 commit comments

Comments
 (0)