use of org.eclipse.json.schema.IJSONSchemaProperty in project webtools.sourceediting by eclipse.
the class Validator method validate.
private void validate(IJSONNode node, IJSONSchemaProperty schemaProperty, JSONValidationInfo valinfo) {
if (node == null || schemaProperty == null) {
return;
}
JsonObject schema = schemaProperty.getJsonObject();
validate(node, schema, valinfo);
IJSONNode child = node.getFirstChild();
while (child != null) {
IJSONSchemaProperty property = schemaProperty.getSchemaDocument().getProperty(child.getPath());
validate(child, property, valinfo);
if (child instanceof IJSONPair) {
IJSONValue value = ((IJSONPair) child).getValue();
if (value instanceof IJSONObject) {
IJSONSchemaProperty prop = schemaProperty.getSchemaDocument().getProperty(value.getPath());
validate(value, prop, valinfo);
}
}
child = child.getNextSibling();
}
}
use of org.eclipse.json.schema.IJSONSchemaProperty in project webtools.sourceediting by eclipse.
the class HTMLJSONPrinter method getAdditionalProposalInfo.
public static String getAdditionalProposalInfo(IJSONPair pair) {
StringBuilder builder = new StringBuilder();
ImageDescriptor descriptor = null;
IJSONValue value = pair.getValue();
if (value != null) {
descriptor = JSONEditorPluginImageHelper.getInstance().getImageDescriptor(value.getNodeType());
}
startPage(builder, getTitleKey(value), descriptor);
startDefinitionList(builder);
StringBuilder build = new StringBuilder();
build.append(pair.getName());
try {
IJSONSchemaDocument schemaDocument = JSONCorePlugin.getDefault().getSchemaDocument(pair);
if (schemaDocument != null) {
IJSONPath path = pair.getPath();
IJSONSchemaProperty property = schemaDocument.getProperty(path);
if (property != null) {
String description = property.getDescription();
if (description != null) {
build.append(" - ");
build.append(description);
}
}
}
} catch (IOException e) {
Logger.logException(e);
}
addDefinitionListItem(builder, "Key", build.toString());
addDefinitionListItem(builder, "Type", getValueType(pair.getValue()));
endDefinitionList(builder);
endPage(builder);
return builder.toString();
}
use of org.eclipse.json.schema.IJSONSchemaProperty in project webtools.sourceediting by eclipse.
the class JSONSchemaDocument method getProperty.
private IJSONSchemaProperty getProperty(IJSONSchemaProperty node, String[] segments, int level) {
if (segments.length < (level + 1)) {
return node;
}
String segment = segments[level];
Collection<IJSONSchemaProperty> props = node.getProperties().values();
for (IJSONSchemaProperty property : props) {
if (segment.equals(property.getName())) {
return getProperty(property, segments, level + 1);
}
}
return null;
}
use of org.eclipse.json.schema.IJSONSchemaProperty in project webtools.sourceediting by eclipse.
the class JSONSchemaDocument method getProperty.
@Override
public IJSONSchemaProperty getProperty(IJSONPath path) {
if (path == null || path.getSegments() == null || path.getSegments().length <= 0) {
return this;
}
String[] segments = path.getSegments();
String segment = segments[0];
if (segment == null) {
return null;
}
IJSONSchemaProperty property = getProperties().get(segment);
if (property != null) {
return getProperty(property, segments, 1);
}
return null;
}
use of org.eclipse.json.schema.IJSONSchemaProperty in project webtools.sourceediting by eclipse.
the class JSONCompletionProposalComputer method collectProposalsFromSchema.
/**
* Collect completion proposals from JSON Schema.
*
* @param contentAssistRequest
* @param context
*/
private void collectProposalsFromSchema(ContentAssistRequest contentAssistRequest, CompletionProposalInvocationContext context) {
try {
IJSONNode node = contentAssistRequest.getNode();
IJSONSchemaDocument schemaDocument = JSONCorePlugin.getDefault().getSchemaDocument(node);
if (schemaDocument != null) {
String matchString = contentAssistRequest.getMatchString();
if (matchString == null) {
// $NON-NLS-1$
matchString = "";
}
if ((matchString.length() > 0) && (matchString.startsWith(QUOTE))) {
matchString = matchString.substring(1);
}
// Loop for each properties of the JSON Schema.
IJSONPath path = node.getPath();
if (node instanceof IJSONPair) {
IJSONSchemaProperty thisProperty = schemaDocument.getProperty(path);
ITextRegion region = contentAssistRequest.getRegion();
boolean isValue = isPairValue(context, node);
if (thisProperty != null && isValue) {
if (thisProperty.getFirstType() == JSONSchemaType.Boolean) {
if (beginsWith(FALSE, matchString.trim())) {
addStringProposal(contentAssistRequest, FALSE, false);
}
if (beginsWith(TRUE, matchString.trim())) {
addStringProposal(contentAssistRequest, TRUE, false);
}
return;
}
if (thisProperty.getFirstType() == JSONSchemaType.String) {
if (thisProperty.getEnumList() != null && thisProperty.getEnumList().size() > 0) {
for (String prop : thisProperty.getEnumList()) {
boolean showProperty = beginsWith(prop, matchString.trim());
if (showProperty) {
addStringProposal(contentAssistRequest, prop, !(region.getType() == JSONRegionContexts.JSON_VALUE_STRING));
}
}
} else {
if (thisProperty.getDefaultValue() != null) {
boolean showProperty = beginsWith(thisProperty.getDefaultValue(), matchString.trim());
if (showProperty) {
addStringProposal(contentAssistRequest, thisProperty.getDefaultValue(), !(region.getType() == JSONRegionContexts.JSON_VALUE_STRING));
}
}
}
return;
}
}
}
if (!(node instanceof IJSONObject && node.getOwnerPairNode() != null)) {
if (path.getSegments().length > 0) {
String[] segments = new String[path.getSegments().length - 1];
System.arraycopy(path.getSegments(), 0, segments, 0, path.getSegments().length - 1);
path = new JSONPath(segments);
}
}
IJSONSchemaProperty parentProperty = schemaDocument.getProperty(path);
Set<String> existing = new HashSet<String>();
boolean addComma = false;
if (node instanceof IJSONObject) {
addExisting(existing, node);
addComma = addComma(context, node);
} else if (node instanceof IJSONPair && node.getParentNode() instanceof IJSONObject) {
addExisting(existing, node.getParentNode());
}
if (parentProperty != null) {
for (IJSONSchemaProperty property : parentProperty.getPropertyValues()) {
boolean showProperty = !existing.contains(property.getName()) && beginsWith(property.getName(), matchString.trim());
if (showProperty) {
String replacementString;
if (node instanceof IJSONPair) {
replacementString = property.getName();
} else {
replacementString = ContentAssistHelper.getRequiredName(node, property);
if (addComma) {
replacementString = replacementString + ",";
}
}
String additionalProposalInfo = property.getDescription();
Image icon = JSONEditorPluginImageHelper.getInstance().getImage(property.getFirstType());
String displayString = property.getName();
JSONKeyCompletionProposal proposal = new JSONKeyCompletionProposal(replacementString, contentAssistRequest.getReplacementBeginPosition(), contentAssistRequest.getReplacementLength(), replacementString.length() - 2, icon, displayString, null, additionalProposalInfo, JSONRelevanceConstants.R_OBJECT_KEY);
contentAssistRequest.addProposal(proposal);
}
}
}
}
} catch (IOException e) {
Logger.logException(e);
}
}
Aggregations