corba/src/share/classes/com/sun/corba/se/impl/orbutil/ObjectWriter.java
author ohair
Tue, 25 May 2010 15:52:11 -0700
changeset 5555 b2b5ed3f0d0d
parent 4 02bb8761fcce
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4
02bb8761fcce Initial load
duke
parents:
diff changeset
     1
/*
5555
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
     2
 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved.
4
02bb8761fcce Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
02bb8761fcce Initial load
duke
parents:
diff changeset
     4
 *
02bb8761fcce Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
02bb8761fcce Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5555
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
4
02bb8761fcce Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5555
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
4
02bb8761fcce Initial load
duke
parents:
diff changeset
    10
 *
02bb8761fcce Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
02bb8761fcce Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
02bb8761fcce Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
02bb8761fcce Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
02bb8761fcce Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
02bb8761fcce Initial load
duke
parents:
diff changeset
    16
 *
02bb8761fcce Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
02bb8761fcce Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
02bb8761fcce Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
02bb8761fcce Initial load
duke
parents:
diff changeset
    20
 *
5555
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
    23
 * questions.
4
02bb8761fcce Initial load
duke
parents:
diff changeset
    24
 */
02bb8761fcce Initial load
duke
parents:
diff changeset
    25
02bb8761fcce Initial load
duke
parents:
diff changeset
    26
package com.sun.corba.se.impl.orbutil ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    27
02bb8761fcce Initial load
duke
parents:
diff changeset
    28
import java.util.Arrays ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    29
02bb8761fcce Initial load
duke
parents:
diff changeset
    30
