use of org.eclipse.wst.json.core.document.IJSONNode in project webtools.sourceediting by eclipse.
the class JSONStructureImpl method insertBefore.
/**
* insertBefore method
*
* @return org.w3c.dom.Node
* @param newChild
* org.w3c.dom.Node
* @param refChild
* org.w3c.dom.Node
*/
public IJSONNode insertBefore(IJSONNode newChild, IJSONNode refChild) throws JSONException {
if (newChild == null)
// nothing to do
return null;
if (refChild != null && refChild.getParentNode() != this) {
// throw new JSONException(JSONException.NOT_FOUND_ERR,
// JSONMessages.NOT_FOUND_ERR);
}
if (!isChildEditable()) {
// throw new
// JSONException(JSONException.NO_MODIFICATION_ALLOWED_ERR,
// JSONMessages.NO_MODIFICATION_ALLOWED_ERR);
}
if (newChild == refChild)
// nothing to do
return newChild;
// new child can not be a parent of this, would cause cycle
if (isParent(newChild)) {
// throw new JSONException(JSONException.HIERARCHY_REQUEST_ERR,
// JSONMessages.HIERARCHY_REQUEST_ERR);
}
// synchronized in case another thread is getting item, or length
synchronized (lockObject) {
// invalidate child nodes cache
this.childNodesCache = null;
}
JSONNodeImpl child = (JSONNodeImpl) newChild;
JSONNodeImpl next = (JSONNodeImpl) refChild;
JSONNodeImpl prev = null;
IJSONNode oldParent = child.getParentNode();
if (oldParent != null)
oldParent.removeChild(child);
if (next == null) {
prev = this.lastChild;
this.lastChild = child;
} else {
prev = (JSONNodeImpl) next.getPreviousSibling();
next.setPreviousSibling(child);
}
if (prev == null)
this.firstChild = child;
else
prev.setNextSibling(child);
child.setPreviousSibling(prev);
child.setNextSibling(next);
child.setParentNode(this);
// make sure having the same owner document
if (child.getOwnerDocument() == null) {
if (getNodeType() == DOCUMENT_NODE) {
child.setOwnerDocument((IJSONDocument) this);
} else {
child.setOwnerDocument(getOwnerDocument());
}
}
notifyChildReplaced(child, null);
return child;
}
use of org.eclipse.wst.json.core.document.IJSONNode in project webtools.sourceediting by eclipse.
the class JSONStructureImpl method cloneChildNodes.
/**
* cloneChildNodes method
*
* @param container
* org.w3c.dom.Node
* @param deep
* boolean
*/
protected void cloneChildNodes(IJSONNode newParent, boolean deep) {
if (newParent == null || newParent == this)
return;
if (!(newParent instanceof JSONStructureImpl))
return;
JSONStructureImpl container = (JSONStructureImpl) newParent;
container.removeChildNodes();
for (IJSONNode child = getFirstChild(); child != null; child = child.getNextSibling()) {
IJSONNode cloned = child.cloneNode(deep);
if (cloned != null)
container.appendChild(cloned);
}
}
use of org.eclipse.wst.json.core.document.IJSONNode in project webtools.sourceediting by eclipse.
the class JSONArrayFormatter method formatChildren.
@Override
protected void formatChildren(IJSONNode node, IRegion region, StringBuilder source) {
IJSONNode child = node.getFirstChild();
int start = region.getOffset();
int end = region.getOffset() + region.getLength();
while (child != null) {
int curEnd = child.getEndOffset();
StringBuilder childSource = null;
boolean toFinish = false;
if (start < curEnd) {
int curStart = child.getStartOffset();
if (curStart < end) {
// append child
IJSONSourceFormatter formatter = (IJSONSourceFormatter) ((INodeNotifier) child).getAdapterFor(IJSONSourceFormatter.class);
if (formatter == null) {
formatter = JSONSourceFormatterFactory.getInstance().getSourceFormatter(child);
}
if (includes(region, curStart, curEnd))
childSource = ((AbstractJSONSourceFormatter) formatter).formatProc(child);
else
childSource = ((AbstractJSONSourceFormatter) formatter).formatProc(child, overlappedRegion(region, curStart, curEnd));
} else
toFinish = true;
}
if (childSource != null) {
source.append(childSource);
}
if (toFinish)
break;
child = child.getNextSibling();
}
}
use of org.eclipse.wst.json.core.document.IJSONNode in project webtools.sourceediting by eclipse.
the class JSONArrayFormatter method formatChildren.
@Override
protected void formatChildren(IJSONNode node, StringBuilder source) {
if (node instanceof IJSONArray) {
IJSONArray array = (IJSONArray) node;
IJSONNode child = array.getFirstChild();
while (child != null) {
if (child instanceof IJSONObject || child instanceof IJSONArray) {
formatObject(node, source, child);
if (child.getNextSibling() != null) {
int start = child.getEndOffset();
int end = child.getNextSibling().getStartOffset();
if (end > start) {
IJSONCleanupStrategy stgy = getCleanupStrategy(node);
IStructuredDocument structuredDocument = node.getOwnerDocument().getModel().getStructuredDocument();
CompoundRegion[] regions = getRegionsWithoutWhiteSpaces(structuredDocument, new FormatRegion(start, end - start), stgy);
for (int i = 0; i < regions.length; i++) {
source.append(decoratedRegion(regions[i], 0, stgy));
}
}
}
String delim = getLineDelimiter(node);
source.append(delim);
source.append(getIndent(node));
} else {
formatValue(node, source, child);
}
child = child.getNextSibling();
if (child != null) {
source.append(getIndentString());
}
}
}
}
use of org.eclipse.wst.json.core.document.IJSONNode in project webtools.sourceediting by eclipse.
the class JSONFormatUtil method collectJSONNodes.
public List collectJSONNodes(IStructuredModel model, int start, int length) {
List nodes = new ArrayList();
IndexedRegion startNode = model.getIndexedRegion(start);
IndexedRegion endNode = model.getIndexedRegion(start + length - 1);
if (startNode == null || endNode == null) {
return nodes;
}
if (model instanceof IJSONModel && startNode instanceof IJSONNode && endNode instanceof IJSONNode) {
// JSON model
IJSONNode ca = getCommonAncestor((IJSONNode) startNode, (IJSONNode) endNode);
if (ca != null) {
for (IJSONNode node = ca.getFirstChild(); node != null && start + length < ((IndexedRegion) node).getStartOffset(); node = node.getNextSibling()) {
if (start < ((IndexedRegion) node).getEndOffset()) {
nodes.add(node);
}
}
}
}
return nodes;
}
Aggregations