xref: /OpenGrok/opengrok-web/src/main/webapp/xref.jspf (revision d6df19e1b22784c78f567cf74c42f18e3901b900)
1<%--
2CDDL HEADER START
3
4The contents of this file are subject to the terms of the
5Common Development and Distribution License (the "License").
6You may not use this file except in compliance with the License.
7
8See LICENSE.txt included in this distribution for the specific
9language governing permissions and limitations under the License.
10
11When distributing Covered Code, include this CDDL HEADER in each
12file and include the License file at LICENSE.txt.
13If applicable, add the following below this CDDL HEADER, with the
14fields enclosed by brackets "[]" replaced with your own identifying
15information: Portions Copyright [yyyy] [name of copyright owner]
16
17CDDL HEADER END
18
19Copyright (c) 2015, 2021, Oracle and/or its affiliates. All rights reserved.
20Portions Copyright 2011 Jens Elkner.
21Portions Copyright (c) 2017-2020, Chris Fraire <cfraire@me.com>.
22--%>
23<%@page errorPage="error.jsp" import="
24java.io.BufferedInputStream,
25java.io.File,
26java.io.FileInputStream,
27java.io.InputStream,
28java.io.InputStreamReader,
29java.io.IOException,
30java.nio.charset.StandardCharsets,
31java.util.logging.Level,
32org.opengrok.indexer.analysis.AnalyzerGuru,
33org.opengrok.indexer.analysis.Ctags,
34org.opengrok.indexer.analysis.Definitions,
35org.opengrok.indexer.analysis.AbstractAnalyzer,
36org.opengrok.indexer.analysis.AbstractAnalyzer.Genre,
37org.opengrok.indexer.analysis.AnalyzerFactory,
38org.opengrok.indexer.history.Annotation,
39org.opengrok.indexer.history.HistoryGuru,
40org.opengrok.indexer.util.ObjectPool,
41org.opengrok.indexer.util.IOUtils,
42org.opengrok.indexer.web.QueryParameters"
43%>
44<%@ page import="org.opengrok.indexer.web.Util" %>
45<%@ page import="static org.opengrok.web.PageConfig.DUMMY_REVISION" %>
46<%
47    /* ---------------------- xref.jspf start --------------------- */
48{
49    // requesting a previous revision or needed to generate xref on the fly
50    // (either economy mode is enabled or the cfg.findDataFile() call failed).
51    AnalyzerFactory a = AnalyzerGuru.find(basename);
52    Genre g = AnalyzerGuru.getGenre(a);
53    String error = null;
54
55    if (g == Genre.PLAIN || g == Genre.HTML || g == null) {
56        InputStream in = null;
57        File tempf = null;
58        try {
59            if (rev.isBlank() || rev.equals(DUMMY_REVISION)) {
60                in = new BufferedInputStream(new FileInputStream(resourceFile));
61            } else {
62                tempf = File.createTempFile("ogtags", basename);
63                if (HistoryGuru.getInstance().getRevision(tempf, resourceFile.getParent(), basename, rev)) {
64                    in = new BufferedInputStream(new FileInputStream(tempf));
65                } else {
66                    tempf.delete();
67                    tempf = null;
68                }
69            }
70        } catch (Exception e) {
71            // fall through to error message
72            error = e.getMessage();
73            if (tempf != null) {
74                tempf.delete();
75                tempf = null;
76            }
77        }
78
79        if (in != null) {
80            try {
81                if (g == null) {
82                    a = AnalyzerGuru.find(in, basename);
83                    g = AnalyzerGuru.getGenre(a);
84                }
85                if (g == AbstractAnalyzer.Genre.DATA || g == AbstractAnalyzer.Genre.XREFABLE || g == null) {
86%>
87<div id="src">
88    Download binary file, <a href="<%= rawPath %>?<%= QueryParameters.REVISION_PARAM_EQ %>
89<%= Util.uriEncode(rev) %>"><%= basename %></a>
90</div><%
91                } else {
92%>
93<div id="src">
94        <pre><%
95                    if (g == AbstractAnalyzer.Genre.PLAIN) {
96                        Definitions defs = null;
97                        ObjectPool<Ctags> ctagsPool = cfg.getEnv().getIndexerParallelizer().getCtagsPool();
98                        int tries = 2;
99                        while (cfg.getEnv().isWebappCtags()) {
100                            Ctags ctags = ctagsPool.get();
101                            try {
102                                ctags.setTabSize(project != null ?
103                                project.getTabSize() : 0);
104                                defs = ctags.doCtags(tempf.getPath());
105                                break;
106                            } catch (InterruptedException ex) {
107                                if (--tries > 0) {
108                                    LOGGER.log(Level.WARNING, "doCtags() interrupted--{0}",
109                                    ex.getMessage());
110                                    continue;
111                                }
112                                LOGGER.log(Level.WARNING, "doCtags()", ex);
113                                break;
114                            } catch (Exception ex) {
115                                LOGGER.log(Level.WARNING, "doCtags()", ex);
116                                break;
117                            } finally {
118                                ctags.reset();
119                                ctagsPool.release(ctags);
120                            }
121                        }
122                        Annotation annotation = cfg.getAnnotation();
123                        //not needed yet
124                        //annotation.writeTooltipMap(out);
125                        // The data under source root is read with UTF-8 as a default.
126                        r = IOUtils.createBOMStrippedReader(in, StandardCharsets.UTF_8.name());
127                        AnalyzerGuru.writeDumpedXref(request.getContextPath(),
128                            a, r, out,
129                            defs, annotation, project);
130                    } else if (g == AbstractAnalyzer.Genre.IMAGE) {
131        %></pre>
132    <img src="<%= rawPath %>?<%= QueryParameters.REVISION_PARAM_EQ %><%= Util.uriEncode(rev) %>" alt="Image from Source Repository"/>
133    <pre><%
134                    } else if (g == AbstractAnalyzer.Genre.HTML) {
135                        /*
136                         * For backward compatibility, read the
137                         * OpenGrok-produced document using the system
138                         * default charset.
139                         */
140                        r = new InputStreamReader(in);
141                        /*
142                         * dumpXref() is also useful here for
143                         * translating links.
144                         */
145                        Util.dumpXref(out, r, request.getContextPath());
146                    } else {
147    %>Download binary file, <a href="<%= rawPath %>?<%= QueryParameters.REVISION_PARAM_EQ %>
148<%= Util.uriEncode(rev) %>"><%= basename %></a><%
149                    }
150                }
151            } catch (IOException e) {
152                LOGGER.log(Level.SEVERE, "Failed xref on-the-fly", e);
153            } finally {
154                if (r != null) {
155                    IOUtils.close(r);
156                    in = null;
157                }
158                if (in != null) {
159                    IOUtils.close(in);
160                    in = null;
161                }
162                if (tempf != null) {
163                    tempf.delete();
164                }
165            }
166    %></pre>
167</div><%
168        } else {
169%>
170<h3 class="error">Error reading file</h3><%
171            if (error != null) {
172%>
173<p class="error"><%= error %></p><%
174            }
175        }
176    } else if (g == AbstractAnalyzer.Genre.IMAGE) {
177%>
178<div id="src">
179    <img src="<%= rawPath %>?<%= QueryParameters.REVISION_PARAM_EQ %><%= Util.uriEncode(rev) %>"
180         alt="Image from Source Repository"/>
181</div><%
182    } else {
183%>
184<div id="src">
185    Download binary file, <a href="<%= rawPath %>?<%= QueryParameters.REVISION_PARAM_EQ %>
186<%= Util.uriEncode(rev) %>"><%= basename %></a>
187</div><%
188    }
189}
190/* ---------------------- xref.jspf end --------------------- */
191%>
192