1.1 --- a/sandbox/attributed-index-web/UI.java Mon Dec 19 11:37:36 2016 +0100
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,423 +0,0 @@
1.4 -/*
1.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
1.6 - *
1.7 - * Copyright 2009-2010 Sun Microsystems, Inc. All rights reserved.
1.8 - *
1.9 - * The contents of this file are subject to the terms of either the GNU
1.10 - * General Public License Version 2 only ("GPL") or the Common
1.11 - * Development and Distribution License("CDDL") (collectively, the
1.12 - * "License"). You may not use this file except in compliance with the
1.13 - * License. You can obtain a copy of the License at
1.14 - * http://www.netbeans.org/cddl-gplv2.html
1.15 - * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
1.16 - * specific language governing permissions and limitations under the
1.17 - * License. When distributing the software, include this License Header
1.18 - * Notice in each file and include the License file at
1.19 - * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
1.20 - * particular file as subject to the "Classpath" exception as provided
1.21 - * by Sun in the GPL Version 2 section of the License file that
1.22 - * accompanied this code. If applicable, add the following below the
1.23 - * License Header, with the fields enclosed by brackets [] replaced by
1.24 - * your own identifying information:
1.25 - * "Portions Copyrighted [year] [name of copyright owner]"
1.26 - *
1.27 - * If you wish your version of this file to be governed by only the CDDL
1.28 - * or only the GPL Version 2, indicate your decision by adding
1.29 - * "[Contributor] elects to include this software in this distribution
1.30 - * under the [CDDL or GPL Version 2] license." If you do not indicate a
1.31 - * single choice of license, a recipient has the option to distribute
1.32 - * your version of this file under either the CDDL, the GPL Version 2 or
1.33 - * to extend the choice of license to its licensees as provided above.
1.34 - * However, if you add GPL Version 2 code and therefore, elected the GPL
1.35 - * Version 2 license, then the option applies only if the new code is
1.36 - * made subject to such option by the copyright holder.
1.37 - *
1.38 - * Contributor(s):
1.39 - *
1.40 - * Portions Copyrighted 2009-2010 Sun Microsystems, Inc.
1.41 - */
1.42 -
1.43 -package org.netbeans.modules.jackpot30.backend.impl.ui;
1.44 -
1.45 -import java.util.Comparator;
1.46 -import javax.ws.rs.core.Response;
1.47 -import java.util.LinkedHashMap;
1.48 -import java.util.Map.Entry;
1.49 -import freemarker.cache.TemplateLoader;
1.50 -import freemarker.template.Configuration;
1.51 -import freemarker.template.Template;
1.52 -import freemarker.template.TemplateException;
1.53 -import java.io.IOException;
1.54 -import java.io.InputStream;
1.55 -import java.io.InputStreamReader;
1.56 -import java.io.Reader;
1.57 -import java.io.StringWriter;
1.58 -import java.net.URI;
1.59 -import java.net.URISyntaxException;
1.60 -import java.util.ArrayList;
1.61 -import java.util.Collections;
1.62 -import java.util.HashMap;
1.63 -import java.util.LinkedList;
1.64 -import java.util.List;
1.65 -import java.util.Map;
1.66 -import javax.ws.rs.DefaultValue;
1.67 -import javax.ws.rs.GET;
1.68 -import javax.ws.rs.Path;
1.69 -import javax.ws.rs.Produces;
1.70 -import javax.ws.rs.QueryParam;
1.71 -import org.codeviation.pojson.Pojson;
1.72 -import org.netbeans.modules.jackpot30.impl.WebUtilities;
1.73 -import static org.netbeans.modules.jackpot30.impl.WebUtilities.escapeForQuery;
1.74 -
1.75 -/**
1.76 - *
1.77 - * @author lahvac
1.78 - */
1.79 -@Path("/index/ui")
1.80 -public class UI {
1.81 -
1.82 - @GET
1.83 - @Path("/search")
1.84 - @Produces("text/html")
1.85 - public String search(@QueryParam("path") String path, @QueryParam("pattern") String pattern) throws URISyntaxException, IOException, TemplateException {
1.86 - Map<String, Object> configurationData = new HashMap<String, Object>();
1.87 -
1.88 - configurationData.put("paths", list());
1.89 - configurationData.put("selectedPath", path);
1.90 - configurationData.put("pattern", pattern);
1.91 - configurationData.put("patternEscaped", escapeForQuery(pattern));
1.92 - configurationData.put("examples", loadExamples());
1.93 -
1.94 - if (pattern != null && path != null) {
1.95 - URI u = new URI("http://localhost:9998/index/find?path=" + escapeForQuery(path) + "&pattern=" + escapeForQuery(pattern));
1.96 - List<Map<String, Object>> results = new LinkedList<Map<String, Object>>();
1.97 - long queryTime = System.currentTimeMillis();
1.98 - List<String> candidates = new ArrayList<String>(WebUtilities.requestStringArrayResponse(u));
1.99 -
1.100 - queryTime = System.currentTimeMillis() - queryTime;
1.101 -
1.102 - Collections.sort(candidates);
1.103 -
1.104 - for (String c : candidates) {
1.105 - Map<String, Object> found = new HashMap<String, Object>(3);
1.106 -
1.107 - found.put("relativePath", c);
1.108 -
1.109 - results.add(found);
1.110 - }
1.111 -
1.112 - configurationData.put("results", results);
1.113 -
1.114 - Map<String, Object> statistics = new HashMap<String, Object>();
1.115 -
1.116 - statistics.put("files", candidates.size());
1.117 - statistics.put("queryTime", queryTime);
1.118 -
1.119 - configurationData.put("statistics", statistics);
1.120 - }
1.121 -
1.122 - return processTemplate("ui-search.html", configurationData);
1.123 - }
1.124 -
1.125 -// @GET
1.126 -// @Path("/searchCategorized")
1.127 -// @Produces("text/html")
1.128 -// public String searchCategorized(@QueryParam("path") String path, @QueryParam("pattern") String pattern) throws URISyntaxException, IOException, TemplateException {
1.129 -// Map<String, Object> configurationData = new HashMap<String, Object>();
1.130 -//
1.131 -// configurationData.put("paths", list());
1.132 -// configurationData.put("selectedPath", path);
1.133 -// configurationData.put("pattern", pattern);
1.134 -// configurationData.put("patternEscaped", escapeForQuery(pattern));
1.135 -// configurationData.put("examples", loadExamples());
1.136 -//
1.137 -// if (pattern != null && path != null) {
1.138 -// Result queryResult = new DoQuery().doQuery(path, pattern, new Cancel() {
1.139 -// public boolean isCancelled() {
1.140 -// return false;
1.141 -// }
1.142 -// });
1.143 -//
1.144 -// configurationData.put("result", queryResult.result);
1.145 -// }
1.146 -//
1.147 -// return processTemplate("ui-search-categorized.html", configurationData);
1.148 -// }
1.149 -
1.150 - @GET
1.151 - @Path("/show")
1.152 - @Produces("text/html")
1.153 - public String show(@QueryParam("path") String path, @QueryParam("relative") String relativePath, @QueryParam("pattern") String pattern) throws URISyntaxException, IOException, TemplateException {
1.154 - Map<String, Object> configurationData = new HashMap<String, Object>();
1.155 - List<Map<String, String>> occurrences = new LinkedList<Map<String, String>>();
1.156 -
1.157 - configurationData.put("occurrences", occurrences);
1.158 -
1.159 - URI codeURL = new URI("http://localhost:9998/index/cat?path=" + escapeForQuery(path) + "&relative=" + escapeForQuery(relativePath));
1.160 - String code = WebUtilities.requestStringResponse(codeURL);
1.161 -
1.162 - if (pattern != null) {
1.163 - URI spansURL = new URI("http://localhost:9998/index/findSpans?path=" + escapeForQuery(path) + "&relativePath=" + escapeForQuery(relativePath) + "&pattern=" + escapeForQuery(pattern));
1.164 - int currentCodePos = 0;
1.165 - for (int[] span : parseSpans(WebUtilities.requestStringResponse(spansURL))) { //XXX: sorted!
1.166 - Map<String, String> occ = new HashMap<String, String>();
1.167 - occ.put("prefix", WebUtilities.escapeForHTMLElement(code.substring(currentCodePos, span[0])));
1.168 - occ.put("occurrence", WebUtilities.escapeForHTMLElement(code.substring(span[0], span[1])));
1.169 - occurrences.add(occ);
1.170 - currentCodePos = span[1];
1.171 - }
1.172 -
1.173 - configurationData.put("suffix", WebUtilities.escapeForHTMLElement(code.substring(currentCodePos, code.length())));
1.174 - } else {
1.175 - configurationData.put("suffix", WebUtilities.escapeForHTMLElement(code));
1.176 - }
1.177 -
1.178 - return processTemplate("ui-cat.html", configurationData);
1.179 - }
1.180 -
1.181 - @GET
1.182 - @Path("/snippet")
1.183 - @Produces("text/html")
1.184 - public String snippet(@QueryParam("path") String path, @QueryParam("relative") String relativePath, @QueryParam("pattern") String pattern) throws URISyntaxException, IOException, TemplateException {
1.185 - List<Map<String, String>> snippets = new LinkedList<Map<String, String>>();
1.186 -
1.187 - URI codeURL = new URI("http://localhost:9998/index/cat?path=" + escapeForQuery(path) + "&relative=" + escapeForQuery(relativePath));
1.188 - String code = WebUtilities.requestStringResponse(codeURL);
1.189 - URI spansURL = new URI("http://localhost:9998/index/findSpans?path=" + escapeForQuery(path) + "&relativePath=" + escapeForQuery(relativePath) + "&pattern=" + escapeForQuery(pattern));
1.190 -
1.191 - for (int[] span : parseSpans(WebUtilities.requestStringResponse(spansURL))) {
1.192 - snippets.add(prepareSnippet(code, span));
1.193 - }
1.194 -
1.195 - return processTemplate("ui-snippet.html", Collections.<String, Object>singletonMap("snippets", snippets));
1.196 - }
1.197 -
1.198 - @GET
1.199 - @Path("/apply")
1.200 - @Produces("text/html")
1.201 - public Response apply(@QueryParam("path") String path, @QueryParam("pattern") String pattern, @QueryParam("preview") @DefaultValue("") String preview, @QueryParam("download") @DefaultValue("") String download) throws URISyntaxException, IOException, TemplateException {
1.202 - if (!download.isEmpty()) {
1.203 - if (pattern != null && path != null) {
1.204 - URI u = new URI("http://localhost:9998/index/apply?path=" + escapeForQuery(path) + "&pattern=" + escapeForQuery(pattern));
1.205 -
1.206 - return Response.temporaryRedirect(u).header("meta", "Content-Disposition: download; filename=\"patch.diff\"").build();
1.207 - }
1.208 - }
1.209 -
1.210 - Map<String, Object> configurationData = new HashMap<String, Object>();
1.211 -
1.212 - configurationData.put("paths", list());
1.213 - configurationData.put("selectedPath", path);
1.214 - configurationData.put("pattern", pattern);
1.215 - configurationData.put("patternEscaped", escapeForQuery(pattern));
1.216 - configurationData.put("examples", loadExamples());
1.217 -
1.218 - if (pattern != null && path != null) {
1.219 - URI u = new URI("http://localhost:9998/index/apply?path=" + escapeForQuery(path) + "&pattern=" + escapeForQuery(pattern));
1.220 - long queryTime = System.currentTimeMillis();
1.221 - String diff = WebUtilities.requestStringResponse(u);
1.222 -
1.223 - queryTime = System.currentTimeMillis() - queryTime;
1.224 -
1.225 - configurationData.put("diff", diff);
1.226 -
1.227 - StringBuilder sb = new StringBuilder();
1.228 -
1.229 - for (String l : diff.split("\n")) {
1.230 - sb.append("<span");
1.231 -
1.232 - for (Entry<String, String> e : prefix2SpanName.entrySet()) {
1.233 - if (l.startsWith(e.getKey())) {
1.234 - sb.append(" class='" + e.getValue() + "'");
1.235 - break;
1.236 - }
1.237 - }
1.238 -
1.239 - sb.append(">");
1.240 - sb.append(l);
1.241 - sb.append("</span>\n");
1.242 - }
1.243 -
1.244 - configurationData.put("result", sb.toString());
1.245 - }
1.246 -
1.247 - return Response.ok(processTemplate("ui-apply.html", configurationData), "text/html").build();
1.248 - }
1.249 -
1.250 - @GET
1.251 - @Path("/searchType")
1.252 - @Produces("text/html")
1.253 - public String searchType(@QueryParam("path") String path, @QueryParam("prefix") String prefix) throws URISyntaxException, IOException, TemplateException {
1.254 - Map<String, Object> configurationData = new HashMap<String, Object>();
1.255 -
1.256 - configurationData.put("paths", list());
1.257 - configurationData.put("selectedPath", path);
1.258 - configurationData.put("prefix", prefix);
1.259 -
1.260 - if (prefix != null && path != null) {
1.261 - URI u = new URI("http://localhost:9998/index/findType?path=" + escapeForQuery(path) + "&prefix=" + escapeForQuery(prefix));
1.262 - long queryTime = System.currentTimeMillis();
1.263 - @SuppressWarnings("unchecked") //XXX: should not trust something got from the network!
1.264 - Map<String, List<String>> types = Pojson.load(LinkedHashMap.class, u);
1.265 - List<Map<String, Object>> results = new LinkedList<Map<String, Object>>();
1.266 -
1.267 - queryTime = System.currentTimeMillis() - queryTime;
1.268 -
1.269 - for (Entry<String, List<String>> e : types.entrySet()) {
1.270 - for (String fqn : e.getValue()) {
1.271 - Map<String, Object> found = new HashMap<String, Object>(3);
1.272 -
1.273 - found.put("fqn", fqn);
1.274 -
1.275 - if (fqn.contains("$")) {
1.276 - fqn = fqn.substring(0, fqn.indexOf("$"));
1.277 - }
1.278 -
1.279 - found.put("relativePath", e.getKey() + "/" + fqn.replace('.', '/') + ".java");
1.280 -
1.281 - results.add(found);
1.282 - }
1.283 - }
1.284 -
1.285 - Collections.sort(results, new Comparator<Map<String, Object>>() {
1.286 - @Override public int compare(Map<String, Object> o1, Map<String, Object> o2) {
1.287 - return ((String) o1.get("fqn")).compareTo((String) o2.get("fqn"));
1.288 - }
1.289 - });
1.290 -
1.291 - configurationData.put("results", results);
1.292 -
1.293 - Map<String, Object> statistics = new HashMap<String, Object>();
1.294 -
1.295 - statistics.put("queryTime", queryTime);
1.296 -
1.297 - configurationData.put("statistics", statistics);
1.298 - }
1.299 -
1.300 - return processTemplate("ui-findType.html", configurationData);
1.301 - }
1.302 -
1.303 - private static final Map<String, String> prefix2SpanName = new LinkedHashMap<String, String>();
1.304 -
1.305 - static {
1.306 - prefix2SpanName.put("-", "diff-removed");
1.307 - prefix2SpanName.put("+", "diff-added");
1.308 - prefix2SpanName.put("@@", "diff-hunk");
1.309 - prefix2SpanName.put("Index:", "diff-index");
1.310 - }
1.311 -
1.312 - private static List<Map<String, String>> list() throws URISyntaxException {
1.313 - List<Map<String, String>> result = new LinkedList<Map<String, String>>();
1.314 -
1.315 - for (String enc : WebUtilities.requestStringArrayResponse(new URI("http://localhost:9998/index/list"))) {
1.316 - Map<String, String> rootDesc = new HashMap<String, String>();
1.317 - String[] col = enc.split(":", 2);
1.318 -
1.319 - rootDesc.put("segment", col[0]);
1.320 - rootDesc.put("displayName", col[1]);
1.321 - result.add(rootDesc);
1.322 - }
1.323 -
1.324 - return result;
1.325 - }
1.326 -
1.327 - private static Iterable<int[]> parseSpans(String from) {
1.328 - if (from.isEmpty()) {
1.329 - return Collections.emptyList();
1.330 - }
1.331 - String[] split = from.split(":");
1.332 - List<int[]> result = new LinkedList<int[]>();
1.333 -
1.334 - for (int i = 0; i < split.length; i += 2) {
1.335 - result.add(new int[] {
1.336 - Integer.parseInt(split[i + 0].trim()),
1.337 - Integer.parseInt(split[i + 1].trim())
1.338 - });
1.339 - }
1.340 -
1.341 - return result;
1.342 - }
1.343 -
1.344 - private static final int DESIRED_CONTEXT = 2;
1.345 -
1.346 - private static Map<String, String> prepareSnippet(String code, int[] span) {
1.347 - int grandStart = span[0];
1.348 - int firstLineStart = grandStart = lineStart(code, grandStart);
1.349 -
1.350 - while (grandStart > 0 && contextLength(code.substring(grandStart, firstLineStart)) < DESIRED_CONTEXT)
1.351 - grandStart = lineStart(code, grandStart - 1);
1.352 -
1.353 - int grandEnd = span[1];
1.354 - int firstLineEnd = grandEnd = lineEnd(code, grandEnd);
1.355 -
1.356 - while (grandEnd < code.length() - 1 && contextLength(code.substring(firstLineEnd, grandEnd)) < DESIRED_CONTEXT)
1.357 - grandEnd = lineEnd(code, grandEnd + 1);
1.358 -
1.359 - Map<String, String> result = new HashMap<String, String>();
1.360 -
1.361 - result.put("prefix", WebUtilities.escapeForHTMLElement(code.substring(grandStart, span[0])));
1.362 - result.put("occurrence", WebUtilities.escapeForHTMLElement(code.substring(span[0], span[1])));
1.363 - result.put("suffix", WebUtilities.escapeForHTMLElement(code.substring(span[1], grandEnd)));
1.364 -
1.365 - return result;
1.366 - }
1.367 -
1.368 - private static int lineStart(String code, int o) {
1.369 - while (o > 0 && code.charAt(o) != '\n') {
1.370 - o--;
1.371 - }
1.372 -
1.373 - return o;
1.374 - }
1.375 -
1.376 - private static int lineEnd(String code, int o) {
1.377 - while (o < code.length() - 1 && code.charAt(o) != '\n') {
1.378 - o++;
1.379 - }
1.380 -
1.381 - return o;
1.382 - }
1.383 -
1.384 - private static int contextLength(String in) {
1.385 - return in.replaceAll("\n[ \t]*\n", "\n").trim().split("\n").length;
1.386 - }
1.387 -
1.388 - @SuppressWarnings("unchecked")
1.389 - private List<Map<String, String>> loadExamples() throws IOException, URISyntaxException {
1.390 - return Pojson.load(LinkedList.class, new URI("http://localhost:9998/index/examples"));
1.391 - }
1.392 -
1.393 - private static String processTemplate(String template, Map<String, Object> configurationData) throws TemplateException, IOException {
1.394 - Configuration conf = new Configuration();
1.395 -
1.396 - conf.setTemplateLoader(new TemplateLoaderImpl());
1.397 -
1.398 - Template templ = conf.getTemplate(template);
1.399 - StringWriter out = new StringWriter();
1.400 -
1.401 - templ.process(configurationData, out);
1.402 -
1.403 - return out.toString();
1.404 - }
1.405 -
1.406 - private static final class TemplateLoaderImpl implements TemplateLoader {
1.407 -
1.408 - public Object findTemplateSource(String name) throws IOException {
1.409 - return TemplateLoaderImpl.class.getResourceAsStream(name);
1.410 - }
1.411 -
1.412 - public long getLastModified(Object templateSource) {
1.413 - return 0L;
1.414 - }
1.415 -
1.416 - public Reader getReader(Object templateSource, String encoding) throws IOException {
1.417 - InputStream in = (InputStream) templateSource;
1.418 -
1.419 - return new InputStreamReader(in);
1.420 - }
1.421 -
1.422 - public void closeTemplateSource(Object templateSource) throws IOException {
1.423 - }
1.424 - }
1.425 -
1.426 -}