Search in sources :

Example 6 with IDOMAttr

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

the class TaglibHyperlinkDetector method getHyperlinkRegion.

// the below methods were copied from URIHyperlinkDetector
private IRegion getHyperlinkRegion(Node node, IRegion boundingRegion) {
    IRegion hyperRegion = null;
    if (node != null) {
        short nodeType = node.getNodeType();
        if (nodeType == Node.DOCUMENT_TYPE_NODE) {
            // handle doc type node
            IDOMNode docNode = (IDOMNode) node;
            hyperRegion = new Region(docNode.getStartOffset(), docNode.getEndOffset() - docNode.getStartOffset());
        } else if (nodeType == Node.ATTRIBUTE_NODE) {
            // handle attribute nodes
            IDOMAttr att = (IDOMAttr) node;
            // do not include quotes in attribute value region
            int regOffset = att.getValueRegionStartOffset();
            ITextRegion valueRegion = att.getValueRegion();
            if (valueRegion != null) {
                int regLength = valueRegion.getTextLength();
                String attValue = att.getValueRegionText();
                if (StringUtils.isQuoted(attValue)) {
                    ++regOffset;
                    regLength = regLength - 2;
                }
                hyperRegion = new Region(regOffset, regLength);
            }
        }
        if (nodeType == Node.ELEMENT_NODE) {
            // Handle doc type node
            IDOMNode docNode = (IDOMNode) node;
            hyperRegion = getNameRegion(docNode.getFirstStructuredDocumentRegion());
            if (hyperRegion == null) {
                hyperRegion = new Region(docNode.getStartOffset(), docNode.getFirstStructuredDocumentRegion().getTextLength());
            }
        }
    }
    /**
     * Only return a hyperlink region that overlaps the search region.
     * This will help us to not underline areas not under the cursor.
     */
    if (hyperRegion != null && intersects(hyperRegion, boundingRegion))
        return hyperRegion;
    return null;
}
Also used : IDOMAttr(org.eclipse.wst.xml.core.internal.provisional.document.IDOMAttr) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) IRegion(org.eclipse.jface.text.IRegion) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion) Region(org.eclipse.jface.text.Region) ITypedRegion(org.eclipse.jface.text.ITypedRegion) IRegion(org.eclipse.jface.text.IRegion)

Example 7 with IDOMAttr

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

the class ElementNodeCleanupHandler method quoteAttrValue.

protected IDOMNode quoteAttrValue(IDOMNode node) {
    IDOMElement element = (IDOMElement) node;
    if (element.isCommentTag())
        // do nothing
        return node;
    boolean quoteAttrValues = getCleanupPreferences().getQuoteAttrValues();
    IDOMNode newNode = node;
    if (quoteAttrValues) {
        NamedNodeMap attributes = newNode.getAttributes();
        int attributesLength = attributes.getLength();
        ISourceGenerator generator = node.getModel().getGenerator();
        for (int i = 0; i < attributesLength; i++) {
            attributes = newNode.getAttributes();
            attributesLength = attributes.getLength();
            IDOMAttr eachAttr = (IDOMAttr) attributes.item(i);
            // ITextRegion oldAttrValueRegion = eachAttr.getValueRegion();
            String oldAttrValue = eachAttr.getValueRegionText();
            if (oldAttrValue == null) {
                IDOMModel structuredModel = node.getModel();
                if (isXMLType(structuredModel)) {
                    // TODO: Kit, please check. Is there any way to not
                    // rely on getting regions from attributes?
                    // $NON-NLS-1$ //$NON-NLS-2$
                    String newAttrValue = "=\"" + eachAttr.getNameRegionText() + "\"";
                    IStructuredDocument structuredDocument = structuredModel.getStructuredDocument();
                    replaceSource(structuredModel, structuredDocument, eachAttr.getNameRegionEndOffset(), 0, newAttrValue);
                    // save
                    newNode = (IDOMNode) structuredModel.getIndexedRegion(node.getStartOffset());
                // new
                // node
                }
            } else {
                char quote = StringUtils.isQuoted(oldAttrValue) ? oldAttrValue.charAt(0) : DOUBLE_QUOTE;
                String newAttrValue = generator.generateAttrValue(eachAttr, quote);
                // Workaround for now...
                if (oldAttrValue.length() == 1) {
                    char firstChar = oldAttrValue.charAt(0);
                    if (firstChar == SINGLE_QUOTE)
                        newAttrValue = SINGLE_QUOTES;
                    else if (firstChar == DOUBLE_QUOTE)
                        newAttrValue = DOUBLE_QUOTES;
                }
                if (newAttrValue != null) {
                    if (newAttrValue.compareTo(oldAttrValue) != 0) {
                        int attrValueStartOffset = eachAttr.getValueRegionStartOffset();
                        int attrValueLength = oldAttrValue.length();
                        int startTagStartOffset = node.getStartOffset();
                        IDOMModel structuredModel = node.getModel();
                        IStructuredDocument structuredDocument = structuredModel.getStructuredDocument();
                        replaceSource(structuredModel, structuredDocument, attrValueStartOffset, attrValueLength, newAttrValue);
                        // save
                        newNode = (IDOMNode) structuredModel.getIndexedRegion(startTagStartOffset);
                    // new
                    // node
                    }
                }
            }
        }
    }
    return newNode;
}
Also used : IDOMAttr(org.eclipse.wst.xml.core.internal.provisional.document.IDOMAttr) NamedNodeMap(org.w3c.dom.NamedNodeMap) CMNamedNodeMap(org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap) ISourceGenerator(org.eclipse.wst.xml.core.internal.provisional.document.ISourceGenerator) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) IDOMElement(org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement)

