Search in sources :

Example 1 with IDOMAttr

use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMAttr in project liferay-ide by liferay.

the class UITestsUtils method getRegion.

public static IRegion getRegion(Node node) {
    if (node != null) {
        switch(node.getNodeType()) {
            case Node.ELEMENT_NODE:
                IDOMElement element = (IDOMElement) node;
                int endOffset;
                if (element.hasEndTag() && element.isClosed()) {
                    endOffset = element.getStartEndOffset();
                } else {
                    endOffset = element.getEndOffset();
                }
                if (element.getFirstChild() == null || element.getFirstChild().getTextContent().isEmpty()) {
                    return new Region(endOffset, 0);
                }
                return new Region(endOffset, element.getTextContent().length());
            case Node.ATTRIBUTE_NODE:
                IDOMAttr att = (IDOMAttr) node;
                int regOffset = att.getValueRegionStartOffset();
                int regLength = att.getValueRegionText().length();
                String attValue = att.getValueRegionText();
                if (StringUtil.isQuoted(attValue)) {
                    regOffset++;
                    regLength -= 2;
                }
                return new Region(regOffset, regLength);
            case Node.TEXT_NODE:
                IDOMText text = (IDOMText) node;
                int startOffset = text.getStartOffset();
                int length = text.getLength();
                return new Region(startOffset, length);
        }
    }
    return null;
}
Also used : IDOMAttr(org.eclipse.wst.xml.core.internal.provisional.document.IDOMAttr) IDOMText(org.eclipse.wst.xml.core.internal.provisional.document.IDOMText) Region(org.eclipse.jface.text.Region) IRegion(org.eclipse.jface.text.IRegion) IDOMElement(org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement)

Example 2 with IDOMAttr

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

the class HTMLAttributeValidator method validate.

/**
 */
