Search in sources :

Example 6 with CMAttributeDeclaration

use of org.eclipse.wst.xml.core.internal.contentmodel.CMAttributeDeclaration in project webtools.sourceediting by eclipse.

the class JSPActionValidator method checkRequiredAttributes.

private void checkRequiredAttributes(IDOMElement element, CMNamedNodeMap attrMap, IReporter reporter, IFile file, IStructuredDocument document, IStructuredDocumentRegion documentRegion) {
    Iterator it = attrMap.iterator();
    CMAttributeDeclaration attr = null;
    while (it.hasNext()) {
        attr = (CMAttributeDeclaration) it.next();
        if (attr.getUsage() == CMAttributeDeclaration.REQUIRED) {
            Attr a = element.getAttributeNode(attr.getAttrName());
            if (a == null) {
                // Attribute may be defined using a jsp:attribute action
                if (!checkJSPAttributeAction(element, attr)) {
                    String msgText = NLS.bind(JSPCoreMessages.JSPDirectiveValidator_5, attr.getAttrName());
                    LocalizedMessage message = new LocalizedMessage(fSeverityMissingRequiredAttribute, msgText, file);
                    int start = element.getStartOffset();
                    int length = element.getStartEndOffset() - start;
                    int lineNo = document.getLineOfOffset(start);
                    message.setLineNo(lineNo);
                    message.setOffset(start);
                    message.setLength(length);
                    reporter.addMessage(fMessageOriginator, message);
                }
            }
        }
    }
}
Also used : Iterator(java.util.Iterator) CMAttributeDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMAttributeDeclaration) Attr(org.w3c.dom.Attr) IDOMAttr(org.eclipse.wst.xml.core.internal.provisional.document.IDOMAttr)

Example 7 with CMAttributeDeclaration

use of org.eclipse.wst.xml.core.internal.contentmodel.CMAttributeDeclaration in project webtools.sourceediting by eclipse.

the class XMLHyperlinkDetector method isLinkableAttr.

/**
 * Checks to see if the given attribute is openable. Attribute is openable
 * if it is a namespace declaration attribute or if the attribute value is
 * of type URI.
 *
 * @param attr
 *            cannot be null
 * @param cmElement
 *            CMElementDeclaration associated with the attribute (can be
 *            null)
 * @return true if this attribute is "openOn-able" false otherwise
 */
private boolean isLinkableAttr(Attr attr, CMElementDeclaration cmElement) {
    String attrName = attr.getName();
    String prefix = DOMNamespaceHelper.getPrefix(attrName);
    String unprefixedName = DOMNamespaceHelper.getUnprefixedName(attrName);
    // determine if attribute is namespace declaration
    if ((XMLNS.equals(prefix)) || (XMLNS.equals(unprefixedName))) {
        return true;
    }
    // determine if attribute contains schema location
    if ((XSI_NAMESPACE_URI.equals(DOMNamespaceHelper.getNamespaceURI(attr))) && ((SCHEMA_LOCATION.equals(unprefixedName)) || (NO_NAMESPACE_SCHEMA_LOCATION.equals(unprefixedName)))) {
        return true;
    }
    // determine if attribute value is of type URI
    if (cmElement != null) {
        CMAttributeDeclaration attrDecl = (CMAttributeDeclaration) cmElement.getAttributes().getNamedItem(attrName);
        if ((attrDecl != null) && (attrDecl.getAttrType() != null) && (CMDataType.URI.equals(attrDecl.getAttrType().getDataTypeName()))) {
            return true;
        }
    }
    return false;
}
Also used : CMAttributeDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMAttributeDeclaration)

Example 8 with CMAttributeDeclaration

use of org.eclipse.wst.xml.core.internal.contentmodel.CMAttributeDeclaration in project webtools.sourceediting by eclipse.

the class HTMLTagInfoTest method getCMAttributeDeclaration.

/**
 * Retreives CMAttributeDeclaration indicated by attribute name within
 * elementDecl
 */
private CMAttributeDeclaration getCMAttributeDeclaration(CMElementDeclaration elementDecl, String attName) {
    CMAttributeDeclaration attrDecl = null;
    if (elementDecl != null) {
        CMNamedNodeMap attributes = elementDecl.getAttributes();
        String noprefixName = DOMNamespaceHelper.getUnprefixedName(attName);
        if (attributes != null) {
            attrDecl = (CMAttributeDeclaration) attributes.getNamedItem(noprefixName);
            if (attrDecl == null) {
                attrDecl = (CMAttributeDeclaration) attributes.getNamedItem(attName);
            }
        }
    }
    return attrDecl;
}
Also used : CMAttributeDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMAttributeDeclaration) CMNamedNodeMap(org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap)

Example 9 with CMAttributeDeclaration

use of org.eclipse.wst.xml.core.internal.contentmodel.CMAttributeDeclaration in project webtools.sourceediting by eclipse.

the class ElementNodeCleanupHandler method insertRequiredAttrs.

