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
26package com.sun.tools.javadoc.main;
27
28import java.io.File;
29import javax.tools.FileObject;
30
31import com.sun.javadoc.SourcePosition;
32import 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@Deprecated
47public class SourcePositionImpl implements SourcePosition {
48    FileObject filename;
49    int position;
50    Position.LineMap lineMap;
51
52    /** The source file. Returns null if no file information is
53     *  available. */
54    public File file() {
55        return (filename == null) ? null : new File(filename.getName());
56    }
57
58    /** The source file. Returns null if no file information is
59     *  available. */
60    public FileObject fileObject() {
61        return filename;
62    }
63
64    /** The line in the source file. The first line is numbered 1;
65     *  0 means no line number information is available. */
66    public int line() {
67        if (lineMap == null) {
68            return 0;
69        } else {
70            return lineMap.getLineNumber(position);
71        }
72    }
73
74    /** The column in the source file. The first column is
75     *  numbered 1; 0 means no column information is available.
76     *  Columns count characters in the input stream; a tab
77     *  advances the column number to the next 8-column tab stop.
78     */
79    public int column() {
80        if (lineMap == null) {
81            return 0;
82        }else {
83            return lineMap.getColumnNumber(position);
84        }
85    }
86
87    private SourcePositionImpl(FileObject file, int position,
88                               Position.LineMap lineMap) {
89        super();
90        this.filename = file;
91        this.position = position;
92        this.lineMap = lineMap;
93    }
94
95    public static SourcePosition make(FileObject file, int pos,
96                                      Position.LineMap lineMap) {
97        if (file == null) return null;
98        return new SourcePositionImpl(file, pos, lineMap);
99    }
100
101    public String toString() {
102        // Backwards compatibility hack. ZipFileObjects use the format
103        // zipfile(zipentry) but javadoc has been using zipfile/zipentry
104        String fn = filename.getName();
105        if (fn.endsWith(")")) {
106            int paren = fn.lastIndexOf("(");
107            if (paren != -1) {
108                int i = paren+1;
109                if (fn.charAt(i) == '/')
110                    i++;
111                fn = fn.substring(0, paren)
112                        + File.separatorChar
113                        + fn.substring(i, fn.length() - 1);
114            }
115        }
116
117        if (position == Position.NOPOS)
118            return fn;
119        else
120            return fn + ":" + line();
121    }
122}
123