Search in sources :

Example 1 with IJSONObject

use of org.eclipse.wst.json.core.document.IJSONObject in project webtools.sourceediting by eclipse.

the class JSONModelUpdater method insertStructuredDocumentRegion.

/**
 */
private void insertStructuredDocumentRegion(IStructuredDocumentRegion newStructuredDocumentRegion) {
    if (newStructuredDocumentRegion == null)
        // error
        return;
    if (this.parentNode == null)
        // error
        return;
    int newOffset = newStructuredDocumentRegion.getStart();
    int newEnd = newStructuredDocumentRegion.getEnd();
    boolean isEndTag = false;
    // find owner node
    JSONNodeImpl ownerNode = null;
    while (this.parentNode != null) {
        if (this.nextNode != null) {
            IStructuredDocumentRegion nextStructuredDocumentRegion = this.nextNode.getStructuredDocumentRegion();
            if (nextStructuredDocumentRegion != null) {
                int nextOffset = nextStructuredDocumentRegion.getStart();
                if (nextOffset == newOffset) {
                    // found
                    ownerNode = this.nextNode;
                    break;
                }
            // if (this.nextNode.getNodeType() == Node.TEXT_NODE) {
            // int nextEnd = nextStructuredDocumentRegion.getEnd();
            // if (nextOffset < newEnd && nextEnd > newOffset) {
            // ownerNode = this.nextNode;
            // break;
            // }
            // }
            }
            IJSONNode child = this.nextNode.getFirstChild();
            if (child != null) {
                this.parentNode = this.nextNode;
                this.nextNode = (JSONNodeImpl) child;
                continue;
            }
            if (this.nextNode.getNodeType() == IJSONNode.OBJECT_NODE) {
                this.parentNode = this.nextNode;
                this.nextNode = null;
                continue;
            }
            this.nextNode = (JSONNodeImpl) this.nextNode.getNextSibling();
            if (this.nextNode != null)
                continue;
        }
        if (this.parentNode.getNodeType() == IJSONNode.OBJECT_NODE) {
            IJSONObject element = (IJSONObject) this.parentNode;
            IStructuredDocumentRegion endStructuredDocumentRegion = element.getEndStructuredDocumentRegion();
            if (endStructuredDocumentRegion != null) {
                int endOffset = endStructuredDocumentRegion.getStart();
                if (endOffset == newOffset) {
                    // found
                    ownerNode = this.parentNode;
                    isEndTag = true;
                    break;
                }
            }
        }
        this.nextNode = (JSONNodeImpl) this.parentNode.getNextSibling();
        this.parentNode = (JSONNodeImpl) this.parentNode.getParentNode();
    }
    if (ownerNode == null)
        throw new StructuredDocumentRegionManagementException();
    short nodeType = ownerNode.getNodeType();
    if (nodeType == IJSONNode.OBJECT_NODE) {
        JSONObjectImpl element = (JSONObjectImpl) ownerNode;
        if (isEndTag) {
            element.setEndStructuredDocumentRegion(newStructuredDocumentRegion);
        } else {
            element.setStartStructuredDocumentRegion(newStructuredDocumentRegion);
            updateAttrRegions(element, newStructuredDocumentRegion);
        }
    } else
        // if (nodeType == Node.TEXT_NODE) {
        // TextImpl text = (TextImpl) ownerNode;
        // IStructuredDocumentRegion oldStructuredDocumentRegion = text
        // .getStructuredDocumentRegion();
        // if (oldStructuredDocumentRegion == null) {
        // throw new StructuredDocumentRegionManagementException();
        // }
        // int oldOffset = oldStructuredDocumentRegion.getStart();
        // int oldEnd = oldStructuredDocumentRegion.getEnd();
        // if (oldOffset == newOffset && oldEnd == newEnd) {
        // text.setStructuredDocumentRegion(newStructuredDocumentRegion);
        // return;
        // }
        // 
        // if (oldStructuredDocumentRegion instanceof StructuredDocumentRegionProxy) {
        // StructuredDocumentRegionProxy proxy = (StructuredDocumentRegionProxy) oldStructuredDocumentRegion;
        // if (oldEnd > newEnd) {
        // StructuredDocumentRegionContainer container = new StructuredDocumentRegionContainer();
        // if (oldOffset == newOffset) {
        // container
        // .appendStructuredDocumentRegion(newStructuredDocumentRegion);
        // } else {
        // StructuredDocumentRegionProxy newProxy = new StructuredDocumentRegionProxy();
        // newProxy.setOffset(oldOffset);
        // newProxy.setLength(newEnd - oldOffset);
        // newProxy.setStructuredDocumentRegion(newStructuredDocumentRegion);
        // container.appendStructuredDocumentRegion(newProxy);
        // }
        // proxy.setOffset(newEnd);
        // proxy.setLength(oldEnd - newEnd);
        // container.appendStructuredDocumentRegion(proxy);
        // text.setStructuredDocumentRegion(container);
        // } else {
        // proxy.setStructuredDocumentRegion(newStructuredDocumentRegion);
        // 
        // if (oldEnd < newEnd) { // to be shared
        // this.nextNode = (JSONNodeImpl) text.getNextSibling();
        // insertStructuredDocumentRegion(newStructuredDocumentRegion);
        // }
        // }
        // return;
        // }
        // 
        // if (oldStructuredDocumentRegion instanceof StructuredDocumentRegionContainer) {
        // StructuredDocumentRegionContainer container = (StructuredDocumentRegionContainer) oldStructuredDocumentRegion;
        // int count = container.getStructuredDocumentRegionCount();
        // for (int i = 0; i < count; i++) {
        // IStructuredDocumentRegion content = container
        // .getStructuredDocumentRegion(i);
        // if (content == null)
        // continue; // error
        // int offset = content.getStart();
        // int end = content.getEnd();
        // if (end <= newOffset)
        // continue;
        // if (offset == newOffset && end == newEnd) {
        // container.replaceStructuredDocumentRegion(
        // newStructuredDocumentRegion, i);
        // return;
        // }
        // 
        // if (content instanceof StructuredDocumentRegionProxy) {
        // StructuredDocumentRegionProxy proxy = (StructuredDocumentRegionProxy) content;
        // if (end > newEnd) {
        // if (offset == newOffset) {
        // container.insertStructuredDocumentRegion(
        // newStructuredDocumentRegion, i);
        // } else {
        // StructuredDocumentRegionProxy newProxy = new StructuredDocumentRegionProxy();
        // newProxy.setOffset(offset);
        // newProxy.setLength(newEnd - offset);
        // newProxy.setStructuredDocumentRegion(newStructuredDocumentRegion);
        // container.insertStructuredDocumentRegion(
        // newProxy, i);
        // }
        // proxy.setOffset(newEnd);
        // proxy.setLength(end - newEnd);
        // return;
        // } else {
        // proxy.setStructuredDocumentRegion(newStructuredDocumentRegion);
        // if (end == newEnd)
        // return;
        // }
        // }
        // }
        // 
        // if (oldEnd < newEnd) { // to be shared
        // this.nextNode = (JSONNodeImpl) text.getNextSibling();
        // insertStructuredDocumentRegion(newStructuredDocumentRegion);
        // }
        // return;
        // } else {
        // throw new StructuredDocumentRegionManagementException();
        // }
        // } else {
        ownerNode.setStructuredDocumentRegion(newStructuredDocumentRegion);
// }
}
Also used : IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) IJSONObject(org.eclipse.wst.json.core.document.IJSONObject) IJSONNode(org.eclipse.wst.json.core.document.IJSONNode)

