1 2 3 package de.gulden.util.javasource.jjt; 4 5 public class SimpleNode implements Node { 6 protected Node parent; 7 protected Node[] children; 8 protected int id; 9 protected Parser parser; 10 11 public SimpleNode(int i) { 12 id = i; 13 } 14 15 public SimpleNode(Parser p, int i) { 16 this(i); 17 parser = p; 18 } 19 20 public void jjtOpen() { 21 } 22 23 public void jjtClose() { 24 } 25 26 public void jjtSetParent(Node n) { parent = n; } 27 public Node jjtGetParent() { return parent; } 28 29 public void jjtAddChild(Node n, int i) { 30 if (children == null) { 31 children = new Node[i + 1]; 32 } else if (i >= children.length) { 33 Node c[] = new Node[i + 1]; 34 System.arraycopy(children, 0, c, 0, children.length); 35 children = c; 36 } 37 children[i] = n; 38 } 39 40 public Node jjtGetChild(int i) { 41 return children[i]; 42 } 43 44 public int jjtGetNumChildren() { 45 return (children == null) ? 0 : children.length; 46 } 47 48 53 54 public String toString() { return ParserTreeConstants.jjtNodeName[id]; } 55 public String toString(String prefix) { return prefix + toString(); } 56 57 59 60 public void dump(String prefix) { 61 System.out.println(toString(prefix)); 62 if (children != null) { 63 for (int i = 0; i < children.length; ++i) { 64 SimpleNode n = (SimpleNode)children[i]; 65 if (n != null) { 66 n.dump(prefix + " "); 67 } 68 } 69 } 70 } 71 72 73 74 75 private final static Node[] EMPTY=new Node[0]; 76 77 protected String value; 78 protected Token[] tokenRange=new Token[2]; 79 protected TextImage textImage; 80 protected String source; 81 82 public int getId() 83 { 84 return id; 85 } 86 87 90 public Node getChild(int id) 91 { 92 if (children!=null) 93 { 94 for (int i=0;i<children.length;i++) 95 { 96 if (children[i].getId()==id) 97 { 98 return children[i]; 99 } 100 } 101 } 102 return null; 103 } 104 105 public boolean hasChild(int id) 106 { 107 return (getChild(id)!=null); 108 } 109 110 public Node[] getChildren(int id) 111 { 112 if (children!=null) 113 { 114 java.util.Vector v=new java.util.Vector (5); 115 for (int i=0;i<children.length;i++) 116 { 117 if (children[i].getId()==id) 118 { 119 v.addElement(children[i]); 120 } 121 } 122 Node[] n=new Node[v.size()]; 123 v.copyInto(n); 124 return n; 125 } 126 else 127 { 128 return EMPTY; 129 } 130 } 131 132 public Node[] getAllChildren() 133 { 134 if (children!=null) 135 { 136 return children; 137 } 138 else 139 { 140 return EMPTY; 141 } 142 } 143 144 public String getValue() 145 { 146 return value; 147 } 148 149 public void setValue(String v) 150 { 151 value=v; 153 } 154 155 public String getName() 156 { 157 Node node=getChild(ParserTreeConstants.JJT_NAME); 158 if (node!=null) 159 { 160 return node.retrieveName(); 161 } 162 else 163 { 164 return null; 165 } 166 } 167 168 public String retrieveName() 169 { 170 String name; 171 int num=jjtGetNumChildren(); 172 if (num==1) { 174 Node n=jjtGetChild(0); 175 name=n.getValue(); 176 } 177 else { 179 StringBuffer sb=new StringBuffer (); 180 for (int i=0;i<num;i+=2) 181 { 182 String n=jjtGetChild(i).getValue(); 184 sb.append(n); 185 if (i+1<num) 186 { 187 sb.append("."); 189 } 190 } 191 name=sb.toString(); 192 } 193 return name; 194 } 195 196 public void setStartToken(Token t) 197 { 198 tokenRange[0]=t; 199 } 200 201 public void setEndToken(Token t) 202 { 203 tokenRange[1]=t; 204 } 205 206 public Token getStartToken() 207 { 208 return tokenRange[0]; 209 } 210 211 public Token getEndToken() 212 { 213 return tokenRange[1]; 214 } 215 216 public void setTextImage(TextImage ti) 217 { 218 textImage=ti; 219 } 220 221 public TextImage getTextImage() 222 { 223 return textImage; 224 } 225 226 public String getSource() 227 { 228 return source; 229 } 230 231 public void setSource(String source) 232 { 233 this.source=source; 234 } 235 236 public int[] getSourcePosition() 237 { 238 int[] pos={getStartToken().beginLine,getStartToken().beginColumn}; 239 return pos; 240 } 241 } 242 243 | Popular Tags |