Search in sources :

Example 81 with IDOMDocument

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);
        }
    }
}
Also used : IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) CMElementDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) ModelQueryAdapter(org.eclipse.wst.xml.core.internal.ssemodelquery.ModelQueryAdapter) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument)

Example 82 with IDOMDocument

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();
        }
    }
}
Also used : IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) Element(org.w3c.dom.Element) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument)

Example 83 with IDOMDocument

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();
        }
    }
}
Also used : IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) Element(org.w3c.dom.Element) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument) Attr(org.w3c.dom.Attr)

Example 84 with IDOMDocument

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();
        }
    }
}
Also used : CMNamedNodeMapImpl(org.eclipse.wst.xml.core.internal.contentmodel.basic.CMNamedNodeMapImpl) CMAttributeDeclarationImpl(org.eclipse.wst.xml.core.internal.contentmodel.basic.CMAttributeDeclarationImpl) ModelQueryAdapter(org.eclipse.wst.xml.core.internal.ssemodelquery.ModelQueryAdapter) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) Element(org.w3c.dom.Element) URIResolver(org.eclipse.wst.sse.core.internal.util.URIResolver) CMDataTypeImpl(org.eclipse.wst.xml.core.internal.contentmodel.basic.CMDataTypeImpl) CMElementDeclarationImpl(org.eclipse.wst.xml.core.internal.contentmodel.basic.CMElementDeclarationImpl) INodeNotifier(org.eclipse.wst.sse.core.internal.provisional.INodeNotifier) ModelQueryImpl(org.eclipse.wst.xml.core.internal.contentmodel.modelqueryimpl.ModelQueryImpl) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument) CMDocumentCache(org.eclipse.wst.xml.core.internal.contentmodel.util.CMDocumentCache) CMElementDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration) ModelQuery(org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery)

Example 85 with IDOMDocument

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);
}
Also used : DOMException(org.w3c.dom.DOMException) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) Element(org.w3c.dom.Element) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument)

Aggregations

IDOMDocument (org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument)176 IDOMModel (org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel)109 IFile (org.eclipse.core.resources.IFile)48 IStructuredModel (org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)39 Element (org.w3c.dom.Element)39 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)33 NodeList (org.w3c.dom.NodeList)27 IStatus (org.eclipse.core.runtime.IStatus)23 Node (org.w3c.dom.Node)21 CoreException (org.eclipse.core.runtime.CoreException)15 ArrayList (java.util.ArrayList)14 IJsTranslation (org.eclipse.wst.jsdt.web.core.javascript.IJsTranslation)14 JsTranslationAdapter (org.eclipse.wst.jsdt.web.core.javascript.JsTranslationAdapter)14 Document (org.w3c.dom.Document)14 IJSPTranslation (org.eclipse.jst.jsp.core.internal.java.IJSPTranslation)12 IDOMNode (org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode)12 IOException (java.io.IOException)11 CMDocument (org.eclipse.wst.xml.core.internal.contentmodel.CMDocument)11 JSPTranslationAdapter (org.eclipse.jst.jsp.core.internal.java.JSPTranslationAdapter)10 IModelManager (org.eclipse.wst.sse.core.internal.provisional.IModelManager)9