fixed formula checking
@@ -31,6 +31,7 @@ import java.util.regex.Pattern;
import javax.annotation.Nonnull;
import org.openscience.cdk.config.IsotopeFactory;
import org.openscience.cdk.config.Isotopes;
+import org.openscience.cdk.formula.rules.ElementRule;
import org.openscience.cdk.interfaces.IChemObjectBuilder;
import org.openscience.cdk.interfaces.IIsotope;
import org.openscience.cdk.interfaces.IMolecularFormula;
@@ -239,29 +240,18 @@ public class FormulaUtils {
molFormula = MolecularFormulaManipulator.getMajorIsotopeMolecularFormula(formula, builder);
- String simple = MolecularFormulaManipulator.simplifyMolecularFormula(formula);
- String elements[] = simple.split("0");
- String invalid = "";
+ boolean valid = true;
- boolean found = false;
- for (String element : elements) {
- found = false;
for (IIsotope iso : molFormula.isotopes()) {
- if (element.equals(iso.getSymbol()) && (iso.getAtomicNumber() != null)
- && (iso.getAtomicNumber() != 0)) {
+ if ((iso.getAtomicNumber() == null) || (iso.getAtomicNumber() == 0)) {
// iso.getAtomicNumber() != null has to be checked, e.g. for some reason an element with
// Symbol "R" and number 0 exists in the CDK
- found = true;
- }
- }
- if (found == false) {
- invalid += element + ", ";
+ valid = false;
}
}
- if (invalid.length() != 0) {
- invalid = invalid.substring(0, invalid.length() - 2);
- logger.warning("Formula invalid! Element(s) " + invalid + " do not exist.");
+ if (!valid) {
+ logger.warning("Formula invalid! Formula contains element symbols that do not exist.");
return false;
}
return true;
