@Test void redPenFromJSON() throws Exception { String json = "{\"config\": {\"symbols\": {\"SPACE\": {\"value\": \" \",\"invalid_chars\": \"\",\"before_space\": false,\"after_space\": false}},\"validators\": {\"WordNumber\": {\"properties\": {\"max_num\": \"30\"}}},\"lang\": \"ru\"}}"; Configuration conf = new RedPenService(null) .getRedPenFromJSON(new JSONObject(json)).getConfiguration(); assertEquals(conf.getValidatorConfigs().size(), 1); assertEquals(conf.getValidatorConfigs().get(0).getConfigurationName(), "WordNumber"); assertEquals(conf.getLang(), "ru"); assertEquals(conf.getSymbolTable().getSymbol(SymbolType.SPACE).getValue(), ' '); }
public RedPen getRedPenFromJSON(JSONObject requestJSON) throws RedPenException { String lang; Map<String, Map<String, String>> properties = new HashMap<>(); JSONObject config = null; if (!requestJSON.has("config")) { throw new RedPenException("No config block found"); } try { config = requestJSON.getJSONObject("config"); lang = getOrDefault(config, "lang", "en"); if (config.has("validators")) { JSONObject validators = config.getJSONObject("validators"); Iterator keyIter = validators.keys(); appendValidators(properties, validators, keyIter); } else { LOG.warn("No validators are found in config..."); } } catch (Exception e) { LOG.error("Exception when processing JSON properties..."); throw new RedPenException(e); } RedPen redPen = this.getRedPen(lang, properties); if (config.has("symbols")) { try { JSONObject symbols = config.getJSONObject("symbols"); Iterator keyIter = symbols.keys(); while (keyIter.hasNext()) { registerSymbolSettings(redPen, symbols, keyIter); } } catch (Exception e) { LOG.error("Exception when processing JSON symbol overrides"); throw new RedPenException(e); } } return redPen; }
RedPenService { public RedPen getRedPenFromJSON(JSONObject requestJSON) throws RedPenException { String lang; Map<String, Map<String, String>> properties = new HashMap<>(); JSONObject config = null; if (!requestJSON.has("config")) { throw new RedPenException("No config block found"); } try { config = requestJSON.getJSONObject("config"); lang = getOrDefault(config, "lang", "en"); if (config.has("validators")) { JSONObject validators = config.getJSONObject("validators"); Iterator keyIter = validators.keys(); appendValidators(properties, validators, keyIter); } else { LOG.warn("No validators are found in config..."); } } catch (Exception e) { LOG.error("Exception when processing JSON properties..."); throw new RedPenException(e); } RedPen redPen = this.getRedPen(lang, properties); if (config.has("symbols")) { try { JSONObject symbols = config.getJSONObject("symbols"); Iterator keyIter = symbols.keys(); while (keyIter.hasNext()) { registerSymbolSettings(redPen, symbols, keyIter); } } catch (Exception e) { LOG.error("Exception when processing JSON symbol overrides"); throw new RedPenException(e); } } return redPen; } }
RedPenService { public RedPen getRedPenFromJSON(JSONObject requestJSON) throws RedPenException { String lang; Map<String, Map<String, String>> properties = new HashMap<>(); JSONObject config = null; if (!requestJSON.has("config")) { throw new RedPenException("No config block found"); } try { config = requestJSON.getJSONObject("config"); lang = getOrDefault(config, "lang", "en"); if (config.has("validators")) { JSONObject validators = config.getJSONObject("validators"); Iterator keyIter = validators.keys(); appendValidators(properties, validators, keyIter); } else { LOG.warn("No validators are found in config..."); } } catch (Exception e) { LOG.error("Exception when processing JSON properties..."); throw new RedPenException(e); } RedPen redPen = this.getRedPen(lang, properties); if (config.has("symbols")) { try { JSONObject symbols = config.getJSONObject("symbols"); Iterator keyIter = symbols.keys(); while (keyIter.hasNext()) { registerSymbolSettings(redPen, symbols, keyIter); } } catch (Exception e) { LOG.error("Exception when processing JSON symbol overrides"); throw new RedPenException(e); } } return redPen; } RedPenService(ServletContext context); }
RedPenService { public RedPen getRedPenFromJSON(JSONObject requestJSON) throws RedPenException { String lang; Map<String, Map<String, String>> properties = new HashMap<>(); JSONObject config = null; if (!requestJSON.has("config")) { throw new RedPenException("No config block found"); } try { config = requestJSON.getJSONObject("config"); lang = getOrDefault(config, "lang", "en"); if (config.has("validators")) { JSONObject validators = config.getJSONObject("validators"); Iterator keyIter = validators.keys(); appendValidators(properties, validators, keyIter); } else { LOG.warn("No validators are found in config..."); } } catch (Exception e) { LOG.error("Exception when processing JSON properties..."); throw new RedPenException(e); } RedPen redPen = this.getRedPen(lang, properties); if (config.has("symbols")) { try { JSONObject symbols = config.getJSONObject("symbols"); Iterator keyIter = symbols.keys(); while (keyIter.hasNext()) { registerSymbolSettings(redPen, symbols, keyIter); } } catch (Exception e) { LOG.error("Exception when processing JSON symbol overrides"); throw new RedPenException(e); } } return redPen; } RedPenService(ServletContext context); RedPen getRedPen(String lang); RedPen getRedPenFromJSON(JSONObject requestJSON); RedPen getRedPen(String lang, Map<String, Map<String, String>> validatorProperties); Map<String, RedPen> getRedPens(); static String getOrDefault(JSONObject json, String property, String defaultValue); }
RedPenService { public RedPen getRedPenFromJSON(JSONObject requestJSON) throws RedPenException { String lang; Map<String, Map<String, String>> properties = new HashMap<>(); JSONObject config = null; if (!requestJSON.has("config")) { throw new RedPenException("No config block found"); } try { config = requestJSON.getJSONObject("config"); lang = getOrDefault(config, "lang", "en"); if (config.has("validators")) { JSONObject validators = config.getJSONObject("validators"); Iterator keyIter = validators.keys(); appendValidators(properties, validators, keyIter); } else { LOG.warn("No validators are found in config..."); } } catch (Exception e) { LOG.error("Exception when processing JSON properties..."); throw new RedPenException(e); } RedPen redPen = this.getRedPen(lang, properties); if (config.has("symbols")) { try { JSONObject symbols = config.getJSONObject("symbols"); Iterator keyIter = symbols.keys(); while (keyIter.hasNext()) { registerSymbolSettings(redPen, symbols, keyIter); } } catch (Exception e) { LOG.error("Exception when processing JSON symbol overrides"); throw new RedPenException(e); } } return redPen; } RedPenService(ServletContext context); RedPen getRedPen(String lang); RedPen getRedPenFromJSON(JSONObject requestJSON); RedPen getRedPen(String lang, Map<String, Map<String, String>> validatorProperties); Map<String, RedPen> getRedPens(); static String getOrDefault(JSONObject json, String property, String defaultValue);  }