1/*
2* Conditions Of Use
3*
4* This software was developed by employees of the National Institute of
5* Standards and Technology (NIST), an agency of the Federal Government.
6* Pursuant to title 15 Untied States Code Section 105, works of NIST
7* employees are not subject to copyright protection in the United States
8* and are considered to be in the public domain.  As a result, a formal
9* license is not needed to use the software.
10*
11* This software is provided by NIST as a service and is expressly
12* provided "AS IS."  NIST MAKES NO WARRANTY OF ANY KIND, EXPRESS, IMPLIED
13* OR STATUTORY, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF
14* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT
15* AND DATA ACCURACY.  NIST does not warrant or make any representations
16* regarding the use of the software or the results thereof, including but
17* not limited to the correctness, accuracy, reliability or usefulness of
18* the software.
19*
20* Permission to use this software is contingent upon your acceptance
21* of the terms of this agreement
22*
23* .
24*
25*/
26/*******************************************************************************
27 * Product of NIST/ITL Advanced Networking Technologies Division (ANTD)        *
28 ******************************************************************************/
29package gov.nist.javax.sip.message;
30
31import gov.nist.core.*;
32import java.lang.reflect.*;
33
34/**
35 * This is the root object from which all other objects in this package
36 * are derived. This class is never directly instantiated (and hence it
37 * is abstract).
38 *
39 * @version 1.2 $Revision: 1.7 $ $Date: 2009/07/17 18:57:54 $
40 * @since 1.1
41 *
42 * @author M. Ranganathan   <br/>
43 *
44 *
45 */
46public abstract class MessageObject extends GenericObject {
47    public abstract String encode();
48
49    public void dbgPrint() {
50        super.dbgPrint();
51    }
52
53    /**
54     * An introspection based string formatting method. We need this because
55     * in this package (although it is an exact duplicate of the one in
56     * the superclass) because it needs to access the protected members
57     * of the other objects in this class.
58     */
59    public String debugDump() {
60        stringRepresentation = "";
61        Class<?> myclass = getClass();
62        sprint(myclass.getName());
63        sprint("{");
64        Field[] fields = myclass.getDeclaredFields();
65        for (int i = 0; i < fields.length; i++) {
66            Field f = fields[i];
67            // Only print protected and public members.
68            int modifier = f.getModifiers();
69            if (modifier == Modifier.PRIVATE)
70                continue;
71            Class<?> fieldType = f.getType();
72            String fieldName = f.getName();
73            if (fieldName.compareTo("stringRepresentation") == 0) {
74                // avoid nasty recursions...
75                continue;
76            }
77            if (fieldName.compareTo("indentation") == 0) {
78                // formatting stuff - not relevant here.
79                continue;
80            }
81            sprint(fieldName + ":");
82            try {
83                // Primitive fields are printed with type: value
84                if (fieldType.isPrimitive()) {
85                    String fname = fieldType.toString();
86                    sprint(fname + ":");
87                    if (fname.compareTo("int") == 0) {
88                        int intfield = f.getInt(this);
89                        sprint(intfield);
90                    } else if (fname.compareTo("short") == 0) {
91                        short shortField = f.getShort(this);
92                        sprint(shortField);
93                    } else if (fname.compareTo("char") == 0) {
94                        char charField = f.getChar(this);
95                        sprint(charField);
96                    } else if (fname.compareTo("long") == 0) {
97                        long longField = f.getLong(this);
98                        sprint(longField);
99                    } else if (fname.compareTo("boolean") == 0) {
100                        boolean booleanField = f.getBoolean(this);
101                        sprint(booleanField);
102                    } else if (fname.compareTo("double") == 0) {
103                        double doubleField = f.getDouble(this);
104                        sprint(doubleField);
105                    } else if (fname.compareTo("float") == 0) {
106                        float floatField = f.getFloat(this);
107                        sprint(floatField);
108                    }
109                } else if (
110                    GenericObject.class.isAssignableFrom(
111                        fieldType)) {
112                    if (f.get(this) != null) {
113                        sprint(
114                            ((GenericObject) f.get(this)).debugDump(
115                                this.indentation + 1));
116                    } else {
117                        sprint("<null>");
118                    }
119
120                } else if (GenericObjectList.class.isAssignableFrom(
121                        fieldType)) {
122                    if (f.get(this) != null) {
123                        sprint(
124                            ((GenericObjectList) f.get(this)).debugDump(
125                                indentation + 1));
126                    } else {
127                        sprint("<null>");
128                    }
129
130                } else {
131                    // Dont do recursion on things that are not
132                    // of our header type...
133                    if (f.get(this) != null) {
134                        sprint(f.get(this).getClass().getName() + ":");
135                    } else {
136                        sprint(fieldType.getName() + ":");
137                    }
138
139                    sprint("{");
140                    if (f.get(this) != null) {
141                        sprint(f.get(this).toString());
142                    } else {
143                        sprint("<null>");
144                    }
145                    sprint("}");
146                }
147            } catch (IllegalAccessException ex1) {
148                continue; // we are accessing a private field...
149            }
150        }
151        sprint("}");
152        return stringRepresentation;
153    }
154
155
156    protected MessageObject() {
157        super();
158    }
159
160    /**
161     * Formatter with a given starting indentation (for nested structs).
162     */
163    public String dbgPrint(int indent) {
164        int save = indentation;
165        indentation = indent;
166        String retval = this.toString();
167        indentation = save;
168        return retval;
169    }
170}
171