1 23 24 package org.enhydra.xml.xmlc.misc; 25 26 import java.util.ArrayList ; 27 import java.util.EmptyStackException ; 28 29 33 public final class LineNumberRecorder extends LineNumberMap { 34 37 private class FileStackEntry { 38 39 public final String fileName; 40 public final int lineNum; 41 42 43 public FileStackEntry(String fileName, 44 int lineNum) { 45 this.fileName = fileName; 46 this.lineNum = lineNum; 47 } 48 } 49 50 53 private String fFileName; 54 55 58 private int fLineNum = 1; 59 60 63 private int fStreamLineNum = 1; 64 65 68 private int fStreamCharOffset; 69 70 73 private boolean fPrevWasReturn; 74 75 78 private ArrayList fFileStack; 79 80 83 public LineNumberRecorder(String fileName) { 84 pushFile(fileName); 85 } 86 87 90 public LineNumberRecorder() { 91 } 92 93 96 public void pushFile(String fileName) { 97 if (fFileName != null) { 99 if (fFileStack == null) { 100 fFileStack = new ArrayList (); 101 } 102 fFileStack.add(new FileStackEntry(fFileName, fLineNum)); 103 } 104 fFileName = fileName; 105 fLineNum = 1; 106 addLine(); 108 } 109 110 114 public void popFile() { 115 if ((fFileStack == null) || (fFileStack.size() == 0)) { 116 if (fFileName == null) { 117 throw new EmptyStackException (); 118 } 119 fFileName = null; 121 } else { 122 FileStackEntry entry = (FileStackEntry)fFileStack.get(fFileStack.size()-1); 123 fFileName = entry.fileName; 124 fLineNum = entry.lineNum; 125 fFileStack.remove(fFileStack.size()-1); 126 } 127 } 128 129 132 private void addLine() { 133 addLine(fFileName, fLineNum, fStreamLineNum, fStreamCharOffset); 134 } 135 136 139 public final void countChar(char ch) { 140 fStreamCharOffset++; 141 if (fPrevWasReturn) { 142 fPrevWasReturn = false; 143 } else { 144 switch (ch) { 145 case '\r': 146 fPrevWasReturn = true; 147 case '\n': 148 fLineNum++; 149 fStreamLineNum++; 150 addLine(); 151 } 152 } 153 } 154 155 158 public final void countChars(final char[] chBuf, 159 int off, 160 int len) { 161 int endOff = off+len-1; 162 for (int idx = 0; idx <= endOff; idx++) { 163 countChar(chBuf[idx]); 164 } 165 } 166 } 167 | Popular Tags |