use of org.eclipse.wst.json.core.document.IJSONValue in project webtools.sourceediting by eclipse.
the class JSONContentOutlineConfiguration method getFilteredNode.
private Object getFilteredNode(Object o) {
IJSONNode node = null;
if (o instanceof IJSONNode) {
node = (IJSONNode) o;
if (node.getOwnerPairNode() != null) {
IJSONPair owner = node.getOwnerPairNode();
IJSONValue value = owner.getValue();
if (!(value instanceof IJSONArray)) {
return node.getOwnerPairNode();
}
}
/*
* short nodeType = node.getNodeType(); if (node instanceof
* IJSONValue) { while (node != null && !(node instanceof
* IJSONStyleDeclItem)) { node = node.getParentNode(); } } else if
* (nodeType == IJSONNode.STYLEDECLARATION_NODE) { node =
* node.getParentNode(); } else if (nodeType ==
* IJSONNode.MEDIALIST_NODE) { node = node.getParentNode(); }
*/
}
return node;
}
use of org.eclipse.wst.json.core.document.IJSONValue in project webtools.sourceediting by eclipse.
the class JSONModelImpl method getIndexedRegion.
@Override
public IndexedRegion getIndexedRegion(int offset) {
if (this.document == null)
return null;
// search in document children
IJSONNode parent = null;
int length = this.document.getEndOffset();
if (offset * 2 < length) {
// search from the first
IJSONNode child = (IJSONNode) this.document.getFirstChild();
while (child != null) {
if (child.getEndOffset() <= offset) {
child = (IJSONNode) child.getNextSibling();
continue;
}
if (child.getStartOffset() > offset) {
break;
}
IStructuredDocumentRegion startStructuredDocumentRegion = child.getStartStructuredDocumentRegion();
if (startStructuredDocumentRegion != null) {
if (startStructuredDocumentRegion.getEnd() > offset)
return child;
}
IStructuredDocumentRegion endStructuredDocumentRegion = child.getEndStructuredDocumentRegion();
if (endStructuredDocumentRegion != null) {
if (endStructuredDocumentRegion.getStart() <= offset) {
if (child instanceof IJSONPair) {
IJSONValue value = ((IJSONPair) child).getValue();
if (value instanceof IJSONObject || value instanceof IJSONArray) {
if (value.getStartOffset() < offset) {
child = value;
continue;
}
}
}
return child;
}
}
// dig more
parent = child;
if (parent != null && parent.getNodeType() == IJSONNode.PAIR_NODE) {
IJSONPair pair = (IJSONPair) parent;
child = pair.getValue();
} else {
child = (IJSONNode) parent.getFirstChild();
}
}
} else {
// search from the last
IJSONNode child = (IJSONNode) this.document.getLastChild();
while (child != null) {
if (child.getStartOffset() > offset) {
child = (IJSONNode) child.getPreviousSibling();
continue;
}
if (child.getEndOffset() <= offset) {
break;
}
IStructuredDocumentRegion startStructuredDocumentRegion = child.getStartStructuredDocumentRegion();
if (startStructuredDocumentRegion != null) {
if (startStructuredDocumentRegion.getEnd() > offset)
return child;
}
IStructuredDocumentRegion endStructuredDocumentRegion = child.getEndStructuredDocumentRegion();
if (endStructuredDocumentRegion != null) {
if (endStructuredDocumentRegion.getStart() <= offset) {
if (child instanceof IJSONPair) {
IJSONValue value = ((IJSONPair) child).getValue();
if (value instanceof IJSONObject || value instanceof IJSONArray) {
if (value.getStartOffset() < offset) {
child = value;
continue;
}
}
}
return child;
}
}
// dig more
parent = child;
if (parent != null && parent.getNodeType() == IJSONNode.PAIR_NODE) {
IJSONPair pair = (IJSONPair) parent;
child = pair.getValue();
} else {
child = (IJSONNode) parent.getLastChild();
}
}
}
return parent != null ? parent : document.getFirstChild();
}
use of org.eclipse.wst.json.core.document.IJSONValue in project webtools.sourceediting by eclipse.
the class JSONModelNotifierImpl method pairReplaced.
/**
* attrReplaced method
*
* @param element
* org.w3c.dom.Element
* @param newAttr
* org.w3c.dom.IJSONNode
* @param oldAttr
* org.w3c.dom.IJSONNode
*/
public void pairReplaced(IJSONObject element, IJSONPair newAttr, IJSONPair oldAttr) {
if (element == null)
return;
IJSONNode attr = null;
IJSONValue oldValue = null;
IJSONValue newValue = null;
if (oldAttr != null) {
attr = oldAttr;
oldValue = oldAttr.getValue();
}
if (newAttr != null) {
attr = newAttr;
newValue = newAttr.getValue();
}
IJSONNode notifier = (IJSONNode) element;
int offset = notifier.getStartOffset();
notify(notifier, INodeNotifier.CHANGE, attr, oldValue, newValue, offset);
propertyChanged(notifier);
}
use of org.eclipse.wst.json.core.document.IJSONValue 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.wst.json.core.document.IJSONValue 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);
}
}
}
}
Aggregations