use of org.eclipse.json.provisonnal.com.eclipsesource.json.JsonArray in project webtools.sourceediting by eclipse.
the class JSONSchemaNode method add.
private void add(JsonObject jsonObject, IJSONSchemaNode schemaNode, String pref) {
JsonValue values = jsonObject.get(pref);
if (values instanceof JsonArray) {
JsonArray array = (JsonArray) values;
Iterator<JsonValue> iter = array.iterator();
while (iter.hasNext()) {
JsonValue value = iter.next();
if (value != null) {
walk(value.asObject(), schemaNode);
}
}
}
}
use of org.eclipse.json.provisonnal.com.eclipsesource.json.JsonArray in project webtools.sourceediting by eclipse.
the class Validator method validate.
private void validate(IJSONNode node, JsonObject schema, Member member, JSONValidationInfo valinfo) {
if (IJSONSchemaNode.ALL_OF.equals(member.getName()) && member.getValue() instanceof JsonArray) {
JsonArray jsonArray = (JsonArray) member.getValue();
Iterator<JsonValue> iter = jsonArray.iterator();
while (iter.hasNext()) {
JsonValue value = iter.next();
if (value instanceof JsonObject) {
validate(node, (JsonObject) value, valinfo);
}
}
}
if (IJSONSchemaNode.ANY_OF.equals(member.getName()) && member.getValue() instanceof JsonArray) {
JsonArray jsonArray = (JsonArray) member.getValue();
Iterator<JsonValue> iter = jsonArray.iterator();
while (iter.hasNext()) {
JsonValue value = iter.next();
if (value instanceof JsonObject) {
JSONValidationInfo info = new JSONValidationInfo("");
validate(node, (JsonObject) value, info);
if (info.getValidationMessages() == null || info.getValidationMessages().length == 0) {
return;
}
}
}
int offset = node.getStartOffset();
int line = node.getModel().getStructuredDocument().getLineOfOffset(offset);
valinfo.addMessage("Matches a schema that is not allowed", line, 0, offset == 0 ? 1 : offset);
}
if (IJSONSchemaNode.ONE_OF.equals(member.getName()) && member.getValue() instanceof JsonArray) {
JsonArray jsonArray = (JsonArray) member.getValue();
Iterator<JsonValue> iter = jsonArray.iterator();
int count = 0;
while (iter.hasNext()) {
JsonValue value = iter.next();
if (value instanceof JsonObject) {
JSONValidationInfo info = new JSONValidationInfo("");
validate(node, (JsonObject) value, info);
if (info.getValidationMessages() == null || info.getValidationMessages().length == 0) {
count = count + 1;
}
}
}
if (count != 1) {
int offset = node.getStartOffset();
int line = node.getModel().getStructuredDocument().getLineOfOffset(offset);
valinfo.addMessage("Matches a schema that is not allowed", line, 0, offset == 0 ? 1 : offset);
}
}
if (IJSONSchemaNode.NOT.equals(member.getName()) && member.getValue() instanceof JsonObject) {
JsonObject json = (JsonObject) member.getValue();
JSONValidationInfo info = new JSONValidationInfo("");
validate(node, json, info);
if (info.getValidationMessages() == null || info.getValidationMessages().length == 0) {
int offset = node.getStartOffset();
int line = node.getModel().getStructuredDocument().getLineOfOffset(offset);
valinfo.addMessage("Matches a schema that is not allowed", line, 0, offset == 0 ? 1 : offset);
}
}
if (IJSONSchemaNode.TYPE.equals(member.getName())) {
validateType(node, member, valinfo);
}
if (IJSONSchemaNode.ENUM.equals(member.getName())) {
validateEnum(node, schema, valinfo);
}
if (node.getNodeType() == IJSONNode.OBJECT_NODE) {
if (IJSONSchemaNode.REQUIRED.equals(member.getName())) {
validateRequired(node, schema, valinfo);
}
if (IJSONSchemaNode.MAX_PROPERTIES.equals(member.getName())) {
validateMaxProperties(node, schema, valinfo);
}
if (IJSONSchemaNode.MIN_PROPERTIES.equals(member.getName())) {
validateMinProperties(node, schema, valinfo);
}
if (IJSONSchemaNode.ADDITIONAL_PROPERTIES.equals(member.getName())) {
validateAdditionalProperties(node, schema, member.getValue(), valinfo);
}
}
if (node.getNodeType() == IJSONNode.PAIR_NODE) {
IJSONValue value = ((IJSONPair) node).getValue();
JSONSchemaType[] types = JSONSchemaNode.getType(schema.get(IJSONSchemaNode.TYPE));
if (value != null) {
if (value.getNodeType() == IJSONNode.VALUE_STRING_NODE && isType(types, JSONSchemaType.String)) {
validateString(node, schema, member, valinfo, value);
}
if (value.getNodeType() == IJSONNode.VALUE_NUMBER_NODE && (isType(types, JSONSchemaType.Integer) || isType(types, JSONSchemaType.Number))) {
validateNumber(node, schema, member, valinfo, value);
}
if (value.getNodeType() == IJSONNode.ARRAY_NODE && isType(types, JSONSchemaType.Array)) {
validateArray(node, schema, member, valinfo, value);
}
}
}
}
use of org.eclipse.json.provisonnal.com.eclipsesource.json.JsonArray in project webtools.sourceediting by eclipse.
the class CatalogSchemastoreReader method readSchemastore.
protected void readSchemastore() {
File f = getUrl();
if (f != null) {
int type = ICatalogEntry.ENTRY_TYPE_SCHEMA;
JsonValue schemas;
try {
InputStreamReader reader = new InputStreamReader(new FileInputStream(f));
JsonObject json = JsonObject.readFrom(reader);
schemas = json.get(SCHEMAS);
} catch (IOException e) {
Logger.logException(e);
return;
}
if (schemas != null && schemas instanceof JsonArray) {
JsonArray elements = (JsonArray) schemas;
Iterator<JsonValue> iter = elements.iterator();
while (iter.hasNext()) {
JsonValue value = iter.next();
if (value instanceof JsonObject) {
JsonObject jsonObject = (JsonObject) value;
// $NON-NLS-1$
JsonValue urlJson = jsonObject.get("url");
// $NON-NLS-1$
JsonValue fileMatchJson = jsonObject.get("fileMatch");
if (urlJson != null && fileMatchJson != null && urlJson.isString() && fileMatchJson.isArray()) {
String url = urlJson.asString();
JsonArray fileMatchArray = fileMatchJson.asArray();
Iterator<JsonValue> fileIter = fileMatchArray.iterator();
while (fileIter.hasNext()) {
JsonValue fileMatchValue = fileIter.next();
if (fileMatchValue.isString()) {
String fileMatch = fileMatchValue.asString();
ICatalogElement catalogElement = catalog.createCatalogElement(type);
if (catalogElement instanceof ICatalogEntry) {
ICatalogEntry entry = (ICatalogEntry) catalogElement;
entry.setKey(fileMatch);
entry.setURI(url);
entry.setId(fileMatch);
}
catalog.addCatalogElement(catalogElement);
}
}
}
}
}
}
}
}
use of org.eclipse.json.provisonnal.com.eclipsesource.json.JsonArray in project webtools.sourceediting by eclipse.
the class Validator method validateEnum.
private void validateEnum(IJSONNode node, JsonObject schema, JSONValidationInfo valinfo) {
JsonValue value = schema.get(IJSONSchemaNode.ENUM);
if (value instanceof JsonArray) {
JsonArray array = value.asArray();
Iterator<JsonValue> iter = array.iterator();
Set<String> values = new HashSet<String>();
while (iter.hasNext()) {
String v = iter.next().toString();
values.add(JSONUtil.removeQuote(v));
}
if (node instanceof IJSONPair) {
IJSONPair pair = (IJSONPair) node;
String v = JSONUtil.getString(pair.getValue());
if (!values.contains(v)) {
int offset = node.getStartOffset();
int line = node.getModel().getStructuredDocument().getLineOfOffset(offset);
valinfo.addMessage("Value is not an accepted value. Valid values " + values + "'", line, 0, offset == 0 ? 1 : offset);
}
}
}
}
use of org.eclipse.json.provisonnal.com.eclipsesource.json.JsonArray in project webtools.sourceediting by eclipse.
the class Validator method validateRequired.
private void validateRequired(IJSONNode node, JsonObject schema, JSONValidationInfo valinfo) {
JsonValue required = schema.get(IJSONSchemaNode.REQUIRED);
if (required instanceof JsonArray) {
JsonArray array = required.asArray();
Iterator<JsonValue> iter = array.iterator();
Set<String> values = new HashSet<String>();
while (iter.hasNext()) {
JsonValue v = iter.next();
if (v.isString()) {
values.add(v.asString());
}
}
Set<String> properties = getProperties(node);
for (String property : properties) {
if (property != null && values.contains(property)) {
values.remove(property);
}
}
for (String value : values) {
int offset = node.getStartOffset();
int line = node.getModel().getStructuredDocument().getLineOfOffset(offset);
valinfo.addMessage("Missing property '" + value + "'", line, 0, offset == 0 ? 1 : offset);
}
}
}
Aggregations