Switch to unified view

a/tool/run/plugins/spdx/show.java b/tool/run/plugins/spdx/show.java
...
...
14
 */
14
 */
15
15
16
package spdx;
16
package spdx;
17
17
18
import GUI.swingUtils;
18
import GUI.swingUtils;
19
import definitions.Messages;
19
import definitions.is;
20
import definitions.is;
20
import java.io.File;
21
import java.io.File;
21
import www.Table;
22
import www.Table;
22
import java.util.ArrayList;
23
import java.util.ArrayList;
23
import script.Plugin;
24
import script.Plugin;
...
...
37
 *  nuno.brito@triplecheck.de | http://nunobrito.eu
38
 *  nuno.brito@triplecheck.de | http://nunobrito.eu
38
 */
39
 */
39
public class show extends Plugin{
40
public class show extends Plugin{
40
   
41
   
41
    
42
    
43
    String showSPDX = "showSPDX";
44
    
45
    @Override
46
    public void startup(){
47
        // add our node to the tree right after the "Tools" node is added
48
        log.hooks.addAction(Messages.RefreshSPDX, thisFile, "refreshCache");
49
    }
50
    
51
    /**
52
     * Cache our front page to speed up loading time when clicked by end user
53
     */
54
    void refreshCache(){
55
        log.write(is.ACCEPTED, "Re-doing our cache again");
56
    }
42
    
57
    
43
    /**
58
    /**
44
     * Shows a summary of details about the selected package
59
     * Shows a summary of details about the selected package
45
     * @param request
60
     * @param request
46
     */
61
     */
...
...
51
                counterCreators = 0,
66
                counterCreators = 0,
52
                counterLicensesDeclared = 0
67
                counterLicensesDeclared = 0
53
                ;
68
                ;
54
        
69
        
55
        
70
        
56
        
57
        // get some statistical data
71
        // get some statistical data
58
        for(Object object : core.products){
72
        for(Object object : core.products){
59
            SPDXfile spdx = (SPDXfile) object;
73
            SPDXfile spdx = (SPDXfile) object;
60
            counterFiles += spdx.fileSection.files.size();
74
            counterFiles += spdx.fileSection.files.size();
61
            counterCreators += spdx.creatorSection.people.size();
75
            counterCreators += spdx.creatorSection.people.size();
...
...
71
        
85
        
72
        String result = 
86
        String result = 
73
                html.div()
87
                html.div()
74
                + "<h2>"
88
                + "<h2>"
75
                //+ html.getCommonFolderIcon("wooden-box-label.png")
89
                //+ html.getCommonFolderIcon("wooden-box-label.png")
76
                + "SPDX archive"
90
                + "SPDX documents"
77
                + "</h2>"
91
                + "</h2>"
78
                + html._div
92
                + html._div
79
                + html.div(20)
93
                + html.div(20)
80
                //+ html.getCommonFolderIcon("documents-stack.png")
94
                //+ html.getCommonFolderIcon("documents-stack.png")
95
                + core.products.size() + " SPDX documents containing "
81
                + counterFiles + " files in total" 
96
                + counterFiles + " files in total" 
82
                + html.br
97
                + html.br
83
                //+ html.getCommonFolderIcon("calculator.png")
98
                //+ html.getCommonFolderIcon("calculator.png")
84
                + counterLicensesDeclared + " files with declared licenses" 
99
                + counterLicensesDeclared + " files with declared licenses" 
85
                + percentage
100
                + percentage
86
                //+ html.getCommonFolderIcon("stickman.png")
101
                //+ html.getCommonFolderIcon("stickman.png")
87
                + counterCreators + " SPDX creators" + html.br
102
                + counterCreators + " SPDX creators" + html.br
103
                + html.br
104
                + listFilesSPDX(request)
88
                //+ html.br
105
                //+ html.br
89
                + html._div
106
                + html._div
90
                + html.br
107
                + html.br
91
                + "";
108
                + "";
92
        
109
        
93
        // provide an answer
110
        // provide an answer
94
        request.setAnswer(result);
111
        request.setAnswer(result);
95
    }
112
    }
113
 
114
    /**
115
     * List all the files that are on the disk
116
     */
117
    private String listFilesSPDX(WebRequest request){
118
        
119
//        String[] line = new String[]{"SPDX", "Last modified"};
120
        
121
//        Table table = new Table(line);
122
        
123
        String result = "";
124
//        for(SPDXfile product : core.products){
125
//            String lastModified = utils.time.getTimeFromLong
126
//                (product.file.lastModified());
127
//            
128
//            line = new String[]{product.getId(), lastModified};
129
//            table.add(line);
130
////        }
131
        
132
        result = findFiles(core.getProductsFolder(), 25, request);
133
        
134
        return result;           
135
    }
136
  
137
    
138
    
139
    
140
    /**
141
     * Find all files in a given folder and respective subfolders
142
     * @param where A file object of the start folder
143
     * @param maxDeep How deep is the crawl allowed to proceed
144
     * @param request
145
     * @return An array containing all the found files, returns null if none is
146
     * found
147
     */
148
    public String findFiles(File where, int maxDeep, WebRequest request){
96
149
150
    File[] files = where.listFiles();
151
    String result = "";
97
  
152
    
153
    int[] sizes = new int[]{10,200};
154
    
155
    if(files != null)
156
    for (File file : files) {
157
      if (file.isFile()){
158
         String filePath = file.getAbsolutePath();
159
         // we only want .spdx files
160
         if(filePath.endsWith("spdx")){
161
             
162
            // remove the local disk path with a generic one
163
             String filteredPath = "." 
164
                     + file.getAbsolutePath().replace(core.getProductsFolder()
165
                             .getAbsolutePath(), "");
166
             
167
             String fileLink = html.link(file.getName(), 
168
                             "?x=summary&spdx="
169
                             + filteredPath
170
                             + "");
171
             
172
             
173
           result +=  Table.alignedTable(new String[]{
174
                 html.getIcon("document-text.png", request), 
175
                 fileLink},
176
                 sizes);
177
             
178
            
179
      }
180
         
181
      }
182
      else
183
      if ( (file.isDirectory())
184
         &&( maxDeep-1 > 0 ) ){
185
            String folderName = file.getName();
186
            // do the recursive crawling
187
            String temp = findFiles(file, maxDeep-1, request);
188
            // we don't need empty folders
189
            if(temp.length() == 0){
190
                continue;
191
            }
192
            
193
            String folderText = Table.alignedTable(new String[]{
194
                 html.getIcon("folder-horizontal-open.png", request), 
195
                 folderName},
196
                 sizes);
197
            
198
            String current = 
199
                    html.div()
200
                    + folderText
201
                    //+ html.br
202
                    + html.div()
203
                    + temp
204
                    + html._div
205
                    + html._div;
206
207
            // add a pretty paragraph
208
            if(maxDeep == 25){
209
                current = html.br + current;
210
            }
211
            
212
            result += current;
213
            
214
            
215
      }
216
    
217
    }
218
    
219
    return result;
220
    }
221
    
98
    /**
222
    /**
99
     * Verifies if a given SPDX document exists inside our archive or or not
223
     * Verifies if a given SPDX document exists inside our archive or or not
100
     * @param spdxTarget The file inside the SPDX Archive
224
     * @param spdxTarget The file inside the SPDX Archive
101
     * @return null if the file does not exists, otherwise return a pointer
225
     * @return null if the file does not exists, otherwise return a pointer
102
     */
226
     */
...
...
107
        }
231
        }
