Search in sources :

Example 41 with IDOMElement

use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement in project webtools.sourceediting by eclipse.

the class XMLModelUpdater method replaceChild.

/**
 * replaceChild method
 *
 * @param parentNode
 *            org.w3c.dom.Node
 * @param newChild
 *            org.w3c.dom.Node
 * @param oldChild
 *            org.w3c.dom.Node
 */
void replaceChild(Node parentNode, Node newChild, Node oldChild) {
    if (parentNode == null)
        return;
    if (newChild == null && oldChild == null)
        return;
    if (getStructuredDocument() == null)
        return;
    int start = 0;
    int end = 0;
    String preTag = null;
    String postTag = null;
    ElementImpl postElement = null;
    if (oldChild != null) {
        NodeImpl node = (NodeImpl) oldChild;
        start = node.getStartOffset();
        end = node.getEndOffset();
        if (oldChild.getNodeType() == Node.TEXT_NODE) {
            this.gapStructuredDocumentRegion = node.getStructuredDocumentRegion();
        }
        // reset values from
        node.resetStructuredDocumentRegions();
    // IStructuredDocumentRegion
    } else {
        NodeImpl prev = (NodeImpl) newChild.getPreviousSibling();
        if (prev != null) {
            start = prev.getEndOffset();
            end = start;
            preTag = getCloseTag(prev);
        } else {
            // first child
            NodeImpl next = (NodeImpl) newChild.getNextSibling();
            if (next != null) {
                start = next.getStartOffset();
                end = start;
                if (parentNode.getNodeType() == Node.ELEMENT_NODE) {
                    preTag = getStartCloseTag((IDOMElement) parentNode);
                }
            } else {
                // newly having a child
                if (parentNode.getNodeType() == Node.ELEMENT_NODE) {
                    ElementImpl element = (ElementImpl) parentNode;
                    if (element.isEmptyTag() || isSelfClosedContainer(element)) {
                        // empty tag format
                        // need to generate the start and the end tags
                        end = element.getEndOffset();
                        // for "/>"
                        start = end - 2;
                        element.setEmptyTag(false);
                        preTag = this.generator.generateCloseTag(element);
                        postTag = this.generator.generateEndTag(element);
                        postElement = element;
                    } else if (!element.hasStartTag()) {
                        start = element.getStartOffset();
                        end = start;
                        // invalid end tag or implicit tag
                        // need to generate the start tag
                        preTag = this.generator.generateStartTag(element);
                        if (preTag != null) {
                            int length = preTag.length();
                            if (length > 0) {
                                IStructuredDocumentRegion flatNode = new StructuredDocumentRegionProxy(start, length);
                                element.setStartStructuredDocumentRegion(flatNode);
                            }
                        }
                        if (!element.hasEndTag()) {
                            // implicit tag
                            // need to generate the end tags
                            postTag = this.generator.generateEndTag(element);
                            postElement = element;
                        }
                    } else {
                        start = element.getStartEndOffset();
                        end = start;
                        preTag = getStartCloseTag(element);
                        if (preTag != null && preTag.length() > 0) {
                            if (!element.hasEndTag() && (element.isJSPContainer() || element.isCDATAContainer())) {
                                // need to generate the end tag
                                postTag = this.generator.generateEndTag(element);
                                postElement = element;
                            }
                        }
                    }
                }
            // else might DOCUMENT_NODE, start and end are 0
            }
        }
    }
    String source = null;
    if (newChild != null) {
        StringBuffer buffer = new StringBuffer();
        int offset = start;
        if (preTag != null) {
            int length = preTag.length();
            if (length > 0) {
                offset += length;
                buffer.append(preTag);
            }
        }
        NodeImpl node = (NodeImpl) newChild;
        while (node != null) {
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                ElementImpl element = (ElementImpl) node;
                if (element.preferEmptyTag())
                    element.setEmptyTag(true);
                IStructuredDocumentRegion flatNode = null;
                String startTag = this.generator.generateStartTag(element);
                if (startTag != null) {
                    int length = startTag.length();
                    if (length > 0) {
                        buffer.append(startTag);
                        flatNode = new StructuredDocumentRegionProxy(offset, length);
                        offset += length;
                    }
                }
                element.setStartStructuredDocumentRegion(flatNode);
            } else {
                String content = this.generator.generateSource(node);
                if (content == null)
                    content = NodeImpl.EMPTY_STRING;
                int length = content.length();
                IStructuredDocumentRegion flatNode = null;
                if (length > 0) {
                    buffer.append(content);
                    flatNode = new StructuredDocumentRegionProxy(offset, length);
                    offset += length;
                }
                node.setStructuredDocumentRegion(flatNode);
            }
            NodeImpl child = (NodeImpl) node.getFirstChild();
            if (child != null) {
                node = child;
                continue;
            }
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                ElementImpl element = (ElementImpl) node;
                IStructuredDocumentRegion flatNode = null;
                String endTag = this.generator.generateEndTag(element);
                if (endTag != null) {
                    int length = endTag.length();
                    if (length > 0) {
                        buffer.append(endTag);
                        flatNode = new StructuredDocumentRegionProxy(offset, length);
                        offset += length;
                    }
                }
                element.setEndStructuredDocumentRegion(flatNode);
            }
            while (node != null) {
                if (node == newChild) {
                    node = null;
                    break;
                }
                NodeImpl next = (NodeImpl) node.getNextSibling();
                if (next != null) {
                    node = next;
                    break;
                }
                node = (NodeImpl) node.getParentNode();
                if (node.getNodeType() != Node.ELEMENT_NODE)
                    continue;
                ElementImpl element = (ElementImpl) node;
                IStructuredDocumentRegion flatNode = null;
                String endTag = this.generator.generateEndTag(element);
                if (endTag != null) {
                    int length = endTag.length();
                    if (length > 0) {
                        buffer.append(endTag);
                        flatNode = new StructuredDocumentRegionProxy(offset, length);
                        offset += length;
                    }
                }
                element.setEndStructuredDocumentRegion(flatNode);
            }
        }
        if (postTag != null) {
            int length = postTag.length();
            if (length > 0) {
                buffer.append(postTag);
                if (postElement != null) {
                    IStructuredDocumentRegion flatNode = new StructuredDocumentRegionProxy(offset, length);
                    postElement.setEndStructuredDocumentRegion(flatNode);
                }
            }
        }
        source = buffer.toString();
    }
    if (start == end && (source == null || source.length() == 0)) {
        // no thing changed
        return;
    }
    replaceSource(source, start, end);
}
Also used : IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) IDOMElement(org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement)

