Search in sources :

Example 1 with AttrImpl

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

the class TestOrphan method testNonOrphanInBothReversedOrder.

public void testNonOrphanInBothReversedOrder() {
    Logger.trace(fCategory, "testNonOrphanInBothReversedOrder");
    Document htmlDoc = getHTMLDoc();
    Element commonElement = makeElement(htmlDoc);
    AttrImpl attr = (AttrImpl) commonElement.getAttributeNode("src");
    String attrValue = attr.getValue();
    Logger.trace(fCategory, "attrValue: " + attrValue);
    htmlDoc.appendChild(commonElement);
    boolean isJspValue = attr.hasNestedValue();
    Logger.trace(fCategory, "isJspValue: " + isJspValue);
    assertFalse(isJspValue);
    Document jspDoc = getJSPDoc();
    // this little test shows its important to
    // actually create the element with the right kind of
    // document, not just append.
    // (and, append is needed too, as can be seen by
    // commenting out one or the other of the following
    // two lines.
    commonElement = makeElement(jspDoc);
    jspDoc.appendChild(commonElement);
    // 
    attr = (AttrImpl) commonElement.getAttributeNode("src");
    attrValue = attr.getValue();
    Logger.trace(fCategory, "attrValue: " + attrValue);
    isJspValue = attr.hasNestedValue();
    Logger.trace(fCategory, "isJspValue: " + isJspValue);
    assertTrue(isJspValue);
}
Also used : Element(org.w3c.dom.Element) AttrImpl(org.eclipse.wst.xml.core.internal.document.AttrImpl) Document(org.w3c.dom.Document)

Example 2 with AttrImpl

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

the class AbstractContentAssistProcessor method addAttributeNameProposals.

