final String autodetectParam = parameters.get("autodetect");
if (langParam == null && (autodetectParam == null || !autodetectParam.equals("1"))) {
throw new IllegalArgumentException("Missing 'language' parameter. Specify language or use autodetect=1 for auto-detecting the language of the input text.");
}
final Language lang;
if (autodetectParam != null && autodetectParam.equals("1")) {
lang = detectLanguageOfString(text, langParam);
print("Auto-detected language: " + lang.getShortNameWithVariant());
} else {
lang = Language.getLanguageForShortName(langParam);
}
final String motherTongueParam = parameters.get("motherTongue");
Language motherTongue = null;
if (null != motherTongueParam) {
motherTongue = Language.getLanguageForShortName(motherTongueParam);
}
final String enabledParam = parameters.get("enabled");
enabledRules = new String[0];
if (null != enabledParam) {
enabledRules = enabledParam.split(",");
}
useEnabledOnly = false;
final String enabledOnly = parameters.get("enabledOnly");
if (null != enabledOnly) {
useEnabledOnly = enabledOnly.equals("yes");
}
final String disabledParam = parameters.get("disabled");
disabledRules = new String[0];
if (null != disabledParam) {
disabledRules = disabledParam.split(",");
}
if (disabledRules.length > 0 && useEnabledOnly) {
throw new IllegalArgumentException("You cannot specify disabled rules using enabledOnly=yes");
}
useQuerySettings = enabledRules.length > 0 || disabledRules.length > 0;
final List<RuleMatch> matches;
final String sourceText = parameters.get("srctext");
if (sourceText == null) {
final JLanguageTool lt = getLanguageToolInstance(lang, motherTongue);
matches = lt.check(text);
} else {
if (motherTongueParam == null) {
throw new IllegalArgumentException("Missing 'motherTongue' for bilingual checks");
}
print("Checking bilingual text, with source length " + sourceText.length() +
" and target length " + text.length() + " (characters), source language " +
motherTongue + " and target language " + langParam);
final JLanguageTool sourceLt = getLanguageToolInstance(motherTongue, null);
final JLanguageTool targetLt = getLanguageToolInstance(lang, null);
final List<BitextRule> bRules = Tools.getBitextRules(motherTongue, lang);
matches = Tools.checkBitext(sourceText, text, sourceLt, targetLt, bRules);
}
setCommonHeaders(httpExchange);
final String response = StringTools.ruleMatchesToXML(matches, text,
CONTEXT_SIZE, StringTools.XmlPrintMode.NORMAL_XML, lang, motherTongue);
httpExchange.sendResponseHeaders(HttpURLConnection.HTTP_OK, response.getBytes(ENCODING).length);
httpExchange.getResponseBody().write(response.getBytes(ENCODING));
String languageMessage = lang.getShortNameWithVariant();
if (motherTongue != null) {
languageMessage += " (mother tongue: " + motherTongue.getShortNameWithVariant() + ")";
}
final String referrer = httpExchange.getRequestHeaders().getFirst("Referer");
print("Check done: " + text.length() + " chars, " + languageMessage + ", " + referrer + ", "
+ (System.currentTimeMillis() - timeStart) + "ms");
}