Example 42 with IDOMElement

use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement in project webtools.sourceediting by eclipse.

the class HTMLModelParserAdapter method createMetaElement.

/**
 */
private Element createMetaElement(Document document, String data, boolean isJSPTag) {
    if (data == null || data.length() == 0)
        return null;
    // one line
    TagScanner scanner = new TagScanner(data, 0, true);
    String name = scanner.nextName();
    if (name == null || !name.equalsIgnoreCase(MetaData.METADATA))
        return null;
    String type = null;
    boolean isStartSpan = false;
    boolean isEndSpan = false;
    name = scanner.nextName();
    while (name != null) {
        String value = scanner.nextValue();
        if (name.equalsIgnoreCase(MetaData.TYPE)) {
            if (value == null)
                return null;
            if (value.equalsIgnoreCase(MetaData.DESIGNER_CONTROL)) {
                type = MetaData.DESIGNER_CONTROL;
            } else if (value.equalsIgnoreCase(MetaData.DYNAMIC_DATA)) {
                type = MetaData.DYNAMIC_DATA;
            } else if (value.equalsIgnoreCase(MetaData.AUTHOR_TIME_VISUAL)) {
                type = MetaData.AUTHOR_TIME_VISUAL;
            } else if (value.equalsIgnoreCase(MetaData.ANNOTATION)) {
                type = MetaData.ANNOTATION;
            } else {
                return null;
            }
        } else if (name.equalsIgnoreCase(MetaData.STARTSPAN)) {
            isStartSpan = true;
        } else if (name.equalsIgnoreCase(MetaData.ENDSPAN)) {
            if (!isStartSpan)
                isEndSpan = true;
        }
        name = scanner.nextName();
    }
    if (type == null)
        return null;
    if (!isStartSpan && !isEndSpan)
        return null;
    String metaData = null;
    // skip new line
    int offset = scanner.getNextOffset();
    if (offset < data.length())
        metaData = data.substring(offset);
    if (metaData == null)
        metaData = new String();
    IDOMElement element = (IDOMElement) document.createElement(MetaData.PREFIX + type);
    MetaDataAdapter adapter = new MetaDataAdapter(type);
    if (isStartSpan) {
        if (metaData != null)
            adapter.setData(metaData);
    } else {
        if (metaData != null)
            adapter.setEndData(metaData);
    }
    element.addAdapter(adapter);
    adapter.setElement(element);
    element.setJSPTag(isJSPTag);
    return element;
}
Also used : IDOMElement(org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement)

