1
1
package com .telerik .metadata ;
2
2
3
+ import com .google .gson .JsonArray ;
4
+ import com .google .gson .JsonObject ;
3
5
import com .telerik .metadata .TreeNode .FieldInfo ;
4
6
import com .telerik .metadata .TreeNode .MethodInfo ;
5
7
10
12
import java .nio .charset .StandardCharsets ;
11
13
import java .util .ArrayDeque ;
12
14
import java .util .HashMap ;
15
+ import java .util .HashSet ;
13
16
import java .util .List ;
14
17
import java .util .Optional ;
15
18
@@ -18,14 +21,20 @@ public class Writer {
18
21
private final StreamWriter outNodeStream ;
19
22
private final StreamWriter outValueStream ;
20
23
private final StreamWriter outStringsStream ;
24
+ private final StreamWriter outDebugStream ;
21
25
22
26
private int commonInterfacePrefixPosition ;
23
27
24
28
public Writer (StreamWriter outNodeStream , StreamWriter outValueStream ,
25
29
StreamWriter outStringsStream ) {
30
+ this (outNodeStream , outValueStream , outStringsStream , null );
31
+ }
32
+ public Writer (StreamWriter outNodeStream , StreamWriter outValueStream ,
33
+ StreamWriter outStringsStream , StreamWriter outDebugStream ) {
26
34
this .outNodeStream = outNodeStream ;
27
35
this .outValueStream = outValueStream ;
28
36
this .outStringsStream = outStringsStream ;
37
+ this .outDebugStream = outDebugStream ;
29
38
}
30
39
31
40
private final static byte [] writeUniqueName_lenBuff = new byte [2 ];
@@ -214,6 +223,10 @@ public void writeTree(TreeNode root) throws Exception {
214
223
d .push (root );
215
224
while (!d .isEmpty ()) {
216
225
TreeNode n = d .pollFirst ();
226
+ if (Short .toUnsignedInt ((short )(curId + 1 )) < Short .toUnsignedInt (curId )) {
227
+ // we have overflowed our maximum (16 bit) metadata size
228
+ throw new Exception ("Metadata is too big and has overflown our current limit, please report this issue" );
229
+ }
217
230
n .id = n .firstChildId = n .nextSiblingId = curId ++;
218
231
219
232
String name = n .getName ();
@@ -351,7 +364,7 @@ public void writeTree(TreeNode root) throws Exception {
351
364
while (!d .isEmpty ()) {
352
365
TreeNode n = d .pollFirst ();
353
366
354
- nodeData [0 ] = n .firstChildId + (n .nextSiblingId << 16 );
367
+ nodeData [0 ] = ( n .firstChildId & 0xFFFF ) | (n .nextSiblingId << 16 );
355
368
nodeData [1 ] = n .offsetName ;
356
369
nodeData [2 ] = n .offsetValue ;
357
370
@@ -364,5 +377,24 @@ public void writeTree(TreeNode root) throws Exception {
364
377
365
378
outNodeStream .flush ();
366
379
outNodeStream .close ();
380
+
381
+ if (outDebugStream != null ) {
382
+ d .push (root );
383
+ JsonArray rootArray = new JsonArray ();
384
+ while (!d .isEmpty ()) {
385
+ TreeNode n = d .pollFirst ();
386
+ JsonObject obj = new JsonObject ();
387
+ obj .addProperty ("id" , Short .toUnsignedInt (n .id ));
388
+ obj .addProperty ("nextSiblingId" , Short .toUnsignedInt (n .nextSiblingId ));
389
+ obj .addProperty ("firstChildId" , Short .toUnsignedInt (n .firstChildId ));
390
+ obj .addProperty ("name" , n .getName ());
391
+ obj .addProperty ("nodeType" , n .nodeType );
392
+ rootArray .add (obj );
393
+ d .addAll (n .children );
394
+ }
395
+ outDebugStream .write (rootArray .toString ().getBytes ());
396
+ outDebugStream .flush ();
397
+ outDebugStream .close ();
398
+ }
367
399
}
368
400
}
0 commit comments