01: package uk.org.ponder.xml;
02:
03: import uk.org.ponder.byteutil.ByteWrap;
04: import uk.org.ponder.conversion.LeafObjectParser;
05: import uk.org.ponder.conversion.StaticLeafParser;
06:
07: import uk.org.ponder.stringutil.ByteToCharBase64;
08: import uk.org.ponder.stringutil.CharToByteBase64;
09: import uk.org.ponder.stringutil.CharWrap;
10:
11: /**
12: * RandomAccessPointer represents an 8-byte pointer into an XMLAccessFile. Since
13: * it implements SAXLeafTypeParser, RandomAccessPointers may be used as leaf
14: * nodes and attribute values together with the SAXalizer. The pointer value is
15: * serialized as an 11-character Base64-encoded value.
16: */
17:
18: public class RandomAccessPointer implements LeafObjectParser {
19: static {
20: StaticLeafParser.instance().registerParser(
21: RandomAccessPointer.class, new RandomAccessPointer());
22: }
23:
24: public long offset;
25:
26: /** Constructs a RandomAccessPointer with initial offset 0. */
27: public RandomAccessPointer() {
28: }
29:
30: /**
31: * Constructs a RandomAccessPointer with the specified offset.
32: *
33: * @param offset
34: * The required byte offset within the file.
35: */
36: public RandomAccessPointer(long offset) {
37: this .offset = offset;
38: }
39:
40: public Object parse(String toparse) {
41: // all this will go away with the new parser
42: int length = toparse.length();
43: System.out.println("Parsing randomaccesspointer with length "
44: + length);
45: char[] unnecessarybuffer = new char[length];
46: toparse.getChars(0, length, unnecessarybuffer, 0);
47: byte[] bytes = CharToByteBase64.decode(unnecessarybuffer, 0,
48: length);
49: ByteWrap wrap = new ByteWrap(bytes, bytes.length - 8, 8);
50: return new RandomAccessPointer(wrap.read_at8(0));
51: }
52:
53: public String render(Object torendero) {
54: RandomAccessPointer torender = (RandomAccessPointer) torendero;
55: CharWrap renderinto = new CharWrap(12);
56: ByteToCharBase64.writeLong(renderinto, torender.offset, true);
57: return renderinto.toString();
58: }
59:
60: public Object copy(Object tocopy) {
61: RandomAccessPointer togo = new RandomAccessPointer(
62: ((RandomAccessPointer) tocopy).offset);
63: return togo;
64: }
65: }
|