Example 8 with IDOMAttr

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

the class BasicCommentElementHandler method createElement.

public Element createElement(Document document, String data, boolean isJSPTag) {
    Element element = null;
    String str = data.trim();
    CommentElementFactory factory = new CommentElementFactory(document, isJSPTag, this);
    if (str.charAt(0) == '/') {
        // end tag
        // skip '/'
        TagScanner scanner = new TagScanner(str, 1);
        String name = scanner.nextName();
        if (name.equals(elementName)) {
            element = factory.create(name, CommentElementFactory.IS_END);
        }
    } else {
        // start tag
        TagScanner scanner = new TagScanner(str, 0);
        String name = scanner.nextName();
        if (name.equals(elementName)) {
            element = factory.create(name, (isEmpty) ? CommentElementFactory.IS_EMPTY : CommentElementFactory.IS_START);
            // set attributes
            String attrName = scanner.nextName();
            while (attrName != null) {
                String attrValue = scanner.nextValue();
                Attr attr = document.createAttribute(attrName);
                if (attr != null) {
                    if (attrValue != null)
                        ((IDOMAttr) attr).setValueSource(attrValue);
                    element.setAttributeNode(attr);
                }
                attrName = scanner.nextName();
            }
        }
    }
    return element;
}
Also used : CommentElementFactory(org.eclipse.wst.xml.core.internal.commentelement.util.CommentElementFactory) Element(org.w3c.dom.Element) IDOMElement(org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement) TagScanner(org.eclipse.wst.xml.core.internal.commentelement.util.TagScanner) IDOMAttr(org.eclipse.wst.xml.core.internal.provisional.document.IDOMAttr) Attr(org.w3c.dom.Attr)

Example 9 with IDOMAttr

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

the class BaseHyperlinkDetector method isLinkable.

/**
 * Determines whether a node is "linkable" that is, the component it refers to
 * can be the target of a "go to definition" navigation.
 *
 * @param node the node to test, must not be null;
 * @return true if the node is linkable, false otherwise.
 */
private boolean isLinkable(IDOMNode node) {
    if (node == null) {
        return false;
    }
    short nodeType = node.getNodeType();
    boolean isLinkable = false;
    if (nodeType == Node.ATTRIBUTE_NODE) {
        IDOMAttr attr = (IDOMAttr) node;
        String name = attr.getName();
        // isLinkableAttribute is a template method. Derived classes should
        // override.
        isLinkable = isLinkableAttribute(name);
    }
    return isLinkable;
}
Also used : IDOMAttr(org.eclipse.wst.xml.core.internal.provisional.document.IDOMAttr)

Example 10 with IDOMAttr

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

the class ElementNodeCleanupHandler method quoteAttrValue.