108
        // does this file exists?
232
        // does this file exists?
109
        File file = new File(core.getProductsFolder(), spdxTarget);
233
        File file = new File(core.getProductsFolder(), spdxTarget);
110
        // this file needs to exist
234
        // this file needs to exist
111
        if((file.exists() == false) || (file.isDirectory())){
235
        if((file.exists() == false) || (file.isDirectory())){
112
            request.setAnswer("File was not found in our archive, sorry");
236
            request.setAnswer("Sorry, the file was not found: " + spdxTarget);
113
            return null;
237
            return null;
114
        }
238
        }
115
        // all done
239
        // all done
116
        return file;
240
        return file;
117
    }
241
    }
...
...
145
        // does this file exists?
269
        // does this file exists?
146
        File file = getFile(spdxTarget, request);
270
        File file = getFile(spdxTarget, request);
147
        if(file == null){
271
        if(file == null){
148
            return;
272
            return;
149
        }
273
        }
274
        
275
//        String cache = (String) core.temp.get(showSPDX);
276
//        if(cache != null){
277
//            log.write(is.COMPLETED, "Using the cached version of show SPDX");
278
//            request.setAnswer(cache);
279
//            return;
280
//        }
281
        
150
        
282
        
151
        // get the SPDX file from the root node
283
        // get the SPDX file from the root node
152
        SPDXfile spdx = new SPDXfile(file);
284
        SPDXfile spdx = new SPDXfile(file);
153
        // compute some of our useful statistics about the SPDX document
285
        // compute some of our useful statistics about the SPDX document
154
        int counterLicensesDeclared = doStatistics(spdx);
286
        int counterLicensesDeclared = doStatistics(spdx);
...
...
223
                + html.link("With license", "?x=list&"
355
                + html.link("With license", "?x=list&"
224
                        + param.filter + "=withlicense&"
356
                        + param.filter + "=withlicense&"
225
                        + param.spdx + "=" + spdxTarget)
357
                        + param.spdx + "=" + spdxTarget)
226
                + html._div
358
                + html._div
227
                + "";
359
                + "";
360
        
361
        // save our cache for next time
362
        core.temp.put(showSPDX, result);
228
        
363
        
229
        // write everything on our UI text area
364
        // write everything on our UI text area
230
        request.setAnswer(result);
365
        request.setAnswer(result);
231
    }
366
    }
232
    
367