use of org.eclipse.wst.json.core.document.IJSONNode in project webtools.sourceediting by eclipse.
the class JSONModelParser method insertStartObjectOLD.
protected void insertStartObjectOLD(IJSONObject element) {
if (element == null)
return;
if (this.context == null)
return;
insertNode(element);
JSONObjectImpl newElement = (JSONObjectImpl) element;
if (newElement.isEmptyTag() || !newElement.isContainer())
return;
// Ignore container tags that have been closed
String type = newElement.getStartStructuredDocumentRegion().getLastRegion().getType();
// if (newElement.isContainer()
// && type == JSONRegionContexts.JSON_EMPTY_TAG_CLOSE)
// return;
// demote siblings
IJSONNode parent = this.context.getParentNode();
if (parent == null)
// error
return;
IJSONNode next = this.context.getNextNode();
demoteNodes(element, element, parent, next);
// update context
IJSONNode firstChild = element.getFirstChild();
if (firstChild != null)
this.context.setCurrentNode(firstChild);
else
this.context.setParentNode(element);
}
use of org.eclipse.wst.json.core.document.IJSONNode in project webtools.sourceediting by eclipse.
the class JSONModelParser method changeStartObject.
/**
*/
private void changeStartObject(IStructuredDocumentRegion flatNode, ITextRegionList newRegions, ITextRegionList oldRegions) {
int offset = flatNode.getStart();
if (offset < 0)
// error
return;
JSONNodeImpl root = (JSONNodeImpl) this.context.getRootNode();
if (root == null)
// error
return;
IJSONNode node = root.getNodeAt(offset);
if (node == null)
// error
return;
if (node.getNodeType() != IJSONNode.OBJECT_NODE) {
changeStructuredDocumentRegion(flatNode);
return;
}
JSONObjectImpl element = (JSONObjectImpl) node;
// check if changes are only for attributes and close tag
boolean tagNameUnchanged = false;
if (newRegions != null) {
Iterator e = newRegions.iterator();
while (e.hasNext()) {
ITextRegion region = (ITextRegion) e.next();
String regionType = region.getType();
// if (regionType == JSONRegionContexts.JSON_TAG_ATTRIBUTE_NAME
// || regionType == JSONRegionContexts.JSON_TAG_ATTRIBUTE_EQUALS
// || regionType == JSONRegionContexts.JSON_TAG_ATTRIBUTE_VALUE)
// continue;
// if (regionType == JSONRegionContexts.JSON_TAG_CLOSE) {
// // change from empty tag may have impact on structure
// if (!element.isEmptyTag())
// continue;
// } else if (regionType == JSONRegionContexts.JSON_TAG_NAME
// || isNestedTagName(regionType)) {
// String oldTagName = element.getTagName();
// String newTagName = flatNode.getText(region);
// if (oldTagName != null && newTagName != null
// && oldTagName.equals(newTagName)) {
// // the tag name is unchanged
// tagNameUnchanged = true;
// continue;
// }
// }
// other region has changed
changeStructuredDocumentRegion(flatNode);
return;
}
}
// if (oldRegions != null) {
// Iterator e = oldRegions.iterator();
// while (e.hasNext()) {
// ITextRegion region = (ITextRegion) e.next();
// String regionType = region.getType();
// if (regionType == JSONRegionContexts.JSON_TAG_ATTRIBUTE_NAME
// || regionType == JSONRegionContexts.JSON_TAG_ATTRIBUTE_EQUALS
// || regionType == JSONRegionContexts.JSON_TAG_ATTRIBUTE_VALUE)
// continue;
// if (regionType == JSONRegionContexts.JSON_TAG_CLOSE) {
// // change from empty tag may have impact on structure
// if (!element.isEmptyTag())
// continue;
// } else if (regionType == JSONRegionContexts.JSON_TAG_NAME
// || isNestedTagName(regionType)) {
// // if new tag name is unchanged, it's OK
// if (tagNameUnchanged)
// continue;
// }
//
// // other region has changed
// changeStructuredDocumentRegion(flatNode);
// return;
// }
// }
// update attributes
ITextRegionList regions = flatNode.getRegions();
if (regions == null)
// error
return;
// NamedNodeMap attributes = element.getAttributes();
// if (attributes == null)
// return; // error
//
// // first remove attributes
// int regionIndex = 0;
// int attrIndex = 0;
// AttrImpl attr = null;
// while (attrIndex < attributes.getLength()) {
// attr = (AttrImpl) attributes.item(attrIndex);
// if (attr == null) { // error
// attrIndex++;
// continue;
// }
// ITextRegion nameRegion = attr.getNameRegion();
// if (nameRegion == null) { // error
// element.removeAttributeNode(attr);
// continue;
// }
// boolean found = false;
// for (int i = regionIndex; i < regions.size(); i++) {
// ITextRegion region = regions.get(i);
// if (region == nameRegion) {
// regionIndex = i + 1; // next region
// found = true;
// break;
// }
// }
// if (found) {
// attrIndex++;
// } else {
// element.removeAttributeNode(attr);
// }
// }
//
// // insert or update attributes
// attrIndex = 0; // reset to first
// AttrImpl newAttr = null;
// ITextRegion oldValueRegion = null;
// Iterator e = regions.iterator();
// while (e.hasNext()) {
// ITextRegion region = (ITextRegion) e.next();
// String regionType = region.getType();
// if (regionType == JSONRegionContexts.JSON_TAG_ATTRIBUTE_NAME) {
// if (newAttr != null) {
// // insert deferred new attribute
// element.insertAttributeNode(newAttr, attrIndex++);
// newAttr = null;
// } else if (attr != null && oldValueRegion != null) {
// // notify existing attribute value removal
// attr.notifyValueChanged();
// }
//
// oldValueRegion = null;
// attr = (AttrImpl) attributes.item(attrIndex);
// if (attr != null && attr.getNameRegion() == region) {
// // existing attribute
// attrIndex++;
// // clear other regions
// oldValueRegion = attr.getValueRegion();
// attr.setEqualRegion(null);
// attr.setValueRegion(null);
// } else {
// String name = flatNode.getText(region);
// attr = (AttrImpl) this.model.getDocument().createAttribute(
// name);
// if (attr != null)
// attr.setNameRegion(region);
// // defer insertion of new attribute
// newAttr = attr;
// }
// } else if (regionType ==
// JSONRegionContexts.JSON_TAG_ATTRIBUTE_EQUALS) {
// if (attr != null) {
// attr.setEqualRegion(region);
// }
// } else if (regionType ==
// JSONRegionContexts.JSON_TAG_ATTRIBUTE_VALUE) {
// if (attr != null) {
// attr.setValueRegion(region);
// if (attr != newAttr && oldValueRegion != region) {
// // notify existing attribute value changed
// attr.notifyValueChanged();
// }
// oldValueRegion = null;
// attr = null;
// }
// }
// }
//
// if (newAttr != null) {
// // insert deferred new attribute
// element.appendAttributeNode(newAttr);
// } else if (attr != null && oldValueRegion != null) {
// // notify existing attribute value removal
// attr.notifyValueChanged();
// }
}
use of org.eclipse.wst.json.core.document.IJSONNode in project webtools.sourceediting by eclipse.
the class JSONNodeImpl method getEndOffset.
/**
* getEndOffset method
*
* @return int
*/
@Override
public int getEndOffset() {
IJSONNode node = this;
while (node != null) {
if (node.getNodeType() == IJSONNode.OBJECT_NODE) {
JSONObjectImpl element = (JSONObjectImpl) node;
IStructuredDocumentRegion endStructuredDocumentRegion = element.getEndStructuredDocumentRegion();
if (endStructuredDocumentRegion != null)
return endStructuredDocumentRegion.getEnd();
}
IJSONNode last = node.getLastChild();
if (last != null) {
// dig into the last
node = last;
continue;
}
IStructuredDocumentRegion lastStructuredDocumentRegion = ((JSONNodeImpl) node).getStructuredDocumentRegion();
if (lastStructuredDocumentRegion != null)
return lastStructuredDocumentRegion.getEnd();
IJSONNode prev = node.getPreviousSibling();
if (prev != null) {
// move to the previous
node = prev;
continue;
}
IJSONNode parent = node.getParentNode();
node = null;
while (parent != null) {
if (parent.getNodeType() == IJSONNode.OBJECT_NODE) {
JSONObjectImpl element = (JSONObjectImpl) parent;
IStructuredDocumentRegion startStructuredDocumentRegion = element.getStartStructuredDocumentRegion();
if (startStructuredDocumentRegion != null)
return startStructuredDocumentRegion.getEnd();
}
IJSONNode parentPrev = parent.getPreviousSibling();
if (parentPrev != null) {
// move to the previous
node = parentPrev;
break;
}
parent = parent.getParentNode();
}
}
return 0;
}
use of org.eclipse.wst.json.core.document.IJSONNode in project webtools.sourceediting by eclipse.
the class JSONStructureImpl method removeChildNodes.
/**
* removeChildNodes method
*/
public void removeChildNodes() {
if (!isChildEditable()) {
// JSONMessages.NO_MODIFICATION_ALLOWED_ERR);
throw new JSONException();
}
IJSONNode nextChild = null;
for (IJSONNode child = getFirstChild(); child != null; child = nextChild) {
nextChild = child.getNextSibling();
removeChild(child);
}
}
use of org.eclipse.wst.json.core.document.IJSONNode in project webtools.sourceediting by eclipse.
the class JSONModelParser method insertNode.
/**
* insertNode method
*
* @param child
* org.w3c.dom.Node
*/
private void insertNode(IJSONNode node) {
if (node == null || this.context == null) {
return;
}
IJSONNode parent = this.context.getParentNode();
if (parent == null) {
return;
}
if (parent.getNodeType() == IJSONNode.PAIR_NODE) {
IJSONPair pair = (IJSONPair) parent;
((JSONPairImpl) pair).setValue((IJSONValue) node);
return;
}
if (parent.getLastChild() != null && parent.getLastChild().getNodeType() == IJSONNode.PAIR_NODE) {
IJSONPair pair = (IJSONPair) parent.getLastChild();
((JSONPairImpl) pair).setValue((IJSONValue) node);
return;
}
IJSONNode next = this.context.getNextNode();
insertNode(parent, node, next);
next = node.getNextSibling();
if (next != null) {
this.context.setCurrentNode(next);
} else {
this.context.setParentNode(node.getParentNode());
}
// if (node != null && this.context != null) {
// IJSONNode aparent = this.context.getParentNode();
// if (parent != null) {
// IJSONNode next = this.context.getNextNode();
// // Reset parents which are closed container elements; should not
// // be parents
// if (parent.getNodeType() == IJSONNode.OBJECT_NODE) {
// String type = ((JSONObjectImpl) parent)
// .getStartStructuredDocumentRegion().getLastRegion()
// .getType();
// if (((JSONObjectImpl) parent).isContainer()
// /* && type == JSONRegionContexts.JSON_EMPTY_TAG_CLOSE */) {
// // next = parent.getNextSibling();
// // parent = parent.getParentNode();
// } else {
// // ModelParserAdapter adapter = getParserAdapter();
// // if (adapter != null) {
// // while (parent.getNodeType() == IJSONNode.OBJECT_NODE
// // && !adapter.canContain((Element) parent,
// // node)
// // && adapter
// // .isEndTagOmissible((Element) parent)) {
// // next = parent.getNextSibling();
// // parent = parent.getParentNode();
// // }
// // }
// }
// }
// insertNode(parent, node, next);
// next = node.getNextSibling();
// if (next != null) {
// this.context.setCurrentNode(next);
// } else {
// this.context.setParentNode(node.getParentNode());
// }
// }
// }
}
Aggregations