Search in sources :

Example 11 with IJSONNode

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

the class AbstractJSONSourceFormatter method getIndent.

protected String getIndent(IJSONNode node) {
    if (node == null)
        // $NON-NLS-1$
        return "";
    IJSONNode parent = node.getParentNode();
    if (parent == null || parent instanceof IJSONDocument)
        // $NON-NLS-1$
        return "";
    String parentIndent = getIndent(parent);
    return parentIndent + getIndentString();
}
Also used : IJSONDocument(org.eclipse.wst.json.core.document.IJSONDocument) IJSONNode(org.eclipse.wst.json.core.document.IJSONNode)

Example 12 with IJSONNode

use of org.eclipse.wst.json.core.document.IJSONNode 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 13 with IJSONNode

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

the class JSONModelNotifierImpl method childReplaced.

/**
 * childReplaced method
 *
 * @param parentNode
 *            org.w3c.dom.Node
 * @param newChild
 *            org.w3c.dom.Node
 * @param oldChild
 *            org.w3c.dom.Node
 */
@Override
public void childReplaced(IJSONNode parentNode, IJSONNode newChild, IJSONNode oldChild) {
    if (parentNode == null)
        return;
    IJSONNode notifier = (IJSONNode) parentNode;
    int type = INodeNotifier.CHANGE;
    if (newChild == null)
        type = INodeNotifier.REMOVE;
    else if (oldChild == null)
        type = INodeNotifier.ADD;
    int offset = notifier.getStartOffset();
    notify(notifier, type, oldChild, oldChild, newChild, offset);
    structureChanged(notifier);
}
Also used : IJSONNode(org.eclipse.wst.json.core.document.IJSONNode)

Example 14 with IJSONNode

use of org.eclipse.wst.json.core.document.IJSONNode 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);
}
Also used : IJSONValue(org.eclipse.wst.json.core.document.IJSONValue) IJSONNode(org.eclipse.wst.json.core.document.IJSONNode)

Example 15 with IJSONNode

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

the class Validator method getProperties.

private Set<String> getProperties(IJSONNode node) {
    Set<String> properties = new HashSet<String>();
    IJSONNode child = node.getFirstChild();
    while (child != null) {
        if (child instanceof IJSONPair) {
            IJSONPair pair = (IJSONPair) child;
            if (pair.getName() != null) {
                properties.add(pair.getName());
            }
        }
        child = child.getNextSibling();
    }
    return properties;
}
Also used : IJSONPair(org.eclipse.wst.json.core.document.IJSONPair) IJSONNode(org.eclipse.wst.json.core.document.IJSONNode) HashSet(java.util.HashSet)

Aggregations

IJSONNode (org.eclipse.wst.json.core.document.IJSONNode)56 IStructuredDocumentRegion (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)10 IJSONObject (org.eclipse.wst.json.core.document.IJSONObject)9 IJSONPair (org.eclipse.wst.json.core.document.IJSONPair)9 IJSONValue (org.eclipse.wst.json.core.document.IJSONValue)8 IJSONArray (org.eclipse.wst.json.core.document.IJSONArray)4 IJSONDocument (org.eclipse.wst.json.core.document.IJSONDocument)4 ITextRegion (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)4 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 Iterator (java.util.Iterator)3 IndexedRegion (org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)3 IJSONSchemaProperty (org.eclipse.json.schema.IJSONSchemaProperty)2 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)2 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 List (java.util.List)1