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