summaryrefslogtreecommitdiffstats
path: root/java/gov/nist/core/GenericObject.java
blob: 53eea6b59a8e89988c427da3d2e9fedb00d60a40 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
/*
* Conditions Of Use
*
* This software was developed by employees of the National Institute of
* Standards and Technology (NIST), an agency of the Federal Government.
* Pursuant to title 15 Untied States Code Section 105, works of NIST
* employees are not subject to copyright protection in the United States
* and are considered to be in the public domain.  As a result, a formal
* license is not needed to use the software.
*
* This software is provided by NIST as a service and is expressly
* provided "AS IS."  NIST MAKES NO WARRANTY OF ANY KIND, EXPRESS, IMPLIED
* OR STATUTORY, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT
* AND DATA ACCURACY.  NIST does not warrant or make any representations
* regarding the use of the software or the results thereof, including but
* not limited to the correctness, accuracy, reliability or usefulness of
* the software.
*
* Permission to use this software is contingent upon your acceptance
* of the terms of this agreement
*
* .
*
*/
/******************************************************************************
 * Product of NIST/ITL Advanced Networking Technologies Division (ANTD).      *
 ******************************************************************************/
package gov.nist.core;
import java.lang.reflect.*;
import java.io.Serializable;
import java.util.*;

/**
* The base class from which all the other classes in the
* sipheader, sdpfields and sipmessage packages are extended.
* Provides a few utility funcitons such as indentation and
* pretty printing that all other classes benifit from.
*
*@version 1.2
*
*@author M. Ranganathan   <br/>
*
*
*
*/

public abstract class GenericObject implements Serializable, Cloneable {
    // Useful constants.
    protected static final String SEMICOLON = Separators.SEMICOLON;
    protected static final String COLON = Separators.COLON;
    protected static final String COMMA = Separators.COMMA;
    protected static final String SLASH = Separators.SLASH;
    protected static final String SP = Separators.SP;
    protected static final String EQUALS = Separators.EQUALS;
    protected static final String STAR = Separators.STAR;
    protected static final String NEWLINE = Separators.NEWLINE;
    protected static final String RETURN = Separators.RETURN;
    protected static final String LESS_THAN = Separators.LESS_THAN;
    protected static final String GREATER_THAN = Separators.GREATER_THAN;
    protected static final String AT = Separators.AT;
    protected static final String DOT = Separators.DOT;
    protected static final String QUESTION = Separators.QUESTION;
    protected static final String POUND = Separators.POUND;
    protected static final String AND = Separators.AND;
    protected static final String LPAREN = Separators.LPAREN;
    protected static final String RPAREN = Separators.RPAREN;
    protected static final String DOUBLE_QUOTE = Separators.DOUBLE_QUOTE;
    protected static final String QUOTE = Separators.QUOTE;
    protected static final String HT = Separators.HT;
    protected static final String PERCENT = Separators.PERCENT;

    protected static final Set<Class<?>> immutableClasses = new HashSet<Class<?>> (10);
    static final String[] immutableClassNames ={
        "String", "Character",
        "Boolean", "Byte", "Short", "Integer", "Long",
        "Float", "Double"
        };

    protected int indentation;
    protected String stringRepresentation;
    protected Match matchExpression; // Pattern matcher.

    static {
        try {
            for (int i = 0; i < immutableClassNames.length; i++)
                immutableClasses.add(Class.forName("java.lang." + immutableClassNames [i]));
        } catch (ClassNotFoundException e) {
            throw new RuntimeException ("Internal error", e);
        }
    }

    /** Set the  pattern matcher. To match on the
     * field of a sip message, set the match expression in the match template
     * and invoke the match function. This useful because
     * SIP headers and parameters may appear in different orders and are not
     * necessarily in canonical form. This makes it hard to write a pattern
     * matcher that relies on regular expressions alone.
     * Thus we rely on the following  strategy i.e. To do pattern matching on
     * an incoming message, first parse it, and then construct a match template,
     * filling in the fields that you want to
     * match. The rules for matching are: A null object matches wild card -
     * that is a match template of null matches any parsed SIP object.
     * To match with any subfield, set the match template on a template object
     * of the same type and invoke the match interface.
     * Regular expressions matching implements the gov.nist.sip.Match interface
     * that can be done using the Jakarta regexp package for example.
     * package included herein. This can be used to implement the Match interface
     * <a href=http://www.apache.org> See the APACHE website for documents </a>
     *
     */
    public void setMatcher(Match matchExpression) {
        if (matchExpression == null)
            throw new IllegalArgumentException("null arg!");
        this.matchExpression = matchExpression;
    }