Example 43 with IDOMElement

use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement in project webtools.sourceediting by eclipse.

the class HTMLModelParserAdapter method canContain.

/**
 */
public boolean canContain(Element element, Node child) {
    if (element == null || child == null)
        return false;
    IDOMElement impl = (IDOMElement) element;
    if (child.getNodeType() == Node.ELEMENT_NODE) {
        if (!impl.isGlobalTag())
            // non HTML tag
            return true;
        IDOMElement childElement = (IDOMElement) child;
        CMElementDeclaration myDec = CMNodeUtil.getElementDeclaration(element);
        if (myDec == null)
            return true;
        // if (!(myDec instanceof HTMLElementDeclaration)) return true;
        if (myDec.getContentType() == CMElementDeclaration.EMPTY)
            return false;
        if (!childElement.isGlobalTag())
            // non HTML tag
            return true;
        CMElementDeclaration childDec = CMNodeUtil.getElementDeclaration(childElement);
        if (childDec == null)
            return true;
        if (myDec instanceof HTMLElementDeclaration) {
            if (((Boolean) ((HTMLElementDeclaration) myDec).getProperty(HTMLCMProperties.IS_JSP)).booleanValue())
                return true;
        }
        if (shouldTerminateAt(myDec, childDec) && !isValidChild(myDec, childDec)) {
            return false;
        }
        String tagName = impl.getTagName();
        if (tagName == null)
            return true;
        String childName = childElement.getTagName();
        if (childName == null)
            return true;
        if (!impl.hasStartTag() && !impl.hasEndTag()) {
            // implicit element
            if (tagName.equalsIgnoreCase(childElement.getTagName()))
                return false;
            if (tagName.equalsIgnoreCase(HTML40Namespace.ElementName.HEAD)) {
                if (!childName.equalsIgnoreCase(HTML40Namespace.ElementName.META) && !childName.equalsIgnoreCase(HTML40Namespace.ElementName.TITLE) && !childName.equalsIgnoreCase(HTML40Namespace.ElementName.LINK) && !childName.equalsIgnoreCase(HTML40Namespace.ElementName.STYLE) && !childName.equalsIgnoreCase(HTML40Namespace.ElementName.BASE) && !childName.equalsIgnoreCase(HTML40Namespace.ElementName.ISINDEX)) {
                    return false;
                }
            }
            Node parent = element.getParentNode();
            if (parent != null && parent.getNodeType() == Node.ELEMENT_NODE) {
                IDOMElement parentElement = (IDOMElement) parent;
                if (!parentElement.hasStartTag() && !parentElement.hasEndTag()) {
                    if (!canContain(parentElement, child))
                        return false;
                }
            }
            return true;
        }
        // contexual termination for TABLE content tags
        boolean isTableContent = false;
        if (childName.equalsIgnoreCase(HTML40Namespace.ElementName.TBODY) || childName.equalsIgnoreCase(HTML40Namespace.ElementName.THEAD) || childName.equalsIgnoreCase(HTML40Namespace.ElementName.TFOOT)) {
            if (tagName.equalsIgnoreCase(HTML40Namespace.ElementName.TABLE))
                return true;
            isTableContent = true;
        } else if (childName.equalsIgnoreCase(HTML40Namespace.ElementName.TR)) {
            if (tagName.equalsIgnoreCase(HTML40Namespace.ElementName.TBODY) || tagName.equalsIgnoreCase(HTML40Namespace.ElementName.THEAD) || tagName.equalsIgnoreCase(HTML40Namespace.ElementName.TFOOT) || tagName.equalsIgnoreCase(HTML40Namespace.ElementName.TABLE))
                return true;
            isTableContent = true;
        } else if (childName.equalsIgnoreCase(HTML40Namespace.ElementName.TD) || childName.equalsIgnoreCase(HTML40Namespace.ElementName.TH)) {
            if (tagName.equalsIgnoreCase(HTML40Namespace.ElementName.TR) || tagName.equalsIgnoreCase(HTML40Namespace.ElementName.TBODY) || tagName.equalsIgnoreCase(HTML40Namespace.ElementName.THEAD) || tagName.equalsIgnoreCase(HTML40Namespace.ElementName.TFOOT) || tagName.equalsIgnoreCase(HTML40Namespace.ElementName.TABLE))
                return true;
            isTableContent = true;
        }
        if (isTableContent) {
            // if in TABLE
            for (Node parent = element.getParentNode(); parent != null; parent = parent.getParentNode()) {
                if (parent.getNodeType() != Node.ELEMENT_NODE)
                    break;
                IDOMElement parentElement = (IDOMElement) parent;
                String parentName = parentElement.getTagName();
                if (parentName == null)
                    continue;
                if (parentName.equalsIgnoreCase(HTML40Namespace.ElementName.TABLE))
                    return false;
            }
        }
        if (tagName.equalsIgnoreCase(HTML40Namespace.ElementName.EMBED)) {
            if (!childName.equalsIgnoreCase(HTML40Namespace.ElementName.NOEMBED))
                return false;
        }
    } else if (child.getNodeType() == Node.TEXT_NODE) {
        String tagName = impl.getTagName();
        if (tagName != null && tagName.equalsIgnoreCase(HTML40Namespace.ElementName.EMBED)) {
            IDOMText text = (IDOMText) child;
            if (!text.isElementContentWhitespace())
                return false;
        }
    } else if (child.getNodeType() == Node.DOCUMENT_TYPE_NODE) {
        if (impl.isImplicitTag())
            return false;
    }
    return true;
}
Also used : CMElementDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration) Node(org.w3c.dom.Node) CMNode(org.eclipse.wst.xml.core.internal.contentmodel.CMNode) IDOMText(org.eclipse.wst.xml.core.internal.provisional.document.IDOMText) HTMLElementDeclaration(org.eclipse.wst.html.core.internal.contentmodel.HTMLElementDeclaration) IDOMElement(org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement)

