<commit_before>/*
 *     Copyright 2016-2017 <NAME> (Kantenkugel)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package com.kantenkugel.discordbot.jdocparser;

import com.almightyalpaca.discord.jdabutler.Bot;
import com.kantenkugel.discordbot.jenkinsutil.JenkinsApi;
import com.kantenkugel.discordbot.jenkinsutil.JenkinsBuild;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
import org.apache.commons.lang3.tuple.Pair;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.*;
import java.util.regex.PatternSyntaxException;
import java.util.stream.Collectors;

public class JDoc {
    private static final Map<String, JDocParser.ClassDocumentation> docs = new HashMap<>();
    private static final Map<String, String> javaJavaDocs = new HashMap<>();

    public static List<Documentation> get(final String name) {
        final String[] split = name.toLowerCase().split("[#\\.]");
        JDocParser.ClassDocumentation classDoc;
        synchronized(docs) {
            if (!docs.containsKey(split[0]))
                return new ArrayList<>(0);
            classDoc = docs.get(split[0]);
        }
        for(int i=1; i < split.length - 1; i++) {
            if(!classDoc.subClasses.containsKey(split[i]))
                return new ArrayList<>(0);
            classDoc = classDoc.subClasses.get(split[i]);
        }

        String searchObj = split[split.length - 1];
        if(split.length == 1 || classDoc.subClasses.containsKey(searchObj)) {
            if(split.length > 1)
                classDoc = classDoc.subClasses.get(searchObj);
            return Collections.singletonList(classDoc);
        } else if(classDoc.classValues.containsKey(searchObj)) {
            return Collections.singletonList(classDoc.classValues.get(searchObj));
        } else {
            boolean fuzzy = false;
            String fixedSearchObj = searchObj;
            if(fixedSearchObj.charAt(fixedSearchObj.length() - 1) != ')') {
                fixedSearchObj += "()";
                fuzzy = true;
            }
            String[] methodParts = fixedSearchObj.split("[\\(\\)]");
            String methodName = methodParts[0];
            if(classDoc.methodDocs.containsKey(methodName.toLowerCase())) {
                return getMethodDocs(classDoc, methodName, fixedSearchObj, fuzzy);
            } else if(classDoc.inheritedMethods.containsKey(methodName.toLowerCase())) {
                return get(classDoc.inheritedMethods.get(methodName.toLowerCase()) + '.' + searchObj);
            }
            return new ArrayList<>(0);
        }
    }

    public static List<Documentation> getJava(final String name) {
        final String[] split = name.toLowerCase().split("[#\\.]");
        String urlPath;
        synchronized(javaJavaDocs) {
            urlPath = javaJavaDocs.get(split[0]);
            if (urlPath == null)
                return Collections.emptyList();

        }

        Map<String, JDocParser.ClassDocumentation> resultMap = new HashMap<>();
        InputStream is = null;
        try {
            Response res = Bot.httpClient.newCall(new Request.Builder().url(JDocUtil.JAVA_JDOCS_PREFIX+urlPath).get().build()).execute();
            if(!res.isSuccessful()) {
                JDocUtil.LOG.warn("OkHttp returned failure for java8 index: "+res.code());
                return Collections.emptyList();
            }
            is = res.body().byteStream();
            JDocParser.parse(JDocUtil.JAVA_JDOCS_PREFIX, urlPath, is, resultMap);
            if(resultMap.size() != 1) {
                JDocUtil.LOG.warn("Parser returned amount of results != 1");
                return Collections.emptyList();
            }

        } catch(Exception e) {
            e.printStackTrace();
        } finally {
            if(is != null)
                try { is.close(); } catch(Exception ignored) {}
        }

        JDocParser.ClassDocumentation doc = resultMap.values().iterator().next();

        if(split.length == 1)
            return Collections.singletonList(doc);

        String searchObj = split[split.length - 1];
        if(doc.classValues.containsKey(searchObj)) {
            return Collections.singletonList(doc.classValues.get(searchObj));
        } else {
            boolean fuzzy = false;
            String fixedSearchObj = searchObj;
            if(fixedSearchObj.charAt(fixedSearchObj.length() - 1) != ')') {
                fixedSearchObj += "()";
                fuzzy = true;
            }
            String[] methodParts = fixedSearchObj.split("[\\(\\)]");
            String methodName = methodParts[0];
            if(doc.methodDocs.containsKey(methodName.toLowerCase())) {
                return getMethodDocs(doc, methodName, fixedSearchObj, fuzzy);
            } else if(doc.inheritedMethods.containsKey(methodName.toLowerCase())) {
                return getJava(doc.inheritedMethods.get(methodName.toLowerCase()) + '.' + searchObj);
            }
            return Collections.emptyList();
        }<commit_msg>java8 docs now properly work for nested classes and vararg parameters<commit_after>/*
 *     Copyright 2016-2017 <NAME> (Kantenkugel)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package com.kantenkugel.discordbot.jdocparser;

import com.almightyalpaca.discord.jdabutler.Bot;
import com.kantenkugel.discordbot.jenkinsutil.JenkinsApi;
import com.kantenkugel.discordbot.jenkinsutil.JenkinsBuild;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
import org.apache.commons.lang3.tuple.Pair;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.*;
import java.util.regex.PatternSyntaxException;
import java.util.stream.Collectors;

public class JDoc {
    private static final Map<String, JDocParser.ClassDocumentation> docs = new HashMap<>();
    private static final Map<String, String> javaJavaDocs = new HashMap<>();

    public static List<Documentation> get(final String name) {
        final String[] split = name.toLowerCase().split("[#.]");
        JDocParser.ClassDocumentation classDoc;
        synchronized(docs) {
            if (!docs.containsKey(split[0]))
                return Collections.emptyList();
            classDoc = docs.get(split[0]);
        }
        for(int i=1; i < split.length - 1; i++) {
            if(!classDoc.subClasses.containsKey(split[i]))
                return Collections.emptyList();
            classDoc = classDoc.subClasses.get(split[i]);
        }

        String searchObj = split[split.length - 1];
        if(split.length == 1 || classDoc.subClasses.containsKey(searchObj)) {
            if(split.length > 1)
                classDoc = classDoc.subClasses.get(searchObj);
            return Collections.singletonList(classDoc);
        } else if(classDoc.classValues.containsKey(searchObj)) {
            return Collections.singletonList(classDoc.classValues.get(searchObj));
        } else {
            boolean fuzzy = false;
            String fixedSearchObj = searchObj;
            if(fixedSearchObj.charAt(fixedSearchObj.length() - 1) != ')') {
                fixedSearchObj += "()";
                fuzzy = true;
            }
            String[] methodParts = fixedSearchObj.split("[()]");
            String methodName = methodParts[0];
            if(classDoc.methodDocs.containsKey(methodName.toLowerCase())) {
                return getMethodDocs(classDoc, methodName, fixedSearchObj, fuzzy);
            } else if(classDoc.inheritedMethods.containsKey(methodName.toLowerCase())) {
                return get(classDoc.inheritedMethods.get(methodName.toLowerCase()) + '.' + searchObj);
            }
            return Collections.emptyList();
        }
    }

    public static List<Documentation> getJava(final String name) {
        final String[] noArgNames = name.toLowerCase().split("\\(")[0].split("[#.]");
        String className = String.join(".", Arrays.copyOf(noArgNames, noArgNames.length - 1));
        String urlPath;
        synchronized(javaJavaDocs) {
            urlPath = javaJavaDocs.get(name);
            if(urlPath == null)
                urlPath = javaJavaDocs.get(className);
            else
                className = name;
            if (urlPath == null)
                return Collections.emptyList();
        }

        Map<String, JDocParser.ClassDocumentation> resultMap = new HashMap<>();
        InputStream is = null;
        try {
            Response res = Bot.httpClient.newCall(new Request.Builder().url(JDocUtil.JAVA_JDOCS_PREFIX+urlPath).get().build()).execute();
            if(!res.isSuccessful()) {
                JDocUtil.LOG.warn("OkHttp returned failure for java8 index: "+res.code());
                return Collections.emptyList();
            }
            is = res.body().byteStream();
            JDocParser.parse(JDocUtil.JAVA_JDOCS_PREFIX, urlPath, is, resultMap);
        } catch(Exception e) {
            e.printStackTrace();
        } finally {
            if(is != null)
                try { is.close(); } catch(Exception ignored) {}
        }

        if(!resultMap.containsKey(className)) {
            JDocUtil.LOG.warn("Parser didn't return wanted docs");
            return Collections.emptyList();
        }

        JDocParser.ClassDocumentation doc = resultMap.get(className);

        if(noArgNames.length == 1 || className.equals(name))
            return Collections.singletonList(doc);

        String searchObj = name.substring(className.length() + 1);//class name + seperator dot
        if(doc.classValues.containsKey(searchObj)) {
            return Collections.singletonList(doc.classValues.get(searchObj));
        } else {
            boolean fuzzy = false;
            String fixedSearchObj = searchObj;
            if(fixedSearchObj.charAt(fixedSearchObj.length() - 1) != ')') {
                fixedSearchObj += "()";
                fuzzy = true;
            }
            String[] methodParts = fixedSearchObj.split("[()]");
            String methodName = methodParts[0];
            if(doc.methodDocs.containsKey(methodName.toLowerCase())) {
                return getMethodDocs(doc, methodName, fixedSearchObj, fuzzy);
            } else if(doc.inheritedMethods.containsKey(methodName.toLowerCase())) {
                return getJava(doc.inheritedMethods.get(methodName.toLowerCase()) + '.' + searchObj);
            }
            return Collections.emptyList();
        }
{'fraction_non_alphanumeric': 0.11735171474768105, 'fraction_numerical': 0.005956939834907668, 'mean_word_length': 2.4944989592625633, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1086544', 'n_tokens_mistral': 3249, 'n_tokens_neox': 3183, 'n_words': 688}