Search in sources :

Example 1 with DirectoryEntry

use of org.opengrok.indexer.search.DirectoryEntry in project OpenGrok by OpenGrok.

the class FileExtraZipper method zip.

/**
 * Merge the specified lists by looking up a possible entry in
 * {@code extras} for every element in {@code files}.
 * @param dir the files' directory
 * @param files the file names
 * @param extras some OpenGrok-analyzed extra metadata
 * @return a list of the same size as {@code files}
 */
public List<DirectoryEntry> zip(File dir, List<String> files, List<NullableNumLinesLOC> extras) {
    if (extras == null) {
        return files.stream().map(f -> new DirectoryEntry(new File(dir, f))).collect(Collectors.toList());
    }
    Map<String, NullableNumLinesLOC> byName = indexExtraByName(extras);
    List<DirectoryEntry> result = new ArrayList<>(files.size());
    for (String file : files) {
        File fileobj = new File(dir, file);
        NullableNumLinesLOC extra = findExtra(byName, fileobj);
        DirectoryEntry entry = new DirectoryEntry(fileobj, extra);
        result.add(entry);
    }
    return result;
}
Also used : List(java.util.List) Map(java.util.Map) NullableNumLinesLOC(org.opengrok.indexer.analysis.NullableNumLinesLOC) HashMap(java.util.HashMap) Collectors(java.util.stream.Collectors) DirectoryEntry(org.opengrok.indexer.search.DirectoryEntry) File(java.io.File) ArrayList(java.util.ArrayList) NullableNumLinesLOC(org.opengrok.indexer.analysis.NullableNumLinesLOC) ArrayList(java.util.ArrayList) DirectoryEntry(org.opengrok.indexer.search.DirectoryEntry) File(java.io.File)

Example 2 with DirectoryEntry

use of org.opengrok.indexer.search.DirectoryEntry in project OpenGrok by OpenGrok.

the class DirectoryListingTest method directoryListingWithEftarException.

@Test
public void directoryListingWithEftarException() throws IOException, HistoryException {
    EftarFileReader mockReader = mock(EftarFileReader.class);
    when(mockReader.getNode(anyString())).thenThrow(IOException.class);
    DirectoryListing instance = new DirectoryListing(mockReader);
    File file = new File(directory, "foo");
    StringWriter mockWriter = spy(StringWriter.class);
    instance.extraListTo("ctx", directory, mockWriter, directory.getPath(), Collections.singletonList(new DirectoryEntry(file)));
    verify(mockWriter, atLeast(20)).write(anyString());
}
Also used : StringWriter(java.io.StringWriter) DirectoryEntry(org.opengrok.indexer.search.DirectoryEntry) File(java.io.File) EftarFileReader(org.opengrok.indexer.web.EftarFileReader) Test(org.junit.jupiter.api.Test)

Example 3 with DirectoryEntry

use of org.opengrok.indexer.search.DirectoryEntry in project OpenGrok by OpenGrok.

the class DirectoryListing method extraListTo.

/**
 * Write a HTML-ized listing of the given directory to the given destination.
 *
 * @param contextPath path used for link prefixes
 * @param dir the directory to list
 * @param out write destination
 * @param path virtual path of the directory (usually the path name of
 *  <var>dir</var> with the source root directory stripped off).
 * @param entries basenames of potential children of the directory to list,
 *  but filtered by {@link PathAccepter}.
 * @return a possible empty list of README files included in the written
 *  listing.
 * @throws IOException when cannot write to the {@code out} parameter
 * @throws HistoryException when failed to get last modified time for files in directory
 */