    /** Return the match expression.
     *@return the match expression that has previously been set.
     */
    public Match getMatcher() {
        return matchExpression;
    }

    public static Class<?> getClassFromName(String className) {
        try {
            return Class.forName(className);
        } catch (Exception ex) {
            InternalErrorHandler.handleException(ex);
            return null;
        }
    }

    public static boolean isMySubclass(Class<?> other) {

            return GenericObject.class.isAssignableFrom(other);

    }

    /** Clones the given object.
     *  If the object is a wrapped type, an array, a GenericObject
     *  or a GenericObjectList, it is cast to the appropriate type
     *  and the clone() method is invoked. Else if the object implements
     *  Cloneable, reflection is used to discover and invoke
     *  clone() method. Otherwise, the original object is returned.
     */
    public static Object makeClone(Object obj) {
        if (obj == null)
            throw new NullPointerException("null obj!");
        Class<?> c = obj.getClass();
        Object clone_obj = obj;
        if (immutableClasses.contains (c))
            return obj;
        else if (c.isArray ()) {
            Class<?> ec = c.getComponentType();
            if (! ec.isPrimitive())
                clone_obj = ((Object []) obj).clone();
            else {
                if (ec == Character.TYPE)
                    clone_obj = ((char []) obj).clone();
                else if (ec == Boolean.TYPE)
                    clone_obj = ((boolean []) obj).clone();
                if (ec == Byte.TYPE)
                    clone_obj = ((byte []) obj).clone();
                else if (ec == Short.TYPE)
                    clone_obj = ((short []) obj).clone();
                else if (ec == Integer.TYPE)
                    clone_obj = ((int []) obj).clone();
                else if (ec == Long.TYPE)
                    clone_obj = ((long []) obj).clone();
                else if (ec == Float.TYPE)
                    clone_obj = ((float []) obj).clone();
                else if (ec == Double.TYPE)
                    clone_obj = ((double []) obj).clone();
            }
        } else if (GenericObject.class.isAssignableFrom (c))
            clone_obj = ((GenericObject) obj).clone();
        else if (GenericObjectList.class.isAssignableFrom (c))
            clone_obj = ((GenericObjectList) obj).clone();
        else if (Cloneable.class.isAssignableFrom (c)) {
            // If a clone method exists for the object, then
            // invoke it
            try {
                Method meth = c.getMethod("clone", (Class[]) null);
                clone_obj = meth.invoke(obj,(Object[]) null);
            } catch (SecurityException ex) {
            } catch (IllegalArgumentException ex) {
                InternalErrorHandler.handleException(ex);
            } catch (IllegalAccessException ex) {
            } catch (InvocationTargetException ex) {
            } catch (NoSuchMethodException ex) {
            }
        }
        return clone_obj;
    }

    /** Clones this object.
     */
    public Object clone() {
        try {
            return super.clone();
        } catch (CloneNotSupportedException e) {
            throw new RuntimeException("Internal error");
        }
    }
    /**
     * Recursively override the fields of this object with the fields
     * of a new object. This is useful when you want to genrate a template
     * and override the fields of an incoming SIPMessage with another
     * SIP message that you have already generated.
     *
     * @param mergeObject is the replacement object.  The override
     * obect must be of the same class as this object.
     * Set any fields that you do not want to override as null in the
     * mergeOject object.
     */
    public void merge(Object mergeObject) {
        // Base case.
        if (mergeObject == null)
            return;

        if (!mergeObject.getClass().equals(this.getClass()))
            throw new IllegalArgumentException("Bad override object");
                        
        Class<?> myclass = this.getClass();
        while (true) {
            Field[] fields = myclass.getDeclaredFields();
            for (int i = 0; i < fields.length; i++) {
                Field f = fields[i];
                int modifier = f.getModifiers();
                if (Modifier.isPrivate(modifier)) {
                    continue;
                } else if (Modifier.isStatic(modifier)) {
                    continue;
                } else if (Modifier.isInterface(modifier)) {
                    continue;
                }
                Class<?> fieldType = f.getType();
                String fname = fieldType.toString();
                try {
                    // Primitive fields are printed with type: value
                    if (fieldType.isPrimitive()) {
                        if (fname.compareTo("int") == 0) {
                            int intfield = f.getInt(mergeObject);
                            f.setInt(this, intfield);
                        } else if (fname.compareTo("short") == 0) {
                            short shortField = f.getShort(mergeObject);
                            f.setShort(this, shortField);
                        } else if (fname.compareTo("char") == 0) {
                            char charField = f.getChar(mergeObject);
                            f.setChar(this, charField);
                        } else if (fname.compareTo("long") == 0) {
                            long longField = f.getLong(mergeObject);
                            f.setLong(this, longField);
                        } else if (fname.compareTo("boolean") == 0) {
                            boolean booleanField = f.getBoolean(mergeObject);
                            f.setBoolean(this, booleanField);
                        } else if (fname.compareTo("double") == 0) {
                            double doubleField = f.getDouble(mergeObject);
                            f.setDouble(this, doubleField);
                        } else if (fname.compareTo("float") == 0) {
                            float floatField = f.getFloat(mergeObject);
                            f.setFloat(this, floatField);
                        }
                    } else {
                        Object obj = f.get(this);
                        Object mobj = f.get(mergeObject);
                        if (mobj == null)
                            continue;
                        if (obj == null) {
                            f.set(this, mobj);
                            continue;
                        }
                        if (obj instanceof GenericObject) {
                            GenericObject gobj = (GenericObject) obj;
                            gobj.merge(mobj);
                        } else {
                            f.set(this, mobj);
                        }
                    }
                } catch (IllegalAccessException ex1) {
                    ex1.printStackTrace();
                    continue; // we are accessing a private field...
                }
            }
            myclass = myclass.getSuperclass();
            if (myclass.equals(GenericObject.class))
                break;
        }
    }

