use of org.opensolaris.opengrok.egrok.model.Hit in project OpenGrok by OpenGrok.
the class Query method parseJSON.
private List<Hit> parseJSON(String json) throws ParseException {
JSONObject results = (JSONObject) new JSONParser().parse(json.toString());
JSONArray array = (JSONArray) results.get("results");
List<Hit> resultList = new ArrayList<>();
for (Object obj : array) {
JSONObject result = (JSONObject) obj;
Hit hit = new Hit(result);
if (hit.isValid()) {
resultList.add(hit);
}
}
return resultList;
}
use of org.opensolaris.opengrok.egrok.model.Hit in project OpenGrok by OpenGrok.
the class ResultsDialog method setResults.
public void setResults(List<Hit> results) {
final Map<String, HitContainer> roots = new HashMap<String, HitContainer>();
for (Hit hit : results) {
String key = hit.getDirectory() + "/" + hit.getFilename();
HitContainer container = roots.get(key);
if (container == null) {
container = new HitContainer(key);
roots.put(key, container);
}
container.add(hit);
}
viewer.setHits(roots);
if (resultsView != null) {
resultsView.setHits(roots);
}
Display.getDefault().asyncExec(new Runnable() {
@Override
public void run() {
if (!viewer.getControl().isDisposed()) {
viewer.refresh();
}
}
});
if (Activator.getDefault().getPreferenceStore().getBoolean(EGrokPreferencePage.WORKSPACE_MATCHES)) {
Runnable workspaceLocator = new Runnable() {
@Override
public void run() {
IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
final Map<HitContainer, HashMap<IProject, Integer>> potentialProjects = new HashMap<HitContainer, HashMap<IProject, Integer>>();
final Map<IProject, ArrayList<String>> locationSegments = new HashMap<IProject, ArrayList<String>>();
try {
workspaceRoot.accept(new IResourceVisitor() {
@Override
public boolean visit(IResource resource) throws CoreException {
if (resource instanceof IWorkspaceRoot) {
return true;
}
if (resource instanceof IProject) {
IProject project = (IProject) resource;
IPath location = project.getLocation();
for (String segment : location.segments()) {
ArrayList<String> segments = locationSegments.get(project);
if (segments == null) {
segments = new ArrayList<String>();
locationSegments.put(project, segments);
}
segments.add(segment);
}
}
return false;
}
});
} catch (CoreException e) {
e.printStackTrace();
}
Map<HitContainer, ArrayList<String>> hitcontainerSegments = new HashMap<HitContainer, ArrayList<String>>();
for (HitContainer hitcontainer : roots.values()) {
ArrayList<String> segments = new ArrayList<String>();
for (String segment : hitcontainer.getName().split("/")) {
segments.add(segment);
}
hitcontainerSegments.put(hitcontainer, segments);
}
for (IProject project : locationSegments.keySet()) {
ArrayList<String> segments = locationSegments.get(project);
int idx = 0;
for (String segment : segments) {
for (HitContainer container : hitcontainerSegments.keySet()) {
for (String containerPathSegment : hitcontainerSegments.get(container)) {
if (segment.equals(containerPathSegment)) {
HashMap<IProject, Integer> matches = potentialProjects.get(container);
if (matches == null) {
matches = new HashMap<IProject, Integer>();
potentialProjects.put(container, matches);
}
matches.put(project, idx);
}
}
}
idx++;
}
}
for (HitContainer container : potentialProjects.keySet()) {
String fullLocation = container.getName();
HashMap<IProject, Integer> matches = potentialProjects.get(container);
System.out.println(container.getName());
for (Entry<IProject, Integer> match : matches.entrySet()) {
IProject project = match.getKey();
Integer matchingLocation = match.getValue();
String matchingString = project.getLocation().segment(matchingLocation);
System.out.println("match: " + matchingString);
String local = fullLocation.substring(fullLocation.indexOf(matchingString) + matchingString.length());
System.out.println("local: " + local);
IResource member = project.findMember(local);
System.out.println("member: " + member);
if (member instanceof IFile) {
IFile file = (IFile) member;
container.setCorrespondingFile(file);
}
}
}
Display.getDefault().asyncExec(new Runnable() {
@Override
public void run() {
if (!viewer.getControl().isDisposed()) {
viewer.refresh();
}
if (resultsView != null) {
resultsView.refresh();
}
}
});
}
};
workspaceLocator.run();
}
}
use of org.opensolaris.opengrok.egrok.model.Hit in project OpenGrok by OpenGrok.
the class Query method run.
public void run(final ResultsDialog dialog) {
try {
String baseUrl = Activator.getDefault().getPreferenceStore().getString(EGrokPreferencePage.BASE_URL);
String userName = Activator.getDefault().getPreferenceStore().getString(EGrokPreferencePage.USERNAME);
String password = Activator.getDefault().getPreferenceStore().getString(EGrokPreferencePage.PASSWORD);
if (baseUrl == null || baseUrl.isEmpty()) {
throw new RuntimeException("Invalid base url, check your configuration!");
}
URL url = new URL(baseUrl + JSON_SUFFIX + "?freetext=" + URLEncoder.encode(freetext, "UTF-8"));
final HttpURLConnection conn = (HttpURLConnection) (baseUrl.startsWith("https") ? createHttpsUrlConnection(url) : url.openConnection());
if (userName != null && password != null && !userName.isEmpty() && !password.isEmpty()) {
String base64 = EncodingUtils.encodeBase64((userName + ":" + password).getBytes());
conn.setRequestProperty("Authorization", "Basic " + base64);
}
Runnable runnable = new Runnable() {
@Override
public void run() {
try {
conn.connect();
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String tmp = null;
StringBuffer buffer = new StringBuffer();
while ((tmp = br.readLine()) != null) {
buffer.append(tmp);
}
List<Hit> resultList = parseJSON(buffer.toString());
dialog.setResults(resultList);
} catch (Exception e) {
handleException(e);
}
}
};
new Thread(runnable).start();
dialog.open();
} catch (Exception e) {
handleException(e);
}
}
Aggregations