1 21 22 package net.percederberg.grammatica.parser; 23 24 import java.io.PrintStream ; 25 import java.io.PrintWriter ; 26 import java.util.ArrayList ; 27 import java.util.Vector ; 28 29 36 public abstract class Node { 37 38 41 private Node parent = null; 42 43 46 private ArrayList values = null; 47 48 55 boolean isHidden() { 56 return false; 57 } 58 59 66 public abstract int getId(); 67 68 73 public abstract String getName(); 74 75 83 public int getStartLine() { 84 int line; 85 86 for (int i = 0; i < getChildCount(); i++) { 87 line = getChildAt(i).getStartLine(); 88 if (line >= 0) { 89 return line; 90 } 91 } 92 return -1; 93 } 94 95 103 public int getStartColumn() { 104 int col; 105 106 for (int i = 0; i < getChildCount(); i++) { 107 col = getChildAt(i).getStartColumn(); 108 if (col >= 0) { 109 return col; 110 } 111 } 112 return -1; 113 } 114 115 123 public int getEndLine() { 124 int line; 125 126 for (int i = getChildCount() - 1; i >= 0; i--) { 127 line = getChildAt(i).getEndLine(); 128 if (line >= 0) { 129 return line; 130 } 131 } 132 return -1; 133 } 134 135 143 public int getEndColumn() { 144 int col; 145 146 for (int i = getChildCount() - 1; i >= 0; i--) { 147 col = getChildAt(i).getEndColumn(); 148 if (col >= 0) { 149 return col; 150 } 151 } 152 return -1; 153 } 154 155 160 public Node getParent() { 161 return parent; 162 } 163 164 169 void setParent(Node parent) { 170 this.parent = parent; 171 } 172 173 178 public int getChildCount() { 179 return 0; 180 } 181 182 190 public Node getChildAt(int index) { 191 return null; 192 } 193 194 201 public int getDescendantCount() { 202 int count = 0; 203 204 for (int i = 0; i < getChildCount(); i++) { 205 count += 1 + getChildAt(i).getDescendantCount(); 206 } 207 return count; 208 } 209 210 217 public int getValueCount() { 218 if (values == null) { 219 return 0; 220 } else { 221 return values.size(); 222 } 223 } 224 225 235 public Object getValue(int pos) { 236 if (values == null || pos < 0 || pos >= values.size()) { 237 return null; 238 } else { 239 return values.get(pos); 240 } 241 } 242 243 253 public ArrayList getAllValues() { 254 return values; 255 } 256 257 264 public void addValue(Object value) { 265 if (value != null) { 266 if (this.values == null) { 267 this.values = new ArrayList (); 268 } 269 values.add(value); 270 } 271 } 272 273 278 public void addValues(Vector values) { 279 if (values != null) { 280 for (int i = 0; i < values.size(); i++) { 281 addValue(values.get(i)); 282 } 283 } 284 } 285 286 293 public void addValues(ArrayList values) { 294 if (values != null) { 295 for (int i = 0; i < values.size(); i++) { 296 addValue(values.get(i)); 297 } 298 } 299 } 300 301 304 public void removeAllValues() { 305 values = null; 306 } 307 308 314 public void printTo(PrintStream output) { 315 printTo(new PrintWriter (output)); 316 } 317 318 324 public void printTo(PrintWriter output) { 325 printTo(output, ""); 326 output.flush(); 327 } 328 329 336 private void printTo(PrintWriter output, String indent) { 337 output.println(indent + toString()); 338 indent = indent + " "; 339 for (int i = 0; i < getChildCount(); i++) { 340 getChildAt(i).printTo(output, indent); 341 } 342 } 343 } 344 | Popular Tags |