private IDOMNode insertRequiredAttrs(IDOMNode node) {
    boolean insertRequiredAttrs = getCleanupPreferences().getInsertRequiredAttrs();
    IDOMNode newNode = node;
    if (insertRequiredAttrs) {
        List requiredAttrs = getRequiredAttrs(newNode);
        if (requiredAttrs.size() > 0) {
            NamedNodeMap currentAttrs = node.getAttributes();
            List insertAttrs = new ArrayList();
            if (currentAttrs.getLength() == 0)
                insertAttrs.addAll(requiredAttrs);
            else {
                for (int i = 0; i < requiredAttrs.size(); i++) {
                    String requiredAttrName = ((CMAttributeDeclaration) requiredAttrs.get(i)).getAttrName();
                    boolean found = false;
                    for (int j = 0; j < currentAttrs.getLength(); j++) {
                        String currentAttrName = currentAttrs.item(j).getNodeName();
                        if (requiredAttrName.compareToIgnoreCase(currentAttrName) == 0) {
                            found = true;
                            break;
                        }
                    }
                    if (!found)
                        insertAttrs.add(requiredAttrs.get(i));
                }
            }
            if (insertAttrs.size() > 0) {
                IStructuredDocumentRegion startStructuredDocumentRegion = newNode.getStartStructuredDocumentRegion();
                int index = startStructuredDocumentRegion.getEndOffset();
                ITextRegion lastRegion = startStructuredDocumentRegion.getLastRegion();
                if (lastRegion.getType() == DOMRegionContext.XML_TAG_CLOSE) {
                    index--;
                    lastRegion = startStructuredDocumentRegion.getRegionAtCharacterOffset(index - 1);
                } else if (lastRegion.getType() == DOMRegionContext.XML_EMPTY_TAG_CLOSE) {
                    index = index - 2;
                    lastRegion = startStructuredDocumentRegion.getRegionAtCharacterOffset(index - 1);
                }
                MultiTextEdit multiTextEdit = new MultiTextEdit();
                try {
                    for (int i = insertAttrs.size() - 1; i >= 0; i--) {
                        CMAttributeDeclaration attrDecl = (CMAttributeDeclaration) insertAttrs.get(i);
                        String requiredAttributeName = attrDecl.getAttrName();
                        String defaultValue = attrDecl.getDefaultValue();
                        if (defaultValue == null)
                            // $NON-NLS-1$
                            defaultValue = "";
                        // $NON-NLS-1$
                        String nameAndDefaultValue = " ";
                        if (i == 0 && lastRegion.getLength() > lastRegion.getTextLength())
                            // $NON-NLS-1$
                            nameAndDefaultValue = "";
                        // $NON-NLS-1$ //$NON-NLS-2$
                        nameAndDefaultValue += requiredAttributeName + "=\"" + defaultValue + "\"";
                        multiTextEdit.addChild(new InsertEdit(index, nameAndDefaultValue));
                    // BUG3381: MultiTextEdit applies all child
                    // TextEdit's basing on offsets
                    // in the document before the first TextEdit, not
                    // after each
                    // child TextEdit. Therefore, do not need to
                    // advance the index.
                    // index += nameAndDefaultValue.length();
                    }
                    multiTextEdit.apply(newNode.getStructuredDocument());
                } catch (BadLocationException e) {
                    // log or now, unless we find reason not to
                    Logger.log(Logger.INFO, e.getMessage());
                }
            }
        }
    }
    return newNode;
}
Also used : IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) InsertEdit(org.eclipse.text.edits.InsertEdit) NamedNodeMap(org.w3c.dom.NamedNodeMap) CMNamedNodeMap(org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap) ArrayList(java.util.ArrayList) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) ITextRegionList(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList) ArrayList(java.util.ArrayList) NodeList(org.w3c.dom.NodeList) List(java.util.List) CMAttributeDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMAttributeDeclaration) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 10 with CMAttributeDeclaration

use of org.eclipse.wst.xml.core.internal.contentmodel.CMAttributeDeclaration in project webtools.sourceediting by eclipse.

the class ElementNodeCleanupHandler method getRequiredAttrs.

protected List getRequiredAttrs(Node node) {
    List result = new ArrayList();
    ModelQuery modelQuery = getModelQuery(node);
    if (modelQuery != null) {
        CMElementDeclaration elementDecl = modelQuery.getCMElementDeclaration((Element) node);
        if (elementDecl != null) {
            CMNamedNodeMap attrMap = elementDecl.getAttributes();
            Iterator it = attrMap.iterator();
            CMAttributeDeclaration attr = null;
            while (it.hasNext()) {
                attr = (CMAttributeDeclaration) it.next();
                if (attr.getUsage() == CMAttributeDeclaration.REQUIRED) {
                    result.add(attr);
                }
            }
        }
    }
    return result;
}
Also used : CMElementDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) ITextRegionList(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList) ArrayList(java.util.ArrayList) NodeList(org.w3c.dom.NodeList) List(java.util.List) ModelQuery(org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery) CMAttributeDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMAttributeDeclaration) CMNamedNodeMap(org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap)

Aggregations

CMAttributeDeclaration (org.eclipse.wst.xml.core.internal.contentmodel.CMAttributeDeclaration)57 CMElementDeclaration (org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration)30 CMNamedNodeMap (org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap)29 List (java.util.List)24 ArrayList (java.util.ArrayList)18 CMNode (org.eclipse.wst.xml.core.internal.contentmodel.CMNode)17 NamedNodeMap (org.w3c.dom.NamedNodeMap)17 ITextRegionList (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList)14 CMNamedNodeMapImpl (org.eclipse.wst.xml.core.internal.contentmodel.basic.CMNamedNodeMapImpl)13 IStructuredDocumentRegion (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)12 ITextRegion (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)12 CMNodeList (org.eclipse.wst.xml.core.internal.contentmodel.CMNodeList)11 IDOMNode (org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode)11 Attr (org.w3c.dom.Attr)11 ModelQuery (org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery)10 Element (org.w3c.dom.Element)10 Iterator (java.util.Iterator)9 NodeList (org.w3c.dom.NodeList)8 CMDataType (org.eclipse.wst.xml.core.internal.contentmodel.CMDataType)7 HashMap (java.util.HashMap)4