Example 44 with IDOMElement

use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement in project webtools.sourceediting by eclipse.

the class AnnotationSection method doSerialize.

private String doSerialize(Element element) throws IOException {
    // $NON-NLS-1$
    String source = "";
    Node firstChild = element.getFirstChild();
    Node lastChild = element.getLastChild();
    int start = 0;
    int end = 0;
    if (element instanceof IDOMElement) {
        IDOMElement domElement = (IDOMElement) element;
        IDOMModel model = domElement.getModel();
        IDOMDocument doc = model.getDocument();
        if (firstChild instanceof IDOMNode) {
            IDOMNode first = (IDOMNode) firstChild;
            start = first.getStartOffset();
        }
        if (lastChild instanceof IDOMNode) {
            IDOMNode last = (IDOMNode) lastChild;
            end = last.getEndOffset();
        }
        source = doc.getSource().substring(start, end);
    }
    return source;
}
Also used : IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) Node(org.w3c.dom.Node) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument) IDOMElement(org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement)

Example 45 with IDOMElement

use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement in project webtools.sourceediting by eclipse.

the class SelectAttributeContentAssist method adjustXPathStart.

/**
 * This needs to setup the content assistance correctly. Here is what needs
 * to happen: 1. Adjust the matchString (This should have been calculated
 * earlier) 2. Get the current tokens offset position..this will be the
 * starting offset. 3. Get the replacement length...this is the difference
 * between the token offset and the next token or end of the string
 *
 * @param attrName
 *            The name of the attribute to use as the starting node.
 */
protected void adjustXPathStart(String attrName) {
    IDOMElement elem = (IDOMElement) getNode();
    IDOMAttr xpathNode = (IDOMAttr) elem.getAttributeNode(attrName);
    if (xpathNode == null) {
        return;
    }
    String xpathString = xpathNode.getValue();
    if (xpathString.length() == 0) {
        return;
    }
    int startOffset = xpathNode.getValueRegionStartOffset();
    replacementLength = getReplacementBeginPosition() - startOffset;
}
Also used : IDOMAttr(org.eclipse.wst.xml.core.internal.provisional.document.IDOMAttr) IDOMElement(org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement)

Aggregations

IDOMElement (org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement)59 IDOMModel (org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel)20 IDOMNode (org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode)19 Node (org.w3c.dom.Node)18 NodeList (org.w3c.dom.NodeList)13 Element (org.w3c.dom.Element)11 IStructuredDocumentRegion (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)9 IDOMAttr (org.eclipse.wst.xml.core.internal.provisional.document.IDOMAttr)9 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)8 CMElementDeclaration (org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration)8 NamedNodeMap (org.w3c.dom.NamedNodeMap)8 ArrayList (java.util.ArrayList)7 ITextRegion (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)7 IDOMDocument (org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument)7 IFile (org.eclipse.core.resources.IFile)6 ITextRegionList (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList)6 List (java.util.List)5 CMNamedNodeMap (org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap)5 Text (org.w3c.dom.Text)5 ModelQuery (org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery)4