public void validate(IndexedRegion node) {
    Element target = (Element) node;
    if (CMUtil.isForeign(target))
        return;
    CMElementDeclaration edec = CMUtil.getDeclaration(target);
    if (edec == null) {
        NamedNodeMap attrs = target.getAttributes();
        // unknown tag - go to validators from extension point
        for (int i = 0; i < attrs.getLength(); i++) {
            Attr a = (Attr) attrs.item(i);
            final String attrName = a.getName().toLowerCase(Locale.US);
            // Check for user-defined exclusions
            if (shouldValidateAttributeName(target, attrName)) {
                validateWithExtension(target, a, attrName);
            }
        }
    } else {
        CMNamedNodeMap declarations = edec.getAttributes();
        List modelQueryNodes = null;
        NamedNodeMap attrs = target.getAttributes();
        for (int i = 0; i < attrs.getLength(); i++) {
            int rgnType = REGION_NAME;
            int state = ErrorState.NONE_ERROR;
            Attr a = (Attr) attrs.item(i);
            // D203637; If the target attr has prefix, the validator should
            // not
            // warn about it. That is, just ignore. It is able to check
            // whether
            // an attr has prefix or not by calling XMLAttr#isGlobalAttr().
            // When a attr has prefix (not global), it returns false.
            boolean isXMLAttr = a instanceof IDOMAttr;
            if (isXMLAttr) {
                IDOMAttr xmlattr = (IDOMAttr) a;
                if (!xmlattr.isGlobalAttr() || xmlattr.getNameRegion() instanceof ITextRegionContainer)
                    // skip futher validation and begin next loop.
                    continue;
            }
            CMAttributeDeclaration adec = (CMAttributeDeclaration) declarations.getNamedItem(a.getName());
            final String attrName = a.getName().toLowerCase(Locale.US);
            /* Check the modelquery if nothing is declared by the element declaration */
            if (adec == null) {
                if (modelQueryNodes == null)
                    modelQueryNodes = ModelQueryUtil.getModelQuery(target.getOwnerDocument()).getAvailableContent((Element) node, edec, ModelQuery.INCLUDE_ATTRIBUTES);
                for (int k = 0; k < modelQueryNodes.size(); k++) {
                    CMNode cmnode = (CMNode) modelQueryNodes.get(k);
                    if (cmnode.getNodeType() == CMNode.ATTRIBUTE_DECLARATION && cmnode.getNodeName().toLowerCase(Locale.US).equals(attrName)) {
                        adec = (CMAttributeDeclaration) cmnode;
                        break;
                    }
                }
            }
            if (adec == null) {
                if ((attrName.startsWith(ATTR_NAME_DATA) && attrName.length() > ATTR_NAME_DATA_LENGTH) || (attrName.startsWith(ATTR_NAME_USER_AGENT_FEATURE) && attrName.length() > ATTR_NAME_USER_AGENT_FEATURE_LENGTH) || (attrName.startsWith(ATTR_NAME_WAI_ARIA) && attrName.length() > ATTR_NAME_WAI_ARIA_LENGTH)) {
                    if (isHTML5(target))
                        continue;
                }
                // Check for user-defined exclusions
                if (!shouldValidateAttributeName(target, attrName))
                    continue;
                // No attr declaration was found. That is, the attr name is
                // undefined.
                // but not regard it as undefined name if it includes nested
                // region
                // Then look into extension point for external validator
                validateWithExtension(target, a, attrName);
            } else {
                // At 1st, the name should be checked.
                if (CMUtil.isObsolete(adec)) {
                    state = ErrorState.OBSOLETE_ATTR_NAME_ERROR;
                }
                if (CMUtil.isHTML(edec) && (!CMUtil.isXHTML(edec))) {
                    // specifically.
                    if (CMUtil.isBooleanAttr(adec) && ((IDOMAttr) a).hasNameOnly())
                        // OK, keep going. No more check is needed
                        continue;
                // against this attr.
                } else {
                    // If the target is other than pure HTML (JSP or XHTML),
                    // the name
                    // must be checked exactly (ie in case sensitive way).
                    String actual = a.getName();
                    String desired = adec.getAttrName();
                    if (!actual.equals(desired)) {
                        // case mismatch
                        rgnType = REGION_NAME;
                        state = ErrorState.MISMATCHED_ERROR;
                    }
                }
                // Then, the value must be checked.
                if (state == ErrorState.NONE_ERROR) {
                    // Need more check.
                    // Now, the value should be checked, if the type is ENUM.
                    CMDataType attrType = adec.getAttrType();
                    if (a instanceof IDOMAttr) {
                        final ITextRegion region = ((IDOMAttr) a).getEqualRegion();
                        if (region == null) {
                            rgnType = REGION_NAME;
                            state = ErrorState.MISSING_ATTR_VALUE_EQUALS_ERROR;
                        }
                    }
                    String actualValue = a.getValue();
                    if (attrType.getImpliedValueKind() == CMDataType.IMPLIED_VALUE_FIXED) {
                        // Check FIXED value.
                        String validValue = attrType.getImpliedValue();
                        if (!actualValue.equals(validValue)) {
                            rgnType = REGION_VALUE;
                            state = ErrorState.UNDEFINED_VALUE_ERROR;
                        }
                    } else if (CMDataType.URI.equals(attrType.getDataTypeName())) {
                        if (actualValue.indexOf('#') < 0 && actualValue.indexOf(":/") < 0 && !actualValue.toLowerCase(Locale.ENGLISH).startsWith(JAVASCRIPT_PREFIX) && CMUtil.isHTML(edec)) {
                            // $NON-NLS-1$ //$NON-NLS-2$
                            IStructuredDocumentRegion start = ((IDOMNode) node).getStartStructuredDocumentRegion();
                            // roundabout start tag check
                            if (start != null && start.getFirstRegion().getTextLength() == 1) {
                                // only check when we have a way to set dependencies
                                Collection dependencies = (Collection) ((IDOMNode) ((IDOMNode) node).getOwnerDocument()).getUserData(HTMLValidationAdapterFactory.DEPENDENCIES);
                                if (dependencies != null) {
                                    IPath basePath = new Path(((IDOMNode) node).getModel().getBaseLocation());
                                    if (basePath.segmentCount() > 1) {
                                        IPath path = ModuleCoreSupport.resolve(basePath, actualValue);
                                        IResource found = ResourcesPlugin.getWorkspace().getRoot().findMember(path);
                                        if (found != null) {
                                            dependencies.add(found);
                                        }
                                    }
                                }
                            }
                        }
                    } else if (CMDataType.ENUM.equals(attrType.getDataTypeName())) {
                        /*
							 * Check current value is valid among a known list.
							 * There may be enumerated values provided even when
							 * the datatype is not ENUM, but we'll only validate
							 * against that list if the type matches.
							 */
                        String[] enumeratedValues = attrType.getEnumeratedValues();
                        // several candidates are found.
                        boolean found = false;
                        for (int j = 0; j < enumeratedValues.length; j++) {
                            // At 1st, compare ignoring case.
                            if (actualValue.equalsIgnoreCase(enumeratedValues[j])) {
                                found = true;
                                if (CMUtil.isCaseSensitive(edec) && (!actualValue.equals(enumeratedValues[j]))) {
                                    rgnType = REGION_VALUE;
                                    state = ErrorState.MISMATCHED_VALUE_ERROR;
                                }
                                // exit the loop.
                                break;
                            }
                        }
                        if (!found) {
                            // retrieve and check extended values (retrieval can call extensions, which may take longer)
                            String[] modelQueryExtensionValues = ModelQueryUtil.getModelQuery(target.getOwnerDocument()).getPossibleDataTypeValues((Element) node, adec);
                            // copied loop from above
                            for (int j = 0; j < modelQueryExtensionValues.length; j++) {
                                // At 1st, compare ignoring case.
                                if (actualValue.equalsIgnoreCase(modelQueryExtensionValues[j])) {
                                    found = true;
                                    if (CMUtil.isCaseSensitive(edec) && (!actualValue.equals(modelQueryExtensionValues[j]))) {
                                        rgnType = REGION_VALUE;
                                        state = ErrorState.MISMATCHED_VALUE_ERROR;
                                    }
                                    // exit the loop.
                                    break;
                                }
                            }
                            // includes nested region.
                            if (!hasNestedRegion(((IDOMNode) a).getValueRegion())) {
                                rgnType = REGION_VALUE;
                                state = ErrorState.UNDEFINED_VALUE_ERROR;
                            }
                        }
                    }
                }
                // <<D210422
                if (state == ErrorState.NONE_ERROR) {
                    // Need more check.
                    if (isXMLAttr) {
                        String source = ((IDOMAttr) a).getValueRegionText();
                        if (source != null) {
                            char firstChar = source.charAt(0);
                            char lastChar = source.charAt(source.length() - 1);
                            boolean unclosedAttr = false;
                            if (isQuote(firstChar) || isQuote(lastChar)) {
                                if (lastChar != firstChar) {
                                    unclosedAttr = true;
                                }
                            } else {
                                if (CMUtil.isXHTML(edec)) {
                                    unclosedAttr = true;
                                }
                            }
                            if (unclosedAttr) {
                                rgnType = REGION_VALUE;
                                state = ErrorState.UNCLOSED_ATTR_VALUE;
                            }
                        }
                    }
                }
            // D210422
            }
            if (state != ErrorState.NONE_ERROR) {
                Segment seg = getErrorSegment((IDOMNode) a, rgnType);
                if (seg != null)
                    reporter.report(new ErrorInfoImpl(state, seg, a));
            }
        }
    }
}
Also used : IDOMAttr(org.eclipse.wst.xml.core.internal.provisional.document.IDOMAttr) IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) CMDataType(org.eclipse.wst.xml.core.internal.contentmodel.CMDataType) IConfigurationElement(org.eclipse.core.runtime.IConfigurationElement) Element(org.w3c.dom.Element) IDOMElement(org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement) Attr(org.w3c.dom.Attr) IDOMAttr(org.eclipse.wst.xml.core.internal.provisional.document.IDOMAttr) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) List(java.util.List) ITextRegionList(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList) ArrayList(java.util.ArrayList) IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) CMNamedNodeMap(org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap) NamedNodeMap(org.w3c.dom.NamedNodeMap) IPath(org.eclipse.core.runtime.IPath) ITextRegionContainer(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionContainer) CMElementDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) Collection(java.util.Collection) CMAttributeDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMAttributeDeclaration) CMNode(org.eclipse.wst.xml.core.internal.contentmodel.CMNode) CMNamedNodeMap(org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap) IResource(org.eclipse.core.resources.IResource)