Example 2 with IJSONObject

use of org.eclipse.wst.json.core.document.IJSONObject in project webtools.sourceediting by eclipse.

the class JSONModelUpdater method changeStructuredDocumentRegion.

private void changeStructuredDocumentRegion(IStructuredDocumentRegion oldStructuredDocumentRegion) {
    if (oldStructuredDocumentRegion == null)
        // error
        return;
    if (this.parentNode == null)
        // error
        return;
    int oldOffset = oldStructuredDocumentRegion.getStart();
    int oldEnd = oldStructuredDocumentRegion.getEnd();
    boolean isEndTag = false;
    // find owner node
    JSONNodeImpl ownerNode = null;
    while (this.parentNode != null) {
        if (this.nextNode != null) {
            IStructuredDocumentRegion nextStructuredDocumentRegion = this.nextNode.getStructuredDocumentRegion();
            if (nextStructuredDocumentRegion != null) {
                if (nextStructuredDocumentRegion == oldStructuredDocumentRegion) {
                    ownerNode = this.nextNode;
                    break;
                }
                int nextOffset = nextStructuredDocumentRegion.getStart();
                if (nextOffset == oldOffset) {
                    // found
                    ownerNode = this.nextNode;
                    break;
                }
            // if (this.nextNode.getNodeType() == Node.TEXT_NODE) {
            // TextImpl text = (TextImpl) this.nextNode;
            // if (text.hasStructuredDocumentRegion(oldStructuredDocumentRegion)) {
            // ownerNode = this.nextNode;
            // break;
            // }
            // int nextEnd = nextStructuredDocumentRegion.getEnd();
            // if (nextOffset < oldEnd && nextEnd > oldOffset) {
            // ownerNode = this.nextNode;
            // break;
            // }
            // }
            }
            IJSONNode child = this.nextNode.getFirstChild();
            if (child != null) {
                this.parentNode = this.nextNode;
                this.nextNode = (JSONNodeImpl) child;
                continue;
            }
            if (this.nextNode.getNodeType() == IJSONNode.OBJECT_NODE) {
                this.parentNode = this.nextNode;
                this.nextNode = null;
                continue;
            }
            this.nextNode = (JSONNodeImpl) this.nextNode.getNextSibling();
            if (this.nextNode != null)
                continue;
        }
        if (this.parentNode.getNodeType() == IJSONNode.OBJECT_NODE) {
            IJSONObject element = (IJSONObject) this.parentNode;
            IStructuredDocumentRegion endStructuredDocumentRegion = element.getEndStructuredDocumentRegion();
            if (endStructuredDocumentRegion != null) {
                if (endStructuredDocumentRegion == oldStructuredDocumentRegion) {
                    ownerNode = this.parentNode;
                    isEndTag = true;
                    break;
                }
                int endOffset = endStructuredDocumentRegion.getStart();
                if (endOffset == oldOffset) {
                    // found
                    ownerNode = this.parentNode;
                    isEndTag = true;
                    break;
                }
            }
        }
        this.nextNode = (JSONNodeImpl) this.parentNode.getNextSibling();
        this.parentNode = (JSONNodeImpl) this.parentNode.getParentNode();
    }
    if (ownerNode == null)
        throw new StructuredDocumentRegionManagementException();
    short nodeType = ownerNode.getNodeType();
    if (nodeType == IJSONNode.OBJECT_NODE) {
        JSONObjectImpl element = (JSONObjectImpl) ownerNode;
        if (isEndTag) {
            element.setEndStructuredDocumentRegion(oldStructuredDocumentRegion);
        } else {
            element.setStartStructuredDocumentRegion(oldStructuredDocumentRegion);
            updateAttrRegions(element, oldStructuredDocumentRegion);
        }
    } else
        /*if (nodeType == Node.TEXT_NODE) {
			TextImpl text = (TextImpl) ownerNode;

			IStructuredDocumentRegion flatNode = text
					.getStructuredDocumentRegion();
			if (flatNode == oldStructuredDocumentRegion) {
				int newOffset = oldOffset;
				int newEnd = oldEnd;
				if (oldOffset == this.gapOffset) {
					newOffset += this.diff;
				} else {
					newEnd = this.gapOffset;
				}
				int newLength = newEnd - newOffset;
				IStructuredDocumentRegion newStructuredDocumentRegion = new StructuredDocumentRegionProxy(
						newOffset, newLength, oldStructuredDocumentRegion);
				text.setStructuredDocumentRegion(newStructuredDocumentRegion);

				if (oldEnd > newEnd) {
					this.nextNode = (JSONNodeImpl) text.getNextSibling();
					changeStructuredDocumentRegion(oldStructuredDocumentRegion);
				}
				return;
			}

			if (flatNode instanceof StructuredDocumentRegionProxy) {
				StructuredDocumentRegionProxy proxy = (StructuredDocumentRegionProxy) flatNode;
				int offset = proxy.getOffset();
				int end = offset + proxy.getLength();
				if (proxy.getStructuredDocumentRegion() == null) {
					if (offset == oldOffset && end == oldEnd) {
						text.setStructuredDocumentRegion(oldStructuredDocumentRegion);
					} else {
						if (end > oldEnd) {
							StructuredDocumentRegionContainer container = new StructuredDocumentRegionContainer();
							container
									.appendStructuredDocumentRegion(oldStructuredDocumentRegion);
							proxy.setOffset(oldEnd);
							proxy.setLength(end - oldEnd);
							container.appendStructuredDocumentRegion(proxy);
							text.setStructuredDocumentRegion(container);
						} else {
							proxy.setStructuredDocumentRegion(oldStructuredDocumentRegion);

							if (end < oldEnd) { // to be shared
								this.nextNode = (JSONNodeImpl) text
										.getNextSibling();
								changeStructuredDocumentRegion(oldStructuredDocumentRegion);
							}
						}
					}
					return;
				}

				if (offset >= this.gapOffset) {
					proxy.setOffset(offset + this.diff);
					end += this.diff;
				}
				if (end < oldEnd) { // to be shared
					this.nextNode = (JSONNodeImpl) text.getNextSibling();
					changeStructuredDocumentRegion(oldStructuredDocumentRegion);
					return;
				}
			} else if (flatNode instanceof StructuredDocumentRegionContainer) {
				StructuredDocumentRegionContainer container = (StructuredDocumentRegionContainer) flatNode;
				int count = container.getStructuredDocumentRegionCount();
				for (int i = 0; i < count; i++) {
					IStructuredDocumentRegion content = container
							.getStructuredDocumentRegion(i);
					if (content == null)
						continue; // error
					if (content == oldStructuredDocumentRegion) {
						int newOffset = oldOffset;
						int newEnd = oldEnd;
						if (oldOffset == this.gapOffset) {
							newOffset += this.diff;
						} else {
							newEnd = this.gapOffset;
						}
						int newLength = newEnd - newOffset;
						IStructuredDocumentRegion newStructuredDocumentRegion = new StructuredDocumentRegionProxy(
								newOffset, newLength,
								oldStructuredDocumentRegion);
						container.replaceStructuredDocumentRegion(
								newStructuredDocumentRegion, i);

						if (oldEnd > newEnd) { // to be shared
							this.nextNode = (JSONNodeImpl) text.getNextSibling();
							changeStructuredDocumentRegion(oldStructuredDocumentRegion);
						}
						return;
					}

					if (content instanceof StructuredDocumentRegionProxy) {
						StructuredDocumentRegionProxy proxy = (StructuredDocumentRegionProxy) content;
						int offset = proxy.getOffset();
						int end = offset + proxy.getLength();
						if (end <= oldOffset)
							continue;
						if (proxy.getStructuredDocumentRegion() == null) {
							if (offset == oldOffset && end == oldEnd) {
								container.replaceStructuredDocumentRegion(
										oldStructuredDocumentRegion, i);
							} else {
								if (end > oldEnd) {
									container.insertStructuredDocumentRegion(
											oldStructuredDocumentRegion, i);
									proxy.setOffset(oldEnd);
									proxy.setLength(end - oldEnd);
								} else {
									proxy.setStructuredDocumentRegion(oldStructuredDocumentRegion);

									if (end < oldEnd) { // to be shared
										this.nextNode = (JSONNodeImpl) text
												.getNextSibling();
										changeStructuredDocumentRegion(oldStructuredDocumentRegion);
									}
								}
							}
							return;
						}

						if (offset >= this.gapOffset) {
							proxy.setOffset(offset + this.diff);
							end += this.diff;
						}
						if (end < oldEnd) { // to be shared
							this.nextNode = (JSONNodeImpl) text.getNextSibling();
							changeStructuredDocumentRegion(oldStructuredDocumentRegion);
							return;
						}
					}
				}
			} else {
				throw new StructuredDocumentRegionManagementException();
			}
		} else {*/
        ownerNode.setStructuredDocumentRegion(oldStructuredDocumentRegion);
// }
}
Also used : IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) IJSONObject(org.eclipse.wst.json.core.document.IJSONObject) IJSONNode(org.eclipse.wst.json.core.document.IJSONNode)

