langtools/src/jdk.javadoc/share/classes/com/sun/tools/javadoc/SourcePositionImpl.java
changeset 37938 42baa89d2156
parent 37858 7c04fcb12bd4
child 37939 3eb8c2a89b77
equal deleted inserted replaced
37858:7c04fcb12bd4 37938:42baa89d2156
     1 /*
       
     2  * Copyright (c) 2001, 2015, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 
       
    26 package com.sun.tools.javadoc;
       
    27 
       
    28 import java.io.File;
       
    29 import javax.tools.FileObject;
       
    30 
       
    31 import com.sun.javadoc.SourcePosition;
       
    32 import com.sun.tools.javac.util.Position;
       
    33 
       
    34 /**
       
    35  * A source position: filename, line number, and column number.
       
    36  *
       
    37  *  <p><b>This is NOT part of any supported API.
       
    38  *  If you write code that depends on this, you do so at your own risk.
       
    39  *  This code and its internal interfaces are subject to change or
       
    40  *  deletion without notice.</b>
       
    41  *
       
    42  * @since J2SE1.4
       
    43  * @author Neal M Gafter
       
    44  * @author Michael Van De Vanter (position representation changed to char offsets)
       
    45  */
       
    46 public class SourcePositionImpl implements SourcePosition {
       
    47     FileObject filename;
       
    48     int position;
       
    49     Position.LineMap lineMap;
       
    50 
       
    51     /** The source file. Returns null if no file information is
       
    52      *  available. */
       
    53     public File file() {
       
    54         return (filename == null) ? null : new File(filename.getName());
       
    55     }
       
    56 
       
    57     /** The source file. Returns null if no file information is
       
    58      *  available. */
       
    59     public FileObject fileObject() {
       
    60         return filename;
       
    61     }
       
    62 
       
    63     /** The line in the source file. The first line is numbered 1;
       
    64      *  0 means no line number information is available. */
       
    65     public int line() {
       
    66         if (lineMap == null) {
       
    67             return 0;
       
    68         } else {
       
    69             return lineMap.getLineNumber(position);
       
    70         }
       
    71     }
       
    72 
       
    73     /** The column in the source file. The first column is
       
    74      *  numbered 1; 0 means no column information is available.
       
    75      *  Columns count characters in the input stream; a tab
       
    76      *  advances the column number to the next 8-column tab stop.
       
    77      */
       
    78     public int column() {
       
    79         if (lineMap == null) {
       
    80             return 0;
       
    81         }else {
       
    82             return lineMap.getColumnNumber(position);
       
    83         }
       
    84     }
       
    85 
       
    86     private SourcePositionImpl(FileObject file, int position,
       
    87                                Position.LineMap lineMap) {
       
    88         super();
       
    89         this.filename = file;
       
    90         this.position = position;
       
    91         this.lineMap = lineMap;
       
    92     }
       
    93 
       
    94     public static SourcePosition make(FileObject file, int pos,
       
    95                                       Position.LineMap lineMap) {
       
    96         if (file == null) return null;
       
    97         return new SourcePositionImpl(file, pos, lineMap);
       
    98     }
       
    99 
       
   100     public String toString() {
       
   101         // Backwards compatibility hack. ZipFileObjects use the format
       
   102         // zipfile(zipentry) but javadoc has been using zipfile/zipentry
       
   103         String fn = filename.getName();
       
   104         if (fn.endsWith(")")) {
       
   105             int paren = fn.lastIndexOf("(");
       
   106             if (paren != -1) {
       
   107                 int i = paren+1;
       
   108                 if (fn.charAt(i) == '/')
       
   109                     i++;
       
   110                 fn = fn.substring(0, paren)
       
   111                         + File.separatorChar
       
   112                         + fn.substring(i, fn.length() - 1);
       
   113             }
       
   114         }
       
   115 
       
   116         if (position == Position.NOPOS)
       
   117             return fn;
       
   118         else
       
   119             return fn + ":" + line();
       
   120     }
       
   121 }