Search in sources :

Example 6 with IDOMElement

use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement 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 7 with IDOMElement

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

the class SyntaxValidator method validate.

public void validate(IndexedRegion indexedNode) {
    Node node = (Node) indexedNode;
    validateChildren(node);
    if (node.getNodeType() != Node.ELEMENT_NODE)
        return;
    if (!(node instanceof IDOMElement))
        return;
    ElementInfo info = new ElementInfo();
    info.target = (IDOMElement) node;
    if (info.target.getModel().isModelStateChanging()) {
        return;
    }
    // gather information to validate from target at once.
    getInfo(info);
    validateTags(info);
    if (info.target.isGlobalTag()) {
        validateNames(info);
        if (info.decl != null && info.isXHTML) {
            validateTagCase(info);
        }
    }
    // validate the syntax of the attributes
    validateAttributes(info);
}
Also used : IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) Node(org.w3c.dom.Node) IDOMElement(org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement)

Example 8 with IDOMElement

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

the class HTMLFormatter method canInsertBreakAfter.

/**
 */
protected boolean canInsertBreakAfter(Node node) {
    if (node == null)
        return false;
    Node parent = node.getParentNode();
    if (parent == null)
        return false;
    Node next = node.getNextSibling();
    // special exception if this node is a non-HTML tag (like JSP
    // elements)
    // BUG188093 - only preserve whitespace for jsp (not custom) tags
    String prefix = node.getPrefix();
    if (prefix != null && JSP.equals(prefix)) {
        boolean canInsertBreakAfter = false;
        // if a whitespace does not exist after it, do not add one
        if (next != null && next.getNodeType() == Node.TEXT_NODE) {
            String theText = ((Text) next).getData();
            if (theText != null && theText.length() > 0) {
                char theChar = theText.charAt(0);
                canInsertBreakAfter = Character.isWhitespace(theChar);
            }
        }
        // continue processing)
        if (!canInsertBreakAfter)
            return false;
    }
    // BUG188093 - only preserve whitespace for jsp (not custom) tags
    if (next != null) {
        prefix = next.getPrefix();
        if (prefix != null && JSP.equals(prefix)) {
            boolean canInsertBreakAfterPrevious = false;
            // if a whitespace does not exist before it, do not add one
            if (node.getNodeType() == Node.TEXT_NODE) {
                String theText = ((Text) node).getData();
                if (theText != null && theText.length() > 0) {
                    char theChar = theText.charAt(theText.length() - 1);
                    canInsertBreakAfterPrevious = Character.isWhitespace(theChar);
                }
            }
            // continue processing)
            if (!canInsertBreakAfterPrevious)
                return false;
        }
    }
    if (parent.getNodeType() == Node.DOCUMENT_NODE) {
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            // do not insert break after unclosed tag
            if (!((IDOMElement) node).isClosed())
                return false;
        }
        return true;
    } else if (parent.getNodeType() == Node.ELEMENT_NODE) {
        IDOMElement element = (IDOMElement) parent;
        // do not insert break before missing end tag
        if (next == null && element.getEndStructuredDocumentRegion() == null)
            return false;
        // elements
        if (element.getPrefix() != null)
            return true;
        CMElementDeclaration decl = getElementDeclaration(element);
        if (decl != null) {
            if (decl.getContentType() == CMElementDeclaration.ELEMENT)
                return true;
            // causes all closing tags to wrap to a new line
            boolean allowsText = decl.getContentType() == CMElementDeclaration.MIXED || decl.getContentType() == CMElementDeclaration.PCDATA;
            if (allowsNewlineAfter(allowsText, node, element, decl))
                return true;
            String tagName = element.getTagName();
            // special for direct children under BODY
            if (tagName != null && tagName.equalsIgnoreCase(BODY_NAME))
                return true;
        }
    }
    if (node.getNodeType() == Node.ELEMENT_NODE) {
        IDOMElement element = (IDOMElement) node;
        CMElementDeclaration decl = getElementDeclaration(element);
        if (canInsertBreakAfter(decl)) {
            // spcial for BR
            return canFormatChild(parent);
        }
    }
    if (next != null && next.getNodeType() == Node.ELEMENT_NODE) {
        CMElementDeclaration decl = getElementDeclaration((Element) next);
        if (canInsertBreakBefore(decl))
            return true;
    }
    return false;
}
Also used : CMElementDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) Node(org.w3c.dom.Node) Text(org.w3c.dom.Text) IDOMElement(org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement)