Example 3 with IDOMAttr

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

the class HTMLAttributeValidator method getErrorSegment.

/**
 */
public static Segment getErrorSegment(IDOMNode errorNode, int regionType) {
    ITextRegion rgn = null;
    switch(regionType) {
        case REGION_NAME:
            rgn = errorNode.getNameRegion();
            break;
        case REGION_VALUE:
            rgn = errorNode.getValueRegion();
            break;
        default:
            // nothing to do.
            break;
    }
    if (rgn != null) {
        if (errorNode instanceof IDOMAttr) {
            IDOMElement ownerElement = (IDOMElement) ((IDOMAttr) errorNode).getOwnerElement();
            if (ownerElement != null) {
                // if editor closed during validation this could be null
                IStructuredDocumentRegion firstRegion = ownerElement.getFirstStructuredDocumentRegion();
                if (firstRegion != null) {
                    int regionStartOffset = firstRegion.getStartOffset(rgn);
                    int regionLength = rgn.getTextLength();
                    return new Segment(regionStartOffset, regionLength);
                }
            }
        }
    }
    return new Segment(errorNode.getStartOffset(), 1);
}
Also used : IDOMAttr(org.eclipse.wst.xml.core.internal.provisional.document.IDOMAttr) IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) IDOMElement(org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement)

