From 8507a8fbf17b65b1cf939e609287177b20599297 Mon Sep 17 00:00:00 2001 From: mguetlein Date: Fri, 10 Oct 2014 12:05:12 +0200 Subject: fix cdkdescriptor calculation: find classes with 'descriptor' in the middle of the class name --- java/CdkDescriptors.java | 43 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/java/CdkDescriptors.java b/java/CdkDescriptors.java index a42853e..42a44e1 100644 --- a/java/CdkDescriptors.java +++ b/java/CdkDescriptors.java @@ -38,14 +38,7 @@ class CdkDescriptors { descriptorNames.add(args[i]); descriptorName = args[i]; } - String className = "org.openscience.cdk.qsar.descriptors.molecular." + descriptorName + "Descriptor"; - try { - Class.forName(className); - } catch (ClassNotFoundException e) { - System.err.println("Descriptor not found: "+args[i]); - System.exit(1); - } - classNames.add(className); + classNames.add(getDescriptorClassName(descriptorName)); } engine = new DescriptorEngine(new ArrayList(classNames)); @@ -99,4 +92,38 @@ class CdkDescriptors { } catch (Exception e) { e.printStackTrace(); } } + + + /** HACK to find the class for a descriptor + * problem: Descriptor is not always at the end of the class (APolDescriptor), but may be in the middle (AutocorrelationDescriptorPolarizability) + * this method makes a class-lookup using trial and error */ + static String getDescriptorClassName(String descriptorName) { + String split = splitCamelCase(descriptorName)+" "; // space mark possible positions for 'Descriptor' + for(int i = split.length()-1; i>0; i--) { + if (split.charAt(i)==' ') { // iterate over all spaces, starting with the trailing one + String test = split.substring(0,i)+"Descriptor"+split.substring(i+1,split.length()); // replace current space with 'Descriptor' .. + test = test.replaceAll("\\s",""); // .. and remove other spaces + String className = "org.openscience.cdk.qsar.descriptors.molecular." + test; + try { + Class.forName(className); + return className; + } catch (ClassNotFoundException e) {} + } + } + System.err.println("Descriptor not found: "+descriptorName); + System.exit(1); + return null; + } + + /** inserts space in between camel words */ + static String splitCamelCase(String s) { + return s.replaceAll( + String.format("%s|%s|%s", + "(?<=[A-Z])(?=[A-Z][a-z])", + "(?<=[^A-Z])(?=[A-Z])", + "(?<=[A-Za-z])(?=[^A-Za-z])" + ), + " " + ); + } } -- cgit v1.2.3