Source code: org/openscience/compchem/MFAnalyser.java
1 /* MFAnalyser.java
2 *
3 * Copyright (C) 1997, 1998 Dr. Christoph Steinbeck
4 *
5 * Contact: steinbeck@ice.mpg.de
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of the
10 * License, or (at your option) any later version. All I ask is that
11 * proper credit is given for my work, which includes - but is not
12 * limited to - adding the above copyright notice to the beginning of
13 * your source code files, and to any copyright notice that you may
14 * distribute with programs based on this work.
15 *
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
24 * 02111-1307, USA.
25 *
26 */
27
28 package org.openscience.compchem;
29
30 import java.util.Vector;
31
32 /** MFAnalyser.java
33 *
34 * Analyses a molecular formula given in String format and builds set
35 * of org.openscience.compchem.Node's reflecting the elements as given
36 * by the molecular formula.
37 */
38 public class MFAnalyser {
39
40 public String MF;
41 public Node[] setOfNodes;
42
43 /** Construct an instance of MFAnalyser, initialized with a molecular
44 * formula string. The string is immediatly analysed and a set of Nodes
45 * is built based on this analysis
46 */
47 public MFAnalyser(String MF) {
48 this.MF = MF;
49 this.setOfNodes = analyseMF(MF);
50 }
51
52 /** returns the complete set of Nodes, as implied by the molecular
53 * formula, inlcuding all the hydrogens.
54 */
55 public Node[] getSetOfNodes() {
56 return setOfNodes;
57 }
58
59 /** Returns a set of nodes excluding all the hydrogens*/
60 public Node[] getSetOfHeavyNodes() {
61 Node[] son = new Node[setOfNodes.length];
62 Node[] rson;
63 int counter = 0;
64 for (int f = 0; f < setOfNodes.length; f++) {
65 if (!setOfNodes[f].symbol.equals("H"))
66 son[counter++] = setOfNodes[f];
67 }
68 rson = new Node[counter];
69 System.arraycopy(son, 0, rson, 0, counter);
70 return rson;
71 }
72
73 /** Method that actually does the work of analysing the molecular
74 formula */
75 private Node[] analyseMF(String MF) {
76 Vector vnodes = new Vector();
77 Node[] nodes;
78 char ThisChar; /* Buffer for */
79 String RecentElementSymbol = new String();
80 String RecentElementCountString = new String("0"); /* String
81 to be
82 converted
83 to an
84 integer */
85 int RecentElementCount;
86
87 if (MF.length() == 0)
88 return null;
89
90 for (int f = 0; f < MF.length(); f ++) {
91 ThisChar = MF.charAt(f);
92 if (f < MF.length()) {
93 if (ThisChar >= 'A' && ThisChar <= 'Z') { /* New
94 Element
95 begins */
96 RecentElementSymbol = java.lang.String.valueOf(ThisChar);
97 RecentElementCountString = "0";
98 }
99 if (ThisChar >= 'a' && ThisChar<= 'z') { /* Two-letter
100 Element
101 continued */
102 RecentElementSymbol += ThisChar;
103 }
104 if (ThisChar >= '0' && ThisChar<= '9') { /* Two-letter
105 Element
106 continued */
107 RecentElementCountString += ThisChar;
108 }
109 }
110 if (f == MF.length() - 1 || (MF.charAt(f + 1) >= 'A' && MF.charAt(f + 1 ) <= 'Z')) {
111 /* Here an element symbol as well as its number should
112 have been read completely */
113 Integer RecentElementCountInteger = new Integer(RecentElementCountString);
114 RecentElementCount = RecentElementCountInteger.intValue();
115 if (RecentElementCount == 0) {
116 RecentElementCount = 1;
117 }
118 for (int g = 0; g < RecentElementCount; g++) {
119 vnodes.addElement(new Node(RecentElementSymbol));
120 }
121 }
122 }
123 nodes = new Node[vnodes.size()];
124 vnodes.copyInto(nodes);
125 return nodes;
126 }
127 }