    protected GenericObject() {
        indentation = 0;
        stringRepresentation = "";
    }

    protected String getIndentation() {
    char [] chars = new char [indentation];
    java.util.Arrays.fill (chars, ' ');
    return new String (chars);
    }

    /**
     * Add a new string to the accumulated string representation.
     */

    protected void sprint(String a) {
        if (a == null) {
            stringRepresentation += getIndentation();
            stringRepresentation += "<null>\n";
            return;
        }
        if (a.compareTo("}") == 0 || a.compareTo("]") == 0) {
            indentation--;
        }
        stringRepresentation += getIndentation();
        stringRepresentation += a;
        stringRepresentation += "\n";
        if (a.compareTo("{") == 0 || a.compareTo("[") == 0) {
            indentation++;
        }

    }

    /**
     * Pretty printing function accumulator for objects.
     */

    protected void sprint(Object o) {
        sprint(o.toString());
    }

    /**
     * Pretty printing accumulator function for ints
     */

    protected void sprint(int intField) {
        sprint(String.valueOf(intField));
    }

    /**
     * Pretty printing accumulator function for shorts
     */
    protected void sprint(short shortField) {
        sprint(String.valueOf(shortField));
    }

    /**
     * Pretty printing accumulator function for chars
     */

    protected void sprint(char charField) {
        sprint(String.valueOf(charField));

    }

    /**
     * Pretty printing accumulator function for longs
     */

    protected void sprint(long longField) {
        sprint(String.valueOf(longField));
    }

    /**
     * Pretty printing accumulator function for booleans
     */

    protected void sprint(boolean booleanField) {
        sprint(String.valueOf(booleanField));
    }

    /**
     * Pretty printing accumulator function for doubles
     */

    protected void sprint(double doubleField) {
        sprint(String.valueOf(doubleField));
    }

    /**
     * Pretty printing accumulator function for floats
     */

    protected void sprint(float floatField) {
        sprint(String.valueOf(floatField));
    }

    /**
     * Debug printing function.
     */

    protected void dbgPrint() {
        Debug.println(debugDump());
    }

    /**
     * Debug printing function.
     */
    protected void dbgPrint(String s) {
        Debug.println(s);
    }

