1 11 package org.eclipse.jdt.internal.core.jdom; 12 13 import java.util.Stack ; 14 15 import org.eclipse.jdt.core.jdom.*; 16 import org.eclipse.jdt.internal.compiler.env.ICompilationUnit; 17 import org.eclipse.jdt.internal.core.util.ReferenceInfoAdapter; 18 19 25 public class AbstractDOMBuilder extends ReferenceInfoAdapter implements ILineStartFinder { 26 30 protected boolean fAbort; 31 32 37 protected boolean fBuildingCU = false; 38 39 44 protected boolean fBuildingType= false; 45 46 49 protected char[] fDocument= null; 50 51 54 protected int[] fLineStartPositions = new int[] { 0 }; 55 56 61 protected Stack fStack = null; 62 63 69 protected int fFieldCount; 70 71 74 protected DOMNode fNode; 75 78 public AbstractDOMBuilder() { 79 super(); 80 } 81 88 public void acceptLineSeparatorPositions(int[] positions) { 89 if (positions != null) { 90 int length = positions.length; 91 if (length > 0) { 92 fLineStartPositions = new int[length + 1]; 93 fLineStartPositions[0] = 0; 94 int documentLength = fDocument.length; 95 for (int i = 0; i < length; i++) { 96 int iPlusOne = i + 1; 97 int positionPlusOne = positions[i] + 1; 98 if (positionPlusOne < documentLength) { 99 if (iPlusOne < length) { 100 fLineStartPositions[iPlusOne] = positionPlusOne; 102 } else { 103 if (fDocument[positionPlusOne] == '\n') { 105 fLineStartPositions[iPlusOne] = positionPlusOne + 1; 106 } else { 107 fLineStartPositions[iPlusOne] = positionPlusOne; 108 } 109 } 110 } else { 111 fLineStartPositions[iPlusOne] = positionPlusOne; 112 } 113 } 114 } 115 } 116 } 117 125 protected void addChild(IDOMNode child) { 126 if (fStack.size() > 0) { 127 DOMNode parent = (DOMNode) fStack.peek(); 128 if (fBuildingCU || fBuildingType) { 129 parent.basicAddChild(child); 130 } 131 } 132 } 133 136 public IDOMCompilationUnit createCompilationUnit(char[] contents, char[] name) { 137 return createCompilationUnit(new CompilationUnit(contents, name)); 138 } 139 142 public IDOMCompilationUnit createCompilationUnit(ICompilationUnit compilationUnit) { 143 if (fAbort) { 144 return null; 145 } 146 fNode.normalize(this); 147 return (IDOMCompilationUnit)fNode; 148 } 149 152 public void enterCompilationUnit() { 153 if (fBuildingCU) { 154 IDOMCompilationUnit cu= new DOMCompilationUnit(fDocument, new int[] {0, fDocument.length - 1}); 155 fStack.push(cu); 156 } 157 } 158 164 public void exitCompilationUnit(int declarationEnd) { 165 DOMCompilationUnit cu = (DOMCompilationUnit) fStack.pop(); 166 cu.setSourceRangeEnd(declarationEnd); 167 fNode = cu; 168 } 169 176 protected void exitType(int bodyEnd, int declarationEnd) { 177 DOMType type = (DOMType)fStack.pop(); 178 type.setSourceRangeEnd(declarationEnd); 179 type.setCloseBodyRangeStart(bodyEnd); 180 type.setCloseBodyRangeEnd(bodyEnd); 181 fNode = type; 182 } 183 186 public int getLineStart(int position) { 187 int lineSeparatorCount = fLineStartPositions.length; 188 for(int i = lineSeparatorCount - 1; i >= 0; i--) { 190 if (fLineStartPositions[i] <= position) 191 return fLineStartPositions[i]; 192 } 193 return 0; 194 } 195 204 protected void initializeBuild(char[] sourceCode, boolean buildingCompilationUnit, boolean buildingType) { 205 fBuildingCU = buildingCompilationUnit; 206 fBuildingType = buildingType; 207 fStack = new Stack (); 208 fDocument = sourceCode; 209 fFieldCount = 0; 210 fAbort = false; 211 } 212 } 213 | Popular Tags |