Home | History | Annotate | Download | only in doclava
      1 /*
      2  * Copyright (C) 2010 Google Inc.
      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.google.doclava;
     18 
     19 public class SourcePositionInfo implements Comparable {
     20   public static final SourcePositionInfo UNKNOWN = new SourcePositionInfo("(unknown)", 0, 0);
     21 
     22   public SourcePositionInfo(String file, int line, int column) {
     23     this.file = file;
     24     this.line = line;
     25     this.column = column;
     26   }
     27 
     28   public SourcePositionInfo(SourcePositionInfo that) {
     29     this.file = that.file;
     30     this.line = that.line;
     31     this.column = that.column;
     32   }
     33 
     34   /**
     35    * Given this position and str which occurs at that position, as well as str an index into str,
     36    * find the SourcePositionInfo.
     37    *
     38    * @throw StringIndexOutOfBoundsException if index > str.length()
     39    */
     40   public static SourcePositionInfo add(SourcePositionInfo that, String str, int index) {
     41     if (that == null) {
     42       return null;
     43     }
     44     int line = that.line;
     45     char prev = 0;
     46     for (int i = 0; i < index; i++) {
     47       char c = str.charAt(i);
     48       if (c == '\r' || (c == '\n' && prev != '\r')) {
     49         line++;
     50       }
     51       prev = c;
     52     }
     53     return new SourcePositionInfo(that.file, line, 0);
     54   }
     55 
     56   public static SourcePositionInfo findBeginning(SourcePositionInfo that, String str) {
     57     if (that == null) {
     58       return null;
     59     }
     60     int line = that.line - 1; // -1 because, well, it seems to work
     61     int prev = 0;
     62     for (int i = str.length() - 1; i >= 0; i--) {
     63       char c = str.charAt(i);
     64       if ((c == '\r' && prev != '\n') || (c == '\n')) {
     65         line--;
     66       }
     67       prev = c;
     68     }
     69     return new SourcePositionInfo(that.file, line, 0);
     70   }
     71 
     72   @Override
     73   public String toString() {
     74     return file + ':' + line;
     75   }
     76 
     77   public int compareTo(Object o) {
     78     SourcePositionInfo that = (SourcePositionInfo) o;
     79     int r = this.file.compareTo(that.file);
     80     if (r != 0) return r;
     81     return this.line - that.line;
     82   }
     83 
     84   /**
     85    * Build a SourcePositionInfo from the XML source= notation
     86    */
     87   public static SourcePositionInfo fromXml(String source) {
     88     if (source != null) {
     89       for (int i = 0; i < source.length(); i++) {
     90         if (source.charAt(i) == ':') {
     91           return new SourcePositionInfo(source.substring(0, i), Integer.parseInt(source
     92               .substring(i + 1)), 0);
     93         }
     94       }
     95     }
     96 
     97     return UNKNOWN;
     98   }
     99 
    100   public String file;
    101   public int line;
    102   public int column;
    103 }
    104