Example 9 with IDOMElement

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

the class HTMLFormatter method getBreakSpaces.

/**
 */
protected String getBreakSpaces(Node node) {
    if (node == null)
        return null;
    StringBuffer buffer = new StringBuffer();
    String delim = ((IDOMNode) node).getModel().getStructuredDocument().getLineDelimiter();
    if (delim != null && delim.length() > 0)
        buffer.append(delim);
    String indent = getIndent();
    if (indent != null && indent.length() > 0) {
        for (Node parent = node.getParentNode(); parent != null; parent = parent.getParentNode()) {
            if (parent.getNodeType() != Node.ELEMENT_NODE)
                break;
            // ignore omitted tag
            if (((IDOMNode) parent).getStartStructuredDocumentRegion() == null)
                continue;
            IDOMElement element = (IDOMElement) parent;
            if (element.getPrefix() != null) {
                String localName = element.getLocalName();
                // special for html:html
                if (localName != null && !localName.equals(HTML_NAME)) {
                    buffer.append(indent);
                }
                continue;
            } else {
                String localName = element.getLocalName();
                if (HTML_NAME.equalsIgnoreCase(localName) || HEAD_NAME.equalsIgnoreCase(localName))
                    break;
            }
            CMElementDeclaration decl = getElementDeclaration(element);
            if (decl != null && decl.supports(HTMLCMProperties.SHOULD_INDENT_CHILD_SOURCE)) {
                boolean shouldIndent = isIdentable(node, parent);
                if (shouldIndent)
                    buffer.append(indent);
            }
        }
    }
    return buffer.toString();
}
Also used : CMElementDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) Node(org.w3c.dom.Node) IDOMElement(org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement)

Example 10 with IDOMElement

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

the class JSPActionValidator method processDirective.

private void processDirective(IReporter reporter, IFile file, IStructuredModel model, IStructuredDocumentRegion documentRegion) {
    IndexedRegion ir = model.getIndexedRegion(documentRegion.getStartOffset());
    if (ir instanceof IDOMElement) {
        IDOMElement element = (IDOMElement) ir;
        ModelQuery query = ModelQueryUtil.getModelQuery(model);
        if (query != null) {
            CMElementDeclaration cmElement = query.getCMElementDeclaration(element);
            if (cmElement != null) {
                CMNamedNodeMap cmAttributes = null;
                CMNamedNodeMapImpl allAttributes = new CMNamedNodeMapImpl();
                List nodes = query.getAvailableContent(element, cmElement, 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);
                    }
                }
                cmAttributes = allAttributes;
                boolean foundjspattribute = checkUnknownAttributes(element, cmElement, cmAttributes, reporter, file, model.getStructuredDocument(), documentRegion);
                // missing required attributes
                if (!foundjspattribute && fSeverityMissingRequiredAttribute != ValidationMessage.IGNORE)
                    checkRequiredAttributes(element, cmAttributes, reporter, file, model.getStructuredDocument(), documentRegion);
                if (fSeverityNonEmptyInlineTag != ValidationMessage.IGNORE)
                    checkNonEmptyInlineTag(element, cmElement, reporter, file, model.getStructuredDocument());
            }
        }
    }
}
Also used : CMNamedNodeMapImpl(org.eclipse.wst.xml.core.internal.contentmodel.basic.CMNamedNodeMapImpl) CMElementDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration) ModelQuery(org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery) List(java.util.List) ITextRegionList(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList) NodeList(org.w3c.dom.NodeList) CMNode(org.eclipse.wst.xml.core.internal.contentmodel.CMNode) CMNamedNodeMap(org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion) IDOMElement(org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement)

Aggregations

IDOMElement (org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement)59 IDOMModel (org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel)20 IDOMNode (org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode)19 Node (org.w3c.dom.Node)18 NodeList (org.w3c.dom.NodeList)13 Element (org.w3c.dom.Element)11 IStructuredDocumentRegion (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)9 IDOMAttr (org.eclipse.wst.xml.core.internal.provisional.document.IDOMAttr)9 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)8 CMElementDeclaration (org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration)8 NamedNodeMap (org.w3c.dom.NamedNodeMap)8 ArrayList (java.util.ArrayList)7 ITextRegion (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)7 IDOMDocument (org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument)7 IFile (org.eclipse.core.resources.IFile)6 ITextRegionList (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList)6 List (java.util.List)5 CMNamedNodeMap (org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap)5 Text (org.w3c.dom.Text)5 ModelQuery (org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery)4