use of org.eclipse.json.provisonnal.com.eclipsesource.json.JsonArray 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.JsonArray 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.JsonArray in project webtools.sourceediting by eclipse.
the class JSONSchemaNode method resolveReferences.
private void resolveReferences(JsonObject parent, String name, JsonValue value) {
if (value instanceof JsonObject) {
JsonObject json = value.asObject();
String ref = json.getString(REF, null);
if (ref != null && ref.startsWith(DEFINITIONS)) {
String r = ref.substring(DEFINITIONS.length());
JsonValue v = definitions.get(r);
parent.set(name, v);
// json.remove(REF);
} else {
Iterator<Member> members = json.iterator();
while (members.hasNext()) {
Member member = members.next();
JsonValue v = member.getValue();
resolveReferences(json, member.getName(), v);
}
}
} else if (value instanceof JsonArray) {
JsonArray jsonArray = (JsonArray) value;
for (int i = 0; i < jsonArray.size(); i++) {
JsonValue item = jsonArray.get(i);
if (item instanceof JsonObject) {
JsonObject json = item.asObject();
String ref = json.getString(REF, null);
if (ref != null && ref.startsWith(DEFINITIONS)) {
String r = ref.substring(DEFINITIONS.length());
JsonValue v = definitions.get(r);
jsonArray.set(i, v);
} else {
resolveReferences(json);
}
}
}
}
}
use of org.eclipse.json.provisonnal.com.eclipsesource.json.JsonArray in project webtools.sourceediting by eclipse.
the class BowerDependencyCompletionProposalCollector method addProposals.
protected void addProposals(JsonValue json, ContentAssistRequest contentAssistRequest, CompletionProposalInvocationContext context, TargetType target) {
if (json.isArray()) {
// Ex :
// [{"name":"angular-mocks","url":"git://github.com/angular/bower-angular-mocks.git"},
// {"name":"angular-moment","url":"git://github.com/urish/angular-moment.git"}]
String dependency = null;
String replacementString = null;
JsonArray values = (JsonArray) json;
for (JsonValue value : values) {
if (value.isObject()) {
dependency = ((JsonObject) value).get("name").asString();
replacementString = ContentAssistHelper.getRequiredName(dependency, JSONSchemaType.String);
Image icon = BowerEditorPluginImageHelper.getInstance().getImage(BowerEditorPluginImages.IMG_OBJ_BOWER);
JSONKeyCompletionProposal proposal = new JSONKeyCompletionProposal(replacementString, contentAssistRequest.getReplacementBeginPosition(), contentAssistRequest.getReplacementLength(), replacementString.length() - 2, icon, dependency, null, null, JSONRelevanceConstants.R_OBJECT_KEY);
contentAssistRequest.addProposal(proposal);
}
}
}
// System.err.println(json);
}
use of org.eclipse.json.provisonnal.com.eclipsesource.json.JsonArray in project webtools.sourceediting by eclipse.
the class NPMDependencyCompletionProposalCollector method addProposals.
protected void addProposals(JsonValue json, ContentAssistRequest contentAssistRequest, CompletionProposalInvocationContext context, TargetType target) {
if (json.isArray()) {
// Ex :
// [{"value":"express"},{"value":"lodash"},{"value":"gulp"}]
String dependency = null;
String replacementString = null;
JsonArray values = (JsonArray) json;
for (JsonValue value : values) {
if (value.isObject()) {
dependency = ((JsonObject) value).get("value").asString();
replacementString = ContentAssistHelper.getRequiredName(dependency, JSONSchemaType.String);
Image icon = NPMEditorPluginImageHelper.getInstance().getImage(NPMEditorPluginImages.IMG_OBJ_NPM);
JSONKeyCompletionProposal proposal = new JSONKeyCompletionProposal(replacementString, contentAssistRequest.getReplacementBeginPosition(), contentAssistRequest.getReplacementLength(), replacementString.length() - 2, icon, dependency, null, null, JSONRelevanceConstants.R_OBJECT_KEY);
contentAssistRequest.addProposal(proposal);
}
}
}
// System.err.println(json);
}
Aggregations