Example 4 with IDOMAttr

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

the class NamespaceValidator method validate.

public void validate(IndexedRegion node) {
    Element target = (Element) node;
    if (isXMLElement(target) && hasUnknownPrefix(target)) {
        IDOMElement e = (IDOMElement) target;
        if (!isValidPrefix(e.getPrefix(), target) && !e.isCommentTag()) {
            // report unknown tag error.
            Segment errorSeg = null;
            if (e.hasStartTag())
                errorSeg = FMUtil.getSegment(e, FMUtil.SEG_START_TAG);
            else if (e.hasEndTag())
                errorSeg = FMUtil.getSegment(e, FMUtil.SEG_END_TAG);
            if (errorSeg != null)
                reporter.report(new ErrorInfoImpl(UNDEFINED_NAME_ERROR, errorSeg, e));
        }
    }
    // (2) check prefix of each attr
    NamedNodeMap attrs = target.getAttributes();
    for (int i = 0; i < attrs.getLength(); i++) {
        Node n = attrs.item(i);
        // some containers will contain languages that also use ':'
        if (!(n instanceof IDOMAttr) || ((IDOMAttr) n).getNameRegion() instanceof ITextRegionContainer)
            continue;
        IDOMAttr a = (IDOMAttr) n;
        String prefix = a.getPrefix();
        if ((prefix != null) && isUnknownAttr(a, target)) {
            // The attr has unknown prefix.  So, check it.
            if (!isValidPrefix(prefix, target)) {
                // report unknown attr error.
                ITextRegion r = a.getNameRegion();
                if (r == null)
                    continue;
                int a_offset = a.getNameRegionStartOffset();
                int a_length = a.getNameRegion().getLength();
                reporter.report(new ErrorInfoImpl(UNDEFINED_NAME_ERROR, new Segment(a_offset, a_length), a));
            }
        }
    }
}
Also used : IDOMAttr(org.eclipse.wst.xml.core.internal.provisional.document.IDOMAttr) CMNamedNodeMap(org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap) NamedNodeMap(org.w3c.dom.NamedNodeMap) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) Element(org.w3c.dom.Element) IDOMElement(org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement) Node(org.w3c.dom.Node) ITextRegionContainer(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionContainer) IDOMElement(org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement)

Example 5 with IDOMAttr

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

the class JSPActionValidator method checkUnknownAttributes.

