Search in sources :

Example 71 with ITextRegionList

use of org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList in project webtools.sourceediting by eclipse.

the class XMLModelParser method insertNestedTag.

/**
 * insertJSPTag method
 */
private void insertNestedTag(IStructuredDocumentRegion flatNode) {
    ITextRegionList regions = flatNode.getRegions();
    if (regions == null)
        return;
    String tagName = null;
    AttrImpl attr = null;
    List attrNodes = null;
    boolean isCloseTag = false;
    Iterator e = regions.iterator();
    while (e.hasNext()) {
        ITextRegion region = (ITextRegion) e.next();
        String regionType = region.getType();
        if (isNestedTagOpen(regionType) || isNestedTagName(regionType)) {
            tagName = computeNestedTag(regionType, tagName, flatNode, region);
        } else if (isNestedTagClose(regionType)) {
            isCloseTag = true;
        } else if (regionType == DOMRegionContext.XML_TAG_ATTRIBUTE_NAME) {
            String name = flatNode.getText(region);
            attr = (AttrImpl) this.model.getDocument().createAttribute(name);
            if (attr != null) {
                attr.setNameRegion(region);
                if (attrNodes == null)
                    attrNodes = new ArrayList();
                attrNodes.add(attr);
            }
        } else if (regionType == DOMRegionContext.XML_TAG_ATTRIBUTE_EQUALS) {
            if (attr != null) {
                attr.setEqualRegion(region);
            }
        } else if (regionType == DOMRegionContext.XML_TAG_ATTRIBUTE_VALUE) {
            if (attr != null) {
                attr.setValueRegion(region);
                attr = null;
            }
        }
    }
    if (tagName == null) {
        if (isCloseTag) {
            // close JSP tag
            Node parent = this.context.getParentNode();
            if (parent != null && parent.getNodeType() == Node.ELEMENT_NODE) {
                ElementImpl start = (ElementImpl) parent;
                if (start.isJSPContainer()) {
                    insertEndTag(start);
                    start.setEndStructuredDocumentRegion(flatNode);
                    return;
                }
            }
        }
        // invalid JSP tag
        // regard as invalid text
        insertText(flatNode);
        return;
    }
    ElementImpl element = null;
    try {
        element = (ElementImpl) this.model.getDocument().createElement(tagName);
    } catch (DOMException ex) {
    }
    if (element == null) {
        // invalid tag
        // regard as invalid text
        insertText(flatNode);
        return;
    }
    if (attrNodes != null) {
        for (int i = 0; i < attrNodes.size(); i++) {
            element.appendAttributeNode((Attr) attrNodes.get(i));
        }
    }
    element.setJSPTag(true);
    element.setStartStructuredDocumentRegion(flatNode);
    insertStartTag(element);
}
Also used : ITextRegionList(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList) DOMException(org.w3c.dom.DOMException) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) Node(org.w3c.dom.Node) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) ITextRegionList(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList) IStructuredDocumentRegionList(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegionList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 72 with ITextRegionList

use of org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList in project webtools.sourceediting by eclipse.

the class XMLModelParser method changeStartTag.

/**
 */
private void changeStartTag(IStructuredDocumentRegion flatNode, ITextRegionList newRegions, ITextRegionList oldRegions) {
    int offset = flatNode.getStart();
    if (offset < 0)
        // error
        return;
    NodeImpl root = (NodeImpl) this.context.getRootNode();
    if (root == null)
        // error
        return;
    Node node = root.getNodeAt(offset);
    if (node == null)
        // error
        return;
    if (node.getNodeType() != Node.ELEMENT_NODE) {
        changeStructuredDocumentRegion(flatNode);
        return;
    }
    ElementImpl element = (ElementImpl) 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 == DOMRegionContext.XML_TAG_ATTRIBUTE_NAME || regionType == DOMRegionContext.XML_TAG_ATTRIBUTE_EQUALS || regionType == DOMRegionContext.XML_TAG_ATTRIBUTE_VALUE)
                continue;
            if (regionType == DOMRegionContext.XML_TAG_CLOSE) {
                // change from empty tag may have impact on structure
                if (!element.isEmptyTag())
                    continue;
            } else if (regionType == DOMRegionContext.XML_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 == DOMRegionContext.XML_TAG_ATTRIBUTE_NAME || regionType == DOMRegionContext.XML_TAG_ATTRIBUTE_EQUALS || regionType == DOMRegionContext.XML_TAG_ATTRIBUTE_VALUE)
                continue;
            if (regionType == DOMRegionContext.XML_TAG_CLOSE) {
                // change from empty tag may have impact on structure
                if (!element.isEmptyTag())
                    continue;
            } else if (regionType == DOMRegionContext.XML_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)
        // error
        return;
    // 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) {
                // next region
                regionIndex = i + 1;
                found = true;
                break;
            }
        }
        if (found) {
            attrIndex++;
        } else {
            element.removeAttributeNode(attr);
        }
    }
    // insert or update attributes
    // reset to first
    attrIndex = 0;
    AttrImpl newAttr = null;
    ITextRegion oldValueRegion = null;
    Iterator e = regions.iterator();
    while (e.hasNext()) {
        ITextRegion region = (ITextRegion) e.next();
        String regionType = region.getType();
        if (regionType == DOMRegionContext.XML_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 == DOMRegionContext.XML_TAG_ATTRIBUTE_EQUALS) {
            if (attr != null) {
                attr.setEqualRegion(region);
            }
        } else if (regionType == DOMRegionContext.XML_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();
    }
}
Also used : ITextRegionList(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList) NamedNodeMap(org.w3c.dom.NamedNodeMap) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) Node(org.w3c.dom.Node) Iterator(java.util.Iterator)