    /**
     * An introspection based equality predicate for GenericObjects.
     *@param that is the other object to test against.
     *@return true if the objects are euqal and false otherwise
     */
    public boolean equals(Object that) {
        if ( that == null ) return false;
        if (!this.getClass().equals(that.getClass()))
            return false;
        Class<?> myclass = this.getClass();
        Class<?> hisclass = that.getClass();
        while (true) {
            Field[] fields = myclass.getDeclaredFields();
            Field[] hisfields = hisclass.getDeclaredFields();
            for (int i = 0; i < fields.length; i++) {
                Field f = fields[i];
                Field g = hisfields[i];
                // Only print protected and public members.
                int modifier = f.getModifiers();
                if ((modifier & Modifier.PRIVATE) == Modifier.PRIVATE)
                    continue;
                Class<?> fieldType = f.getType();
                String fieldName = f.getName();
                if (fieldName.compareTo("stringRepresentation") == 0) {
                    continue;
                }
                if (fieldName.compareTo("indentation") == 0) {
                    continue;
                }
                try {
                    // Primitive fields are printed with type: value
                    if (fieldType.isPrimitive()) {
                        String fname = fieldType.toString();
                        if (fname.compareTo("int") == 0) {
                            if (f.getInt(this) != g.getInt(that))
                                return false;
                        } else if (fname.compareTo("short") == 0) {
                            if (f.getShort(this) != g.getShort(that))
                                return false;
                        } else if (fname.compareTo("char") == 0) {
                            if (f.getChar(this) != g.getChar(that))
                                return false;
                        } else if (fname.compareTo("long") == 0) {
                            if (f.getLong(this) != g.getLong(that))
                                return false;
                        } else if (fname.compareTo("boolean") == 0) {
                            if (f.getBoolean(this) != g.getBoolean(that))
                                return false;
                        } else if (fname.compareTo("double") == 0) {
                            if (f.getDouble(this) != g.getDouble(that))
                                return false;
                        } else if (fname.compareTo("float") == 0) {
                            if (f.getFloat(this) != g.getFloat(that))
                                return false;
                        }
                    } else if (g.get(that) == f.get(this))
                        return true;
                    else if (f.get(this) == null)
                        return false;
                    else if (g.get(that) == null)
                        return false;
                    else if (g.get(that) == null && f.get(this) != null)
                        return false;
                    else if (!f.get(this).equals(g.get(that)))
                        return false;
                } catch (IllegalAccessException ex1) {
                    InternalErrorHandler.handleException(ex1);
                }
            }
            if (myclass.equals(GenericObject.class))
                break;
            else {
                myclass = myclass.getSuperclass();
                hisclass = hisclass.getSuperclass();
            }

        }
        return true;
    }

    /** An introspection based predicate matching using a template
     * object. Allows for partial match of two protocl Objects.
     *@param other the match pattern to test against. The match object
     * has to be of the same type (class). Primitive types
     * and non-sip fields that are non null are matched for equality.
     * Null in any field  matches anything. Some book-keeping fields
     * are ignored when making the comparison.
     */

    public boolean match(Object other) {
        if (other == null)
            return true;
        if (!this.getClass().equals(other.getClass()))
            return false;
        GenericObject that = (GenericObject) other;
        Class<?> myclass = this.getClass();
        Field[] fields = myclass.getDeclaredFields();
        Class<?> hisclass = other.getClass();
        Field[] hisfields = hisclass.getDeclaredFields();
        for (int i = 0; i < fields.length; i++) {
            Field f = fields[i];
            Field g = hisfields[i];
            // Only print protected and public members.
            int modifier = f.getModifiers();
            if ((modifier & Modifier.PRIVATE) == Modifier.PRIVATE)
                continue;
            Class<?> fieldType = f.getType();
            String fieldName = f.getName();
            if (fieldName.compareTo("stringRepresentation") == 0) {
                continue;
            }
            if (fieldName.compareTo("indentation") == 0) {
                continue;
            }
            try {
                // Primitive fields are printed with type: value
                if (fieldType.isPrimitive()) {
                    String fname = fieldType.toString();
                    if (fname.compareTo("int") == 0) {
                        if (f.getInt(this) != g.getInt(that))
                            return false;
                    } else if (fname.compareTo("short") == 0) {
                        if (f.getShort(this) != g.getShort(that))
                            return false;
                    } else if (fname.compareTo("char") == 0) {
                        if (f.getChar(this) != g.getChar(that))
                            return false;
                    } else if (fname.compareTo("long") == 0) {
                        if (f.getLong(this) != g.getLong(that))
                            return false;
                    } else if (fname.compareTo("boolean") == 0) {
                        if (f.getBoolean(this) != g.getBoolean(that))
                            return false;
                    } else if (fname.compareTo("double") == 0) {
                        if (f.getDouble(this) != g.getDouble(that))
                            return false;
                    } else if (fname.compareTo("float") == 0) {
                        if (f.getFloat(this) != g.getFloat(that))
                            return false;
                    }
                } else {
                    Object myObj = f.get(this);
                    Object hisObj = g.get(that);
                    if (hisObj != null && myObj == null)
                        return false;
                    else if (hisObj == null && myObj != null)
                        continue;
                    else if (hisObj == null && myObj == null)
                        continue;
                    else if (
                        hisObj instanceof java.lang.String
                            && myObj instanceof java.lang.String) {
                        if ((((String) hisObj).trim()).equals(""))
                            continue;
                        if (((String) myObj)
                            .compareToIgnoreCase((String) hisObj)
                            != 0)
                            return false;
                    } else if (
                        GenericObject.isMySubclass(myObj.getClass())
                            && !((GenericObject) myObj).match(hisObj))
                        return false;
                    else if (
                        GenericObjectList.isMySubclass(myObj.getClass())
                            && !((GenericObjectList) myObj).match(hisObj))
                        return false;

                }
            } catch (IllegalAccessException ex1) {
                InternalErrorHandler.handleException(ex1);
            }
        }
        return true;
    }