public abstract class ObjectWriter {
02bb8761fcce Initial load
duke
parents:
diff changeset
    31
    public static ObjectWriter make( boolean isIndenting,
02bb8761fcce Initial load
duke
parents:
diff changeset
    32
        int initialLevel, int increment )
02bb8761fcce Initial load
duke
parents:
diff changeset
    33
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
    34
        if (isIndenting)
02bb8761fcce Initial load
duke
parents:
diff changeset
    35
            return new IndentingObjectWriter( initialLevel, increment ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    36
        else
02bb8761fcce Initial load
duke
parents:
diff changeset
    37
            return new SimpleObjectWriter() ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    38
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
    39
02bb8761fcce Initial load
duke
parents:
diff changeset
    40
    public abstract void startObject( Object obj ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    41
02bb8761fcce Initial load
duke
parents:
diff changeset
    42
    public abstract void startElement() ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    43
02bb8761fcce Initial load
duke
parents:
diff changeset
    44
    public abstract void endElement() ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    45
02bb8761fcce Initial load
duke
parents:
diff changeset
    46
    public abstract void endObject( String str ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    47
02bb8761fcce Initial load
duke
parents:
diff changeset
    48
    public abstract void endObject() ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    49
02bb8761fcce Initial load
duke
parents:
diff changeset
    50
    public String toString() { return result.toString() ; }
02bb8761fcce Initial load
duke
parents:
diff changeset
    51
02bb8761fcce Initial load
duke
parents:
diff changeset
    52
    public void append( boolean arg ) { result.append( arg ) ; }
02bb8761fcce Initial load
duke
parents:
diff changeset
    53
02bb8761fcce Initial load
duke
parents:
diff changeset
    54
    public void append( char arg ) { result.append( arg ) ; }
02bb8761fcce Initial load
duke
parents:
diff changeset
    55
02bb8761fcce Initial load
duke
parents:
diff changeset
    56
    public void append( short arg ) { result.append( arg ) ; }
02bb8761fcce Initial load
duke
parents:
diff changeset
    57
02bb8761fcce Initial load
duke
parents:
diff changeset
    58
    public void append( int arg ) { result.append( arg ) ; }
02bb8761fcce Initial load
duke
parents:
diff changeset
    59
02bb8761fcce Initial load
duke
parents:
diff changeset
    60
    public void append( long arg ) { result.append( arg ) ; }
02bb8761fcce Initial load
duke
parents:
diff changeset
    61
02bb8761fcce Initial load
duke
parents:
diff changeset
    62
    public void append( float arg ) { result.append( arg ) ; }
02bb8761fcce Initial load
duke
parents:
diff changeset
    63
02bb8761fcce Initial load
duke
parents:
diff changeset
    64
    public void append( double arg ) { result.append( arg ) ; }
02bb8761fcce Initial load
duke
parents:
diff changeset
    65
02bb8761fcce Initial load
duke
parents:
diff changeset
    66
    public void append( String arg ) { result.append( arg ) ; }
02bb8761fcce Initial load
duke
parents:
diff changeset
    67
02bb8761fcce Initial load
duke
parents:
diff changeset
    68
//=================================================================================================
02bb8761fcce Initial load
duke
parents:
diff changeset
    69
// Implementation
02bb8761fcce Initial load
duke
parents:
diff changeset
    70
//=================================================================================================
02bb8761fcce Initial load
duke
parents:
diff changeset
    71
02bb8761fcce Initial load
duke
parents:
diff changeset
    72
    protected StringBuffer result ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    73
02bb8761fcce Initial load
duke
parents:
diff changeset
    74
    protected ObjectWriter()
02bb8761fcce Initial load
duke
parents:
diff changeset
    75
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
    76
        result = new StringBuffer() ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    77
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
    78
02bb8761fcce Initial load
duke
parents:
diff changeset
    79
    protected void appendObjectHeader( Object obj )
02bb8761fcce Initial load
duke
parents:
diff changeset
    80
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
    81
        result.append( obj.getClass().getName() ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    82
        result.append( "<" ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    83
        result.append( System.identityHashCode( obj ) ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    84
        result.append( ">" ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    85
        Class compClass = obj.getClass().getComponentType() ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    86
02bb8761fcce Initial load
duke
parents:
diff changeset
    87
        if (compClass != null) {
02bb8761fcce Initial load
duke
parents:
diff changeset
    88
            result.append( "[" ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    89
            if (compClass == boolean.class) {
02bb8761fcce Initial load
duke
parents:
diff changeset
    90
                boolean[] arr = (boolean[])obj ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    91
                result.append( arr.length ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    92
                result.append( "]" ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    93
            } else if (compClass == byte.class) {
02bb8761fcce Initial load
duke
parents:
diff changeset
    94
                byte[] arr = (byte[])obj ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    95
                result.append( arr.length ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    96
                result.append( "]" ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    97
            } else if (compClass == short.class) {
02bb8761fcce Initial load
duke
parents:
diff changeset
    98
                short[] arr = (short[])obj ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    99
                result.append( arr.length ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   100
                result.append( "]" ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   101
            } else if (compClass == int.class) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   102
                int[] arr = (int[])obj ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   103
                result.append( arr.length ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   104
                result.append( "]" ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   105
            } else if (compClass == long.class) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   106
                long[] arr = (long[])obj ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   107
                result.append( arr.length ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   108
                result.append( "]" ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   109
            } else if (compClass == char.class) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   110
                char[] arr = (char[])obj ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   111
                result.append( arr.length ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   112
                result.append( "]" ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   113
            } else if (compClass == float.class) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   114
                float[] arr = (float[])obj ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   115
                result.append( arr.length ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   116
                result.append( "]" ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   117
            } else if (compClass == double.class) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   118
                double[] arr = (double[])obj ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   119
                result.append( arr.length ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   120
                result.append( "]" ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   121
            } else { // array of object
02bb8761fcce Initial load
duke
parents:
diff changeset
   122
                java.lang.Object[] arr = (java.lang.Object[])obj ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   123
                result.append( arr.length ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   124
                result.append( "]" ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   125
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   126
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   127
02bb8761fcce Initial load
duke
parents:
diff changeset
   128
        result.append( "(" ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   129
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   130
02bb8761fcce Initial load
duke
parents:
diff changeset
   131
    /** Expected patterns:
02bb8761fcce Initial load
duke
parents:
diff changeset
   132
    * startObject endObject( str )
02bb8761fcce Initial load
duke
parents:
diff changeset
   133
    *   header( elem )\n
02bb8761fcce Initial load
duke
parents:
diff changeset
   134
    * startObject ( startElement append* endElement ) * endObject
02bb8761fcce Initial load
duke
parents:
diff changeset
   135
    *   header(\n
02bb8761fcce Initial load
duke
parents:
diff changeset
   136
    *       append*\n *
02bb8761fcce Initial load
duke
parents:
diff changeset
   137
    *   )\n
02bb8761fcce Initial load
duke
parents:
diff changeset
   138
    */
02bb8761fcce Initial load
duke
parents:
diff changeset
   139
    private static class IndentingObjectWriter extends ObjectWriter {
02bb8761fcce Initial load
duke
parents:
diff changeset
   140
        private int level ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   141
        private int increment ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   142
02bb8761fcce Initial load
duke
parents:
diff changeset
   143
        public IndentingObjectWriter( int initialLevel, int increment )
02bb8761fcce Initial load
duke
parents:
diff changeset
   144
        {
02bb8761fcce Initial load
duke
parents:
diff changeset
   145
            this.level = initialLevel ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   146
            this.increment = increment ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   147
            startLine() ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   148
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   149
02bb8761fcce Initial load
duke
parents:
diff changeset
   150
        private void startLine()
02bb8761fcce Initial load
duke
parents:
diff changeset
   151
        {
02bb8761fcce Initial load
duke
parents:
diff changeset
   152
            char[] fill = new char[ level * increment ] ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   153
            Arrays.fill( fill, ' ' ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   154
            result.append( fill ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   155
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   156
02bb8761fcce Initial load
duke
parents:
diff changeset
   157
        public void startObject( java.lang.Object obj )
02bb8761fcce Initial load
duke
parents:
diff changeset
   158
        {
02bb8761fcce Initial load
duke
parents:
diff changeset
   159
            appendObjectHeader( obj ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   160
            level++ ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   161
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   162
02bb8761fcce Initial load
duke
parents:
diff changeset
   163
        public void startElement()
02bb8761fcce Initial load
duke
parents:
diff changeset
   164
        {
02bb8761fcce Initial load
duke
parents:
diff changeset
   165
            result.append( "\n" ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   166
            startLine() ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   167
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   168
02bb8761fcce Initial load
duke
parents:
diff changeset
   169
        public void endElement()
02bb8761fcce Initial load
duke
parents:
diff changeset
   170
        {
02bb8761fcce Initial load
duke
parents:
diff changeset
   171
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   172
02bb8761fcce Initial load
duke
parents:
diff changeset
   173
        public void endObject( String str )
02bb8761fcce Initial load
duke
parents:
diff changeset
   174
        {
02bb8761fcce Initial load
duke
parents:
diff changeset
   175
            level-- ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   176
            result.append( str ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   177
            result.append( ")" ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   178
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   179
02bb8761fcce Initial load
duke
parents:
diff changeset
   180
        public void endObject( )
02bb8761fcce Initial load
duke
parents:
diff changeset
   181
        {
02bb8761fcce Initial load
duke
parents:
diff changeset
   182
            level-- ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   183
            result.append( "\n" ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   184
            startLine() ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   185
            result.append( ")" ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   186
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   187
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   188
02bb8761fcce Initial load
duke
parents:
diff changeset
   189
    private static class SimpleObjectWriter extends ObjectWriter {
02bb8761fcce Initial load
duke
parents:
diff changeset
   190
        public void startObject( java.lang.Object obj )
02bb8761fcce Initial load
duke
parents:
diff changeset
   191
        {
02bb8761fcce Initial load
duke
parents:
diff changeset
   192
            appendObjectHeader( obj ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   193
            result.append( " " ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   194
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   195
02bb8761fcce Initial load
duke
parents:
diff changeset
   196
        public void startElement()
02bb8761fcce Initial load
duke
parents:
diff changeset
   197
        {
02bb8761fcce Initial load
duke
parents:
diff changeset
   198
            result.append( " " ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   199
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   200
02bb8761fcce Initial load
duke
parents:
diff changeset
   201
        public void endObject( String str )
02bb8761fcce Initial load
duke
parents:
diff changeset
   202
        {
02bb8761fcce Initial load
duke
parents:
diff changeset
   203
            result.append( str ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   204
            result.append( ")" ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   205
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   206
02bb8761fcce Initial load
duke
parents:
diff changeset
   207
        public void endElement()
02bb8761fcce Initial load
duke
parents:
diff changeset
   208
        {
02bb8761fcce Initial load
duke
parents:
diff changeset
   209
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   210
02bb8761fcce Initial load
duke
parents:
diff changeset
   211
        public void endObject()
02bb8761fcce Initial load
duke
parents:
diff changeset
   212
        {
02bb8761fcce Initial load
duke
parents:
diff changeset
   213
            result.append( ")" ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   214
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   215
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   216
}