root/dydoc/ClassMethodNavigation.java

Revision 19 (checked in by rsz, 4 years ago)

Initial import

Line 
1 //
2 //      ===========================================================================
3 //
4 //      Title:          ClassMethodNavigation.java
5 //      Description:    Somehwat similar to javadoc
6 //      Author:         Rapha‘l Szwarc <zoe (underscore) info (at) mac (dot) com>
7 //      Creation Date:  Thu Dec 09 2004
8 //      Legal:          Copyright (C) 2004 Rapha‘l Szwarc
9 //
10 //      This software is provided 'as-is', without any express or implied warranty.
11 //      In no event will the author be held liable for any damages arising from
12 //      the use of this software.
13 //
14 //      Permission is granted to anyone to use this software for any purpose,
15 //      including commercial applications, and to alter it and
16 //      redistribute it freely, subject to the following restrictions:
17 //
18 //      1. The origin of this software must not be misrepresented;
19 //      you must not claim that you wrote the original software.
20 //      If you use this software in a product, an acknowledgment
21 //      in the product documentation would be appreciated but is not required.
22 //
23 //      2. Altered source versions must be plainly marked as such,
24 //      and must not be misrepresented as being the original software.
25 //
26 //      3. This notice may not be removed or altered from any source distribution.
27 //
28 //      ---------------------------------------------------------------------------
29 //
30
31 package alt.dev.dydoc;
32
33 import java.lang.reflect.Constructor;
34 import java.lang.reflect.Method;
35 import java.lang.reflect.Modifier;
36
37 import java.util.Arrays;
38 import java.util.Collections;
39 import java.util.Collection;
40 import java.util.List;
41 import java.util.Map;
42 import java.util.HashMap;
43 import java.util.Iterator;
44
45 import alt.dev.szapp.SZComponent;
46 import alt.dev.szapp.SZContext;
47 import alt.dev.szapp.SZTemplate;
48 import alt.dev.szapp.SZHTMLString;
49 import alt.dev.szapp.SZHTMLCoder;
50 import alt.dev.szapp.SZURIEncoder;
51
52 import alt.dev.szfoundation.SZString;
53 import alt.dev.szfoundation.SZLog;
54
55 public class ClassMethodNavigation extends Component
56 {
57
58 //      ===========================================================================
59 //      Constant(s)
60 //      ---------------------------------------------------------------------------
61
62 //      ===========================================================================
63 //      Class variable(s)
64 //      ---------------------------------------------------------------------------
65
66 //      ===========================================================================
67 //      Instance variable(s)
68 //      ---------------------------------------------------------------------------
69
70         private Class           _value = null;
71         private Map             _valueProperties = null;
72         private Collection      _constructors = null;
73         private Collection      _classMethods = null;
74         private Collection      _instanceMethods = null;
75
76 //      ===========================================================================
77 //      Constructor method(s)
78 //      ---------------------------------------------------------------------------
79
80         protected ClassMethodNavigation(final SZContext aContext, final SZComponent aParent, final Map someBindings)
81         {
82                 super( aContext, aParent, someBindings );
83         }
84
85 //      ===========================================================================
86 //      Class method(s)
87 //      ---------------------------------------------------------------------------
88
89 //      ===========================================================================
90 //      Instance method(s)
91 //      ---------------------------------------------------------------------------
92
93         protected Class value()
94         {
95                 if ( _value == null )
96                 {
97                         _value = (Class) this.valueForBinding( "value" );
98                 }
99                
100                 return _value;
101         }
102        
103         protected Map valueProperties()
104         {
105                 if ( _valueProperties == null )
106                 {
107                         Map     someProperties = (Map) this.valueForBinding( "valueProperties" );
108                        
109                         _valueProperties = new HashMap();
110                        
111                         if ( someProperties != null )
112                         {
113                                 Map     anotherMap = null;
114                                
115                                 anotherMap = (Map) someProperties.get( "class.methods" );
116                                
117                                 if ( anotherMap != null )
118                                 {
119                                         _valueProperties.putAll( anotherMap );
120                                 }
121                                
122                                 anotherMap = (Map) someProperties.get( "instance.methods" );
123                                
124                                 if ( anotherMap != null )
125                                 {
126                                         _valueProperties.putAll( anotherMap );
127                                 }
128                                
129                                 anotherMap = (Map) someProperties.get( "constructors" );
130                                
131                                 if ( anotherMap != null )
132                                 {
133                                         _valueProperties.putAll( anotherMap );
134                                 }
135                         }
136                 }
137                
138                 return _valueProperties;
139         }
140
141         protected Collection constructors()
142         {
143                 if ( _constructors == null )
144                 {
145                         _constructors = ClassInfo.constructors( this.value() );
146                
147                         if ( _constructors == null )
148                         {
149                                 _constructors = Collections.EMPTY_LIST;
150                         }
151                 }
152                
153                 return _constructors;
154         }
155
156         protected Collection classMethods()
157         {
158                 if ( _classMethods == null )
159                 {
160                         _classMethods = ClassInfo.classMethods( this.value() );
161                
162                         if ( _classMethods == null )
163                         {
164                                 _classMethods = Collections.EMPTY_LIST;
165                         }
166                 }
167                
168                 return _classMethods;
169         }
170
171         protected Collection instanceMethods()
172         {
173                 if ( _instanceMethods == null )
174                 {
175                         _instanceMethods = ClassInfo.instanceMethods( this.value() );
176                
177                         if ( _instanceMethods == null )
178                         {
179                                 _instanceMethods = Collections.EMPTY_LIST;
180                         }
181                 }
182                
183                 return _instanceMethods;
184         }
185        
186         protected boolean isEmpty()
187         {
188                 if ( ( this.constructors().isEmpty() == true ) &&
189                         ( this.classMethods().isEmpty() == true ) &&
190                         ( this.instanceMethods().isEmpty() == true ) )
191                 {
192                         return true;
193                 }
194        
195                 return false;
196         }
197
198 //      ===========================================================================
199 //      Display method(s)
200 //      ---------------------------------------------------------------------------
201
202         protected String parameterName(final Class aClass, final String aMethodName, final int anIndex )
203         {
204                 String          aValue = null;
205                 List            someNames = (List) this.valueProperties().get( aMethodName + ".names" );
206                
207                 if ( ( someNames != null ) && ( anIndex < someNames.size() ) )
208                 {
209                         aValue = (String) someNames.get( anIndex );
210                 }
211                
212                 if ( aValue == null )
213                 {
214                         char    aChar = 0;
215                         int     aDotIndex = 0;
216                        
217                         aValue = ClassInfo.altNameForClass( aClass );
218                        
219                         aDotIndex = aValue.lastIndexOf( '.' );
220                        
221                         if ( aDotIndex > 0 )
222                         {
223                                 aValue = aValue.substring( aDotIndex + 1, aValue.length() );
224                         }
225                        
226                         aValue = SZString.capitalizeString( aValue );
227                        
228                         aChar = Character.toLowerCase( aValue.charAt( 0 ) );
229                        
230                         if ( ( aChar == 'a' ) ||
231                                 ( aChar == 'e' ) ||
232                                 ( aChar == 'i' ) ||
233                                 ( aChar == 'o' ) ||
234                                 ( aChar == 'u' ) ||
235                                 ( aChar == 'y' ) )
236                         {
237                                 aValue = "an" + aValue;
238                         }
239                         else
240                         {
241                                 aValue = "a" + aValue;
242                         }
243                        
244                         if ( aValue.endsWith( "[]" ) == true )
245                         {
246                                 aValue = SZString.trimSuffix( aValue, "[]" );
247                                 aValue = aValue + "Array";
248                         }
249                 }
250
251                 return aValue;
252         }
253
254         protected String classAnchor()
255         {
256                 return "#class";
257         }
258
259         protected String classValue()
260         {
261                 return ClassInfo.altNameForClass( this.value() );
262         }
263
264         protected String constructorAnchor(final Constructor aValue)
265         {
266                 StringBuffer    aBuffer = new StringBuffer();
267                 String          aName = ClassInfo.nameForConstructor( aValue );
268                
269                 aName = aName.replace( ':', '.' );
270                
271                 aBuffer.append( "#" );
272                 aBuffer.append( aName );
273                
274                 return aBuffer.toString();
275         }
276
277         protected String constructorValue(final Constructor aValue)
278         {
279                 StringBuffer    aBuffer = new StringBuffer();
280                 String          aName = ClassInfo.altNameForClass( aValue.getDeclaringClass() );
281                 Collection      someParameters = Arrays.asList( aValue.getParameterTypes() );
282                 String          aMethodName = ClassInfo.nameForConstructor( aValue );
283                 int             anIndex = 0;
284                
285                 aBuffer.append( aName );
286                 aBuffer.append( '(' );
287                
288                 if ( someParameters.isEmpty() == false )
289                 {
290                         aBuffer.append( ' ' );
291                 }
292                
293                 for ( Iterator anIterator = someParameters.iterator(); anIterator.hasNext(); )
294                 {
295                         Class   aParameter = (Class) anIterator.next();
296                         String  aParameterName = this.parameterName( aParameter, aMethodName, anIndex );
297                        
298                         aBuffer.append( aParameterName );
299
300                         if ( anIterator.hasNext() == true )
301                         {
302                                 aBuffer.append( ", " );
303                         }
304                        
305                         anIndex += 1;
306                 }
307                
308                 if ( someParameters.isEmpty() == false )
309                 {
310                         aBuffer.append( ' ' );
311                 }
312                
313                 aBuffer.append( ')' );
314
315                 return SZHTMLCoder.encode( aBuffer.toString() );
316         }
317
318         protected String methodAnchor(final Method aValue)
319         {
320                 StringBuffer    aBuffer = new StringBuffer();
321                 String          aName = ClassInfo.nameForMethod( aValue );
322                
323                 aName = aName.replace( ':', '.' );
324                
325                 aBuffer.append( "#" );
326                 aBuffer.append( aName );
327                
328                 return aBuffer.toString();
329         }
330
331         protected String methodValue(final Method aValue)
332         {
333                 StringBuffer    aBuffer = new StringBuffer();
334                 String          aName = aValue.getName();
335                 Collection      someParameters = Arrays.asList( aValue.getParameterTypes() );
336                 String          aMethodName = ClassInfo.nameForMethod( aValue );
337                 int             anIndex = 0;
338                 String          aString = null;
339                
340                 aBuffer.append( aName );
341                 aBuffer.append( '(' );
342                
343                 if ( someParameters.isEmpty() == false )
344                 {
345                         aBuffer.append( ' ' );
346                 }
347                
348                 for ( Iterator anIterator = someParameters.iterator(); anIterator.hasNext(); )
349                 {
350                         Class   aParameter = (Class) anIterator.next();
351                         String  aParameterName = this.parameterName( aParameter, aMethodName, anIndex );
352                        
353                         aBuffer.append( aParameterName );
354
355                         if ( anIterator.hasNext() == true )
356                         {
357                                 aBuffer.append( ", " );
358                         }
359                        
360                         anIndex += 1;
361                 }
362                
363                 if ( someParameters.isEmpty() == false )
364                 {
365                         aBuffer.append( ' ' );
366                 }
367                
368                 aBuffer.append( ')' );
369
370                 aString = SZHTMLCoder.encode( aBuffer.toString() );
371
372                 if ( Modifier.isAbstract( aValue.getModifiers() ) == true )
373                 {
374                         aString = "<i>" + aString + "</i>";
375                 }
376                
377                 return aString;
378         }
379
380         public String toString()
381         {
382                 if ( this.isEmpty() == false )
383                 {
384                         SZTemplate      aTemplate = this.template();
385                        
386                         aTemplate.set( "classAnchor", this.classAnchor() );
387                         aTemplate.set( "classValue", this.classValue() );
388
389                         if ( this.constructors().isEmpty() == true )
390                         {
391                                 aTemplate.set( "constructorsSeparator", null );
392                         }
393                         else
394                         {
395                                 aTemplate.set( "constructorsSeparator", "<option value = \"\"></option>" );
396                         }
397
398                         for ( Iterator anIterator = this.constructors().iterator(); anIterator.hasNext(); )
399                         {
400                                 Constructor     aValue = (Constructor) anIterator.next();
401                                 SZTemplate      aComponentTemplate = aTemplate.get( "constructors" );
402
403                                 aComponentTemplate.set( "constructorAnchor", this.constructorAnchor( aValue ) );
404                                 aComponentTemplate.set( "constructorValue", this.constructorValue( aValue ) );
405                                
406                                 aTemplate.append( "constructors", aComponentTemplate );
407                         }
408                        
409                         if ( this.classMethods().isEmpty() == true )
410                         {
411                                 aTemplate.set( "classMethodsSeparator", null );
412                         }
413                         else
414                         {
415                                 aTemplate.set( "classMethodsSeparator", "<option value = \"\"></option>" );
416                         }
417
418                         for ( Iterator anIterator = this.classMethods().iterator(); anIterator.hasNext(); )
419                         {
420                                 Method          aValue = (Method) anIterator.next();
421                                 SZTemplate      aComponentTemplate = aTemplate.get( "classMethods" );
422
423                                 aComponentTemplate.set( "classMethodAnchor", this.methodAnchor( aValue ) );
424                                 aComponentTemplate.set( "classMethodValue", this.methodValue( aValue ) );
425                                
426                                 aTemplate.append( "classMethods", aComponentTemplate );
427                         }
428
429                         if ( this.instanceMethods().isEmpty() == true )
430                         {
431                                 aTemplate.set( "instanceMethodsSeparator", null );
432                         }
433                         else
434                         {
435                                 aTemplate.set( "instanceMethodsSeparator", "<option value = \"\"></option>" );
436                         }
437
438                         for ( Iterator anIterator = this.instanceMethods().iterator(); anIterator.hasNext(); )
439                         {
440                                 Method          aValue = (Method) anIterator.next();
441                                 SZTemplate      aComponentTemplate = aTemplate.get( "instanceMethods" );
442
443                                 aComponentTemplate.set( "instanceMethodAnchor", this.methodAnchor( aValue ) );
444                                 aComponentTemplate.set( "instanceMethodValue", this.methodValue( aValue ) );
445                                
446                                 aTemplate.append( "instanceMethods", aComponentTemplate );
447                         }
448
449                         return aTemplate.toString();
450                 }
451                
452                 return null;
453         }
454
455 }
Note: See TracBrowser for help on using the browser.