    /**
     * Generic print formatting function:
     * Does depth-first descent of the structure and
     * recursively prints all non-private objects pointed to
     * by this object.
     * <bf>
     * Warning - the following generic string routine will
     * bomb (go into infinite loop) if there are any circularly linked
     * structures so if you have these, they had better be private!
     * </bf>
     * We dont have to worry about such things for our structures
     *(we never use circular linked structures).
     */

    public String debugDump() {
        stringRepresentation = "";
        Class<?> myclass = getClass();
        sprint(myclass.getName());
        sprint("{");
        Field[] fields = myclass.getDeclaredFields();
        for (int i = 0; i < fields.length; i++) {
            Field f = fields[i];
            // Only print protected and public members.
            int modifier = f.getModifiers();
            if ((modifier & Modifier.PRIVATE) == Modifier.PRIVATE)
                continue;
            Class<?> fieldType = f.getType();
            String fieldName = f.getName();
            if (fieldName.compareTo("stringRepresentation") == 0) {
                // avoid nasty recursions...
                continue;
            }
            if (fieldName.compareTo("indentation") == 0) {
                // formatting stuff - not relevant here.
                continue;
            }
            sprint(fieldName + ":");
            try {
                // Primitive fields are printed with type: value
                if (fieldType.isPrimitive()) {
                    String fname = fieldType.toString();
                    sprint(fname + ":");
                    if (fname.compareTo("int") == 0) {
                        int intfield = f.getInt(this);
                        sprint(intfield);
                    } else if (fname.compareTo("short") == 0) {
                        short shortField = f.getShort(this);
                        sprint(shortField);
                    } else if (fname.compareTo("char") == 0) {
                        char charField = f.getChar(this);
                        sprint(charField);
                    } else if (fname.compareTo("long") == 0) {
                        long longField = f.getLong(this);
                        sprint(longField);
                    } else if (fname.compareTo("boolean") == 0) {
                        boolean booleanField = f.getBoolean(this);
                        sprint(booleanField);
                    } else if (fname.compareTo("double") == 0) {
                        double doubleField = f.getDouble(this);
                        sprint(doubleField);
                    } else if (fname.compareTo("float") == 0) {
                        float floatField = f.getFloat(this);
                        sprint(floatField);
                    }
                } else if (GenericObject.class.isAssignableFrom(fieldType)) {
                    if (f.get(this) != null) {
                        sprint(
                            ((GenericObject) f.get(this)).debugDump(
                                indentation + 1));
                    } else {
                        sprint("<null>");
                    }

                } else if (
                    GenericObjectList.class.isAssignableFrom(fieldType)) {
                    if (f.get(this) != null) {
                        sprint(
                            ((GenericObjectList) f.get(this)).debugDump(
                                indentation + 1));
                    } else {
                        sprint("<null>");
                    }

                } else {
                    // Dont do recursion on things that are not
                    // of our header type...
                    if (f.get(this) != null) {
                        sprint(f.get(this).getClass().getName() + ":");
                    } else {
                        sprint(fieldType.getName() + ":");
                    }

                    sprint("{");
                    if (f.get(this) != null) {
                        sprint(f.get(this).toString());
                    } else {
                        sprint("<null>");
                    }
                    sprint("}");
                }
            } catch (IllegalAccessException ex1) {
                continue; // we are accessing a private field...
            } catch (Exception ex) {
                InternalErrorHandler.handleException(ex);
            }
        }
        sprint("}");
        return stringRepresentation;
    }

    /**
     * Formatter with a given starting indentation.
     */
    public String debugDump(int indent) {
        indentation = indent;
        String retval = this.debugDump();
        indentation = 0;
        return retval;
    }


    /**
     *  Get the string encoded version of this object
     * @since v1.0
     */
    public abstract String encode();

    /**
     * Put the encoded version of this object in the given StringBuffer.
     */
    public StringBuffer encode(StringBuffer buffer) {
        return buffer.append(encode());
    }
}