Search in sources :

Example 1 with CMNode

use of org.eclipse.wst.xml.core.internal.contentmodel.CMNode 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 2 with CMNode

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

the class HMQUtil method extractDeclarations.

private static void extractDeclarations(Hashtable availables, CMGroup group) {
    CMNodeList content = group.getChildNodes();
    for (int i = 0; i < content.getLength(); i++) {
        CMNode cmn = content.item(i);
        if (cmn == null || cmn.getNodeType() != CMNode.ELEMENT_DECLARATION)
            continue;
        addInclusion(availables, (CMElementDeclaration) cmn);
    }
}
Also used : CMNode(org.eclipse.wst.xml.core.internal.contentmodel.CMNode) CMNodeList(org.eclipse.wst.xml.core.internal.contentmodel.CMNodeList)

Example 3 with CMNode

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

the class HTMLModelQueryImpl method isExcluded.

// utilities
private static boolean isExcluded(CMElementDeclaration candidate, Element target) {
    CMNamedNodeMap prohibited = getProhibitedAncestors(candidate);
    if (prohibited == null)
        return false;
    Element parent = target;
    while (parent != null) {
        CMNode pdec = prohibited.getNamedItem(parent.getNodeName());
        if (pdec != null)
            return true;
        parent = getExplicitParentElement(parent);
    }
    return false;
}
Also used : Element(org.w3c.dom.Element) IDOMElement(org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement) CMNode(org.eclipse.wst.xml.core.internal.contentmodel.CMNode) CMNamedNodeMap(org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap)

Example 4 with CMNode

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

the class HedTIME method getExclusion.

public CMContent getExclusion() {
    if (exclusion != null)
        // already created.
        return exclusion;
    if (elementCollection == null)
        return null;
    exclusion = new CMGroupImpl(CMGroup.CHOICE, 1, 1);
    CMNode time = elementCollection.getNamedItem(HTML50Namespace.ElementName.TIME);
    if (time != null)
        exclusion.appendChild(time);
    return exclusion;
}
Also used : CMNode(org.eclipse.wst.xml.core.internal.contentmodel.CMNode)

Example 5 with CMNode

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

the class CtdHead method createContent.

/**
 * for HEAD.
 * To avoid using inclusion, the content model comes from the XHTML 1.0.
 *
 * (%head.misc;, ((title, %head.misc;, (base, %head.misc;)?) | (base, %head.misc;, (title, %head.misc;))))
 * And %head.misc; is:
 * (script|style|meta|link|object|isindex)*
 *
 * 0: (%head.misc, A)
 * A: (B | C)
 * B: (title, %head.misc;, D)
 * C: (base, %head.misc;, E)
 * D: (base, %head.misc;)?
 * E: (title, %head.misc;)
 */
protected void createContent() {
    if (content != null)
        // already created.
        return;
    if (collection == null)
        return;
    // At 1st, create %head.misc; content.
    // %head.misc;
    // ( | )*
    CMGroupImpl misc = new CMGroupImpl(CMGroup.CHOICE, 0, CMContentImpl.UNBOUNDED);
    if (misc == null)
        return;
    String[] names = { CHTMLNamespace.ElementName.META };
    collection.getDeclarations(misc, Arrays.asList(names).iterator());
    // 2nd, get a title
    CMNode title = collection.getNamedItem(CHTMLNamespace.ElementName.TITLE);
    // 3rd, get a base
    CMNode base = collection.getNamedItem(CHTMLNamespace.ElementName.BASE);
    if (title == null || base == null)
        return;
    // Top level content is a sequence of %head.misc; and A.
    // 0: (%head.misc;, A)
    // create a sequence
    content = new CMGroupImpl(CMGroup.SEQUENCE, 1, 1);
    if (content == null)
        return;
    // append %head.misc;
    content.appendChild(misc);
    // create A and append it to the top level.
    {
        // A is a choice of B and C.
        // A: (B | C)
        // create a choice
        CMGroupImpl gA = new CMGroupImpl(CMGroup.CHOICE, 1, 1);
        if (gA == null)
            return;
        // append A to the top level.
        content.appendChild(gA);
        // create B and append it to A
        {
            // B is a sequence of title, %head.misc;, and D.
            // B: (title, %head.misc;, D)
            // create a sequence
            CMGroupImpl gB = new CMGroupImpl(CMGroup.SEQUENCE, 1, 1);
            if (gB == null)
                return;
            // append B to A.
            gA.appendChild(gB);
            // append title to B
            gB.appendChild(title);
            // append %head.misc; to B
            gB.appendChild(misc);
            // create D and append it to B.
            {
                // D is a sequence of base, %head.misc;.
                // D: (base, %head.misc;)?
                // create a sequence
                CMGroupImpl gD = new CMGroupImpl(CMGroup.SEQUENCE, 0, 1);
                if (gD == null)
                    return;
                // append D to B.
                gB.appendChild(gD);
                // append base to D
                gD.appendChild(base);
                // append %head.misc; to D.
                gD.appendChild(misc);
            }
        }
        // create C and append it to A
        {
            // C is a sequence of base, %head.misc;, and E
            // C: (base, %head.misc;, E)
            // create a sequence
            CMGroupImpl gC = new CMGroupImpl(CMGroup.SEQUENCE, 1, 1);
            if (gC == null)
                return;
            // append C to A.
            gA.appendChild(gC);
            // append base to C
            gC.appendChild(base);
            // append %head.misc; to C
            gC.appendChild(misc);
            // create E and append it to C.
            {
                // E is a sequence of title and %head.misc;.
                // E: (title, %head.misc;)
                // create a sequence
                CMGroupImpl gE = new CMGroupImpl(CMGroup.SEQUENCE, 1, 1);
                if (gE == null)
                    return;
                // append E to C.
                gC.appendChild(gE);
                // append title to E
                gE.appendChild(title);
                // append %head.misc; to E.
                gE.appendChild(misc);
            }
        }
    }
}
Also used : CMNode(org.eclipse.wst.xml.core.internal.contentmodel.CMNode)

Aggregations

CMNode (org.eclipse.wst.xml.core.internal.contentmodel.CMNode)133 CMElementDeclaration (org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration)37 List (java.util.List)36 CMNamedNodeMap (org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap)35 ArrayList (java.util.ArrayList)28 Iterator (java.util.Iterator)23 ITextRegionList (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList)20 CMNodeList (org.eclipse.wst.xml.core.internal.contentmodel.CMNodeList)19 CMAttributeDeclaration (org.eclipse.wst.xml.core.internal.contentmodel.CMAttributeDeclaration)17 CMDocument (org.eclipse.wst.xml.core.internal.contentmodel.CMDocument)17 Element (org.w3c.dom.Element)16 NodeList (org.w3c.dom.NodeList)15 CMNamedNodeMapImpl (org.eclipse.wst.xml.core.internal.contentmodel.basic.CMNamedNodeMapImpl)14 ModelQuery (org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery)13 Node (org.w3c.dom.Node)13 NamedNodeMap (org.w3c.dom.NamedNodeMap)10 ModelQueryAction (org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQueryAction)9 IDOMNode (org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode)9 Image (org.eclipse.swt.graphics.Image)8 CustomCompletionProposal (org.eclipse.wst.sse.ui.internal.contentassist.CustomCompletionProposal)8