Example 3 with IJSONObject

use of org.eclipse.wst.json.core.document.IJSONObject in project webtools.sourceediting by eclipse.

the class JSONModelUpdater method replaceChild.

/**
 * replaceChild method
 *
 * @param parentNode
 *            org.w3c.dom.Node
 * @param newChild
 *            org.w3c.dom.Node
 * @param oldChild
 *            org.w3c.dom.Node
 */
void replaceChild(IJSONNode parentNode, IJSONNode newChild, IJSONNode oldChild) {
    if (parentNode == null)
        return;
    if (newChild == null && oldChild == null)
        return;
    if (getStructuredDocument() == null)
        return;
    int start = 0;
    int end = 0;
    String preTag = null;
    String postTag = null;
    JSONObjectImpl postIJSONObject = null;
    if (oldChild != null) {
        JSONNodeImpl node = (JSONNodeImpl) oldChild;
        start = node.getStartOffset();
        end = node.getEndOffset();
        // if (oldChild.getNodeType() == Node.TEXT_NODE) {
        // this.gapStructuredDocumentRegion = node
        // .getStructuredDocumentRegion();
        // }
        // reset values from
        node.resetStructuredDocumentRegions();
    // IStructuredDocumentRegion
    } else {
        JSONNodeImpl prev = (JSONNodeImpl) newChild.getPreviousSibling();
        if (prev != null) {
            start = prev.getEndOffset();
            end = start;
        // preTag = getCloseTag(prev);
        } else {
            // first child
            JSONNodeImpl next = (JSONNodeImpl) newChild.getNextSibling();
            if (next != null) {
                start = next.getStartOffset();
                end = start;
                if (parentNode.getNodeType() == IJSONNode.OBJECT_NODE) {
                // preTag = getStartCloseTag((IJSONIJSONObject) parentNode);
                }
            } else {
                // newly having a child
                if (parentNode.getNodeType() == IJSONNode.OBJECT_NODE) {
                    IJSONObject element = (IJSONObject) parentNode;
                // if (element.isEmptyTag()
                // || isSelfClosedContainer(element)) { // empty
                // // tag
                // // format
                // // need to generate the start and the end tags
                // end = element.getEndOffset();
                // start = end - 2; // for "/>"
                // element.setEmptyTag(false);
                // preTag = this.generator.generateCloseTag(element);
                // postTag = this.generator.generateEndTag(element);
                // postIJSONObject = element;
                // }
                // else if (!element.hasStartTag()) {
                // start = element.getStartOffset();
                // end = start;
                // // invalid end tag or implicit tag
                // // need to generate the start tag
                // preTag = this.generator.generateStartTag(element);
                // if (preTag != null) {
                // int length = preTag.length();
                // if (length > 0) {
                // IStructuredDocumentRegion flatNode = new StructuredDocumentRegionProxy(
                // start, length);
                // element.setStartStructuredDocumentRegion(flatNode);
                // }
                // }
                // if (!element.hasEndTag()) {
                // // implicit tag
                // // need to generate the end tags
                // postTag = this.generator
                // .generateEndTag(element);
                // postIJSONObject = element;
                // }
                // } else {
                // start = element.getStartEndOffset();
                // end = start;
                // preTag = getStartCloseTag(element);
                // if (preTag != null && preTag.length() > 0) {
                // if (!element.hasEndTag()
                // && (element.isJSPContainer() || element
                // .isCDATAContainer())) {
                // // need to generate the end tag
                // postTag = this.generator
                // .generateEndTag(element);
                // postIJSONObject = element;
                // }
                // }
                // }
                }
            // else might DOCUMENT_NODE, start and end are 0
            }
        }
    }
    String source = null;
    if (newChild != null) {
        StringBuffer buffer = new StringBuffer();
        int offset = start;
        if (preTag != null) {
            int length = preTag.length();
            if (length > 0) {
                offset += length;
                buffer.append(preTag);
            }
        }
        JSONNodeImpl node = (JSONNodeImpl) newChild;
        while (node != null) {
            if (node.getNodeType() == IJSONNode.OBJECT_NODE) {
                JSONObjectImpl element = (JSONObjectImpl) node;
                // if (element.preferEmptyTag())
                // element.setEmptyTag(true);
                IStructuredDocumentRegion flatNode = null;
                String startTag = this.generator.generateStartTag(element);
                if (startTag != null) {
                    int length = startTag.length();
                    if (length > 0) {
                        buffer.append(startTag);
                        flatNode = new StructuredDocumentRegionProxy(offset, length);
                        offset += length;
                    }
                }
                element.setStartStructuredDocumentRegion(flatNode);
            } else {
                String content = this.generator.generateSource(node);
                if (content == null)
                    content = JSONNodeImpl.EMPTY_STRING;
                int length = content.length();
                IStructuredDocumentRegion flatNode = null;
                if (length > 0) {
                    buffer.append(content);
                    flatNode = new StructuredDocumentRegionProxy(offset, length);
                    offset += length;
                }
                node.setStructuredDocumentRegion(flatNode);
            }
            JSONNodeImpl child = (JSONNodeImpl) node.getFirstChild();
            if (child != null) {
                node = child;
                continue;
            }
            if (node.getNodeType() == IJSONNode.OBJECT_NODE) {
                JSONObjectImpl element = (JSONObjectImpl) node;
                IStructuredDocumentRegion flatNode = null;
                String endTag = this.generator.generateEndTag(element);
                if (endTag != null) {
                    int length = endTag.length();
                    if (length > 0) {
                        buffer.append(endTag);
                        flatNode = new StructuredDocumentRegionProxy(offset, length);
                        offset += length;
                    }
                }
                element.setEndStructuredDocumentRegion(flatNode);
            }
            while (node != null) {
                if (node == newChild) {
                    node = null;
                    break;
                }
                JSONNodeImpl next = (JSONNodeImpl) node.getNextSibling();
                if (next != null) {
                    node = next;
                    break;
                }
                node = (JSONNodeImpl) node.getParentNode();
                if (node.getNodeType() != IJSONNode.OBJECT_NODE)
                    continue;
                JSONObjectImpl element = (JSONObjectImpl) node;
                IStructuredDocumentRegion flatNode = null;
                String endTag = this.generator.generateEndTag(element);
                if (endTag != null) {
                    int length = endTag.length();
                    if (length > 0) {
                        buffer.append(endTag);
                        flatNode = new StructuredDocumentRegionProxy(offset, length);
                        offset += length;
                    }
                }
                element.setEndStructuredDocumentRegion(flatNode);
            }
        }
        if (postTag != null) {
            int length = postTag.length();
            if (length > 0) {
                buffer.append(postTag);
                if (postIJSONObject != null) {
                    IStructuredDocumentRegion flatNode = new StructuredDocumentRegionProxy(offset, length);
                    postIJSONObject.setEndStructuredDocumentRegion(flatNode);
                }
            }
        }
        source = buffer.toString();
    }
    if (start == end && (source == null || source.length() == 0)) {
        // no thing changed
        return;
    }
    replaceSource(source, start, end);
}
Also used : IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) IJSONObject(org.eclipse.wst.json.core.document.IJSONObject)

