1 11 package org.eclipse.jdt.internal.debug.core.hcr; 12 13 14 import java.util.ArrayList ; 15 16 import org.eclipse.jdt.core.ToolFactory; 17 import org.eclipse.jdt.core.compiler.IScanner; 18 import org.eclipse.jdt.core.compiler.ITerminalSymbols; 19 import org.eclipse.jdt.core.compiler.InvalidInputException; 20 import org.eclipse.jdt.internal.core.JavaElement; 21 22 25 class JavaNode { 26 27 public static final int CU= 0; 28 public static final int PACKAGE= 1; 29 public static final int IMPORT_CONTAINER= 2; 30 public static final int IMPORT= 3; 31 public static final int INTERFACE= 4; 32 public static final int CLASS= 5; 33 public static final int FIELD= 6; 34 public static final int INIT= 7; 35 public static final int CONSTRUCTOR= 8; 36 public static final int METHOD= 9; 37 38 private String fID; 39 private int fTypeCode; 40 private char[] fBuffer; 41 private int fStart; 42 private int fLength; 43 private ArrayList fChildren; 44 private int fInitializerCount= 1; 45 46 58 JavaNode(int typeCode, String id, char[] buffer, int start, int length) { 59 fTypeCode= typeCode; 60 fID= id; 61 fBuffer= buffer; 62 fStart= start; 63 fLength= length; 64 } 65 66 73 JavaNode(JavaNode parent, int type, String name, int start, int length) { 74 this(type, buildID(type, name), parent.fBuffer, start, length); 75 if (parent != null) { 76 parent.addChild(this); 77 } 78 } 79 80 85 JavaNode(char[] buffer) { 86 this(CU, buildID(CU, "root"), buffer, 0, buffer.length); } 88 89 93 private static String buildID(int type, String name) { 94 StringBuffer sb= new StringBuffer (); 95 switch (type) { 96 case JavaNode.CU: 97 sb.append(JavaElement.JEM_COMPILATIONUNIT); 98 break; 99 case JavaNode.CLASS: 100 case JavaNode.INTERFACE: 101 sb.append(JavaElement.JEM_TYPE); 102 sb.append(name); 103 break; 104 case JavaNode.FIELD: 105 sb.append(JavaElement.JEM_FIELD); 106 sb.append(name); 107 break; 108 case JavaNode.CONSTRUCTOR: 109 case JavaNode.METHOD: 110 sb.append(JavaElement.JEM_METHOD); 111 sb.append(name); 112 break; 113 case JavaNode.INIT: 114 sb.append(JavaElement.JEM_INITIALIZER); 115 sb.append(name); 116 break; 117 case JavaNode.PACKAGE: 118 sb.append(JavaElement.JEM_PACKAGEDECLARATION); 119 break; 120 case JavaNode.IMPORT: 121 sb.append(JavaElement.JEM_IMPORTDECLARATION); 122 sb.append(name); 123 break; 124 case JavaNode.IMPORT_CONTAINER: 125 sb.append('<'); 126 break; 127 default: 128 break; 130 } 131 return sb.toString(); 132 } 133 134 public String getInitializerCount() { 135 return Integer.toString(fInitializerCount++); 136 } 137 138 public int getStart() { 139 return fStart; 140 } 141 142 148 public int getTypeCode() { 149 return fTypeCode; 150 } 151 152 158 public String getId() { 159 return fID; 160 } 161 162 168 public void setId(String id) { 169 fID= id; 170 } 171 172 177 public void addChild(JavaNode node) { 178 if (fChildren == null) { 179 fChildren= new ArrayList (); 180 } 181 fChildren.add(node); 182 } 183 184 187 public Object [] getChildren() { 188 if (fChildren != null) { 189 return fChildren.toArray(); 190 } 191 return null; 192 } 193 194 199 public void setLength(int length) { 200 fLength= length; 201 } 202 203 206 public boolean equals(Object other) { 207 if (other != null && other.getClass() == getClass()) { 208 JavaNode tn= (JavaNode) other; 209 return fTypeCode == tn.fTypeCode && fID.equals(tn.fID); 210 } 211 return super.equals(other); 212 } 213 214 217 public int hashCode() { 218 return fID.hashCode(); 219 } 220 221 228 229 public String getContents() { 230 char[] b= new char[fLength]; 231 System.arraycopy(fBuffer, fStart, b, 0, fLength); 232 233 boolean ignoreWhiteSpace= true; 234 if (ignoreWhiteSpace) { 235 StringBuffer buf= new StringBuffer (); 237 238 IScanner scanner= ToolFactory.createScanner(true, true, false, false); scanner.setSource(b); 242 try { 243 int token; 244 while ((token= scanner.getNextToken()) != ITerminalSymbols.TokenNameEOF) { 245 switch (token) { 246 case ITerminalSymbols.TokenNameWHITESPACE: 247 case ITerminalSymbols.TokenNameCOMMENT_BLOCK: 248 case ITerminalSymbols.TokenNameCOMMENT_JAVADOC: 249 case ITerminalSymbols.TokenNameCOMMENT_LINE: 250 int l= buf.length(); 251 if (l > 0 && buf.charAt(l-1) != ' ') { 252 buf.append(' '); 253 } 254 break; 255 default: 256 buf.append(b, scanner.getCurrentTokenStartPosition(), (scanner.getCurrentTokenEndPosition() + 1) - scanner.getCurrentTokenStartPosition()); 257 buf.append(' '); 258 break; 259 } 260 } 261 return buf.toString(); } catch (InvalidInputException ex) { 263 } 264 } 265 return new String (b); } 267 } 268 269 | Popular Tags |