private IDOMNode quoteAttrValue(IDOMNode node) {
    IDOMNode newNode = node;
    // XMLElement element = (XMLElement) node;
    if (isCommentTag(node))
        // do nothing
        return node;
    boolean quoteAttrValues = getCleanupPreferences().getQuoteAttrValues();
    if (quoteAttrValues) {
        NamedNodeMap attributes = newNode.getAttributes();
        if (attributes != null) {
            int attributesLength = attributes.getLength();
            ISourceGenerator generator = node.getModel().getGenerator();
            for (int i = 0; i < attributesLength; i++) {
                attributes = newNode.getAttributes();
                attributesLength = attributes.getLength();
                IDOMAttr eachAttr = (IDOMAttr) attributes.item(i);
                // ITextRegion oldAttrValueRegion =
                // eachAttr.getValueRegion();
                String oldAttrValue = eachAttr.getValueRegionText();
                if (oldAttrValue == null) {
                    IDOMModel structuredModel = node.getModel();
                    if (isXMLType(structuredModel)) {
                        // $NON-NLS-1$ //$NON-NLS-2$
                        String newAttrValue = "\"" + eachAttr.getNameRegionText() + "\"";
                        IStructuredDocument structuredDocument = structuredModel.getStructuredDocument();
                        if (eachAttr.getEqualRegion() != null)
                            // equal region exists
                            structuredDocument.replaceText(structuredDocument, eachAttr.getEndOffset(), 0, newAttrValue);
                        else
                            // no equal region
                            // $NON-NLS-1$
                            structuredDocument.replaceText(structuredDocument, eachAttr.getNameRegionTextEndOffset(), 0, "=".concat(newAttrValue));
                        // save
                        newNode = (IDOMNode) structuredModel.getIndexedRegion(node.getStartOffset());
                    // new
                    // node
                    }
                } else {
                    // String oldAttrValue = oldAttrValueRegion.getText();
                    char quote = StringUtils.isQuoted(oldAttrValue) ? oldAttrValue.charAt(0) : DOUBLE_QUOTE;
                    String newAttrValue = generator.generateAttrValue(eachAttr, quote);
                    // Workaround for now...
                    if (oldAttrValue.length() == 1) {
                        char firstChar = oldAttrValue.charAt(0);
                        if (firstChar == SINGLE_QUOTE)
                            newAttrValue = SINGLE_QUOTES;
                        else if (firstChar == DOUBLE_QUOTE)
                            newAttrValue = DOUBLE_QUOTES;
                    }
                    if (newAttrValue != null) {
                        if (newAttrValue.compareTo(oldAttrValue) != 0) {
                            int attrValueStartOffset = eachAttr.getValueRegionStartOffset();
                            int attrValueLength = oldAttrValue.length();
                            int startTagStartOffset = node.getStartOffset();
                            IDOMModel structuredModel = node.getModel();
                            IStructuredDocument structuredDocument = structuredModel.getStructuredDocument();
                            structuredDocument.replaceText(structuredDocument, attrValueStartOffset, attrValueLength, newAttrValue);
                            // save
                            newNode = (IDOMNode) structuredModel.getIndexedRegion(startTagStartOffset);
                        // new
                        // node
                        }
                    }
                }
            }
        }
    }
    return newNode;
}
Also used : IDOMAttr(org.eclipse.wst.xml.core.internal.provisional.document.IDOMAttr) NamedNodeMap(org.w3c.dom.NamedNodeMap) CMNamedNodeMap(org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap) ISourceGenerator(org.eclipse.wst.xml.core.internal.provisional.document.ISourceGenerator) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)

Aggregations

IDOMAttr (org.eclipse.wst.xml.core.internal.provisional.document.IDOMAttr)29 IDOMElement (org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement)10 IDOMNode (org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode)10 ITextRegion (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)9 NamedNodeMap (org.w3c.dom.NamedNodeMap)9 IRegion (org.eclipse.jface.text.IRegion)7 Region (org.eclipse.jface.text.Region)7 IndexedRegion (org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)6 CMNamedNodeMap (org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap)5 Node (org.w3c.dom.Node)5 Attr (org.w3c.dom.Attr)3 Element (org.w3c.dom.Element)3 SearchMatch (org.eclipse.wst.common.core.search.SearchMatch)2 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)2 IStructuredDocumentRegion (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)2 ITextRegionContainer (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionContainer)2 CMAttributeDeclaration (org.eclipse.wst.xml.core.internal.contentmodel.CMAttributeDeclaration)2 CMElementDeclaration (org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration)2 IDOMModel (org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel)2 IDOMText (org.eclipse.wst.xml.core.internal.provisional.document.IDOMText)2