• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 package com.android.apicheck;
18 
19 import java.lang.Comparable;
20 
21 public class SourcePositionInfo implements Comparable
22 {
SourcePositionInfo()23     public SourcePositionInfo() {
24         this.file = "<unknown>";
25         this.line = 0;
26         this.column = 0;
27     }
28 
SourcePositionInfo(String file, int line, int column)29     public SourcePositionInfo(String file, int line, int column)
30     {
31         this.file = file;
32         this.line = line;
33         this.column = column;
34     }
35 
SourcePositionInfo(SourcePositionInfo that)36     public SourcePositionInfo(SourcePositionInfo that)
37     {
38         this.file = that.file;
39         this.line = that.line;
40         this.column = that.column;
41     }
42 
43     /**
44      * Given this position and str which occurs at that position, as well as str an index into str,
45      * find the SourcePositionInfo.
46      *
47      * @throw StringIndexOutOfBoundsException if index &gt; str.length()
48      */
add(SourcePositionInfo that, String str, int index)49     public static SourcePositionInfo add(SourcePositionInfo that, String str, int index)
50     {
51         if (that == null) {
52             return null;
53         }
54         int line = that.line;
55         char prev = 0;
56         for (int i=0; i<index; i++) {
57             char c = str.charAt(i);
58             if (c == '\r' || (c == '\n' && prev != '\r')) {
59                 line++;
60             }
61             prev = c;
62         }
63         return new SourcePositionInfo(that.file, line, 0);
64     }
65 
findBeginning(SourcePositionInfo that, String str)66     public static SourcePositionInfo findBeginning(SourcePositionInfo that, String str)
67     {
68         if (that == null) {
69             return null;
70         }
71         int line = that.line-1; // -1 because, well, it seems to work
72         int prev = 0;
73         for (int i=str.length()-1; i>=0; i--) {
74             char c = str.charAt(i);
75             if ((c == '\r' && prev != '\n') || (c == '\n')) {
76                 line--;
77             }
78             prev = c;
79         }
80         return new SourcePositionInfo(that.file, line, 0);
81     }
82 
83     @Override
toString()84     public String toString()
85     {
86         if (this.file == null) {
87             return "(unknown)";
88         } else {
89             if (this.line == 0) {
90                 return this.file + ':';
91             } else {
92                 return this.file + ':' + this.line + ':';
93             }
94         }
95     }
96 
compareTo(Object o)97     public int compareTo(Object o) {
98         SourcePositionInfo that = (SourcePositionInfo)o;
99         int r = this.file.compareTo(that.file);
100         if (r != 0) return r;
101         return this.line - that.line;
102     }
103 
104     /**
105      * Build a SourcePositionInfo from the XML source= notation
106      */
fromXml(String source)107     public static SourcePositionInfo fromXml(String source) {
108         if (source != null) {
109             for (int i = 0; i < source.length(); i++) {
110                 if (source.charAt(i) == ':') {
111                     return new SourcePositionInfo(source.substring(0, i),
112                             Integer.parseInt(source.substring(i+1)), 0);
113                 }
114             }
115         }
116 
117         return new SourcePositionInfo("(unknown)", 0, 0);
118     }
119 
120     public String file;
121     public int line;
122     public int column;
123 }
124