Example 73 with ITextRegionList

use of org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList in project webtools.sourceediting by eclipse.

the class SourceValidator method hasNestedRegion.

/**
 * True if the text has nested regions, meaning container is probably too
 * complicated (like EL regions) to validate with this validator.
 *
 * @param text
 * @return
 */
private boolean hasNestedRegion(TextImpl text) {
    boolean done = false;
    IStructuredDocumentRegion currRegion = text.getFirstStructuredDocumentRegion();
    IStructuredDocumentRegion lastRegion = text.getLastStructuredDocumentRegion();
    while (currRegion != null && !done) {
        ITextRegionList regions = currRegion.getRegions();
        for (int i = 0; i < regions.size(); ++i) {
            ITextRegion container = regions.get(i);
            if ((container instanceof ITextRegionContainer)) {
                ITextRegionList regions2 = ((ITextRegionContainer) container).getRegions();
                if (regions2 != null) {
                    return true;
                }
            }
        }
        done = currRegion == lastRegion;
        currRegion = currRegion.getNext();
    }
    return false;
}
Also used : ITextRegionList(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList) IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) ITextRegionContainer(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionContainer)

Example 74 with ITextRegionList

use of org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList in project webtools.sourceediting by eclipse.

the class AttrImpl method hasNestedValue.

/**
 * Check if Attr has JSP in value
 */
public boolean hasNestedValue() {
    if (this.fValueRegion == null)
        return false;
    if (!(this.fValueRegion instanceof ITextRegionContainer))
        return false;
    ITextRegionList regions = ((ITextRegionContainer) this.fValueRegion).getRegions();
    if (regions == null)
        return false;
    Iterator e = regions.iterator();
    while (e.hasNext()) {
        ITextRegion region = (ITextRegion) e.next();
        if (region == null)
            continue;
        String regionType = region.getType();
        if (regionType == DOMRegionContext.XML_TAG_OPEN || isNestedLanguageOpening(regionType))
            return true;
    }
    return false;
}
Also used : ITextRegionList(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) Iterator(java.util.Iterator) ITextRegionContainer(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionContainer)

Example 75 with ITextRegionList

use of org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList in project webtools.sourceediting by eclipse.

the class CommentImpl method getData.

/**
 */
private String getData(IStructuredDocumentRegion flatNode) {
    if (flatNode == null)
        return null;
    ITextRegionList regions = flatNode.getRegions();
    if (regions == null)
        return null;
    ITextRegion contentRegion = null;
    StringBuffer buffer = null;
    Iterator e = regions.iterator();
    while (e.hasNext()) {
        ITextRegion region = (ITextRegion) e.next();
        String regionType = region.getType();
        if (regionType == DOMRegionContext.XML_COMMENT_OPEN || regionType == DOMRegionContext.XML_COMMENT_CLOSE || isNestedCommentOpenClose(regionType)) {
            continue;
        }
        if (contentRegion == null) {
            // first content
            contentRegion = region;
        } else {
            // multiple contents
            if (buffer == null) {
                buffer = new StringBuffer(flatNode.getText(contentRegion));
            }
            buffer.append(flatNode.getText(region));
        }
    }
    if (buffer != null)
        return buffer.toString();
    if (contentRegion != null)
        return flatNode.getText(contentRegion);
    return null;
}
Also used : ITextRegionList(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) Iterator(java.util.Iterator)

Aggregations

ITextRegionList (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList)193 ITextRegion (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)171 IStructuredDocumentRegion (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)74 Iterator (java.util.Iterator)46 IDOMNode (org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode)27 ITextRegionContainer (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionContainer)24 ArrayList (java.util.ArrayList)21 List (java.util.List)18 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)16 StyleRange (org.eclipse.swt.custom.StyleRange)13 TextAttribute (org.eclipse.jface.text.TextAttribute)12 LocalizedMessage (org.eclipse.wst.validation.internal.operations.LocalizedMessage)12 TextRegionListImpl (org.eclipse.wst.sse.core.internal.text.TextRegionListImpl)10 IDOMModel (org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel)9 IExtensionPoint (org.eclipse.core.runtime.IExtensionPoint)8 CustomCompletionProposal (org.eclipse.wst.sse.ui.internal.contentassist.CustomCompletionProposal)8 DOMException (org.w3c.dom.DOMException)8 BadLocationException (org.eclipse.jface.text.BadLocationException)7 IndexedRegion (org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)7 ITextRegionCollection (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionCollection)7