55import java .io .IOException ;
66import java .io .InputStream ;
77import java .io .OutputStream ;
8- import java .math .BigDecimal ;
9- import java .math .BigInteger ;
108import java .util .ArrayList ;
119import java .util .LinkedHashMap ;
1210import java .util .List ;
1311import java .util .Map ;
14- import java .util .Map .Entry ;
15- import java .util .TreeSet ;
1612
1713import javax .xml .transform .TransformerException ;
1814
1915import org .w3c .dom .Document ;
2016import org .w3c .dom .Node ;
2117
22- import com .google .gson .JsonArray ;
23- import com .google .gson .JsonElement ;
24- import com .google .gson .JsonObject ;
25- import com .google .gson .JsonPrimitive ;
26- import com .google .gson .internal .LazilyParsedNumber ;
27-
2818import firemerald .mcms .api .data .attributes .AttributeBoolean ;
2919import firemerald .mcms .api .data .attributes .AttributeByte ;
3020import firemerald .mcms .api .data .attributes .AttributeDouble ;
@@ -278,284 +268,4 @@ public void setXML(Document doc, Node parent)
278268 getAttributes ().forEach ((name , attribute ) -> element .setAttribute (name , attribute .getString ()));
279269 ((List <Element >) getChildren ()).forEach (child -> child .setXML (doc , element ));
280270 }
281-
282- public static Element loadJSON (JsonElement element )
283- {
284- String name = "root" ;
285- if (element instanceof JsonObject )
286- {
287- JsonObject object2 = (JsonObject ) element ;
288- if (object2 .has ("#name" )) name = object2 .get ("#name" ).getAsString ();
289- }
290- Element el = new Element (name );
291- el .loadFromJSON (element );
292- return el ;
293- }
294-
295- public void loadFromJSON (JsonElement element )
296- {
297- if (element instanceof JsonObject ) loadFromJSON ((JsonObject ) element );
298- else if (element instanceof JsonArray ) loadFromJSON ((JsonArray ) element );
299- else if (element instanceof JsonPrimitive ) loadFromJSON ((JsonPrimitive ) element );
300- }
301-
302- public void loadFromJSON (JsonObject object )
303- {
304- object .entrySet ().forEach (entry -> {
305- String name = entry .getKey ();
306- if (name .equals ("#value" ))
307- {
308- this .setValue (entry .getValue ().getAsString ());
309- }
310- else if (!name .equals ("#name" ))
311- {
312- JsonElement element = entry .getValue ();
313- if (element instanceof JsonPrimitive ) setAttribute (name , (JsonPrimitive ) element );
314- else
315- {
316- if (element instanceof JsonObject )
317- {
318- JsonObject object2 = (JsonObject ) element ;
319- if (object2 .has ("#name" )) name = object2 .get ("#name" ).getAsString ();
320- }
321- Element el = this .addChild (name );
322- el .loadFromJSON (element );
323- }
324- }
325- });
326- }
327-
328- /*
329- * no children and all attributes have positive integer names
330- * or
331- * no attributes and all children have positive integer names
332- */
333- public void loadFromJSON (JsonArray array )
334- {
335- boolean isPrimitive = true ;
336- for (int i = 0 ; i < array .size (); i ++) if (!(array .get (i ) instanceof JsonPrimitive ))
337- {
338- isPrimitive = false ;
339- break ;
340- }
341- for (int i = 0 ; i < array .size (); i ++)
342- {
343- if (isPrimitive ) setAttribute (Integer .toString (i ), (JsonPrimitive ) array .get (i ));
344- else
345- {
346- JsonElement element2 = array .get (i );
347- Element el = this .addChild (Integer .toString (i ));
348- el .loadFromJSON (element2 );
349- }
350- }
351- }
352-
353- /*
354- * value != null, no attributes, no children.
355- */
356- public void loadFromJSON (JsonPrimitive primitive )
357- {
358- this .setValue (primitive .getAsString ());
359- }
360-
361- public void setAttribute (String name , JsonPrimitive value )
362- {
363- if (value .isString ()) this .setString (name , value .getAsString ());
364- else if (value .isBoolean ()) this .setBoolean (name , value .getAsBoolean ());
365- else if (value .isNumber ())
366- {
367- Number num = value .getAsNumber ();
368- if (num instanceof Byte ) this .setByte (name , value .getAsByte ());
369- else if (num instanceof Short ) this .setShort (name , value .getAsShort ());
370- else if (num instanceof Integer ) this .setInt (name , value .getAsInt ());
371- else if (num instanceof Long ) this .setLong (name , value .getAsLong ());
372- else if (num instanceof BigInteger ) this .setLong (name , value .getAsBigInteger ().longValueExact ());
373- else if (num instanceof Float ) this .setFloat (name , value .getAsFloat ());
374- else if (num instanceof Double ) this .setDouble (name , value .getAsDouble ());
375- else if (num instanceof BigDecimal ) this .setDouble (name , value .getAsBigDecimal ().doubleValue ());
376- else if (num instanceof LazilyParsedNumber )
377- {
378- LazilyParsedNumber l = (LazilyParsedNumber ) num ;
379- if (l .toString ().contains ("." ) || l .toString ().contains ("E" ) || l .toString ().contains ("e" )) this .setDouble (name , l .doubleValue ());
380- else this .setLong (name , l .longValue ());
381- }
382- else System .err .println ("Invalid number type: " + num .getClass ());
383- //TODO else exception
384- }
385- else System .err .println ("Invalid primitive type: " + value );
386- //TODO else exception
387- }
388-
389- public static class NumberedElement implements Comparable <NumberedElement >
390- {
391- public final Element el ;
392- public final int num ;
393-
394- public NumberedElement (Element el , int num )
395- {
396- this .el = el ;
397- this .num = num ;
398- }
399-
400- @ Override
401- public int compareTo (NumberedElement arg0 )
402- {
403- return num - arg0 .num ;
404- }
405- }
406-
407- public static class NumberedAttribute implements Comparable <NumberedAttribute >
408- {
409- public final IAttribute attr ;
410- public final int num ;
411-
412- public NumberedAttribute (IAttribute attr , int num )
413- {
414- this .attr = attr ;
415- this .num = num ;
416- }
417-
418- @ Override
419- public int compareTo (NumberedAttribute arg0 )
420- {
421- return num - arg0 .num ;
422- }
423- }
424-
425- public JsonElement makeElement (boolean needsName )
426- {
427- String value = this .getValue ();
428- Map <String , IAttribute > attributes = this .getAttributes ();
429- @ SuppressWarnings ("unchecked" )
430- List <Element > children = (List <Element >) this .getChildren ();
431- if (attributes .isEmpty ())
432- {
433- if (children .isEmpty ())
434- {
435- if (value == null ) return new JsonObject ();
436- else return new JsonPrimitive (value );
437- }
438- else
439- {
440- if (value == null )
441- {
442- boolean flag = false ;
443- TreeSet <NumberedElement > s = new TreeSet <>();
444- for (Element child : children )
445- {
446- try
447- {
448- NumberedElement el ;
449- Integer i = Integer .parseInt (child .getName ());
450- if (i < 0 || s .contains (el = new NumberedElement (child , i )))
451- {
452- flag = true ;
453- break ;
454- }
455- else s .add (el );
456- }
457- catch (NumberFormatException e )
458- {
459- flag = true ;
460- break ;
461- }
462- }
463- if (!flag )
464- {
465- JsonArray array = new JsonArray ();
466- int targetNum = 0 ;
467- for (NumberedElement el : s )
468- {
469- if (el .num == targetNum )
470- {
471- array .add (el .el .makeElement (false ));
472- targetNum ++;
473- }
474- else
475- {
476- flag = true ;
477- break ;
478- }
479- }
480- if (!flag ) return array ;
481- }
482- }
483- }
484- }
485- else
486- {
487- if (children .isEmpty ())
488- {
489- if (value == null )
490- {
491- boolean flag = false ;
492- TreeSet <NumberedAttribute > s = new TreeSet <>();
493- for (Entry <String , IAttribute > entry : attributes .entrySet ())
494- {
495- try
496- {
497- NumberedAttribute el ;
498- Integer i = Integer .parseInt (entry .getKey ());
499- if (i < 0 || s .contains (el = new NumberedAttribute (entry .getValue (), i )))
500- {
501- flag = true ;
502- break ;
503- }
504- else s .add (el );
505- }
506- catch (NumberFormatException e )
507- {
508- flag = true ;
509- break ;
510- }
511- }
512- if (!flag )
513- {
514- JsonArray array = new JsonArray ();
515- int targetNum = 0 ;
516- for (NumberedAttribute attr : s )
517- {
518- if (attr .num == targetNum )
519- {
520- array .add (attr .attr .makeElement ());
521- targetNum ++;
522- }
523- else
524- {
525- flag = true ;
526- break ;
527- }
528- }
529- if (!flag ) return array ;
530- }
531- }
532- }
533- }
534- List <String > childNames = new ArrayList <>();
535- JsonObject obj = new JsonObject ();
536- if (needsName ) obj .addProperty ("\" #name\" " , getName ());
537- if (value != null ) obj .addProperty ("\" #value\" " , value );
538- attributes .forEach ((name , attr ) -> {
539- childNames .add (name );
540- obj .add ('"' + name + '"' , attr .makeElement ());
541- });
542- int i = 0 ;
543- for (Element child : children )
544- {
545- String name = child .getName ();
546- if (childNames .contains (name ))
547- {
548- String oldName = name ;
549- while (childNames .contains (name = (0 == i ++) ? (oldName + " (duplicate)" ) : (oldName + " (duplicate " + i + ")" ))) {};
550- childNames .add (name );
551- obj .add ('"' + name + '"' , child .makeElement (true ));
552- }
553- else
554- {
555- childNames .add (name );
556- obj .add ('"' + name + '"' , child .makeElement (false ));
557- }
558- }
559- return obj ;
560- }
561271}
0 commit comments