root/dydoc/Dydoc.java

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

Initial import

Line 
1 //
2 //      ===========================================================================
3 //
4 //      Title:          Dydoc.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.io.File;
34
35 import java.net.URI;
36
37 import java.util.Collections;
38 import java.util.Collection;
39 import java.util.List;
40 import java.util.ArrayList;
41 import java.util.Map;
42 import java.util.HashMap;
43 import java.util.HashSet;
44 import java.util.TreeSet;
45 import java.util.Iterator;
46
47 import alt.dev.pl.PL;
48
49 import alt.dev.szfoundation.SZClassLoader;
50 import alt.dev.szfoundation.SZLog;
51
52 public class Dydoc extends Object implements Runnable
53 {
54
55 //      ===========================================================================
56 //      Constant(s)
57 //      ---------------------------------------------------------------------------
58
59 //      ===========================================================================
60 //      Class variable(s)
61 //      ---------------------------------------------------------------------------
62
63 //      ===========================================================================
64 //      Instance variable(s)
65 //      ---------------------------------------------------------------------------
66
67         private URI             _uri = null;
68         private transient PL    _properties = null;
69
70 //      ===========================================================================
71 //      Constructor method(s)
72 //      ---------------------------------------------------------------------------
73
74         public Dydoc(final URI anURI)
75         {
76                 super();
77                
78                 this.setURI( anURI );
79         }
80
81 //      ===========================================================================
82 //      Class method(s)
83 //      ---------------------------------------------------------------------------
84
85         public static void main(final String[] someArguments) throws Throwable
86         {
87                 try
88                 {
89                         Dydoc   aTool = null;
90                         URI     anURI = null;
91                        
92                         SZClassLoader.setup();
93                        
94                         System.setProperty( "platform.file.encoding", System.getProperty( "file.encoding" ) );
95                         System.setProperty( "file.encoding", "UTF-8" );
96                         System.setProperty( "java.awt.headless", Boolean.TRUE.toString() );
97                        
98                         if ( ( someArguments != null ) && ( someArguments.length > 0 ) )
99                         {
100                                 try
101                                 {
102                                         anURI = new URI( someArguments[ 0 ] );
103                                 }
104                                 catch (Exception anException)
105                                 {
106                                         SZLog.warning( anException );
107                                 }
108                         }
109                        
110                         aTool = new Dydoc( anURI );
111                         aTool.run();
112
113                         System.exit( 0 );
114                 }
115                 catch (Throwable anException)
116                 {
117                         SZLog.error( anException );
118                 }
119
120                 System.exit( 1 );
121         }
122
123 //      ===========================================================================
124 //      Instance method(s)
125 //      ---------------------------------------------------------------------------
126
127         public URI uri()
128         {
129                 return _uri;
130         }
131        
132         private void setURI(final URI aValue)
133         {
134                 _uri = aValue;
135         }
136        
137 //      ===========================================================================
138 //      Properties method(s)
139 //      ---------------------------------------------------------------------------
140
141         public PL properties()
142         {
143                 if ( _properties == null )
144                 {
145                         URI     anURI = this.uri();
146                        
147                         _properties = new PL();
148                        
149                         if ( ( anURI == null ) || ( anURI.getPath().endsWith( ".plist" ) == false ) )
150                         {
151                                 File    aFile = new File( "dydoc.plist" );
152                                
153                                 if ( aFile.exists() == true )
154                                 {
155                                         anURI = aFile.toURI();
156                                 }
157                         }
158                        
159                         if ( ( anURI != null ) && ( anURI.getPath().endsWith( ".plist" ) == true ) )
160                         {
161                                 SZLog.info( "Loading properties from " + anURI );
162                        
163                                 try
164                                 {
165                                         _properties.load( anURI );
166                                 }
167                                 catch (Exception anException)
168                                 {
169                                         SZLog.warning( anException );
170                                 }
171                         }
172                 }
173                
174                 return _properties;
175         }
176        
177         public Collection jarURIs()
178         {
179                 Collection      aCollection = new ArrayList();
180                 Collection      someURINames = this.properties().getCollection( "jar.dirs", null );
181                
182                 if ( ( this.uri() != null ) && ( this.uri().getPath().endsWith( ".jar" ) == true ) )
183                 {
184                         aCollection.add( this.uri() );
185                 }
186                 else
187                 if ( someURINames != null )
188                 {
189                         for ( Iterator anIterator = someURINames.iterator(); anIterator.hasNext(); )
190                         {
191                                 String  aName = (String) anIterator.next();
192                                
193                                 try
194                                 {
195                                         URI     anURI = new URI( aName );
196                                        
197                                         aCollection.add( anURI );
198                                 }
199                                 catch (Exception anException)
200                                 {
201                                         SZLog.warning( anException );
202                                 }
203                         }
204                 }
205                
206                 if ( aCollection.isEmpty() == true )
207                 {
208                         aCollection = null;
209                 }
210                
211                 return aCollection;
212         }
213
214         public boolean includeDependencies()
215         {
216                 return this.properties().getBoolean( "dependencies", false );
217         }
218
219 //      ===========================================================================
220 //      Convenience method(s)
221 //      ---------------------------------------------------------------------------
222
223         public Collection classesWithURI(final URI anURI, final boolean includeDependencies)
224         {
225                 if ( anURI != null )
226                 {
227                         Collection      aCollection = new TreeSet( ClassInfo.ClassComparator.comparator() );
228                        
229                         SZLog.info( "Loading classes from " + anURI );
230
231                         for ( Iterator anIterator = new JarIterator( anURI ); anIterator.hasNext(); )
232                         {
233                                 Class   aClass = (Class) anIterator.next();
234                                
235                                 if ( includeDependencies == true )
236                                 {
237                                         ClassInfo.classDependencies( aClass, aCollection );
238                                 }
239                                 else
240                                 if ( ClassInfo.accept( aClass, aClass.getModifiers() ) == true )
241                                 {
242                                         aCollection.add( aClass );
243                                 }
244                         }
245                        
246                         if ( aCollection.isEmpty() == true )
247                         {
248                                 aCollection = null;
249                         }
250                        
251                         return aCollection;
252                 }
253                
254                 return null;
255         }
256
257         public Collection classes()
258         {
259                 Collection      someJarURIs = this.jarURIs();
260                
261                 if ( someJarURIs != null )
262                 {
263                         Collection      aCollection = new TreeSet( ClassInfo.ClassComparator.comparator() );
264                         boolean         includeDependencies = this.includeDependencies();
265                        
266                         for ( Iterator anIterator = someJarURIs.iterator(); anIterator.hasNext(); )
267                         {
268                                 URI             anURI = (URI) anIterator.next();
269                                 Collection      someClasses = this.classesWithURI( anURI, includeDependencies );
270                                
271                                 if ( someClasses != null )
272                                 {
273                                         aCollection.addAll( someClasses );
274                                 }
275                         }
276                        
277                         if ( aCollection.isEmpty() == true )
278                         {
279                                 aCollection = null;
280                         }
281                        
282                         return aCollection;
283                 }
284                
285                 return null;
286         }
287        
288         public Map packages()
289         {
290                 return ClassInfo.packages( this.classes() );
291         }
292
293 //      ===========================================================================
294 //      Runnable method(s)
295 //      ---------------------------------------------------------------------------
296
297         public void run()
298         {
299                 Collection      someClasses = this.classes();
300                
301                 if ( someClasses != null )
302                 {
303                         PL              someProperties = this.properties();
304                         ClassStub       aClassStub = new ClassStub( someProperties );
305                         Map             somePackages = this.packages();
306                        
307                         if ( somePackages != null )
308                         {
309                                 Map             someBindings = new HashMap();
310                                                 someBindings.put( "values", somePackages.keySet() );
311                                                 someBindings.put( "docProperties", someProperties );
312                                 DocDoc          aDocDoc = new DocDoc( null, null, someBindings );
313                                 DocStub         aDocStub = new DocStub( someProperties );
314                                 PackageStub     aPackageStub = new PackageStub( someProperties );
315                                
316                                 SZLog.info( "Generating dydoc index..." );
317
318                                 aDocStub.stub( null );
319                                 aDocDoc.store();
320
321                                 SZLog.info( "Generating dydoc library indices..." );
322
323                                 for ( Iterator anIterator = somePackages.keySet().iterator(); anIterator.hasNext(); )
324                                 {
325                                         Package         aPackage = (Package) anIterator.next();
326                                                         aPackageStub.stub( aPackage );
327                                                         someBindings = new HashMap();
328                                                         someBindings.put( "value", aPackage );
329                                                         someBindings.put( "values", somePackages.get( aPackage ) );
330                                                         someBindings.put( "docProperties", someProperties );
331                                         PackageDoc      aPackageDoc = new PackageDoc( null, null, someBindings );
332                                        
333                                         aPackageDoc.store();
334                                 }
335                         }
336
337                         SZLog.info( "Generating dydoc classes..." );
338
339                         for ( Iterator anIterator = someClasses.iterator(); anIterator.hasNext(); )
340                         {
341                                 Class           aClass = (Class) anIterator.next();
342                                                 aClassStub.stub( aClass );
343                                 Map             someBindings = new HashMap();
344                                                 someBindings.put( "value", aClass );
345                                                 someBindings.put( "values", someClasses );
346                                                 someBindings.put( "docProperties", someProperties );
347                                 ClassDoc        aClassDoc = new ClassDoc( null, null, someBindings );
348                                
349                                 aClassDoc.store();
350                         }
351
352                         SZLog.info( "Done" );
353                 }
354         }
355
356 }
Note: See TracBrowser for help on using the browser.