Skip to content

Commit 60e4af3

Browse files
authored
Merge pull request #821 from takahi-i/specify-configuration-for-lang
[WIP] Specify configuration for lang
2 parents a8f05ae + e595a1e commit 60e4af3

2 files changed

Lines changed: 92 additions & 86 deletions

File tree

redpen-server/src/main/java/cc/redpen/server/api/RedPenConfigurationResource.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public class RedPenConfigurationResource {
5151
@Context
5252
private ServletContext context;
5353

54-
RedPenService getRedPenService() {
54+
RedPenService getRedPenService() throws RedPenException {
5555
return new RedPenService(context);
5656
}
5757

@@ -127,21 +127,16 @@ public Response getRedPens(@QueryParam("lang") @DefaultValue("") String lang) th
127127
@Consumes(MediaType.APPLICATION_JSON)
128128
@Produces(MediaType.APPLICATION_XML)
129129
@WinkAPIDescriber.Description("Returns the configuration XML corresponds to the UI")
130-
public Response exportConfiguration(JSONObject requestJSON) {
131-
130+
public Response exportConfiguration(JSONObject requestJSON) throws RedPenException {
132131
LOG.info("Exporting configuration using JSON request");
133-
134132
RedPen redPen = new RedPenService(context).getRedPenFromJSON(requestJSON);
135-
136133
String result = null;
137-
138134
try(ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
139135
new ConfigurationExporter().export(redPen.getConfiguration(), baos);
140136
result = new String(baos.toByteArray());
141137
} catch (IOException e) {
142138
LOG.error("Exception when exporting configuration", e);
143139
}
144-
145140
return Response.ok(result, RedPenResource.MIME_TYPE_XML).build();
146141
}
147142
}

redpen-server/src/main/java/cc/redpen/server/api/RedPenService.java

Lines changed: 90 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import cc.redpen.RedPenException;
2323
import cc.redpen.config.*;
2424
import cc.redpen.model.Document;
25+
import org.json.JSONException;
2526
import org.json.JSONObject;
2627
import org.slf4j.Logger;
2728
import org.slf4j.LoggerFactory;
@@ -44,41 +45,40 @@ public class RedPenService {
4445
*
4546
* @param context the servlet context
4647
*/
47-
public RedPenService(ServletContext context) {
48+
public RedPenService(ServletContext context) throws RedPenException {
49+
if (!redPens.isEmpty()) {
50+
LOG.debug("Default RedPen objects are found...");
51+
return;
52+
}
4853
synchronized (redPens) {
49-
if (redPens.isEmpty()) {
50-
LOG.info("Creating RedPen instances");
54+
LOG.info("Creating RedPen instances");
55+
List<Document> emptyDocuments = new ArrayList<>();
56+
emptyDocuments.add(Document.builder().build());
57+
for (String key : Configuration.getDefaultConfigKeys()) {
58+
RedPen redpen = new RedPen(Configuration.builder(key).secure().addAvailableValidatorConfigs().build());
59+
redpen.validate(emptyDocuments);
60+
redPens.put(key, redpen);
61+
}
62+
63+
String configPath = context != null ? context.getInitParameter("redpen.conf.path") : null;
64+
if (configPath != null) {
65+
LOG.info("Config Path is set to \"{}\"", configPath);
66+
Configuration configuration;
5167
try {
52-
List<Document> emptyDocuments = new ArrayList<>();
53-
emptyDocuments.add(Document.builder().build());
54-
for (String key : Configuration.getDefaultConfigKeys()) {
55-
RedPen redpen = new RedPen(Configuration.builder(key).secure().addAvailableValidatorConfigs().build());
56-
redpen.validate(emptyDocuments);
57-
redPens.put(key, redpen);
58-
}
59-
60-
String configPath = context != null ? context.getInitParameter("redpen.conf.path") : null;
61-
if (configPath != null) {
62-
LOG.info("Config Path is set to \"{}\"", configPath);
63-
Configuration configuration;
64-
try {
65-
configuration = new ConfigurationLoader().secure().loadFromResource(configPath);
66-
} catch (RedPenException rpe) {
67-
configuration = new ConfigurationLoader().secure().load(new File(configPath));
68-
}
69-
RedPen defaultRedPen = new RedPen(configuration);
70-
redPens.put(DEFAULT_LANGUAGE, defaultRedPen);
71-
} else {
72-
// if config path is not set, fallback to default config path
73-
LOG.info("No Config Path set, using default configurations");
74-
redPens.put(DEFAULT_LANGUAGE, redPens.get("en"));
75-
}
76-
LOG.info("Document Validator Server is running.");
77-
} catch (RedPenException e) {
78-
LOG.error("Unable to initialize RedPen", e);
79-
throw new ExceptionInInitializerError(e);
68+
configuration = new ConfigurationLoader().secure().loadFromResource(configPath);
69+
} catch (RedPenException rpe) {
70+
configuration = new ConfigurationLoader().secure().load(new File(configPath));
8071
}
72+
RedPen defaultRedPen = new RedPen(configuration);
73+
defaultRedPen.validate(emptyDocuments);
74+
redPens.put(DEFAULT_LANGUAGE, defaultRedPen);
75+
redPens.put(configuration.getLang(), defaultRedPen);
76+
} else {
77+
// if config path is not set, fallback to default config path
78+
LOG.info("No Config Path set, using default configurations");
79+
redPens.put(DEFAULT_LANGUAGE, redPens.get("en"));
8180
}
81+
LOG.info("Document Validator Server is running.");
8282
}
8383
}
8484

@@ -91,73 +91,84 @@ public RedPen getRedPen(String lang) {
9191
* @param requestJSON the JSON contains configurations
9292
* @return a configured redpen instance
9393
*/
94-
public RedPen getRedPenFromJSON(JSONObject requestJSON) {
95-
String lang = "en";
96-
94+
public RedPen getRedPenFromJSON(JSONObject requestJSON) throws RedPenException {
95+
String lang;
9796
Map<String, Map<String, String>> properties = new HashMap<>();
9897
JSONObject config = null;
99-
if (requestJSON.has("config")) {
100-
try {
101-
config = requestJSON.getJSONObject("config");
102-
lang = getOrDefault(config, "lang", "en");
103-
if (config.has("validators")) {
104-
JSONObject validators = config.getJSONObject("validators");
105-
Iterator keyIter = validators.keys();
106-
while (keyIter.hasNext()) {
107-
String validator = String.valueOf(keyIter.next());
108-
Map<String, String> props = new HashMap<>();
109-
properties.put(validator, props);
110-
JSONObject validatorConfig = validators.getJSONObject(validator);
111-
if ((validatorConfig != null) && validatorConfig.has("properties")) {
112-
JSONObject validatorProps = validatorConfig.getJSONObject("properties");
113-
Iterator propsIter = validatorProps.keys();
114-
while (propsIter.hasNext()) {
115-
String propname = String.valueOf(propsIter.next());
116-
props.put(propname, validatorProps.getString(propname));
117-
}
118-
}
119-
}
120-
}
121-
} catch (Exception e) {
122-
LOG.error("Exception when processing JSON properties", e);
98+
if (!requestJSON.has("config")) {
99+
throw new RedPenException("No config block found");
100+
}
101+
102+
try {
103+
config = requestJSON.getJSONObject("config");
104+
lang = getOrDefault(config, "lang", "en");
105+
if (config.has("validators")) {
106+
JSONObject validators = config.getJSONObject("validators");
107+
Iterator keyIter = validators.keys();
108+
appendValidators(properties, validators, keyIter);
109+
} else {
110+
LOG.warn("No validators are found in config...");
123111
}
112+
} catch (Exception e) {
113+
LOG.error("Exception when processing JSON properties...");
114+
throw new RedPenException(e);
124115
}
125116

126117
RedPen redPen = this.getRedPen(lang, properties);
127118

128119
// override any symbols
129-
if ((config != null) && config.has("symbols")) {
120+
if (config.has("symbols")) {
130121
try {
131122
JSONObject symbols = config.getJSONObject("symbols");
132123
Iterator keyIter = symbols.keys();
133124
while (keyIter.hasNext()) {
134-
String symbolName = String.valueOf(keyIter.next());
135-
try {
136-
SymbolType symbolType = SymbolType.valueOf(symbolName);
137-
JSONObject symbolConfig = symbols.getJSONObject(symbolName);
138-
Symbol originalSymbol = redPen.getConfiguration().getSymbolTable().getSymbol(symbolType);
139-
if ((originalSymbol != null) && (symbolConfig != null) && symbolConfig.has("value")) {
140-
String value = symbolConfig.has("value") ? symbolConfig.getString("value") : String.valueOf(originalSymbol.getValue());
141-
boolean spaceBefore = symbolConfig.has("before_space") ? symbolConfig.getBoolean("before_space") : originalSymbol.isNeedBeforeSpace();
142-
boolean spaceAfter = symbolConfig.has("after_space") ? symbolConfig.getBoolean("after_space") : originalSymbol.isNeedAfterSpace();
143-
String invalidChars = symbolConfig.has("invalid_chars") ? symbolConfig.getString("invalid_chars") : String.valueOf(originalSymbol.getInvalidChars());
144-
if ((value != null) && !value.isEmpty()) {
145-
redPen.getConfiguration().getSymbolTable().overrideSymbol(new Symbol(symbolType, value.charAt(0), invalidChars, spaceBefore, spaceAfter));
146-
}
147-
}
148-
149-
} catch (IllegalArgumentException iae) {
150-
LOG.error("Ignoring unknown SymbolType " + symbolName);
151-
}
125+
registerSymbolSettings(redPen, symbols, keyIter);
152126
}
153127
} catch (Exception e) {
154-
LOG.error("Exception when processing JSON symbol overrides", e);
128+
LOG.error("Exception when processing JSON symbol overrides");
129+
throw new RedPenException(e);
155130
}
156131
}
157-
158132
return redPen;
159133
}
160134

135+
private void registerSymbolSettings(RedPen redPen, JSONObject symbols, Iterator keyIter) throws JSONException {
136+
String symbolName = String.valueOf(keyIter.next());
137+
try {
138+
SymbolType symbolType = SymbolType.valueOf(symbolName);
139+
JSONObject symbolConfig = symbols.getJSONObject(symbolName);
140+
Symbol originalSymbol = redPen.getConfiguration().getSymbolTable().getSymbol(symbolType);
141+
if ((originalSymbol != null) && (symbolConfig != null) && symbolConfig.has("value")) {
142+
String value = symbolConfig.has("value") ? symbolConfig.getString("value") : String.valueOf(originalSymbol.getValue());
143+
boolean spaceBefore = symbolConfig.has("before_space") ? symbolConfig.getBoolean("before_space") : originalSymbol.isNeedBeforeSpace();
144+
boolean spaceAfter = symbolConfig.has("after_space") ? symbolConfig.getBoolean("after_space") : originalSymbol.isNeedAfterSpace();
145+
String invalidChars = symbolConfig.has("invalid_chars") ? symbolConfig.getString("invalid_chars") : String.valueOf(originalSymbol.getInvalidChars());
146+
if ((value != null) && !value.isEmpty()) {
147+
redPen.getConfiguration().getSymbolTable().overrideSymbol(new Symbol(symbolType, value.charAt(0), invalidChars, spaceBefore, spaceAfter));
148+
}
149+
}
150+
} catch (IllegalArgumentException iae) {
151+
LOG.error("Ignoring unknown SymbolType " + symbolName);
152+
}
153+
}
154+
155+
private void appendValidators(Map<String, Map<String, String>> properties, JSONObject validators, Iterator keyIter) throws JSONException {
156+
while (keyIter.hasNext()) {
157+
String validator = String.valueOf(keyIter.next());
158+
Map<String, String> props = new HashMap<>();
159+
properties.put(validator, props);
160+
JSONObject validatorConfig = validators.getJSONObject(validator);
161+
if ((validatorConfig != null) && validatorConfig.has("properties")) {
162+
JSONObject validatorProps = validatorConfig.getJSONObject("properties");
163+
Iterator propsIter = validatorProps.keys();
164+
while (propsIter.hasNext()) {
165+
String propname = String.valueOf(propsIter.next());
166+
props.put(propname, validatorProps.getString(propname));
167+
}
168+
}
169+
}
170+
}
171+
161172
/**
162173
* Create a new redpen for the specified language. The validator properties map is a map of validator names to their (optional) properties.
163174
* Only validitors present in this map are added to the redpen configuration

0 commit comments

Comments
 (0)