11package  com .telerik .metadata ;
22
3+ import  com .google .gson .JsonArray ;
4+ import  com .google .gson .JsonObject ;
35import  com .telerik .metadata .TreeNode .FieldInfo ;
46import  com .telerik .metadata .TreeNode .MethodInfo ;
57
1012import  java .nio .charset .StandardCharsets ;
1113import  java .util .ArrayDeque ;
1214import  java .util .HashMap ;
15+ import  java .util .HashSet ;
1316import  java .util .List ;
1417import  java .util .Optional ;
1518
@@ -18,14 +21,20 @@ public class Writer {
1821    private  final  StreamWriter  outNodeStream ;
1922    private  final  StreamWriter  outValueStream ;
2023    private  final  StreamWriter  outStringsStream ;
24+     private  final  StreamWriter  outDebugStream ;
2125
2226    private  int  commonInterfacePrefixPosition ;
2327
2428    public  Writer (StreamWriter  outNodeStream , StreamWriter  outValueStream ,
2529                  StreamWriter  outStringsStream ) {
30+         this (outNodeStream , outValueStream , outStringsStream , null );
31+     }
32+     public  Writer (StreamWriter  outNodeStream , StreamWriter  outValueStream ,
33+                   StreamWriter  outStringsStream , StreamWriter  outDebugStream ) {
2634        this .outNodeStream  = outNodeStream ;
2735        this .outValueStream  = outValueStream ;
2836        this .outStringsStream  = outStringsStream ;
37+         this .outDebugStream  = outDebugStream ;
2938    }
3039
3140    private  final  static  byte [] writeUniqueName_lenBuff  = new  byte [2 ];
@@ -214,6 +223,10 @@ public void writeTree(TreeNode root) throws Exception {
214223        d .push (root );
215224        while  (!d .isEmpty ()) {
216225            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+             }
217230            n .id  = n .firstChildId  = n .nextSiblingId  = curId ++;
218231
219232            String  name  = n .getName ();
@@ -351,7 +364,7 @@ public void writeTree(TreeNode root) throws Exception {
351364        while  (!d .isEmpty ()) {
352365            TreeNode  n  = d .pollFirst ();
353366
354-             nodeData [0 ] = n .firstChildId  +  (n .nextSiblingId  << 16 );
367+             nodeData [0 ] = ( n .firstChildId  &  0xFFFF ) |  (n .nextSiblingId  << 16 );
355368            nodeData [1 ] = n .offsetName ;
356369            nodeData [2 ] = n .offsetValue ;
357370
@@ -364,5 +377,24 @@ public void writeTree(TreeNode root) throws Exception {
364377
365378        outNodeStream .flush ();
366379        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+         }
367399    }
368400}
0 commit comments