1 23 24 package org.enhydra.xml.xmlc.misc; 25 26 import java.util.ArrayList ; 27 28 34 public class LineNumberMap { 35 39 public final class Line { 40 41 private final String fFileName; 42 43 44 private final int fLineNum; 45 46 47 private final int fStreamLineNum; 48 49 50 private final int fStreamCharOffset; 51 52 55 Line(String fileName, 56 int lineNum, 57 int streamLineNum, 58 int streamCharOffset) { 59 fFileName = fileName; 60 fLineNum = lineNum; 61 fStreamLineNum = streamLineNum; 62 fStreamCharOffset = streamCharOffset; 63 } 64 65 67 public String getFileName() { 68 return fFileName; 69 } 70 71 72 public int getLineNum() { 73 return fLineNum; 74 } 75 76 77 public int getStreamLineNum() { 78 return fStreamLineNum; 79 } 80 81 82 public int getStreamCharOffset() { 83 return fStreamCharOffset; 84 } 85 86 87 public String toString() { 88 return "(" + fFileName + " " + fLineNum + ") => (" 89 + fStreamLineNum + " " + fStreamCharOffset + ")"; 90 91 } 92 } 93 94 99 private ArrayList fLines = new ArrayList (); 100 101 105 public final void addLine(String fileName, 106 int lineNum, 107 int streamLineNum, 108 int streamCharOffset) { 109 fLines.add(new Line(fileName, lineNum, 110 streamLineNum, streamCharOffset)); 111 } 112 113 116 public final Line getLineFromOffset(int streamCharOffset) { 117 for (int idx = fLines.size() - 1; idx >= 0; idx--) { 120 Line line = (Line)fLines.get(idx); 121 if (streamCharOffset >= line.getStreamCharOffset()) { 122 return line; 123 } 124 } 125 return null; 126 } 127 128 131 public final Line getLineFromLineNum(int streamLineNum) { 132 for (int idx = fLines.size() - 1; idx >= 0; idx--) { 135 Line line = (Line)fLines.get(idx); 136 if (streamLineNum >= line.getStreamLineNum()) { 137 return line; 138 } 139 } 140 return null; 141 } 142 143 144 public String toString() { 145 StringBuffer buf = new StringBuffer (4096); 147 for (int idx = 0; idx < fLines.size(); idx++) { 148 buf.append(fLines.get(idx).toString()); 149 buf.append('\n'); 150 } 151 return buf.toString(); 152 } 153 } 154 | Popular Tags |