public List<String> extraListTo(String contextPath, File dir, Writer out, String path, List<DirectoryEntry> entries) throws IOException, HistoryException {
    // TODO this belongs to a jsp, not here
    ArrayList<String> readMes = new ArrayList<>();
    int offset = -1;
    EftarFileReader.FNode parentFNode = null;
    if (desc != null) {
        try {
            parentFNode = desc.getNode(path);
        } catch (IOException e) {
            LOGGER.log(Level.WARNING, String.format("cannot get Eftar node for path ''%s''", path), e);
        }
        if (parentFNode != null) {
            offset = parentFNode.getChildOffset();
        }
    }
    out.write("<table id=\"dirlist\" class=\"tablesorter tablesorter-default\">\n");
    out.write("<thead>\n");
    out.write("<tr>\n");
    out.write("<th class=\"sorter-false\"></th>\n");
    out.write("<th>Name</th>\n");
    out.write("<th class=\"sorter-false\"></th>\n");
    out.write("<th class=\"sort-dates\">Date</th>\n");
    out.write("<th class=\"sort-groksizes\">Size</th>\n");
    out.write("<th>#Lines</th>\n");
    out.write("<th>LOC</th>\n");
    if (offset > 0) {
        out.write("<th><samp>Description</samp></th>\n");
    }
    out.write("</tr>\n</thead>\n<tbody>\n");
    PathAccepter pathAccepter = RuntimeEnvironment.getInstance().getPathAccepter();
    Format dateFormatter = new SimpleDateFormat("dd-MMM-yyyy", Locale.getDefault());
    // Print the '..' entry even for empty directories.
    if (path.length() != 0) {
        out.write("<tr><td><p class=\"'r'\"/></td><td>");
        out.write("<b><a href=\"..\">..</a></b></td><td></td>");
        printDateSize(out, dir.getParentFile(), null, dateFormatter);
        out.write("</tr>\n");
    }
    Map<String, Date> modTimes = HistoryGuru.getInstance().getLastModifiedTimes(dir);
    if (entries != null) {
        for (DirectoryEntry entry : entries) {
            File child = entry.getFile();
            if (!pathAccepter.accept(child)) {
                continue;
            }
            String filename = child.getName();
            String filenameLower = filename.toLowerCase(Locale.ROOT);
            if (filenameLower.startsWith("readme") || filenameLower.endsWith("readme")) {
                readMes.add(filename);
            }
            boolean isDir = child.isDirectory();
            out.write("<tr><td>");
            out.write("<p class=\"");
            out.write(isDir ? 'r' : 'p');
            out.write("\"/>");
            out.write("</td><td><a href=\"");
            if (isDir) {
                String longpath = getSimplifiedPath(child);
                out.write(Util.uriEncodePath(longpath));
                out.write("/\"><b>");
                int idx;
                if ((idx = longpath.lastIndexOf('/')) > 0) {
                    out.write("<span class=\"simplified-path\">");
                    out.write(longpath.substring(0, idx + 1));
                    out.write("</span>");
                    out.write(longpath.substring(idx + 1));
                } else {
                    out.write(longpath);
                }
                out.write("</b></a>/");
            } else {
                out.write(Util.uriEncodePath(filename));
                out.write("\">");
                out.write(filename);
                out.write("</a>");
            }
            out.write("</td>");
            Util.writeHAD(out, contextPath, path + filename, isDir);
            printDateSize(out, child, modTimes.get(filename), dateFormatter);
            printNumlines(out, entry, isDir);
            printLoc(out, entry, isDir);
            if (offset > 0) {
                String briefDesc = desc.getChildTag(parentFNode, filename);
                if (briefDesc == null) {
                    out.write("<td/>");
                } else {
                    out.write("<td>");
                    out.write(briefDesc);
                    out.write("</td>");
                }
            }
            out.write("</tr>\n");
        }
    }
    out.write("</tbody>\n</table>");
    return readMes;
}
Also used : ArrayList(java.util.ArrayList) IOException(java.io.IOException) DirectoryEntry(org.opengrok.indexer.search.DirectoryEntry) EftarFileReader(org.opengrok.indexer.web.EftarFileReader) Date(java.util.Date) PathAccepter(org.opengrok.indexer.configuration.PathAccepter) Format(java.text.Format) SimpleDateFormat(java.text.SimpleDateFormat) SimpleDateFormat(java.text.SimpleDateFormat) File(java.io.File)

Aggregations

File (java.io.File)3 DirectoryEntry (org.opengrok.indexer.search.DirectoryEntry)3 ArrayList (java.util.ArrayList)2 EftarFileReader (org.opengrok.indexer.web.EftarFileReader)2 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 Format (java.text.Format)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Collectors (java.util.stream.Collectors)1 Test (org.junit.jupiter.api.Test)1 NullableNumLinesLOC (org.opengrok.indexer.analysis.NullableNumLinesLOC)1 PathAccepter (org.opengrok.indexer.configuration.PathAccepter)1