Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/org/opensolaris/opengrok/analysis/AnalyzerGuru.java
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,9 @@ public void populateDocument(Document doc, File file, String path,
));
}
fa.analyze(doc, StreamSource.fromFile(file), xrefOut);

String fileType = fa.getFileTypeName();
doc.add(new StringField(QueryBuilder.FILETYPE, fileType, Store.YES));
}
}

Expand Down
17 changes: 17 additions & 0 deletions src/org/opensolaris/opengrok/analysis/FileAnalyzer.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,23 @@ public FileAnalyzer(FileAnalyzerFactory factory) {
this.factory = factory;

}

/**
* Returns the normalized name of the analyzer,
* which should corresponds to the file type.
* Example: The analyzer for the C language (CAnalyzer) would return “c”.
* @return Normalized name of the analyzer.
*/
public String getFileTypeName() {
String name = this.getClass().getSimpleName().toLowerCase();
String suffix = "analyzer";

if (name.endsWith(suffix)) {
return name.substring(0, name.length() - suffix.length());
}

return name.toLowerCase();
}

/**
* Analyze the contents of a source file. This includes populating the
Expand Down
20 changes: 20 additions & 0 deletions src/org/opensolaris/opengrok/search/QueryBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public class QueryBuilder {
public static final String REFS = "refs";
public static final String PATH = "path";
public static final String HIST = "hist";
public static final String FILETYPE = "fileType";
/**
* Fields we use in lucene internal ones
*/
Expand Down Expand Up @@ -160,6 +161,25 @@ public QueryBuilder setHist(String hist) {
public String getHist() {
return getQueryText(HIST);
}

/**
* Set search string for the "fileType" field.
*
* @param fileType query string to set
* @return this instance
*/
public QueryBuilder setFileType(String fileType) {
return addQueryText(FILETYPE, fileType);
}

/**
* Get search string for the "fileType" field.
*
* @return {@code null} if not set, the query string otherwise.
*/
public String getFileType() {
return getQueryText(FILETYPE);
}

/**
* Get a map containing the query text for each of the fields that have been
Expand Down
10 changes: 7 additions & 3 deletions src/org/opensolaris/opengrok/search/Search.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,14 @@
@SuppressWarnings({"PMD.AvoidPrintStackTrace", "PMD.SystemPrintln"})
final class Search {

private static final String usage = "USAGE: Search -R <configuration.xml> [-d | -r | -p | -h | -f] 'query string' ..\n" +
private static final String usage = "USAGE: Search -R <configuration.xml> [-d | -r | -p | -h | -f | -t] 'query string' ..\n" +
"\t -R <configuration.xml> Read configuration from the specified file\n" +
"\t -d Symbol Definitions\n" +
"\t -r Symbol References\n" +
"\t -p Path\n" +
"\t -h History\n" +
"\t -f Full text";
"\t -f Full text\n" +
"\t -t File Type";

private SearchEngine engine;
final List<Hit> results = new ArrayList<Hit>();
Expand All @@ -52,7 +53,7 @@ final class Search {
@SuppressWarnings({"PMD.SwitchStmtsShouldHaveDefault"})
protected boolean parseCmdLine(String[] argv) {
engine = new SearchEngine();
Getopt getopt = new Getopt(argv, "R:d:r:p:h:f:");
Getopt getopt = new Getopt(argv, "R:d:r:p:h:f:t:");
try {
getopt.parse();
} catch (Exception e) {
Expand Down Expand Up @@ -88,6 +89,9 @@ protected boolean parseCmdLine(String[] argv) {
case 'f':
engine.setFreetext(getopt.getOptarg());
break;
case 't':
engine.setFileType(getopt.getOptarg());
break;
}
}

Expand Down
25 changes: 24 additions & 1 deletion src/org/opensolaris/opengrok/search/SearchEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ public class SearchEngine {
* Holds value of property symbol.
*/
private String symbol;
/**
* Holds value of property fileType
*/
private String fileType;
/**
* Holds value of property indexDatabase.
*/
Expand Down Expand Up @@ -136,7 +140,8 @@ private QueryBuilder createQueryBuilder() {
.setDefs(definition)
.setRefs(symbol)
.setPath(file)
.setHist(history);
.setHist(history)
.setFileType(fileType);
}

public boolean isValidQuery() {
Expand Down Expand Up @@ -488,4 +493,22 @@ public String getSymbol() {
public void setSymbol(String symbol) {
this.symbol = symbol;
}

/**
* Getter for property fileType.
*
* @return Value of property fileType.
*/
public String getFileType() {
return this.fileType;
}

/**
* Setter for property fileType.
*
* @param fileType New value of property fileType.
*/
public void setFileType(String fileType) {
this.fileType = fileType;
}
}
7 changes: 6 additions & 1 deletion src/org/opensolaris/opengrok/web/PageConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,12 @@ public List<SortOrder> getSortOrder() {
*/
public QueryBuilder getQueryBuilder() {
if (queryBuilder == null) {
queryBuilder = new QueryBuilder().setFreetext(req.getParameter("q")).setDefs(req.getParameter("defs")).setRefs(req.getParameter("refs")).setPath(req.getParameter("path")).setHist(req.getParameter("hist"));
queryBuilder = new QueryBuilder().setFreetext(req.getParameter("q"))
.setDefs(req.getParameter("defs"))
.setRefs(req.getParameter("refs"))
.setPath(req.getParameter("path"))
.setHist(req.getParameter("hist"))
.setFileType(req.getParameter("type"));

// This is for backward compatibility with links created by OpenGrok
// 0.8.x and earlier. We used to concatenate the entire query into a
Expand Down
44 changes: 44 additions & 0 deletions src/org/opensolaris/opengrok/web/SearchHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.logging.Level;
Expand Down Expand Up @@ -161,13 +164,54 @@ public class SearchHelper {
* history context usually created via {@link #prepareSummary()}.
*/
public HistoryContext historyContext;
/**
* User readable description for file types.
* Only those listed in fileTypeDescription will be shown
* to the user.
*/
private static final Map<String, String> fileTypeDescription;
/**
* Default query parse error message prefix
*/
public static final String PARSE_ERROR_MSG = "Unable to parse your query: ";
private ExecutorService executor = null;
private static final Logger log = Logger.getLogger(SearchHelper.class.getName());

static {
fileTypeDescription = new TreeMap<>();

fileTypeDescription.put("xml", "XML");
fileTypeDescription.put("troff", "Troff");
fileTypeDescription.put("elf", "ELF");
fileTypeDescription.put("javaclass", "Java class");
fileTypeDescription.put("image", "Image file");
fileTypeDescription.put("c", "C");
fileTypeDescription.put("csharp", "C#");
fileTypeDescription.put("vb", "Visual Basic");
fileTypeDescription.put("cxx", "C++");
fileTypeDescription.put("sh", "Shell script");
fileTypeDescription.put("java", "Java");
fileTypeDescription.put("javascript", "JavaScript");
fileTypeDescription.put("python", "Python");
fileTypeDescription.put("perl", "Perl");
fileTypeDescription.put("php", "PHP");
fileTypeDescription.put("lisp", "Lisp");
fileTypeDescription.put("tcl", "Tcl");
fileTypeDescription.put("scala", "Scala");
fileTypeDescription.put("sql", "SQL");
fileTypeDescription.put("plsql", "PL/SQL");
fileTypeDescription.put("fortran", "Fortran");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could above be autogenerated from analyzers?
just want to save steps when someone adds new analyzer
obviously we can accept it like this, but long term above needs to be dynamic to save footprint of changes when adding new analyzer

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see how those could be autogenerated, “csharp” and “plsql” are good examples.

What about a new method getFileTypeDescription in FileAnalyzer, that, by default, returns the file type (fortran => Fortran, etc.) and can be overridden to give the proper string? Then getFileTypeDescirptions would iterate over all registered analyzers to give the list.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, exactly how I meant it
you can also return array of tuples, which would fix the csharp and plsql problem
anyways, if you agree with OCA, I will integrate this patch and we can merge a fix for above in a new pull

}

/**
* Returns a set of file type descriptions to be used for a
* search form.
* @return Set of tuples with file type and description.
*/
public static Set<Map.Entry<String, String>> getFileTypeDescirptions() {
return fileTypeDescription.entrySet();
}

/**
* Create the searcher to use wrt. to currently set parameters and the given
* projects. Does not produce any {@link #redirect} link. It also does
Expand Down
22 changes: 21 additions & 1 deletion web/menu.jspf
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ Use is subject to license terms.

Portions Copyright 2011 Jens Elkner.

--%><%@page import="
--%><%@page import="org.opensolaris.opengrok.web.SearchHelper"%>
<%@page import="java.util.Map"%>
<%@page import="
java.util.SortedSet,
java.util.TreeMap,
java.util.Map.Entry,
Expand Down Expand Up @@ -101,6 +103,24 @@ org.opensolaris.opengrok.web.Util"
<td><input class="q" tabindex="5" name="hist" id="hist" value="<%=
Util.formQuoteEscape(queryParams.getHist()) %>"/></td>
</tr>
<tr>
<td><label for="s5">Type</label></td>
<td><select class="q" tabindex="6" name="type" id="type"><%
String selection = queryParams.getFileType();
%>
<option value="">Any</option><%
for (Map.Entry<String, String> d : SearchHelper.getFileTypeDescirptions()) {
%>
<option value="<%= d.getKey() %>"<%
if (d.getKey().equals(selection)) {
%> selected="selected"<%
}
%>><%= Util.formQuoteEscape(d.getValue()) %></option><%
}
%>
</select>
</td>
</tr>
<%-- TODO Bug 11749
<%
if (projects.size() != 0) {
Expand Down
1 change: 1 addition & 0 deletions web/search.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ include file="projects.jspf"
Util.appendQuery(url, "refs", qb.getRefs());
Util.appendQuery(url, "path", qb.getPath());
Util.appendQuery(url, "hist", qb.getHist());
Util.appendQuery(url, "type", qb.getFileType());
}
if (sh.projects != null && sh.projects.size() != 0) {
Util.appendQuery(url, "project", cfg.getRequestedProjectsAsString());
Expand Down