use of org.eclipse.wst.json.core.document.IJSONValue in project webtools.sourceediting by eclipse.
the class SchemaProcessorRegistryReader method getSchemaDocument.
public IJSONSchemaDocument getSchemaDocument(IJSONModel model) throws IOException {
IJSONSchemaProcessor processor = getDefaultProcessor();
if (processor == null) {
return null;
}
IJSONDocument document = model.getDocument();
IJSONNode jsonObject = document.getFirstChild();
if (jsonObject != null) {
IJSONNode child = jsonObject.getFirstChild();
while (child != null) {
if (child instanceof IJSONPair) {
IJSONPair pair = (IJSONPair) child;
String name = pair.getName();
IJSONValue valueNode = pair.getValue();
if (valueNode != null && "$schema".equals(name)) {
// $NON-NLS-1$
String schema = JSONUtil.getString(valueNode);
try {
if (schema != null) {
schema = URIHelper.addImpliedFileProtocol(schema);
new URL(schema);
return processor.getSchema(schema);
}
} catch (MalformedURLException e) {
}
}
}
child = child.getNextSibling();
}
}
String base = model == null || model.getResolver() == null ? null : model.getResolver().getFileBaseLocation();
// Assert.isNotNull(base, "Base location is expected to be non null."); //$NON-NLS-1$
if (base != null) {
base = URIHelper.addImpliedFileProtocol(base);
}
String schemaURL = resolve(base, null, null);
if (schemaURL != null) {
return processor.getSchema(schemaURL);
}
return null;
}
use of org.eclipse.wst.json.core.document.IJSONValue 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.wst.json.core.document.IJSONValue in project webtools.sourceediting by eclipse.
the class TestUtil method serialize.
private static void serialize(IJSONNode node, StringBuilder json) {
if (node == null) {
return;
}
// .getNextSibling()) {
switch(node.getNodeType()) {
case IJSONNode.DOCUMENT_NODE:
serialize(node.getFirstChild(), json);
break;
case IJSONNode.OBJECT_NODE:
case IJSONNode.ARRAY_NODE:
IJSONStructure object = (IJSONStructure) node;
json.append(object.getFirstStructuredDocumentRegion().getText());
serialize(node.getFirstChild(), json);
if (object.isClosed()) {
json.append(object.getEndStructuredDocumentRegion().getText());
}
if (node.getNextSibling() != null) {
json.append(",");
serialize(node.getNextSibling(), json);
}
break;
case IJSONNode.PAIR_NODE:
IJSONPair pair = (IJSONPair) node;
json.append("\"");
json.append(pair.getName());
json.append("\"");
if (pair.getValue() != null) {
json.append(":");
serialize(pair.getValue(), json);
}
if (node.getNextSibling() != null) {
json.append(",");
serialize(node.getNextSibling(), json);
}
break;
case IJSONNode.VALUE_BOOLEAN_NODE:
case IJSONNode.VALUE_NULL_NODE:
case IJSONNode.VALUE_NUMBER_NODE:
case IJSONNode.VALUE_STRING_NODE:
IJSONValue value = (IJSONValue) node;
json.append(value.getSimpleValue());
if (node.getNextSibling() != null) {
json.append(",");
serialize(node.getNextSibling(), json);
}
break;
}
// }
}
use of org.eclipse.wst.json.core.document.IJSONValue in project webtools.sourceediting by eclipse.
the class JSONPairFormatter method formatChildren.
@Override
protected void formatChildren(IJSONNode node, StringBuilder source) {
if (node instanceof IJSONPair) {
IJSONPair pair = (IJSONPair) node;
IJSONValue value = pair.getValue();
if (value instanceof IJSONObject || value instanceof IJSONArray) {
formatObject(node, source, value);
} else {
formatValue(node, source, value);
}
}
}
use of org.eclipse.wst.json.core.document.IJSONValue 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);
}
}
}
Aggregations