use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument in project webtools.sourceediting by eclipse.
the class ElementNodeFormatter method formatEndTag.
protected void formatEndTag(IDOMNode node, IStructuredFormatContraints formatContraints) {
if (!isEndTagMissing(node)) {
// end tag exists
IStructuredDocument structuredDocument = node.getModel().getStructuredDocument();
String lineDelimiter = structuredDocument.getLineDelimiter();
String nodeIndentation = getNodeIndent(node);
IDOMNode lastChild = (IDOMNode) node.getLastChild();
if (lastChild != null && lastChild.getNodeType() != Node.TEXT_NODE) {
if (isEndTagMissing(lastChild)) {
// find deepest child
IDOMNode deepestChild = (IDOMNode) lastChild.getLastChild();
while (deepestChild != null && deepestChild.getLastChild() != null && isEndTagMissing(deepestChild)) {
lastChild = deepestChild;
deepestChild = (IDOMNode) deepestChild.getLastChild();
}
if (deepestChild != null) {
if (deepestChild.getNodeType() == Node.TEXT_NODE) {
// Special indentation handling if lastChild's end
// tag is missing and deepestChild is a text node.
String nodeText = deepestChild.getNodeValue();
if (!nodeText.endsWith(lineDelimiter + nodeIndentation)) {
nodeText = StringUtils.appendIfNotEndWith(nodeText, lineDelimiter);
nodeText = StringUtils.appendIfNotEndWith(nodeText, nodeIndentation);
}
replaceNodeValue(deepestChild, nodeText);
} else
insertAfterNode(lastChild, lineDelimiter + nodeIndentation);
}
} else
// indent end tag
insertAfterNode(lastChild, lineDelimiter + nodeIndentation);
} else if (lastChild == null && firstStructuredDocumentRegionContainsLineDelimiters(node)) {
// BUG174243 do not indent end tag if node has empty content
// (otherwise new text node would be introduced)
ModelQueryAdapter adapter = (ModelQueryAdapter) ((IDOMDocument) node.getOwnerDocument()).getAdapterFor(ModelQueryAdapter.class);
CMElementDeclaration elementDeclaration = (CMElementDeclaration) adapter.getModelQuery().getCMNode(node);
if ((elementDeclaration == null) || (elementDeclaration.getContentType() != CMElementDeclaration.EMPTY)) {
// indent end tag
replace(structuredDocument, node.getFirstStructuredDocumentRegion().getEndOffset(), 0, lineDelimiter + nodeIndentation);
}
}
// format end tag name
IStructuredDocumentRegion endTagStructuredDocumentRegion = node.getLastStructuredDocumentRegion();
if (endTagStructuredDocumentRegion.getRegions().size() >= 3) {
ITextRegion endTagNameRegion = endTagStructuredDocumentRegion.getRegions().get(1);
removeRegionSpaces(node, endTagStructuredDocumentRegion, endTagNameRegion);
}
}
}
use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument in project webtools.sourceediting by eclipse.
the class ElementImplTests method testRemoveAttrWithImpliedDefault.
public void testRemoveAttrWithImpliedDefault() {
IDOMModel model = null;
try {
model = (IDOMModel) getModelForRead("testfiles/time.xml");
if (model != null) {
IDOMDocument document = model.getDocument();
Element element = document.getDocumentElement();
assertNotNull(element);
// Default value should be 0
assertEquals("0", element.getAttribute("hour"));
element.setAttribute("hour", "12");
assertEquals("12", element.getAttribute("hour"));
// value should be reset to default/0
element.removeAttribute("hour");
assertEquals("0", element.getAttribute("hour"));
}
} finally {
if (model != null) {
model.releaseFromRead();
}
}
}
use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument in project webtools.sourceediting by eclipse.
the class ElementImplTests method testOwnerElementWithImpliedDefaultAttribute.
public void testOwnerElementWithImpliedDefaultAttribute() {
IDOMModel model = null;
try {
model = (IDOMModel) getModelForRead("testfiles/time.xml");
if (model != null) {
IDOMDocument document = model.getDocument();
Element element = document.getDocumentElement();
assertNotNull(element);
Attr attr = element.getAttributeNode("hour");
assertNull(attr);
}
} finally {
if (model != null) {
model.releaseFromRead();
}
}
}
use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument in project webtools.sourceediting by eclipse.
the class ElementImplTests method testCMAttrWithNullImpliedValue.
public void testCMAttrWithNullImpliedValue() {
IDOMModel model = null;
try {
model = (IDOMModel) getModelForRead("testfiles/time.xml");
if (model != null) {
IDOMDocument document = model.getDocument();
final String ATTR_NAME = "second";
// Setup a ModelQueryAdapter whose sole purpose it to provide a attribute declaration with a null implied value
document.addAdapter(new ModelQueryAdapter() {
public boolean isAdapterForType(Object type) {
return type.equals(ModelQueryAdapter.class);
}
public void notifyChanged(INodeNotifier notifier, int eventType, Object changedFeature, Object oldValue, Object newValue, int pos) {
}
public CMDocumentCache getCMDocumentCache() {
return null;
}
public org.eclipse.wst.common.uriresolver.internal.provisional.URIResolver getIdResolver() {
return null;
}
public ModelQuery getModelQuery() {
return new ModelQueryImpl(null) {
/* (non-Javadoc)
* @see org.eclipse.wst.xml.core.internal.contentmodel.modelqueryimpl.ModelQueryImpl#getCMElementDeclaration(org.w3c.dom.Element)
*/
public CMElementDeclaration getCMElementDeclaration(Element element) {
final CMElementDeclaration decl = new CMElementDeclarationImpl(null, null);
CMNamedNodeMapImpl map = (CMNamedNodeMapImpl) decl.getAttributes();
map.put(new CMAttributeDeclarationImpl(ATTR_NAME, CMAttributeDeclaration.OPTIONAL, new CMDataTypeImpl(ATTR_NAME, (String) null)));
return decl;
}
};
}
public void release() {
}
public void setIdResolver(org.eclipse.wst.common.uriresolver.internal.provisional.URIResolver newIdResolver) {
}
});
Element element = document.getDocumentElement();
assertNotNull(element);
// Default value should be 0
assertEquals("", element.getAttribute(ATTR_NAME));
}
} finally {
if (model != null) {
model.releaseFromRead();
}
}
}
use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument in project webtools.sourceediting by eclipse.
the class NodeContainerTests method testAppendParentAsChildOfChild.
public void testAppendParentAsChildOfChild() {
IDOMModel model = (IDOMModel) StructuredModelManager.getModelManager().createUnManagedStructuredModelFor(ContentTypeIdForXML.ContentTypeID_XML);
model.getStructuredDocument().set(CONTENT_2);
IDOMDocument doc = model.getDocument();
Element a = doc.getElementById("a");
assertNotNull("Could not find element with id 'a' in " + CONTENT_2, a);
Element b = doc.getElementById("b");
assertNotNull("Could not find element with id 'b' in " + CONTENT_2, b);
boolean threwException = false;
try {
b.appendChild(a);
} catch (DOMException e) {
assertEquals("Wrong type of exception was thrown: " + e, DOMException.HIERARCHY_REQUEST_ERR, e.code);
threwException = true;
}
assertTrue("A DOMException with code HIERARCHY_REQUEST_ERR should have been thrown when appending a parent to its own child", threwException);
}
Aggregations