protected void addAttributeNameProposals(ContentAssistRequest contentAssistRequest) {
    IDOMNode node = (IDOMNode) contentAssistRequest.getNode();
    IStructuredDocumentRegion sdRegion = contentAssistRequest.getDocumentRegion();
    // retrieve the list of attributes
    CMElementDeclaration elementDecl = getCMElementDeclaration(node);
    if (elementDecl != null) {
        CMNamedNodeMap attributes = elementDecl.getAttributes();
        CMNamedNodeMapImpl allAttributes = new CMNamedNodeMapImpl(attributes);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            List nodes = ModelQueryUtil.getModelQuery(node.getOwnerDocument()).getAvailableContent((Element) node, elementDecl, ModelQuery.INCLUDE_ATTRIBUTES);
            for (int k = 0; k < nodes.size(); k++) {
                CMNode cmnode = (CMNode) nodes.get(k);
                if (cmnode.getNodeType() == CMNode.ATTRIBUTE_DECLARATION) {
                    allAttributes.put(cmnode);
                }
            }
        }
        attributes = allAttributes;
        String matchString = contentAssistRequest.getMatchString();
        // check whether an attribute really exists for the replacement
        // offsets AND if it possesses a value
        boolean attrAtLocationHasValue = false;
        NamedNodeMap attrs = node.getAttributes();
        for (int i = 0; i < attrs.getLength(); i++) {
            AttrImpl existingAttr = (AttrImpl) attrs.item(i);
            ITextRegion name = existingAttr.getNameRegion();
            if ((sdRegion.getStartOffset(name) <= contentAssistRequest.getReplacementBeginPosition()) && (sdRegion.getStartOffset(name) + name.getLength() >= contentAssistRequest.getReplacementBeginPosition() + contentAssistRequest.getReplacementLength()) && (existingAttr.getValueRegion() != null)) {
                attrAtLocationHasValue = true;
                break;
            }
        }
        // the matchstring
        if (attributes != null) {
            for (int i = 0; i < attributes.getLength(); i++) {
                CMAttributeDeclaration attrDecl = (CMAttributeDeclaration) attributes.item(i);
                int isRequired = 0;
                if (attrDecl.getUsage() == CMAttributeDeclaration.REQUIRED) {
                    isRequired = XMLRelevanceConstants.R_REQUIRED;
                }
                boolean showAttribute = true;
                showAttribute = showAttribute && beginsWith(getRequiredName(node, attrDecl), matchString.trim());
                AttrImpl attr = (AttrImpl) node.getAttributes().getNamedItem(getRequiredName(node, attrDecl));
                ITextRegion nameRegion = attr != null ? attr.getNameRegion() : null;
                // nameRegion.getEndOffset() + 1 is required to allow for
                // matches against the full name of an existing Attr
                showAttribute = showAttribute && (attr == null || nameRegion == null || (nameRegion != null && (sdRegion.getStartOffset(nameRegion) <= contentAssistRequest.getReplacementBeginPosition()) && (sdRegion.getStartOffset(nameRegion) + nameRegion.getLength() >= contentAssistRequest.getReplacementBeginPosition() + contentAssistRequest.getReplacementLength())));
                if (showAttribute) {
                    Image attrImage = CMImageUtil.getImage(attrDecl);
                    if (attrImage == null) {
                        if (isRequired > 0) {
                            attrImage = XMLEditorPluginImageHelper.getInstance().getImage(XMLEditorPluginImages.IMG_OBJ_ATT_REQ_OBJ);
                        } else {
                            attrImage = XMLEditorPluginImageHelper.getInstance().getImage(XMLEditorPluginImages.IMG_OBJ_ATTRIBUTE);
                        }
                    }
                    String proposedText = null;
                    String proposedInfo = getAdditionalInfo(elementDecl, attrDecl);
                    CustomCompletionProposal proposal = null;
                    // attribute is at this location and already exists
                    if (attrAtLocationHasValue) {
                        // only propose the name
                        proposedText = getRequiredName(node, attrDecl);
                        proposal = new CustomCompletionProposal(proposedText, contentAssistRequest.getReplacementBeginPosition(), contentAssistRequest.getReplacementLength(), proposedText.length(), attrImage, proposedText, null, proposedInfo, XMLRelevanceConstants.R_XML_ATTRIBUTE_NAME + isRequired, true);
                    } else // no attribute exists or is elsewhere, generate
                    // minimally
                    {
                        Attr existingAttrNode = (Attr) node.getAttributes().getNamedItem(getRequiredName(node, attrDecl));
                        String value = null;
                        if (existingAttrNode != null && existingAttrNode.getSpecified()) {
                            value = existingAttrNode.getNodeValue();
                        }
                        if ((value != null) && (value.length() > 0)) {
                            proposedText = getRequiredName(node, attrDecl);
                        } else {
                            proposedText = getRequiredText(node, attrDecl);
                        }
                        proposal = new CustomCompletionProposal(proposedText, contentAssistRequest.getReplacementBeginPosition(), contentAssistRequest.getReplacementLength(), attrDecl.getNodeName().length() + 2, attrImage, // and there is no single quote that may be encasing a double quote
                        (showValues && (proposedText.lastIndexOf('\"') - proposedText.indexOf('\"') == 1 && proposedText.indexOf('\'') == -1)) ? getRequiredName(node, attrDecl) : proposedText, null, proposedInfo, XMLRelevanceConstants.R_XML_ATTRIBUTE_NAME + isRequired);
                    }
                    contentAssistRequest.addProposal(proposal);
                }
            }
        }
    } else {
        setErrorMessage(NLS.bind(XMLUIMessages.Element__is_unknown, (new Object[] { node.getNodeName() })));
    }
}
Also used : CMNamedNodeMapImpl(org.eclipse.wst.xml.core.internal.contentmodel.basic.CMNamedNodeMapImpl) IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) CMNamedNodeMap(org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap) NamedNodeMap(org.w3c.dom.NamedNodeMap) CustomCompletionProposal(org.eclipse.wst.sse.ui.internal.contentassist.CustomCompletionProposal) AttrImpl(org.eclipse.wst.xml.core.internal.document.AttrImpl) Image(org.eclipse.swt.graphics.Image) Attr(org.w3c.dom.Attr) CMElementDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) CMNodeList(org.eclipse.wst.xml.core.internal.contentmodel.CMNodeList) List(java.util.List) ITextRegionList(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList) ArrayList(java.util.ArrayList) NodeList(org.w3c.dom.NodeList) CMNode(org.eclipse.wst.xml.core.internal.contentmodel.CMNode) CMAttributeDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMAttributeDeclaration) CMNamedNodeMap(org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap)

Example 3 with AttrImpl

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

the class AbstractXMLModelQueryCompletionProposalComputer method addAttributeNameProposals.

