| The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2008 The Android Open Source Project | 
|  | 3 | * | 
|  | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | * you may not use this file except in compliance with the License. | 
|  | 6 | * You may obtain a copy of the License at | 
|  | 7 | * | 
|  | 8 | *      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | * | 
|  | 10 | * Unless required by applicable law or agreed to in writing, software | 
|  | 11 | * distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | * See the License for the specific language governing permissions and | 
|  | 14 | * limitations under the License. | 
|  | 15 | */ | 
|  | 16 |  | 
|  | 17 | public class SourcePositionInfo implements Comparable | 
|  | 18 | { | 
|  | 19 | public SourcePositionInfo() { | 
|  | 20 | this.file = "<unknown>"; | 
|  | 21 | this.line = 0; | 
|  | 22 | this.column = 0; | 
|  | 23 | } | 
|  | 24 |  | 
|  | 25 | public SourcePositionInfo(String file, int line, int column) | 
|  | 26 | { | 
|  | 27 | this.file = file; | 
|  | 28 | this.line = line; | 
|  | 29 | this.column = column; | 
|  | 30 | } | 
|  | 31 |  | 
|  | 32 | public SourcePositionInfo(SourcePositionInfo that) | 
|  | 33 | { | 
|  | 34 | this.file = that.file; | 
|  | 35 | this.line = that.line; | 
|  | 36 | this.column = that.column; | 
|  | 37 | } | 
|  | 38 |  | 
|  | 39 | /** | 
|  | 40 | * Given this position and str which occurs at that position, as well as str an index into str, | 
|  | 41 | * find the SourcePositionInfo. | 
|  | 42 | * | 
|  | 43 | * @throw StringIndexOutOfBoundsException if index > str.length() | 
|  | 44 | */ | 
|  | 45 | public static SourcePositionInfo add(SourcePositionInfo that, String str, int index) | 
|  | 46 | { | 
|  | 47 | if (that == null) { | 
|  | 48 | return null; | 
|  | 49 | } | 
|  | 50 | int line = that.line; | 
|  | 51 | char prev = 0; | 
|  | 52 | for (int i=0; i<index; i++) { | 
|  | 53 | char c = str.charAt(i); | 
|  | 54 | if (c == '\r' || (c == '\n' && prev != '\r')) { | 
|  | 55 | line++; | 
|  | 56 | } | 
|  | 57 | prev = c; | 
|  | 58 | } | 
|  | 59 | return new SourcePositionInfo(that.file, line, 0); | 
|  | 60 | } | 
|  | 61 |  | 
|  | 62 | public static SourcePositionInfo findBeginning(SourcePositionInfo that, String str) | 
|  | 63 | { | 
|  | 64 | if (that == null) { | 
|  | 65 | return null; | 
|  | 66 | } | 
|  | 67 | int line = that.line-1; // -1 because, well, it seems to work | 
|  | 68 | int prev = 0; | 
|  | 69 | for (int i=str.length()-1; i>=0; i--) { | 
|  | 70 | char c = str.charAt(i); | 
|  | 71 | if ((c == '\r' && prev != '\n') || (c == '\n')) { | 
|  | 72 | line--; | 
|  | 73 | } | 
|  | 74 | prev = c; | 
|  | 75 | } | 
|  | 76 | return new SourcePositionInfo(that.file, line, 0); | 
|  | 77 | } | 
|  | 78 |  | 
|  | 79 | public String toString() | 
|  | 80 | { | 
|  | 81 | return file + ':' + line; | 
|  | 82 | } | 
|  | 83 |  | 
|  | 84 | public int compareTo(Object o) { | 
|  | 85 | SourcePositionInfo that = (SourcePositionInfo)o; | 
|  | 86 | int r = this.file.compareTo(that.file); | 
|  | 87 | if (r != 0) return r; | 
|  | 88 | return this.line - that.line; | 
|  | 89 | } | 
|  | 90 |  | 
|  | 91 | public String file; | 
|  | 92 | public int line; | 
|  | 93 | public int column; | 
|  | 94 | } |