use of org.eclipse.wst.json.core.document.IJSONObject in project webtools.sourceediting by eclipse.
the class JSONModelParser method insertValue.
private void insertValue(IStructuredDocumentRegion flatNode, String regionType) {
ITextRegionList regions = flatNode.getRegions();
if (regions == null)
return;
ITextRegion nameRegion = StructuredDocumentRegionUtil.getFirstRegion(flatNode);
// Create JSON Value
JSONValueImpl value = (JSONValueImpl) createJSONValue(regionType);
value.setStructuredDocumentRegion(flatNode);
// inserted as the value of an existing pair node
if (this.context.getCurrentNode() != null && this.context.getCurrentNode() instanceof JSONPairImpl) {
JSONPairImpl pair = (JSONPairImpl) this.context.getCurrentNode();
pair.setValue(value);
JSONNodeImpl parent = (JSONNodeImpl) this.context.getParentNode();
parent.insertBefore(pair, this.context.getNextNode());
// parent node
if (parent instanceof IJSONObject) {
JSONObjectImpl parentObject = (JSONObjectImpl) parent;
parentObject.add(pair);
}
} else {
// There is no context, it means the model is reloaded or loaded for
// the first time
JSONStructureImpl structure = (JSONStructureImpl) this.context.findParentStructure();
if (structure != null) {
if (structure.getNodeType() == IJSONNode.OBJECT_NODE) {
// children
if (structure.getLastChild() != null && structure.getLastChild().getNodeType() == IJSONNode.PAIR_NODE) {
((JSONPairImpl) structure.getLastChild()).setValue(value);
}
if (structure.getParentOrPairNode() instanceof JSONPairImpl) {
JSONPairImpl parentPair = (JSONPairImpl) structure.getParentOrPairNode();
if (structure.getParentNode() != parentPair.getValue()) {
parentPair.setValue(structure);
}
}
} else if (structure.getNodeType() == IJSONNode.ARRAY_NODE) {
// If the parent is a JSONArray insert the new JSONValue at
// the end of the structure
JSONArrayImpl array = (JSONArrayImpl) structure;
structure.insertBefore(value, null);
array.add(value);
JSONPairImpl ownerPair = (JSONPairImpl) array.getParentOrPairNode();
if (ownerPair.getValue() == null)
ownerPair.setValue(array);
else
ownerPair.updateValue(array);
} else {
insertNode(structure, value, null);
}
}
}
}
use of org.eclipse.wst.json.core.document.IJSONObject in project webtools.sourceediting by eclipse.
the class JSONModelParser method promoteNodes.
/**
*/
private void promoteNodes(IJSONNode root, IJSONNode newParent, IJSONNode newNext, IJSONNode oldParent, IJSONNode next) {
JSONObjectImpl newElement = null;
if (newParent.getNodeType() == IJSONNode.OBJECT_NODE) {
newElement = (JSONObjectImpl) newParent;
}
IJSONNode rootParent = root.getParentNode();
while (oldParent != rootParent) {
while (next != null) {
boolean done = false;
boolean endTag = false;
if (next.getNodeType() == IJSONNode.OBJECT_NODE) {
JSONObjectImpl nextElement = (JSONObjectImpl) next;
if (!nextElement.hasStartTag()) {
IJSONNode nextChild = nextElement.getFirstChild();
if (nextChild != null) {
// promote children
next = nextChild;
oldParent = nextElement;
continue;
}
// if (nextElement.hasEndTag()) {
// if (nextElement.matchEndTag(newElement)) {
// endTag = true;
// }
// } else {
// remove implicit element
next = nextElement.getNextSibling();
oldParent.removeChild(nextElement);
done = true;
// }
}
}
if (!done) {
if (!endTag && newElement != null) /* && !canContain(newElement, next) */
{
newParent = newElement.getParentNode();
if (newParent == null)
// error
return;
IJSONNode elementNext = newElement.getNextSibling();
// promote siblings
promoteNodes(newElement, newParent, elementNext, newElement, newNext);
newNext = newElement.getNextSibling();
if (newParent.getNodeType() == IJSONNode.OBJECT_NODE) {
newElement = (JSONObjectImpl) newParent;
} else {
newElement = null;
}
continue;
}
IJSONNode child = next;
next = next.getNextSibling();
oldParent.removeChild(child);
insertNode(newParent, child, newNext);
IJSONNode childParent = child.getParentNode();
if (childParent != newParent) {
newParent = childParent;
newElement = (JSONObjectImpl) newParent;
newNext = child.getNextSibling();
}
}
}
if (oldParent.getNodeType() != IJSONNode.OBJECT_NODE)
return;
JSONObjectImpl oldElement = (JSONObjectImpl) oldParent;
oldParent = oldElement.getParentNode();
if (oldParent == null)
// error
return;
next = oldElement.getNextSibling();
if (oldElement.hasEndTag()) {
IJSONObject end = null;
if (!oldElement.hasChildNodes() && !oldElement.hasStartTag()) {
oldParent.removeChild(oldElement);
end = oldElement;
} else {
// end = oldElement.removeEndTag();
}
if (end != null) {
insertNode(newParent, end, newNext);
IJSONNode endParent = end.getParentNode();
if (endParent != newParent) {
newParent = endParent;
newElement = (JSONObjectImpl) newParent;
newNext = end.getNextSibling();
}
}
}
}
}
use of org.eclipse.wst.json.core.document.IJSONObject 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.IJSONObject 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.IJSONObject in project webtools.sourceediting by eclipse.
the class JSONModelParser method insertObject.
private void insertObject(IStructuredDocumentRegion flatNode) {
ITextRegionList regions = flatNode.getRegions();
if (regions == null)
return;
JSONObjectImpl element = null;
if (this.context.getCurrentNode() != null && this.context.getCurrentNode() instanceof IJSONObject) {
element = (JSONObjectImpl) this.context.getCurrentNode();
} else {
element = (JSONObjectImpl) this.model.getDocument().createJSONObject();
}
if (element == null) {
return;
}
element.setStartStructuredDocumentRegion(flatNode);
insertStartObjectOLD(element);
}
Aggregations