protected void addAttributeNameProposals(ContentAssistRequest contentAssistRequest, CompletionProposalInvocationContext context) {
    IDOMNode node = (IDOMNode) contentAssistRequest.getNode();
    IStructuredDocumentRegion sdRegion = contentAssistRequest.getDocumentRegion();
    // retrieve the list of attributes
    CMElementDeclaration elementDecl = getCMElementDeclaration(node);
    if (elementDecl != null) {
        CMNamedNodeMapImpl attributes = new CMNamedNodeMapImpl(elementDecl.getAttributes());
        addModelQueryAttributeDeclarations(node, elementDecl, attributes);
        String matchString = contentAssistRequest.getMatchString();
        int cursorOffset = context.getInvocationOffset();
        // check whether an attribute really exists for the replacement
        // offsets AND if it possesses a value
        boolean attrAtLocationHasValue = false;
        boolean proposalNeedsSpace = false;
        NamedNodeMap attrs = node.getAttributes();
        for (int i = 0; i < attrs.getLength(); i++) {
            AttrImpl existingAttr = (AttrImpl) attrs.item(i);
            ITextRegion name = existingAttr.getNameRegion();
            if (name != null && (sdRegion.getStartOffset(name) <= contentAssistRequest.getReplacementBeginPosition()) && (sdRegion.getStartOffset(name) + name.getLength() >= contentAssistRequest.getReplacementBeginPosition() + contentAssistRequest.getReplacementLength()) && (existingAttr.getValueRegion() != null)) {
                // selected region is attribute name
                if (cursorOffset >= sdRegion.getStartOffset(name) && contentAssistRequest.getReplacementLength() != 0)
                    attrAtLocationHasValue = true;
                else // propose new attribute, cursor is at the start of another attribute name
                if (cursorOffset == sdRegion.getStartOffset(name))
                    proposalNeedsSpace = true;
                break;
            }
        }
        // only add proposals for the attributes whose names begin with the matchstring
        if (attributes != null) {
            for (int i = 0; i < attributes.getLength(); i++) {
                CMAttributeDeclaration attrDecl = (CMAttributeDeclaration) attributes.item(i);
                if (validModelQueryNode(attrDecl)) {
                    int isRequired = 0;
                    if (attrDecl.getUsage() == CMAttributeDeclaration.REQUIRED) {
                        isRequired = XMLRelevanceConstants.R_REQUIRED;
                    }
                    boolean showAttribute = true;
                    showAttribute = showAttribute && beginsWith(getRequiredName(node, attrDecl), matchString.trim());
                    AttrImpl attr = (AttrImpl) node.getAttributes().getNamedItem(getRequiredName(node, attrDecl));
                    ITextRegion nameRegion = attr != null ? attr.getNameRegion() : null;
                    // nameRegion.getEndOffset() + 1 is required to allow for
                    // matches against the full name of an existing Attr
                    showAttribute = showAttribute && ((attr == null) || nameRegion == null || ((nameRegion != null) && (sdRegion.getStartOffset(nameRegion) < contentAssistRequest.getReplacementBeginPosition()) && (sdRegion.getStartOffset(nameRegion) + nameRegion.getLength() >= (contentAssistRequest.getReplacementBeginPosition() + contentAssistRequest.getReplacementLength()))));
                    if (showAttribute) {
                        // get the proposal image
                        Image attrImage = CMImageUtil.getImage(attrDecl);
                        if (attrImage == null) {
                            if (isRequired > 0) {
                                attrImage = this.getRequiredAttributeImage();
                            } else {
                                attrImage = this.getNotRequiredAttributeImage();
                            }
                        }
                        String proposedText = null;
                        String proposedInfo = getAdditionalInfo(elementDecl, attrDecl);
                        CustomCompletionProposal proposal = null;
                        // attribute is at this location and already exists
                        if (attrAtLocationHasValue) {
                            // only propose the name
                            proposedText = getRequiredName(node, attrDecl);
                            proposal = new MarkupCompletionProposal(proposedText, contentAssistRequest.getReplacementBeginPosition(), contentAssistRequest.getReplacementLength(), proposedText.length(), attrImage, proposedText, null, proposedInfo, XMLRelevanceConstants.R_XML_ATTRIBUTE_NAME + isRequired, true);
                        } else // no attribute exists or is elsewhere, generate
                        // minimally
                        {
                            Attr existingAttrNode = (Attr) node.getAttributes().getNamedItem(getRequiredName(node, attrDecl));
                            String value = null;
                            if (existingAttrNode != null && existingAttrNode.getSpecified()) {
                                value = existingAttrNode.getNodeValue();
                            }
                            int cursorPosition = 0;
                            if ((value != null) && (value.length() > 0)) {
                                proposedText = getRequiredName(node, attrDecl);
                                cursorPosition = proposedText.length() + 2;
                            } else {
                                proposedText = getRequiredText(node, attrDecl);
                                // skip the cursor past a fixed value
                                if (attrDecl.getAttrType() != null && attrDecl.getAttrType().getImpliedValueKind() == CMDataType.IMPLIED_VALUE_FIXED)
                                    cursorPosition = proposedText.length();
                                else
                                    cursorPosition = getRequiredName(node, attrDecl).length() + 2;
                            }
                            if (proposalNeedsSpace)
                                // $NON-NLS-1$
                                proposedText += " ";
                            proposal = new MarkupCompletionProposal(proposedText, contentAssistRequest.getReplacementBeginPosition(), contentAssistRequest.getReplacementLength(), cursorPosition, attrImage, // and there is no single quote that may be encasing a double quote
                            ((proposedText.lastIndexOf('\"') - proposedText.indexOf('\"') == 1 && proposedText.indexOf('\'') == -1)) ? getRequiredName(node, attrDecl) : proposedText, null, proposedInfo, XMLRelevanceConstants.R_XML_ATTRIBUTE_NAME + isRequired);
                        }
                        contentAssistRequest.addProposal(proposal);
                    }
                }
            }
        }
    } else {
        setErrorMessage(NLS.bind(XMLUIMessages.Element__is_unknown, (new Object[] { node.getNodeName() })));
    }
}
Also used : CMNamedNodeMapImpl(org.eclipse.wst.xml.core.internal.contentmodel.basic.CMNamedNodeMapImpl) IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) CMNamedNodeMap(org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap) NamedNodeMap(org.w3c.dom.NamedNodeMap) CustomCompletionProposal(org.eclipse.wst.sse.ui.internal.contentassist.CustomCompletionProposal) AttrImpl(org.eclipse.wst.xml.core.internal.document.AttrImpl) Image(org.eclipse.swt.graphics.Image) Attr(org.w3c.dom.Attr) CMElementDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) CMAttributeDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMAttributeDeclaration)

