use of org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList in project webtools.sourceediting by eclipse.
the class ElementNodeCleanupHandler method insertMissingTags.
protected IDOMNode insertMissingTags(IDOMNode node) {
boolean insertMissingTags = getCleanupPreferences().getInsertMissingTags();
IDOMNode newNode = node;
if (insertMissingTags) {
IStructuredDocumentRegion startTagStructuredDocumentRegion = node.getStartStructuredDocumentRegion();
if (startTagStructuredDocumentRegion == null) {
// implicit start tag; generate tag for it
newNode = insertStartTag(node);
startTagStructuredDocumentRegion = newNode.getStartStructuredDocumentRegion();
}
IStructuredDocumentRegion endTagStructuredDocumentRegion = newNode.getEndStructuredDocumentRegion();
ITextRegionList regionList = startTagStructuredDocumentRegion.getRegions();
if (startTagStructuredDocumentRegion != null && regionList != null && regionList.get(regionList.size() - 1).getType() == DOMRegionContext.XML_EMPTY_TAG_CLOSE) {
} else {
if (startTagStructuredDocumentRegion == null) {
// start tag missing
if (isStartTagRequired(newNode))
newNode = insertStartTag(newNode);
} else if (endTagStructuredDocumentRegion == null) {
// end tag missing
if (isEndTagRequired(newNode))
newNode = insertEndTag(newNode);
}
}
}
return newNode;
}
use of org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList in project webtools.sourceediting by eclipse.
the class ElementNodeCleanupHandler method applyTagNameCase.
protected IDOMNode applyTagNameCase(IDOMNode node) {
IDOMElement element = (IDOMElement) node;
if (element.isCommentTag())
// do nothing
return node;
int tagNameCase = HTMLCorePreferenceNames.ASIS;
if (!shouldPreserveCase(element)) {
if (isXMLTag(element))
tagNameCase = HTMLCorePreferenceNames.LOWER;
else
tagNameCase = getCleanupPreferences().getTagNameCase();
}
String oldTagName = node.getNodeName();
String newTagName = oldTagName;
IDOMNode newNode = node;
/*
* 254961 - all HTML tag names and attribute names should be in
* English even for HTML files in other languages like Japanese or
* Turkish. English locale should be used to convert between uppercase
* and lowercase (otherwise "link" would be converted to Turkish "I
* Overdot Capital").
*/
if (tagNameCase == HTMLCorePreferenceNames.LOWER)
newTagName = oldTagName.toLowerCase(Locale.US);
else if (tagNameCase == HTMLCorePreferenceNames.UPPER)
newTagName = oldTagName.toUpperCase(Locale.US);
IDOMModel structuredModel = node.getModel();
IStructuredDocument structuredDocument = structuredModel.getStructuredDocument();
IStructuredDocumentRegion startTagStructuredDocumentRegion = node.getStartStructuredDocumentRegion();
if (startTagStructuredDocumentRegion != null) {
ITextRegionList regions = startTagStructuredDocumentRegion.getRegions();
if (regions != null && regions.size() > 0) {
ITextRegion startTagNameRegion = regions.get(1);
int startTagNameStartOffset = startTagStructuredDocumentRegion.getStartOffset(startTagNameRegion);
int startTagNameLength = startTagStructuredDocumentRegion.getTextEndOffset(startTagNameRegion) - startTagNameStartOffset;
if (!newTagName.equals(oldTagName))
replaceSource(structuredModel, structuredDocument, startTagNameStartOffset, startTagNameLength, newTagName);
// save
newNode = (IDOMNode) structuredModel.getIndexedRegion(startTagNameStartOffset);
// new
// node
}
}
IStructuredDocumentRegion endTagStructuredDocumentRegion = node.getEndStructuredDocumentRegion();
if (endTagStructuredDocumentRegion != null) {
ITextRegionList regions = endTagStructuredDocumentRegion.getRegions();
if (regions != null && regions.size() > 0) {
ITextRegion endTagNameRegion = regions.get(1);
int endTagNameStartOffset = endTagStructuredDocumentRegion.getStartOffset(endTagNameRegion);
int endTagNameLength = endTagStructuredDocumentRegion.getTextEndOffset(endTagNameRegion) - endTagNameStartOffset;
if (startTagStructuredDocumentRegion != endTagStructuredDocumentRegion && !newTagName.equals(oldTagName))
replaceSource(structuredModel, structuredDocument, endTagNameStartOffset, endTagNameLength, newTagName);
}
}
return newNode;
}
use of org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList 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.sse.core.internal.provisional.text.ITextRegionList 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.sse.core.internal.provisional.text.ITextRegionList in project webtools.sourceediting by eclipse.
the class JSONSourceParser method getRegions.
/**
* Method getRegions.
*
* @param headNode
* @return List
*/
protected List getRegions(IStructuredDocumentRegion headNode) {
List allRegions = new ArrayList();
IStructuredDocumentRegion currentNode = headNode;
while (currentNode != null) {
ITextRegionList nodeRegions = currentNode.getRegions();
for (int i = 0; i < nodeRegions.size(); i++) {
allRegions.add(nodeRegions.get(i));
}
currentNode = currentNode.getNext();
}
return allRegions;
}
Aggregations