Statistics
| Revision:

gvsig-scripting / trunk / org.gvsig.scripting / org.gvsig.scripting.lib / org.gvsig.scripting.lib.impl / src / main / java / org / gvsig / scripting / impl / DefaultScriptingHelpManager.java @ 181

History | View | Annotate | Download (16.1 KB)

1
package org.gvsig.scripting.impl;
2

    
3
import java.io.BufferedReader;
4
import java.io.BufferedWriter;
5
import java.io.DataInputStream;
6
import java.io.File;
7
import java.io.FileInputStream;
8
import java.io.FileNotFoundException;
9
import java.io.FileReader;
10
import java.io.FileWriter;
11
import java.io.IOException;
12
import java.io.InputStreamReader;
13
import java.lang.reflect.Field;
14
import java.net.MalformedURLException;
15
import java.net.URL;
16
import java.net.URLClassLoader;
17
import java.util.ArrayList;
18
import java.util.Arrays;
19
import java.util.Hashtable;
20
import java.util.Iterator;
21
import java.util.List;
22
import java.util.Map;
23

    
24
import javax.help.HelpSet;
25
import javax.help.HelpSetException;
26

    
27
import org.gvsig.scripting.ScriptingHelpManager;
28
import org.gvsig.scripting.ScriptingManager;
29

    
30
import com.sun.java.help.search.Indexer;
31

    
32

    
33
public class DefaultScriptingHelpManager implements ScriptingHelpManager {
34
        
35
        //private ScriptingManager manager;
36

    
37
        protected HelpSet helpSet;
38
        protected static Indexer indexer;
39
        protected MethodList methodsList;
40
        protected String helpFolder;
41
        
42
        public DefaultScriptingHelpManager(ScriptingManager manager){
43
                //this.manager=manager;
44
                this.methodsList = new MethodList();
45
                File f = new File(System.getProperty("user.home")+File.separator+"gvSIG"+File.separator+"help");
46
                if(!f.exists()){
47
                        f.mkdir();
48
                }
49
                this.helpFolder = f.getPath();
50
                this.initializeHelpSet();
51
                
52
        }
53
        
54
        private void initializeHelpSet() {
55
                HelpSet helpSet = null;
56
                File f = this.getFolder();
57
                if(!f.exists()){
58
                        f.mkdir(); 
59
                }
60
                
61
                ClassLoader loader = this.getClass().getClassLoader();
62
                URL resource = loader.getResource("org/gvsig/scripting/apihelp/api.hs");
63
                URL resource2 = loader.getResource("org/gvsig/scripting/apihelp/config.txt");
64
                        
65
                if(resource2 != null){
66
                        String[] args=new String[] {"-db",f.getPath()+File.separator+"ScriptingFramework Help"+File.separator+"JavaHelpSearch"};
67
                        
68
                        ClearStaticFieldsInJavaHelpIndexer();
69
                        try {
70
                                this.getIndexer().compile(args);
71
                        } catch (Exception e) {
72
                                // TODO Auto-generated catch block
73
                                e.printStackTrace();
74
                        }
75
                }
76

    
77
        if( resource == null ) {
78
             System.out.println("No he podido recuperar el recurso pedido.");
79
        }
80
        else{
81
                        try {
82
                                helpSet = new HelpSet(loader, resource);
83
                        } catch (HelpSetException e) {
84
                                e.printStackTrace();
85
                        }
86
        }
87
        
88
                this.helpSet = helpSet;
89

    
90
                
91
                loadHelps();
92
        }
93

    
94
        public void loadHelps(){
95
                // Recorrer directorio manager.getHelpFolder
96
                File f = this.getFolder();
97
                String[] helps = f.list();
98

    
99
                
100
                for(int i=0;i<helps.length;i++){
101
                        URL helpUrl = getHelpUrl(helps[i]);
102
                        if (helpUrl!=null){
103
                                this.getHelpSet().add(this.loadHelp(helps[i], helpUrl));
104
                                URL indexUrl=null;
105
                                try {
106
                                        indexUrl = (new File(this.getFolder().getPath() + File.separator + helps[i] + File.separator + "IdeHelpIndex.xml")).toURL();
107
                                } catch (MalformedURLException e) {
108
                                        // TODO Auto-generated catch block
109
                                        e.printStackTrace();
110
                                }
111
                                if(indexUrl != null){
112
                                        this.methodsList.addMethods(indexUrl);
113
                                }
114
                        }
115
                }
116
                
117
        }
118

    
119
        /** 
120
         *  Repeated invokation of the javahelp indexer (possibly via multiple classloaders)
121
     *  is causing trouble, residue from previous invokations seems to cause errors
122
     *  this is a nasty workaround for the problem.
123
     *  alternatively we could try invoking the indexer from a separate jvm i guess,
124
     *  ut that's more work.
125
     **/
126

    
127
        public void ClearStaticFieldsInJavaHelpIndexer(){
128
                if (indexer==null){
129
                        return ;
130
                }
131
                try{
132
                        Class clazz = Class.forName("com.sun.java.help.search.Indexer" );
133
                        Field fld = clazz.getDeclaredField( "kitRegistry" );
134
                        fld.setAccessible( true );
135
                        Hashtable hash = (Hashtable) fld.get( null );
136
                        hash.clear();
137

    
138
                        clazz = Class.forName("com.sun.java.help.search.HTMLIndexerKit" );
139
                        fld = clazz.getDeclaredField( "defaultParser" );
140
                        fld.setAccessible( true );
141
                        fld.set( null, null);
142

    
143
                        fld = clazz.getDeclaredField( "defaultCallback" );
144
                        fld.setAccessible( true );
145
                        fld.set( null, null);
146

    
147
                }catch ( IllegalArgumentException ex ){
148
                        // TODO Auto-generated catch block
149
                        ex.printStackTrace();                        
150
                }catch ( IllegalAccessException ex ){
151
                        // TODO Auto-generated catch block
152
                        ex.printStackTrace();
153
                }catch ( NoSuchFieldException ex ){
154
                        // TODO Auto-generated catch block
155
                        ex.printStackTrace();
156
                }catch ( SecurityException ex ){
157
                        // TODO Auto-generated catch block
158
                        ex.printStackTrace();
159
                }catch ( ClassNotFoundException ex ){
160
                        // TODO Auto-generated catch block
161
                        ex.printStackTrace();
162
                }
163
        }
164
        
165
        public void reloadHelp(){
166
                this.helpSet=null;
167
                this.initializeHelpSet();
168
        }
169
        
170
        private URL getHelpUrl(String name){
171
                File f= new File(this.getFolder()+File.separator+name+File.separator+".config.file");
172
                
173
                if(f.exists()&&f.canRead()){
174
                    FileReader fr;
175
                    try {
176
                      fr = new FileReader(f);
177
                      BufferedReader br = new BufferedReader (fr);
178
                      String line = br.readLine();      
179
                      br.close();
180
                      return new URL(line);
181
                    }
182
                    catch (Exception e) {
183
                      e.printStackTrace();
184
                    }
185
                }
186
                return null;
187
        }
188

    
189

    
190
        public boolean existsHelp(String name) {
191
                return new File(this.getFolder()+File.separator+name).exists();
192
        }
193

    
194
        public void removeHelp(String name) {
195
                File f = new File(this.getFolder()+File.separator+name);
196
                if(f.canWrite()){
197
                        String[] subfiles = f.list(); 
198
                        if(f.isDirectory() && subfiles.length>0){
199
                                for(int i=0;i<subfiles.length;i++){
200
                                        removeHelp(name+File.separator+subfiles[i]);                                
201
                                }
202
                        }
203
                        f.delete();
204
                }
205
        }
206

    
207
        private Indexer getIndexer() {
208
                if(indexer==null){
209
                        indexer = new Indexer();
210
                }
211
                return indexer;
212
        }
213

    
214
        
215
        public List<ScriptingHelpMethod> getMethods() {
216
                return this.methodsList;
217
        }
218

    
219
        public Map<String,ScriptingHelpMethod> findMethods(String text) {
220
                return this.methodsList.findMethods(text);
221
        }
222

    
223
        
224
        public File getFolder() {
225
                return new File(this.helpFolder);
226
        }
227
        
228
        public void setFolder(String newFolder) {
229
                File f = new File(newFolder);
230
                if(!f.exists()){
231
                        f.mkdir();
232
                }
233
                this.helpFolder = newFolder;
234
        }
235
        
236
        public HelpSet getHelpSet(){
237
                if(this.helpSet == null) {
238
                        this.initializeHelpSet();
239
                }
240
                return this.helpSet;
241
        }
242
        
243
        
244
        
245
        private ClassLoader getLoader(URL url){
246
                URL[] urls = new URL[2];
247
                urls[0] = url;
248
                try {
249
                        urls[1]= this.getFolder().toURL();
250
                } catch (MalformedURLException e2){
251
                        e2.printStackTrace();
252
                }
253
        ClassLoader loader = new URLClassLoader(urls);
254
                return loader;
255
        }
256

    
257
        public HelpSet loadHelp(String name, URL url){
258
                ClassLoader loader = getLoader(url);
259
                URL packageUrl = loader.getResource(name + File.separator + "api.hs");
260
                
261
                
262
                if( packageUrl == null ) {
263
                        System.out.println("No he podido recuperar el recurso pedido.");
264
                } 
265
                HelpSet hst = null;
266
                try {
267
                        hst = new HelpSet(loader, packageUrl);
268
                } catch (HelpSetException e) {
269
                        // TODO Auto-generated catch block
270
                        e.printStackTrace();
271
                }
272

    
273
                return hst;
274
        }
275
        
276
        
277
        /**
278
         * Recorre las carpetas de {link #org.gvsig.scripting.ScriptingManager.getHelpFolder} y
279
         * va cargando en main las ayudas que allí se encuentren.
280
         * 
281
         * @param main
282
         */
283

    
284
                
285
        public void importHelp(String name, URL url) {
286

    
287
                if(!existsHelp(name)){
288
                        HelpSet importedHelpSet = null;
289

    
290
                        ClassLoader loader = getLoader(url);
291

    
292
                        String title = name;
293

    
294
                        File target = new File(this.getFolder().getPath()+File.separator+name); 
295
                        target.mkdir();
296

    
297
                        File toc = null;
298
                        File api = null;
299
                        File hs = null;
300
                        File config = null;
301
                        File indexerConfig = null;
302

    
303
                        File index = null;
304
                        MethodList methodsIndex = new MethodList();
305

    
306
                        URL packageListUrl = loader.getResource("package-list");
307

    
308
                        toc = new File(target.getPath()+ File.separator + "toc.xml");
309
                        api = new File(target.getPath()+ File.separator + "api.jhm");
310
                        hs = new File(target.getPath()+ File.separator + "api.hs");
311
                        config = new File(target.getPath()+ File.separator + ".config.file");
312
                        indexerConfig = new File(target.getPath()+ File.separator + "config.txt");
313
                        index = new File(target.getPath()+ File.separator + "IdeHelpIndex.xml");
314

    
315
                        try {
316
                                toc.createNewFile();
317
                                api.createNewFile();
318
                                hs.createNewFile();
319
                                config.createNewFile();
320
                                indexerConfig.createNewFile();
321
                                index.createNewFile();
322
                        } catch (IOException e) {
323
                                // TODO Auto-generated catch block
324
                                e.printStackTrace();
325
                        }
326

    
327
                        //Leemos el fichero
328
                        FileInputStream fstream;
329
                        DataInputStream in=null;
330
                        BufferedReader br = null;
331
                        try {
332
                                fstream = new FileInputStream(packageListUrl.getFile());
333
                                in = new DataInputStream(fstream);
334
                                br = new BufferedReader(new InputStreamReader(in));
335
                        } catch (FileNotFoundException e1) {
336
                                // TODO Auto-generated catch block
337
                                e1.printStackTrace();
338
                        }
339

    
340
                        //Creamos los ficheros 'toc.xml' y 'api.jhm'
341
                        FileWriter fstreamToc;
342
                        BufferedWriter outToc;
343
                        FileWriter fstreamApi;
344
                        BufferedWriter outApi;
345
                        FileWriter fstreamHs;
346
                        BufferedWriter outHs;
347
                        FileWriter fstreamConfig;
348
                        BufferedWriter outConfig;
349
                        FileWriter fstreamIndexerConfig;
350
                        BufferedWriter outIndexerConfig;
351
                        FileWriter fstreamIndex;
352
                        BufferedWriter outIndex;
353

    
354
                        //Leemos línea a línea el fichero 'package-list'
355
                        String strLine;
356
                        File lineFile;
357

    
358
                        try {
359
                                fstreamToc = new FileWriter(toc);
360
                                outToc = new BufferedWriter(fstreamToc);
361
                                fstreamApi = new FileWriter(api);
362
                                outApi = new BufferedWriter(fstreamApi);
363
                                fstreamHs = new FileWriter(hs);
364
                                outHs = new BufferedWriter(fstreamHs);
365
                                fstreamConfig = new FileWriter(config);
366
                                outConfig = new BufferedWriter(fstreamConfig);
367
                                fstreamIndexerConfig = new FileWriter(indexerConfig);
368
                                outIndexerConfig = new BufferedWriter(fstreamIndexerConfig);
369
                                fstreamIndex = new FileWriter(index);
370
                                outIndex = new BufferedWriter(fstreamIndex);
371
                                outToc.write("<!DOCTYPE toc\n" +
372
                                                "  PUBLIC \"-//Sun Microsystems Inc.//DTD JavaHelp TOC Version 1.0//EN\"\n    " +
373
                                                "         \"http://java.sun.com/products/javahelp/toc_1_0.dtd\">\n\n" +
374
                                "<toc version=\"1.0\">\n");
375
                                outApi.write("<?xml version='1.0' encoding='ISO-8859-1' ?>\n" +
376
                                                "<!DOCTYPE map\n" +
377
                                                "  PUBLIC \"-//Sun Microsystems Inc.//DTD JavaHelp Map Version 1.0//EN\"\n" +
378
                                                "         \"http://java.sun.com/products/javahelp/map_1_0.dtd\">\n\n" +
379
                                "<map version=\"1.0\">\n");
380
                                outHs.write("<?xml version='1.0' encoding='ISO-8859-1' ?>\n" +
381
                                                "<!DOCTYPE helpset\n" +
382
                                                "  PUBLIC \"-//Sun Microsystems Inc.//DTD JavaHelp HelpSet Version 1.0//EN\"\n" +
383
                                                "                   \"http://java.sun.com/products/javahelp/helpset_1_0.dtd\">\n\n" +
384
                                                "<?TestTarget this is data for the test target ?>\n\n" +
385
                                                "<helpset version=\"1.0\">\n\n" +
386
                                                "  <!-- title -->\n" +
387
                                                "  <title>"+ title +"</title>\n\n" +
388
                                                "  <!-- maps -->\n" +
389
                                                "  <maps>\n"+
390
                                                "     <homeID>main</homeID>\n" +
391
                                                "     <mapref location=\"api.jhm\"/>\n" +
392
                                                "  </maps>\n\n" +
393
                                                "  <!-- views -->\n" +
394
                                                "  <view>\n" +
395
                                                "    <name>TOC</name>\n" +
396
                                                "    <label>TOC</label>\n" +
397
                                                "    <type>javax.help.TOCView</type>\n" +
398
                                                "    <data>toc.xml</data>\n" +
399
                                                "  </view>\n\n" +
400
                                                "  <view>\n" +
401
                                                "    <name>Index</name>\n" +
402
                                                "    <label>Index</label>\n" +
403
                                                "    <type>javax.help.IndexView</type>\n" +
404
                                                "    <data>IdeHelpIndex.xml</data>\n" +
405
                                                "  </view>\n" +
406
                                                "  <view>\n" +
407
                                                "    <name>Search</name>\n" +
408
                                                "    <label>Search</label>\n" +
409
                                                "    <type>javax.help.SearchView</type>\n" +
410
                                                "    <data engine=\"com.sun.java.help.search.DefaultSearchEngine\">JavaHelpSearch</data>\n" +
411
                                                "  </view>\n\n" +
412
                                                "  <view>\n" +
413
                                                "    <name>Favorites</name>\n" +
414
                                                "    <label>Favoritos</label>\n" +
415
                                                "    <type>javax.help.FavoritesView</type>\n" +
416
                                                "  </view>\n\n" +
417
                                                "  <presentation default=\"true\" displayviewimages=\"false\">\n" +
418
                                                "     <name>main window</name>\n" +
419
                                                "     <size width=\"700\" height=\"400\" />\n" +
420
                                                "     <location x=\"200\" y=\"200\" />\n" +
421
                                                "     <title>gvSIG - Ayuda en linea</title>\n" +
422
                                                "     <image>toplevelfolder</image>\n" +
423
                                                "     <toolbar>\n" +
424
                                                "       <helpaction image=\"action.back\">javax.help.BackAction</helpaction>\n" +
425
                                                "       <helpaction image=\"action.forward\">javax.help.ForwardAction</helpaction>\n" +
426
                                                "     </toolbar>\n" +
427
                                                "  </presentation>\n" +
428
                                "</helpset>");
429
                                outConfig.write(url.toString());
430
                                outIndex.write("<?xml version='1.0' encoding='ISO−8859−1' ?>\n"+
431
                                                "<!DOCTYPE index \n\tPUBLIC \"-//Sun Microsystems Inc.//DTD JavaHelp Index Version 1.0//EN\"\n"+
432
                                                "\t\t\"http://java.sun.com/javase/technologies/desktop/javahelp/index_1_0.dtd\">\n"+
433
                                "\n<index version=\"1.0\">\n");
434

    
435
                                strLine = br.readLine();
436
                                while (br!=null && strLine != null)   {
437
                                        outToc.write("\t<tocitem text=\""+strLine+"\">\n");
438

    
439
                                        String strLineDir = strLine.replace(".", File.separator);
440
                                        lineFile = new File(url.getFile()+strLineDir);
441

    
442
                                        if (lineFile.isDirectory() && lineFile.canRead()){
443

    
444
                                                String[] fileNames = lineFile.list();
445
                                                Arrays.sort(fileNames, String.CASE_INSENSITIVE_ORDER);
446
                                                for (int i=0; i<fileNames.length; i++){
447
                                                        if (fileNames[i].toLowerCase().endsWith(".html")){                                                                
448
                                                                outToc.write("\t\t<tocitem text=\""+ fileNames[i].substring(0,fileNames[i].length()-5) +"\" target=\""+ strLine +"."+ fileNames[i] +"\"/>\n");
449
                                                                outApi.write("\t\t<mapID target=\""+ strLine +"."+ fileNames[i] +"\" url=\""+  loader.getResource(strLineDir + File.separator+ fileNames[i]).getFile()+"\" />\n");
450
                                                                outIndexerConfig.write("File "+ loader.getResource(strLineDir + File.separator+ fileNames[i]).getFile() +"\n");
451
                                                                methodsIndex = getMethodsIndex(methodsIndex, fileNames[i], strLine +"."+ fileNames[i], loader.getResource(strLineDir + File.separator+ fileNames[i]).getFile());
452
                                                        }
453
                                                }
454
                                        }
455
                                        outToc.write("\t</tocitem>\n");
456
                                        strLine = br.readLine();
457
                                }
458

    
459
                                Iterator it = methodsIndex.iterator();
460
                                while(it.hasNext()){
461
                                        ScriptingHelpMethod mn = (ScriptingHelpMethod)it.next();
462
                                        outIndex.write("\t<indexitem text=\""+mn.getName()+"\" expand=\"false\">\n");
463

    
464
                                        Iterator<ScriptingHelpClass> itcn = mn.iterator();
465
                                        while(itcn.hasNext()){
466
                                                ScriptingHelpClass cn = itcn.next();
467
                                                outIndex.write("\t\t<indexitem text=\""+cn.getName()+"\" target=\""+cn.getUrl()+"\" />\n");                                        
468
                                        }
469
                                        outIndex.write("\t</indexitem>\n");
470

    
471
                                }
472

    
473

    
474
                                outToc.write("</toc>\n");
475
                                outApi.write("</map>\n");
476
                                outIndex.write("</index>\n");
477
                                //Close the input stream
478
                                if(in!=null)
479
                                        in.close();
480
                                //Close the output stream
481
                                outToc.close();
482
                                outApi.close();
483
                                outHs.close();
484
                                outConfig.close();
485
                                outIndexerConfig.close();
486
                                outIndex.close();
487

    
488

    
489
                                String[] args=new String[] {"-c",indexerConfig.getPath(),"-db",target.getPath()+File.separator+"JavaHelpSearch"};
490

    
491

    
492
                                ClearStaticFieldsInJavaHelpIndexer();
493
                                try {
494
                                        getIndexer().compile(args);
495
                                } catch (Exception e) {
496
                                        // TODO Auto-generated catch block
497
                                        e.printStackTrace();
498
                                }
499

    
500
                        } catch (IOException e1) {
501
                                // TODO Auto-generated catch block
502
                                e1.printStackTrace();
503
                        }
504

    
505
                        importedHelpSet= loadHelp(name,url);
506
                        if(importedHelpSet!=null){
507
                                this.getHelpSet().add(importedHelpSet);
508
                        }
509
                }
510
        }
511
        
512
        private MethodList getMethodsIndex(MethodList methodsIndex, String name, String target, String filePath){
513
                File f = new File(filePath);
514
                if(f.exists()){
515
                        List<String> methods = getClassMethods(filePath);
516
                        methodsIndex.add( name, target, methods);
517
                }
518
                return methodsIndex;
519
        }
520
        
521
        
522
        private List<String> getClassMethods(String filePath){
523
                
524
                String fileCode = null;
525
                
526
                List<String> l = new ArrayList<String>();
527
        
528
                fileCode = DefaultScriptingManager.getStringFromFile(filePath);
529
                String s[] = fileCode.split("<H3>");
530
                if(s.length>1){
531
                        for(int i=0;i<s.length;i++){
532
                                String x = s[i];
533
                                String[] aux = x.split("</H3>");
534
                                if(aux.length>1){
535
                                        l.add(aux[0].trim());
536
                                }
537
                        }
538
                }
539
                return l;
540
                
541
        }
542

    
543
        public List<ScriptingHelpAPI> getAPI() {
544
                String[] files = this.getFolder().list();
545
                List<ScriptingHelpAPI> apis = new ArrayList<ScriptingHelpAPI>();
546
                
547
                for(int i=0;i<files.length;i++){
548
                        apis.add(new DefaultScriptingHelpAPI(files[i]));
549
                }
550
                return apis;
551
        }
552

    
553
        private static class DefaultScriptingHelpAPI implements ScriptingHelpAPI{
554

    
555
                private String name;
556
                
557
                DefaultScriptingHelpAPI(String name){
558
                        this.name = name;
559
                }
560
                public String getName() {
561
                        return this.name;
562
                }
563
                
564
        }
565

    
566

    
567
}