private boolean checkUnknownAttributes(IDOMElement element, CMElementDeclaration elementDecl, CMNamedNodeMap cmAttrs, IReporter reporter, IFile file, IStructuredDocument document, IStructuredDocumentRegion documentRegion) {
    boolean foundjspattribute = false;
    boolean dynamicAttributesAllowed = false;
    CMElementDeclaration decl = elementDecl;
    if (decl instanceof CMNodeWrapper)
        decl = (CMElementDeclaration) ((CMNodeWrapper) decl).getOriginNode();
    if (decl instanceof TLDElementDeclaration) {
        String dynamicAttributes = ((TLDElementDeclaration) decl).getDynamicAttributes();
        dynamicAttributesAllowed = dynamicAttributes != null ? Boolean.valueOf(dynamicAttributes).booleanValue() : false;
    }
    NamedNodeMap attrs = element.getAttributes();
    for (int i = 0; i < attrs.getLength(); i++) {
        Attr a = (Attr) attrs.item(i);
        CMAttributeDeclaration adec = (CMAttributeDeclaration) cmAttrs.getNamedItem(a.getName());
        if (adec == null) {
            /*
				 * No attr declaration was found. That is, the attr name is
				 * undefined. Disregard it includes JSP structure or this
				 * element supports dynamic attributes
				 */
            if (!hasJSPRegion(((IDOMNode) a).getNameRegion()) && fSeverityUnknownAttribute != ValidationMessage.IGNORE) {
                if (!dynamicAttributesAllowed) {
                    String msgText = NLS.bind(JSPCoreMessages.JSPDirectiveValidator_6, a.getName());
                    LocalizedMessage message = new LocalizedMessage(fSeverityUnknownAttribute, msgText, file);
                    int start = ((IDOMAttr) a).getNameRegionStartOffset();
                    int length = ((IDOMAttr) a).getNameRegionEndOffset() - start;
                    int lineNo = document.getLineOfOffset(start);
                    message.setLineNo(lineNo);
                    message.setOffset(start);
                    message.setLength(length);
                    reporter.addMessage(fMessageOriginator, message);
                }
            } else {
                foundjspattribute = true;
            }
        } else {
            if (fSeverityUnexpectedRuntimeExpression != ValidationMessage.IGNORE && adec instanceof TLDAttributeDeclaration) {
                // The attribute cannot have a runtime evaluation of an expression
                if (!isTrue(((TLDAttributeDeclaration) adec).getRtexprvalue())) {
                    IDOMAttr attr = (IDOMAttr) a;
                    if (checkRuntimeValue(attr) && !fIsELIgnored) {
                        String msg = NLS.bind(JSPCoreMessages.JSPActionValidator_1, a.getName());
                        LocalizedMessage message = new LocalizedMessage(fSeverityUnexpectedRuntimeExpression, msg, file);
                        ITextRegion region = attr.getValueRegion();
                        int start = attr.getValueRegionStartOffset();
                        int length = region != null ? region.getTextLength() : 0;
                        int lineNo = document.getLineOfOffset(start);
                        message.setLineNo(lineNo);
                        message.setOffset(start);
                        message.setLength(length);
                        reporter.addMessage(fMessageOriginator, message);
                    }
                }
            }
        }
    }
    return foundjspattribute;
}
Also used : IDOMAttr(org.eclipse.wst.xml.core.internal.provisional.document.IDOMAttr) CMNodeWrapper(org.eclipse.wst.xml.core.internal.provisional.contentmodel.CMNodeWrapper) CMNamedNodeMap(org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap) NamedNodeMap(org.w3c.dom.NamedNodeMap) TLDAttributeDeclaration(org.eclipse.jst.jsp.core.internal.contentmodel.tld.provisional.TLDAttributeDeclaration) Attr(org.w3c.dom.Attr) IDOMAttr(org.eclipse.wst.xml.core.internal.provisional.document.IDOMAttr) CMElementDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration) TLDElementDeclaration(org.eclipse.jst.jsp.core.internal.contentmodel.tld.provisional.TLDElementDeclaration) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) CMAttributeDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMAttributeDeclaration)

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