1 /* Copyright (C) 2004-2007  The Chemistry Development Kit (CDK) project
2  *
3  * This library is free software; you can redistribute it and/or
4  * modify it under the terms of the GNU Lesser General Public
5  * License as published by the Free Software Foundation; either
6  * version 2.1 of the License, or (at your option) any later version.
7  *
8  * This library is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public
14  * License along with this library; if not, write to the Free Software
15  * Foundation, 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
16  * (or see http://www.gnu.org/copyleft/lesser.html)
17  */
18 package org.openscience.cdk.smiles.smarts.parser;
19 
20 import java.lang.reflect.Constructor;
21 import java.lang.reflect.InvocationTargetException;
22 
23 /**
24  * Basic implementation of AST nodes. Automatically generated by JJTree with
25  * some manual changes.
26  *
27  * @author Dazhi Jiao
28  * @cdk.created 2007-04-24
29  * @cdk.module smarts
30  * @cdk.githash
31  * @cdk.keyword SMARTS AST
32  */
33 @Deprecated
34 class SimpleNode implements Node, Cloneable {
35 
36     protected Node         parent;
37 
38     protected Node[]       children;
39 
40     protected int          id;
41 
42     protected SMARTSParser parser;
43 
SimpleNode(int i)44     public SimpleNode(int i) {
45         id = i;
46     }
47 
SimpleNode(SMARTSParser p, int i)48     public SimpleNode(SMARTSParser p, int i) {
49         this(i);
50         parser = p;
51     }
52 
53     /*
54      * (non-Javadoc)
55      * @see java.lang.Object#clone()
56      */
57     @Override
clone()58     public Object clone() {
59         Constructor constructor = null;
60         Node clone = null;
61         try {
62             constructor = this.getClass().getConstructor(new Class[]{SMARTSParser.class, Integer.TYPE});
63             clone = (Node) constructor.newInstance(new Object[]{parser, Integer.valueOf(id)});
64         } catch (NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
65             ex.printStackTrace();
66         }
67 
68         clone.jjtSetParent(parent);
69         if (this.jjtGetNumChildren() > 0) {
70             for (int i = 0; i < children.length; i++) {
71                 clone.jjtAddChild((Node) ((SimpleNode) children[i]).clone(), i);
72             }
73         }
74         return clone;
75     }
76 
77     @Override
jjtOpen()78     public void jjtOpen() {}
79 
80     @Override
jjtClose()81     public void jjtClose() {}
82 
83     @Override
jjtSetParent(Node n)84     public void jjtSetParent(Node n) {
85         parent = n;
86     }
87 
88     @Override
jjtGetParent()89     public Node jjtGetParent() {
90         return parent;
91     }
92 
93     @Override
jjtAddChild(Node n, int i)94     public void jjtAddChild(Node n, int i) {
95         if (children == null) {
96             children = new Node[i + 1];
97         } else if (i >= children.length) {
98             Node c[] = new Node[i + 1];
99             System.arraycopy(children, 0, c, 0, children.length);
100             children = c;
101         }
102         children[i] = n;
103     }
104 
105     @Override
jjtRemoveChild(int i)106     public void jjtRemoveChild(int i) {
107         if (i >= children.length) return;
108         Node[] c = new Node[children.length - 1];
109         System.arraycopy(children, 0, c, 0, i);
110         if (i < c.length) {
111             System.arraycopy(children, i + 1, c, i, c.length - i);
112         }
113         children = c;
114     }
115 
116     @Override
jjtGetChild(int i)117     public Node jjtGetChild(int i) {
118         return children[i];
119     }
120 
121     @Override
jjtGetNumChildren()122     public int jjtGetNumChildren() {
123         return (children == null) ? 0 : children.length;
124     }
125 
126     /** Accept the visitor. * */
127     @Override
jjtAccept(SMARTSParserVisitor visitor, Object data)128     public Object jjtAccept(SMARTSParserVisitor visitor, Object data) {
129         return visitor.visit(this, data);
130     }
131 
132     /** Accept the visitor. * */
childrenAccept(SMARTSParserVisitor visitor, Object data)133     public Object childrenAccept(SMARTSParserVisitor visitor, Object data) {
134         if (children != null) {
135             for (int i = 0; i < children.length; ++i) {
136                 children[i].jjtAccept(visitor, data);
137             }
138         }
139         return data;
140     }
141 
142     /*
143      * You can override these two methods in subclasses of SimpleNode to
144      * customize the way the node appears when the tree is dumped. If your
145      * output uses more than one line you should override toString(String),
146      * otherwise overriding toString() is probably all you need to do.
147      */
148 
149     @Override
toString()150     public String toString() {
151         return SMARTSParserTreeConstants.jjtNodeName[id];
152     }
153 
toString(String prefix)154     public String toString(String prefix) {
155         return prefix + toString();
156     }
157 
158     /*
159      * Override this method if you want to customize how the node dumps out its
160      * children.
161      */
162 
dump(String prefix)163     public void dump(String prefix) {
164         System.out.println(toString(prefix));
165         if (children != null) {
166             for (int i = 0; i < children.length; ++i) {
167                 SimpleNode n = (SimpleNode) children[i];
168                 if (n != null) {
169                     n.dump(prefix + " ");
170                 }
171             }
172         }
173     }
174 
getId()175     public int getId() {
176         return id;
177     }
178 
setId(int id)179     public void setId(int id) {
180         this.id = id;
181     }
182 
getParser()183     public SMARTSParser getParser() {
184         return parser;
185     }
186 
setParser(SMARTSParser parser)187     public void setParser(SMARTSParser parser) {
188         this.parser = parser;
189     }
190 }
191