Example 4 with AttrImpl

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

the class TestOrphan method testNonOrphanInJSPDoc.

public void testNonOrphanInJSPDoc() {
    Logger.trace(fCategory, "testNonOrphanInJSPDoc");
    Document doc = getJSPDoc();
    Element element = makeElement(doc);
    AttrImpl attr = (AttrImpl) element.getAttributeNode("src");
    String attrValue = attr.getValue();
    Logger.trace(fCategory, "attrValue: " + attrValue);
    doc.appendChild(element);
    boolean isJspValue = attr.hasNestedValue();
    Logger.trace(fCategory, "isJspValue: " + isJspValue);
    assertTrue(isJspValue);
}
Also used : Element(org.w3c.dom.Element) AttrImpl(org.eclipse.wst.xml.core.internal.document.AttrImpl) Document(org.w3c.dom.Document)

Example 5 with AttrImpl

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

the class TestOrphan method testNonOrphanInHTMLDoc.

public void testNonOrphanInHTMLDoc() {
    Logger.trace(fCategory, "testNonOrphanInHTMLDoc");
    Document doc = getHTMLDoc();
    Element element = makeElement(doc);
    AttrImpl attr = (AttrImpl) element.getAttributeNode("src");
    String attrValue = attr.getValue();
    Logger.trace(fCategory, "attrValue: " + attrValue);
    doc.appendChild(element);
    boolean isJspValue = attr.hasNestedValue();
    Logger.trace(fCategory, "isJspValue: " + isJspValue);
    assertFalse(isJspValue);
}
Also used : Element(org.w3c.dom.Element) AttrImpl(org.eclipse.wst.xml.core.internal.document.AttrImpl) Document(org.w3c.dom.Document)

Aggregations

AttrImpl (org.eclipse.wst.xml.core.internal.document.AttrImpl)10 Element (org.w3c.dom.Element)6 IDOMNode (org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode)4 Document (org.w3c.dom.Document)4 IStructuredDocumentRegion (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)3 ITextRegion (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)3 CMAttributeDeclaration (org.eclipse.wst.xml.core.internal.contentmodel.CMAttributeDeclaration)3 CMElementDeclaration (org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration)3 CMNamedNodeMap (org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap)3 NamedNodeMap (org.w3c.dom.NamedNodeMap)3 Image (org.eclipse.swt.graphics.Image)2 ITextRegionList (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList)2 CustomCompletionProposal (org.eclipse.wst.sse.ui.internal.contentassist.CustomCompletionProposal)2 CMNamedNodeMapImpl (org.eclipse.wst.xml.core.internal.contentmodel.basic.CMNamedNodeMapImpl)2 Attr (org.w3c.dom.Attr)2 ValidationType (com.liferay.ide.project.core.ValidationPreferences.ValidationType)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 BadLocationException (org.eclipse.jface.text.BadLocationException)1 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)1