1212import java .net .URL ;
1313import java .util .ArrayList ;
1414import java .util .Collection ;
15+ import java .util .Comparator ;
1516import java .util .List ;
1617import java .util .OptionalInt ;
18+ import java .util .regex .Matcher ;
1719import java .util .stream .Collectors ;
1820import java .util .stream .Stream ;
21+
1922import org .eclipse .core .resources .IContainer ;
2023import org .eclipse .core .resources .IFile ;
2124import org .eclipse .core .resources .IProject ;
4952import org .eclipse .ui .IWorkbenchWindow ;
5053import org .eclipse .ui .PlatformUI ;
5154import org .eclipse .ui .part .FileEditorInput ;
55+ import org .ini4j .InvalidFileFormatException ;
56+ import org .ini4j .Wini ;
5257import org .osgi .framework .Bundle ;
58+
5359import com .google .common .base .CharMatcher ;
60+
61+ import crypto .analysis .CrySLRulesetSelector .RuleFormat ;
5462import crypto .cryptslhandler .CrySLModelReader ;
5563import crypto .rules .CryptSLRule ;
5664import crypto .rules .CryptSLRuleReader ;
6270public class Utils {
6371
6472 private static IWorkbenchWindow window = null ;
65- private static String defaultRulesPath = "resources/CrySLRules/JavaCryptographicArchitecture" ;
6673
6774 /**
6875 * This method checks if a project passed as parameter is a Java project or not.
@@ -325,6 +332,51 @@ public static File getResourceFromWithin(final String inputPath, final String pl
325332
326333 return null ;
327334 }
335+
336+ /***
337+ * Returns parsed objects of resources/configuration.ini file.
338+ * @return Wini object
339+ */
340+ public static Wini getConfig () {
341+ Wini ini = null ;
342+ try {
343+ ini = new Wini (getResourceFromWithin (Constants .CONFIG_FILE_PATH ));
344+ } catch (InvalidFileFormatException e ) {
345+ Activator .getDefault ().logError ("Could not read the configuration file due to: " + e .getMessage ());
346+ } catch (IOException e ) {
347+ Activator .getDefault ().logError ("Failed identifying configuration file due to: " + e .getMessage ());
348+ }
349+ return ini ;
350+ }
351+
352+ /***
353+ * This method returns all sub-directories in a directory of the first level.
354+ * @param ruleSet JavaCryptographicArchitecture, BouncyCastle, Tink
355+ * @return array of version numbers
356+ */
357+ public static String [] getRuleVersions (String ruleSet ){
358+ List <String > versions = new ArrayList <String >();
359+ File path = new File (System .getProperty ("user.dir" ) + File .separator + ruleSet );
360+ File [] innerDirs = path .listFiles ();
361+ for (File f : innerDirs ) {
362+ if (f .isDirectory ()) {
363+ String [] versionNumber = f .getPath ().split (Matcher .quoteReplacement (System .getProperty ("file.separator" )));
364+ versions .add (versionNumber [versionNumber .length - 1 ]);
365+ }
366+ }
367+
368+ versions .sort (new Comparator <String >() {
369+ @ Override
370+ public int compare (String o1 , String o2 ) {
371+ Double one = Double .valueOf (o1 );
372+ Double two = Double .valueOf (o2 );
373+ return one .compareTo (two );
374+ }
375+ });
376+
377+ // https://shipilev.net/blog/2016/arrays-wisdom-ancients/
378+ return versions .toArray (new String [0 ]);
379+ }
328380
329381 protected static void setWindow (final IWorkbenchWindow activeWorkbenchWindow ) {
330382 Utils .window = activeWorkbenchWindow ;
@@ -351,16 +403,16 @@ public static int getFirstIndexofUCL(final String searchString) {
351403 * @throws MalformedURLException
352404 */
353405 public static CryptSLRule getCryptSLRule (String cryptslRule ) throws MalformedURLException {
354- File ruleRes = Utils .getResourceFromWithin (Constants .RELATIVE_RULES_DIR + "/" + cryptslRule + ".cryptsl" , de .cognicrypt .core .Activator .PLUGIN_ID );
406+ File ruleRes = Utils .getResourceFromWithin (Constants .RELATIVE_CUSTOM_RULES_DIR + "/" + cryptslRule + RuleFormat . SOURCE . toString () , de .cognicrypt .core .Activator .PLUGIN_ID );
355407 if (ruleRes == null || !ruleRes .exists () || !ruleRes .canRead ()) {
356- ruleRes = Utils .getResourceFromWithin (defaultRulesPath + "/" + cryptslRule + ".cryptsl" , de .cognicrypt .core .Activator .PLUGIN_ID );
408+ ruleRes = Utils .getResourceFromWithin (Constants . RELATIVE_CUSTOM_RULES_DIR + "/" + cryptslRule + RuleFormat . SOURCE . toString () , de .cognicrypt .core .Activator .PLUGIN_ID );
357409 }
358410 return (new CrySLModelReader ()).readRule (ruleRes );
359411 }
360412
361413 public static List <CryptSLRule > readCrySLRules () {
362- return Stream .of (readCrySLRules (Utils .getResourceFromWithin (Constants .RELATIVE_RULES_DIR ).getAbsolutePath ()),
363- readCrySLRules (Utils .getResourceFromWithin (defaultRulesPath ).getAbsolutePath ())).flatMap (Collection ::stream ).collect (Collectors .toList ());
414+ return Stream .of (readCrySLRules (Utils .getResourceFromWithin (Constants .RELATIVE_CUSTOM_RULES_DIR ).getAbsolutePath ()),
415+ readCrySLRules (Utils .getResourceFromWithin (Constants . RELATIVE_CUSTOM_RULES_DIR ).getAbsolutePath ())).flatMap (Collection ::stream ).collect (Collectors .toList ());
364416 }
365417
366418 protected static List <CryptSLRule > readCrySLRules (String rulesFolder ) {
0 commit comments