use of org.eclipse.json.provisonnal.com.eclipsesource.json.JsonValue in project webtools.sourceediting by eclipse.
the class Validator method validateItems.
private void validateItems(IJSONNode node, JsonObject schema, IJSONValue value, JsonValue memberValue, JSONValidationInfo valinfo) {
JsonValue additionalItems = schema.get(IJSONSchemaNode.ADDITIONAL_ITEMS);
if (additionalItems != null && additionalItems.isBoolean() && !additionalItems.asBoolean()) {
if (memberValue != null && memberValue.isArray()) {
if (value instanceof IJSONArray) {
int instanceSize = getSize((IJSONArray) value);
int size = memberValue.asArray().size();
if (instanceSize > size) {
int offset = node.getStartOffset();
int line = node.getModel().getStructuredDocument().getLineOfOffset(offset);
valinfo.addMessage("Array has too many items. Expected " + size + " or fewer", line, 0, offset == 0 ? 1 : offset);
}
}
}
}
}
use of org.eclipse.json.provisonnal.com.eclipsesource.json.JsonValue in project webtools.sourceediting by eclipse.
the class Validator method validateType.
private void validateType(IJSONNode node, Member member, JSONValidationInfo valinfo) {
if (IJSONSchemaNode.TYPE.equals(member.getName())) {
Set<String> types = new HashSet<String>();
if (member.getValue().isString()) {
types.add(member.getValue().asString());
} else if (member.getValue().isArray()) {
JsonArray array = (JsonArray) member.getValue();
for (JsonValue item : array) {
types.add(item.asString());
}
}
boolean valid = false;
for (String type : types) {
if (node.getNodeType() == IJSONNode.OBJECT_NODE && JSONSchemaType.Object.getName().equals(type)) {
valid = true;
break;
}
if (node.getNodeType() == IJSONNode.PAIR_NODE) {
IJSONValue value = ((IJSONPair) node).getValue();
if (value == null && JSONSchemaType.Null.getName().equals(type)) {
valid = true;
break;
}
if (value == null) {
valid = false;
break;
}
if (value.getNodeType() == IJSONNode.OBJECT_NODE && JSONSchemaType.Object.getName().equals(type)) {
valid = true;
break;
}
if (value.getNodeType() == IJSONNode.VALUE_STRING_NODE && JSONSchemaType.String.getName().equals(type)) {
valid = true;
break;
}
if (value.getNodeType() == IJSONNode.ARRAY_NODE && JSONSchemaType.Array.getName().equals(type)) {
valid = true;
break;
}
if (value.getNodeType() == IJSONNode.VALUE_BOOLEAN_NODE && JSONSchemaType.Boolean.getName().equals(type)) {
valid = true;
break;
}
if (value.getNodeType() == IJSONNode.VALUE_NULL_NODE && JSONSchemaType.Null.getName().equals(type)) {
valid = true;
break;
}
if (value.getNodeType() == IJSONNode.VALUE_NUMBER_NODE && JSONSchemaType.Number.getName().equals(type)) {
valid = true;
break;
}
if (value.getNodeType() == IJSONNode.VALUE_NUMBER_NODE && JSONSchemaType.Integer.getName().equals(type)) {
valid = true;
break;
}
}
}
if (!valid) {
int offset = node.getStartOffset();
int line = node.getModel().getStructuredDocument().getLineOfOffset(offset);
StringBuffer buffer = new StringBuffer();
Iterator<String> iter = types.iterator();
buffer.append(OPEN_BRACKET);
while (iter.hasNext()) {
buffer.append(iter.next());
if (iter.hasNext()) {
buffer.append(COMMA);
}
}
buffer.append(CLOSE_BRACKET);
valinfo.addMessage("Incorrect type. Expected " + buffer.toString(), line, 0, offset == 0 ? 1 : offset);
}
}
}
use of org.eclipse.json.provisonnal.com.eclipsesource.json.JsonValue in project webtools.sourceediting by eclipse.
the class HttpHelper method makeRequest.
public static JsonValue makeRequest(String url) throws IOException {
long startTime = 0;
HttpClient httpClient = new DefaultHttpClient();
try {
// Post JSON Tern doc
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity entity = httpResponse.getEntity();
InputStream in = entity.getContent();
// Check the status
StatusLine statusLine = httpResponse.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
// node.js server throws error
/*
* String message = IOUtils.toString(in); if
* (StringUtils.isEmpty(message)) { throw new
* TernException(statusLine.toString()); } throw new
* TernException(message);
*/
}
try {
JsonValue response = JsonValue.readFrom(new InputStreamReader(in));
return response;
} catch (ParseException e) {
throw new IOException(e);
}
} catch (Exception e) {
if (e instanceof IOException) {
throw (IOException) e;
}
throw new IOException(e);
} finally {
httpClient.getConnectionManager().shutdown();
}
}
use of org.eclipse.json.provisonnal.com.eclipsesource.json.JsonValue in project webtools.sourceediting by eclipse.
the class JSONSchemaNode method getType.
public static JSONSchemaType[] getType(JsonValue value) {
if (value == null) {
return JSONSchemaType.EMPTY_TYPES;
}
JSONSchemaType t = null;
List<JSONSchemaType> types = new ArrayList<JSONSchemaType>();
if (value.isString()) {
t = JSONSchemaType.getType(value.asString());
if (t != null) {
types.add(t);
}
} else if (value.isArray()) {
JsonArray array = (JsonArray) value;
for (JsonValue item : array) {
t = JSONSchemaType.getType(item.asString());
if (t != null) {
types.add(t);
}
}
}
return types.toArray(JSONSchemaType.EMPTY_TYPES);
}
use of org.eclipse.json.provisonnal.com.eclipsesource.json.JsonValue in project webtools.sourceediting by eclipse.
the class JSONSchemaNode method addDefinitions.
private void addDefinitions() {
// $NON-NLS-1$
JsonValue defs = jsonObject.get("definitions");
if (defs instanceof JsonObject) {
Iterator<Member> members = ((JsonObject) defs).iterator();
while (members.hasNext()) {
Member member = members.next();
JsonValue value = member.getValue();
if (value instanceof JsonObject) {
definitions.put(member.getName(), member.getValue());
}
}
}
}
Aggregations