use of org.apache.stanbol.entityhub.servicesapi.query.TextConstraint.PatternType in project stanbol by apache.
the class FieldQueryReader method parseTextConstraint.
/**
* @param jConstraint
* @return
* @throws JSONException
*/
private static Constraint parseTextConstraint(JSONObject jConstraint) throws JSONException {
final TextConstraint constraint;
boolean caseSensitive = jConstraint.optBoolean("caseSensitive", false);
//parse patternType
PatternType patternType;
String jPatternType = jConstraint.optString("patternType", null);
if (jPatternType == null) {
patternType = PatternType.none;
} else {
try {
patternType = PatternType.valueOf(jPatternType);
} catch (IllegalArgumentException e) {
log.warn("Encountered unknown patternType for TextConstraint!", e);
patternType = PatternType.none;
StringBuilder message = new StringBuilder();
message.append("Illegal value for field 'patternType'.\n");
message.append("Supported values are: ");
message.append(Arrays.toString(PatternType.values()));
message.append('\n');
message.append("Parsed Constraint: \n");
message.append(jConstraint.toString(4));
throw new IllegalArgumentException(message.toString());
}
}
//parse languages
Collection<String> languages;
//support both "languages" and "language"
String languageKey = null;
if (jConstraint.has("language")) {
languageKey = "language";
} else if (jConstraint.has("languages")) {
log.warn("The key \"languages\" is deprecated. Use \"language\" instead.");
languageKey = "languages";
}
if (languageKey != null) {
JSONArray jLanguages = jConstraint.optJSONArray(languageKey);
if (jLanguages != null && jLanguages.length() > 0) {
languages = new ArrayList<String>(jLanguages.length());
for (int i = 0; i < jLanguages.length(); i++) {
String lang = jLanguages.getString(i);
if (lang != null && !lang.isEmpty()) {
languages.add(lang);
} else if (!languages.contains(null)) {
languages.add(null);
}
}
if (languages.isEmpty()) {
//if no one was successfully added set the list back to null
languages = null;
}
} else {
String language = jConstraint.getString(languageKey);
if (language.isEmpty()) {
languages = null;
} else {
//add the single language
languages = Collections.singletonList(language);
}
}
} else {
languages = null;
}
//parse text and create constraint
if (jConstraint.has("text") && !jConstraint.isNull("text")) {
List<String> textConstraints;
JSONArray jTextConstraints = jConstraint.optJSONArray("text");
if (jTextConstraints != null) {
textConstraints = new ArrayList<String>(jTextConstraints.length());
for (int i = 0; i < jTextConstraints.length(); i++) {
String text = jTextConstraints.getString(i);
if (text != null && !text.isEmpty()) {
textConstraints.add(jTextConstraints.getString(i));
}
}
} else {
String text = jConstraint.getString("text");
if (text == null || text.isEmpty()) {
textConstraints = Collections.emptyList();
} else {
textConstraints = Collections.singletonList(text);
}
}
if (textConstraints.isEmpty()) {
StringBuilder message = new StringBuilder();
message.append("Parsed TextConstraint doese not define a valid (none empty) value for the 'text' property !\n");
message.append("Parsed Constraint: \n");
message.append(jConstraint.toString(4));
throw new IllegalArgumentException(message.toString());
}
constraint = new TextConstraint(textConstraints, patternType, caseSensitive, languages == null ? null : languages.toArray(new String[languages.size()]));
//finally parse the optional termProximity
if (jConstraint.has("proximityRanking")) {
constraint.setProximityRanking(jConstraint.optBoolean("proximityRanking", false));
}
} else {
StringBuilder message = new StringBuilder();
message.append("Parsed TextConstraint doese not define the required field 'text'!\n");
message.append("Parsed Constraint: \n");
message.append(jConstraint.toString(4));
throw new IllegalArgumentException(message.toString());
}
return constraint;
}
Aggregations