Example 4 with IJSONObject

use of org.eclipse.wst.json.core.document.IJSONObject 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();
}
Also used : IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) IJSONArray(org.eclipse.wst.json.core.document.IJSONArray) IJSONPair(org.eclipse.wst.json.core.document.IJSONPair) IJSONValue(org.eclipse.wst.json.core.document.IJSONValue) IJSONObject(org.eclipse.wst.json.core.document.IJSONObject) IJSONNode(org.eclipse.wst.json.core.document.IJSONNode)

Example 5 with IJSONObject

use of org.eclipse.wst.json.core.document.IJSONObject 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();
    }
}
Also used : IJSONSchemaProperty(org.eclipse.json.schema.IJSONSchemaProperty) IJSONPair(org.eclipse.wst.json.core.document.IJSONPair) JsonObject(org.eclipse.json.provisonnal.com.eclipsesource.json.JsonObject) IJSONValue(org.eclipse.wst.json.core.document.IJSONValue) IJSONObject(org.eclipse.wst.json.core.document.IJSONObject) IJSONNode(org.eclipse.wst.json.core.document.IJSONNode)

Aggregations

IJSONObject (org.eclipse.wst.json.core.document.IJSONObject)12 IJSONNode (org.eclipse.wst.json.core.document.IJSONNode)8 IJSONPair (org.eclipse.wst.json.core.document.IJSONPair)4 IStructuredDocumentRegion (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)4 IJSONArray (org.eclipse.wst.json.core.document.IJSONArray)3 IJSONValue (org.eclipse.wst.json.core.document.IJSONValue)3 IJSONSchemaProperty (org.eclipse.json.schema.IJSONSchemaProperty)2 ITextRegion (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)2 ITextRegionList (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList)2 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 Map (java.util.Map)1 IJSONPath (org.eclipse.json.jsonpath.IJSONPath)1 JSONPath (org.eclipse.json.jsonpath.JSONPath)1 JsonObject (org.eclipse.json.provisonnal.com.eclipsesource.json.JsonObject)1 IJSONSchemaDocument (org.eclipse.json.schema.IJSONSchemaDocument)1 Image (org.eclipse.swt.graphics.Image)1 IJSONCleanupStrategy (org.eclipse.wst.json.core.cleanup.IJSONCleanupStrategy)1