Search in sources :

Example 1 with Hit

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;
}
Also used : Hit(org.opensolaris.opengrok.egrok.model.Hit) JSONObject(org.json.simple.JSONObject) JSONArray(org.json.simple.JSONArray) ArrayList(java.util.ArrayList) JSONParser(org.json.simple.parser.JSONParser) JSONObject(org.json.simple.JSONObject)

Example 2 with Hit

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();
    }
}
Also used : IResourceVisitor(org.eclipse.core.resources.IResourceVisitor) IFile(org.eclipse.core.resources.IFile) IPath(org.eclipse.core.runtime.IPath) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) IProject(org.eclipse.core.resources.IProject) Point(org.eclipse.swt.graphics.Point) HitContainer(org.opensolaris.opengrok.egrok.model.HitContainer) Hit(org.opensolaris.opengrok.egrok.model.Hit) IWorkspaceRoot(org.eclipse.core.resources.IWorkspaceRoot) CoreException(org.eclipse.core.runtime.CoreException) IResource(org.eclipse.core.resources.IResource)

Example 3 with Hit

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);
    }
}
Also used : Hit(org.opensolaris.opengrok.egrok.model.Hit) HttpURLConnection(java.net.HttpURLConnection) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) URL(java.net.URL) ParseException(org.json.simple.parser.ParseException)

Aggregations

Hit (org.opensolaris.opengrok.egrok.model.Hit)3 ArrayList (java.util.ArrayList)2 BufferedReader (java.io.BufferedReader)1 InputStreamReader (java.io.InputStreamReader)1 HttpURLConnection (java.net.HttpURLConnection)1 URL (java.net.URL)1 HashMap (java.util.HashMap)1 IFile (org.eclipse.core.resources.IFile)1 IProject (org.eclipse.core.resources.IProject)1 IResource (org.eclipse.core.resources.IResource)1 IResourceVisitor (org.eclipse.core.resources.IResourceVisitor)1 IWorkspaceRoot (org.eclipse.core.resources.IWorkspaceRoot)1 CoreException (org.eclipse.core.runtime.CoreException)1 IPath (org.eclipse.core.runtime.IPath)1 Point (org.eclipse.swt.graphics.Point)1 JSONArray (org.json.simple.JSONArray)1 JSONObject (org.json.simple.JSONObject)1 JSONParser (org.json.simple.parser.JSONParser)1 ParseException (org.json.simple.parser.ParseException)1 HitContainer (org.opensolaris.opengrok.egrok.model.HitContainer)1