Parent: [r15] (diff)

Download this file

actions.java    211 lines (181 with data), 7.6 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
* SPDXVersion: SPDX-1.1
* Creator: Person: Nuno Brito (nuno.brito@triplecheck.de)
* Creator: Organization: TripleCheck (contact@triplecheck.de)
* Created: 2013-09-13T00:00:00Z
* LicenseName: NOASSERTION
* FileName: actions.java
* FileType: SOURCE
* FileCopyrightText: <text> Copyright 2013 Nuno Brito, TripleCheck </text>
* FileComment: <text> A simple static class to provide default actions
* related to SPDX handling </text>
*/
package main;
import GUI.NodeType;
import GUI.TreeNodeSPDX;
import GUI.swingUtils;
import static GUI.swingUtils.nodeCreate;
import definitions.definition;
import definitions.is;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;
import script.log;
import spdxlib.SPDXfile;
public class actions {
/**
* This method adds up all the licenses found on the licenses folder
*/
public static void addLicenses(){
File folder = new File(core.getWorkFolder(), "licenses");
ArrayList<File> files = utils.files.findFilesFiltered(folder, ".java", 2);
for(File file : files){
core.script.runJava(file, null, is.license);
}
}
/**
* Find all files within a given folder, return the respective
* details such as hashes and other details relevant for SPDX processing
* @param folder The folder location where we will look for SPDX files
* @return
*/
public static ArrayList<SPDXfile> findSPDX(File folder){
// only find the SPDX documents with .SPDX extension
ArrayList<File> files = utils.files.findFilesFiltered(folder, ".spdx", 25);
ArrayList<SPDXfile> list = new ArrayList();
try {
for(File file: files){
String filePath = file.getCanonicalPath();
// ignore SVN folders of any kind
if(filePath.contains(".svn")){
continue;
}
SPDXfile spdxFile;
spdxFile = new SPDXfile(file.getCanonicalFile());
list.add(spdxFile);
}
String pathName = folder.getCanonicalPath();
pathName = "."
+ pathName.replace(core.getWorkFolder().getCanonicalPath(), "");
// System.err.println(core.getWorkFolder().getCanonicalPath());
log.write(is.INFO, "Found %1 files in %2",
files.size()
+"", pathName);
} catch (IOException ex) {
Logger.getLogger(actions.class.getName()).log(Level.SEVERE, null, ex);
System.exit(-100);
}
return list;
}
/**
* This is a critical method. It will look for all SPDX files inside a given
* folder and them as tree nodes
* @param tree
*/
public static void addTreeSPDX(JTree tree) {
// only find the SPDX documents with .SPDX extension
ArrayList<File> fileList = utils.files
.findFilesFiltered(core.getProductsFolder(), ".spdx", 25);
if(fileList.isEmpty()){
// no need to continue, just leave here
return;
}
// get the root node where we have our treeview
TreeNodeSPDX rootNode = swingUtils.getRootNode(tree);
// create the node for hosting our list of SPDX documents
TreeNodeSPDX softwareNode = nodeCreate(
definition.nodeSoftware
//+ " (" + fileList.size() + ")"
, NodeType.other, rootNode);
// add the identification to this tag
softwareNode.id = definition.nodeSoftware;
softwareNode.setIcon("box-label.png");
// create a tree based on folder tree on disk
findFolders(core.getProductsFolder(), 25, softwareNode);
// we use this value for later showing the products (SPDX) node
// fully expanded
core.temp.put(is.products, softwareNode.getUID());
// swingUtils.setSelectedNode(softwareNode.getUID());
}
/**
* Find all subfolders in a given folder.
* @param where A file object of the start folder
* @param maxDeep How deep is the crawl allowed to proceed
* @param rootNode
// * @return An array containing all the found files, returns null if none is
// * found
* @return the number of relevant children that should be accounted
*/
public static int findFolders(File where, int maxDeep
, TreeNodeSPDX rootNode){
// get a list of the current files on this folder
File[] files = where.listFiles();
// we use this for counting files that were added
int count = 0;
// go through all the files/folders that were listed
if(files != null)
for (File file : files) {
// only accept folders, ignore files
if ( (file.isDirectory())
&&( maxDeep-1 > 0 ) ){
String folderName = file.getName();
// ignore SVN folders
if(folderName.equals(".svn")){
continue;
}
// create a new node for the current folder
TreeNodeSPDX node = new TreeNodeSPDX(folderName);
node.setIcon("folder-horizontal.png");
node.setIconWhenSelected("folder-horizontal-open.png");
node.nodeType = NodeType.folder;
node.id = folderName;
// we want to track if something new was added
int oldCount = count;
// do the recursive crawling
count += findFolders(file, maxDeep-1, node);
// only add this node if we found something interesting there
if(count > oldCount){
// add the node as a child of the root node
rootNode.add(node);
}
}
// perhaps we can add this file if it is an SPDX?
if(file.isFile()){
String fileName = file.getName();
// we only want files with an SPDX extension
if(file.getName().endsWith(".spdx")==false){
continue;
}
// we don't generic SPDX files here
if(file.getName().endsWith("-info.spdx")){
continue;
}
// create the new node
TreeNodeSPDX node = new TreeNodeSPDX(fileName);
node.setIcon("box.png");
node.nodeType = NodeType.SPDX;
node.id = fileName;
// set here what we want to happen when the user clicks on it
File scriptFile = new File(core.getPluginsFolder(), "/spdx/show.java");
node.scriptFile = scriptFile;
node.scriptFolder = scriptFile.getParentFile();
node.scriptMethod = "summary";
// create the correct parameters
String relativePath =
file.getAbsolutePath().replace(core.getProductsFolder().getAbsolutePath(), "");
node.scriptParameters.add(new String[]{param.spdx, relativePath});
// add it up
rootNode.add(node);
count++;
}
}
// how many files were found here?
rootNode.setCounter(count);
return count;
}
}