Java Code Examples for com.sun.source.doctree.StartElementTree#isSelfClosing()

The following examples show how to use com.sun.source.doctree.StartElementTree#isSelfClosing() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: VeryPretty.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public Void visitStartElement(StartElementTree node, Void p) {
    print("<");
    print(node.getName());
    java.util.List<? extends DocTree> attrs = node.getAttributes();
    if (!attrs.isEmpty()) {
        print(" ");
        for (DocTree docTree : attrs) {
            doAccept((DCTree)docTree);
        }
        DocTree last = attrs.get(attrs.size() - 1);
        if (node.isSelfClosing() && last instanceof AttributeTree
                && ((AttributeTree) last).getValueKind() == ValueKind.UNQUOTED)
            print(" ");
    }
    if (node.isSelfClosing())
        print("/");
    print(">");
    return null;
}
 
Example 2
Source File: Analyzer.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
@Messages({"# {0} - Tag name",
           "TAG_UNKNOWN=Unknown HTML Tag: <{0}>",
           "# {0} - Tag name",
           "TAG_END_UNKNOWN=Unknown HTML End Tag: </{0}>",
           "TAG_SELF_CLOSING=Self-closing element not allowed"})
public Void visitStartElement(StartElementTree node, List<ErrorDescription> errors) {
    DocTreePath currentDocPath = getCurrentPath();
    DocTreePathHandle dtph = DocTreePathHandle.create(currentDocPath, javac);
    if(dtph == null) {
        return null;
    }
    DocSourcePositions sp = (DocSourcePositions) javac.getTrees().getSourcePositions();
    int start = (int) sp.getStartPosition(javac.getCompilationUnit(), currentDocPath.getDocComment(), node);
    int end = (int) sp.getEndPosition(javac.getCompilationUnit(), currentDocPath.getDocComment(), node);


    final Name treeName = node.getName();
    final HtmlTag t = HtmlTag.get(treeName);
    if (t == null) {
        errors.add(ErrorDescriptionFactory.forSpan(ctx, start, end, TAG_UNKNOWN(treeName)));
    } else {
        if(t.endKind != HtmlTag.EndKind.NONE) {
            tagStack.push(node);
        }
    }
    
    if (node.isSelfClosing()) {
        errors.add(ErrorDescriptionFactory.forSpan(ctx, start, end, TAG_SELF_CLOSING()));
    }
    
    return super.visitStartElement(node, errors);
}
 
Example 3
Source File: Checker.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Void visitStartElement(StartElementTree tree, Void ignore) {
    final Name treeName = tree.getName();
    final HtmlTag t = HtmlTag.get(treeName);
    if (t == null) {
        env.messages.error(HTML, tree, "dc.tag.unknown", treeName);
    } else {
        boolean done = false;
        for (TagStackItem tsi: tagStack) {
            if (tsi.tag.accepts(t)) {
                while (tagStack.peek() != tsi) {
                    warnIfEmpty(tagStack.peek(), null);
                    tagStack.pop();
                }
                done = true;
                break;
            } else if (tsi.tag.endKind != HtmlTag.EndKind.OPTIONAL) {
                done = true;
                break;
            }
        }
        if (!done && HtmlTag.BODY.accepts(t)) {
            while (!tagStack.isEmpty()) {
                warnIfEmpty(tagStack.peek(), null);
                tagStack.pop();
            }
        }

        markEnclosingTag(Flag.HAS_ELEMENT);
        checkStructure(tree, t);

        // tag specific checks
        switch (t) {
            // check for out of sequence headers, such as <h1>...</h1>  <h3>...</h3>
            case H1: case H2: case H3: case H4: case H5: case H6:
                checkHeader(tree, t);
                break;
        }

        if (t.flags.contains(HtmlTag.Flag.NO_NEST)) {
            for (TagStackItem i: tagStack) {
                if (t == i.tag) {
                    env.messages.warning(HTML, tree, "dc.tag.nested.not.allowed", treeName);
                    break;
                }
            }
        }
    }

    // check for self closing tags, such as <a id="name"/>
    if (tree.isSelfClosing()) {
        env.messages.error(HTML, tree, "dc.tag.self.closing", treeName);
    }

    try {
        TagStackItem parent = tagStack.peek();
        TagStackItem top = new TagStackItem(tree, t);
        tagStack.push(top);

        super.visitStartElement(tree, ignore);

        // handle attributes that may or may not have been found in start element
        if (t != null) {
            switch (t) {
                case CAPTION:
                    if (parent != null && parent.tag == HtmlTag.TABLE)
                        parent.flags.add(Flag.TABLE_HAS_CAPTION);
                    break;

                case IMG:
                    if (!top.attrs.contains(HtmlTag.Attr.ALT))
                        env.messages.error(ACCESSIBILITY, tree, "dc.no.alt.attr.for.image");
                    break;
            }
        }

        return null;
    } finally {

        if (t == null || t.endKind == HtmlTag.EndKind.NONE)
            tagStack.pop();
    }
}
 
Example 4
Source File: Checker.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Void visitStartElement(StartElementTree tree, Void ignore) {
    final Name treeName = tree.getName();
    final HtmlTag t = HtmlTag.get(treeName);
    if (t == null) {
        env.messages.error(HTML, tree, "dc.tag.unknown", treeName);
    } else {
        boolean done = false;
        for (TagStackItem tsi: tagStack) {
            if (tsi.tag.accepts(t)) {
                while (tagStack.peek() != tsi) {
                    warnIfEmpty(tagStack.peek(), null);
                    tagStack.pop();
                }
                done = true;
                break;
            } else if (tsi.tag.endKind != HtmlTag.EndKind.OPTIONAL) {
                done = true;
                break;
            }
        }
        if (!done && HtmlTag.BODY.accepts(t)) {
            while (!tagStack.isEmpty()) {
                warnIfEmpty(tagStack.peek(), null);
                tagStack.pop();
            }
        }

        markEnclosingTag(Flag.HAS_ELEMENT);
        checkStructure(tree, t);

        // tag specific checks
        switch (t) {
            // check for out of sequence headers, such as <h1>...</h1>  <h3>...</h3>
            case H1: case H2: case H3: case H4: case H5: case H6:
                checkHeader(tree, t);
                break;
        }

        if (t.flags.contains(HtmlTag.Flag.NO_NEST)) {
            for (TagStackItem i: tagStack) {
                if (t == i.tag) {
                    env.messages.warning(HTML, tree, "dc.tag.nested.not.allowed", treeName);
                    break;
                }
            }
        }
    }

    // check for self closing tags, such as <a id="name"/>
    if (tree.isSelfClosing()) {
        env.messages.error(HTML, tree, "dc.tag.self.closing", treeName);
    }

    try {
        TagStackItem parent = tagStack.peek();
        TagStackItem top = new TagStackItem(tree, t);
        tagStack.push(top);

        super.visitStartElement(tree, ignore);

        // handle attributes that may or may not have been found in start element
        if (t != null) {
            switch (t) {
                case CAPTION:
                    if (parent != null && parent.tag == HtmlTag.TABLE)
                        parent.flags.add(Flag.TABLE_HAS_CAPTION);
                    break;

                case IMG:
                    if (!top.attrs.contains(HtmlTag.Attr.ALT))
                        env.messages.error(ACCESSIBILITY, tree, "dc.no.alt.attr.for.image");
                    break;
            }
        }

        return null;
    } finally {

        if (t == null || t.endKind == HtmlTag.EndKind.NONE)
            tagStack.pop();
    }
}
 
Example 5
Source File: Checker.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Void visitStartElement(StartElementTree tree, Void ignore) {
    final Name treeName = tree.getName();
    final HtmlTag t = HtmlTag.get(treeName);
    if (t == null) {
        env.messages.error(HTML, tree, "dc.tag.unknown", treeName);
    } else {
        boolean done = false;
        for (TagStackItem tsi: tagStack) {
            if (tsi.tag.accepts(t)) {
                while (tagStack.peek() != tsi) {
                    warnIfEmpty(tagStack.peek(), null);
                    tagStack.pop();
                }
                done = true;
                break;
            } else if (tsi.tag.endKind != HtmlTag.EndKind.OPTIONAL) {
                done = true;
                break;
            }
        }
        if (!done && HtmlTag.BODY.accepts(t)) {
            while (!tagStack.isEmpty()) {
                warnIfEmpty(tagStack.peek(), null);
                tagStack.pop();
            }
        }

        markEnclosingTag(Flag.HAS_ELEMENT);
        checkStructure(tree, t);

        // tag specific checks
        switch (t) {
            // check for out of sequence headers, such as <h1>...</h1>  <h3>...</h3>
            case H1: case H2: case H3: case H4: case H5: case H6:
                checkHeader(tree, t);
                break;
        }

        if (t.flags.contains(HtmlTag.Flag.NO_NEST)) {
            for (TagStackItem i: tagStack) {
                if (t == i.tag) {
                    env.messages.warning(HTML, tree, "dc.tag.nested.not.allowed", treeName);
                    break;
                }
            }
        }
    }

    // check for self closing tags, such as <a id="name"/>
    if (tree.isSelfClosing()) {
        env.messages.error(HTML, tree, "dc.tag.self.closing", treeName);
    }

    try {
        TagStackItem parent = tagStack.peek();
        TagStackItem top = new TagStackItem(tree, t);
        tagStack.push(top);

        super.visitStartElement(tree, ignore);

        // handle attributes that may or may not have been found in start element
        if (t != null) {
            switch (t) {
                case CAPTION:
                    if (parent != null && parent.tag == HtmlTag.TABLE)
                        parent.flags.add(Flag.TABLE_HAS_CAPTION);
                    break;

                case IMG:
                    if (!top.attrs.contains(HtmlTag.Attr.ALT))
                        env.messages.error(ACCESSIBILITY, tree, "dc.no.alt.attr.for.image");
                    break;
            }
        }

        return null;
    } finally {

        if (t == null || t.endKind == HtmlTag.EndKind.NONE)
            tagStack.pop();
    }
}
 
Example 6
Source File: Checker.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Void visitStartElement(StartElementTree tree, Void ignore) {
    final Name treeName = tree.getName();
    final HtmlTag t = HtmlTag.get(treeName);
    if (t == null) {
        env.messages.error(HTML, tree, "dc.tag.unknown", treeName);
    } else {
        boolean done = false;
        for (TagStackItem tsi: tagStack) {
            if (tsi.tag.accepts(t)) {
                while (tagStack.peek() != tsi) {
                    warnIfEmpty(tagStack.peek(), null);
                    tagStack.pop();
                }
                done = true;
                break;
            } else if (tsi.tag.endKind != HtmlTag.EndKind.OPTIONAL) {
                done = true;
                break;
            }
        }
        if (!done && HtmlTag.BODY.accepts(t)) {
            while (!tagStack.isEmpty()) {
                warnIfEmpty(tagStack.peek(), null);
                tagStack.pop();
            }
        }

        markEnclosingTag(Flag.HAS_ELEMENT);
        checkStructure(tree, t);

        // tag specific checks
        switch (t) {
            // check for out of sequence headers, such as <h1>...</h1>  <h3>...</h3>
            case H1: case H2: case H3: case H4: case H5: case H6:
                checkHeader(tree, t);
                break;
        }

        if (t.flags.contains(HtmlTag.Flag.NO_NEST)) {
            for (TagStackItem i: tagStack) {
                if (t == i.tag) {
                    env.messages.warning(HTML, tree, "dc.tag.nested.not.allowed", treeName);
                    break;
                }
            }
        }
    }

    // check for self closing tags, such as <a id="name"/>
    if (tree.isSelfClosing()) {
        env.messages.error(HTML, tree, "dc.tag.self.closing", treeName);
    }

    try {
        TagStackItem parent = tagStack.peek();
        TagStackItem top = new TagStackItem(tree, t);
        tagStack.push(top);

        super.visitStartElement(tree, ignore);

        // handle attributes that may or may not have been found in start element
        if (t != null) {
            switch (t) {
                case CAPTION:
                    if (parent != null && parent.tag == HtmlTag.TABLE)
                        parent.flags.add(Flag.TABLE_HAS_CAPTION);
                    break;

                case IMG:
                    if (!top.attrs.contains(HtmlTag.Attr.ALT))
                        env.messages.error(ACCESSIBILITY, tree, "dc.no.alt.attr.for.image");
                    break;
            }
        }

        return null;
    } finally {

        if (t == null || t.endKind == HtmlTag.EndKind.NONE)
            tagStack.pop();
    }
}
 
Example 7
Source File: Checker.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Override @DefinedBy(Api.COMPILER_TREE)
public Void visitStartElement(StartElementTree tree, Void ignore) {
    final Name treeName = tree.getName();
    final HtmlTag t = HtmlTag.get(treeName);
    if (t == null) {
        env.messages.error(HTML, tree, "dc.tag.unknown", treeName);
    } else if (t.allowedVersion != HtmlVersion.ALL && t.allowedVersion != env.htmlVersion) {
        env.messages.error(HTML, tree, "dc.tag.not.supported", treeName);
    } else {
        boolean done = false;
        for (TagStackItem tsi: tagStack) {
            if (tsi.tag.accepts(t)) {
                while (tagStack.peek() != tsi) {
                    warnIfEmpty(tagStack.peek(), null);
                    tagStack.pop();
                }
                done = true;
                break;
            } else if (tsi.tag.endKind != HtmlTag.EndKind.OPTIONAL) {
                done = true;
                break;
            }
        }
        if (!done && HtmlTag.BODY.accepts(t)) {
            while (!tagStack.isEmpty()) {
                warnIfEmpty(tagStack.peek(), null);
                tagStack.pop();
            }
        }

        markEnclosingTag(Flag.HAS_ELEMENT);
        checkStructure(tree, t);

        // tag specific checks
        switch (t) {
            // check for out of sequence headers, such as <h1>...</h1>  <h3>...</h3>
            case H1: case H2: case H3: case H4: case H5: case H6:
                checkHeader(tree, t);
                break;
        }

        if (t.flags.contains(HtmlTag.Flag.NO_NEST)) {
            for (TagStackItem i: tagStack) {
                if (t == i.tag) {
                    env.messages.warning(HTML, tree, "dc.tag.nested.not.allowed", treeName);
                    break;
                }
            }
        }
    }

    // check for self closing tags, such as <a id="name"/>
    if (tree.isSelfClosing()) {
        env.messages.error(HTML, tree, "dc.tag.self.closing", treeName);
    }

    try {
        TagStackItem parent = tagStack.peek();
        TagStackItem top = new TagStackItem(tree, t);
        tagStack.push(top);

        super.visitStartElement(tree, ignore);

        // handle attributes that may or may not have been found in start element
        if (t != null) {
            switch (t) {
                case CAPTION:
                    if (parent != null && parent.tag == HtmlTag.TABLE)
                        parent.flags.add(Flag.TABLE_HAS_CAPTION);
                    break;

                case H1: case H2: case H3: case H4: case H5: case H6:
                    if (parent != null && (parent.tag == HtmlTag.SECTION || parent.tag == HtmlTag.ARTICLE)) {
                        parent.flags.add(Flag.HAS_HEADING);
                    }
                    break;

                case IMG:
                    if (!top.attrs.contains(HtmlTag.Attr.ALT))
                        env.messages.error(ACCESSIBILITY, tree, "dc.no.alt.attr.for.image");
                    break;
            }
        }

        return null;
    } finally {

        if (t == null || t.endKind == HtmlTag.EndKind.NONE)
            tagStack.pop();
    }
}
 
Example 8
Source File: Checker.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Void visitStartElement(StartElementTree tree, Void ignore) {
    final Name treeName = tree.getName();
    final HtmlTag t = HtmlTag.get(treeName);
    if (t == null) {
        env.messages.error(HTML, tree, "dc.tag.unknown", treeName);
    } else {
        boolean done = false;
        for (TagStackItem tsi: tagStack) {
            if (tsi.tag.accepts(t)) {
                while (tagStack.peek() != tsi) {
                    warnIfEmpty(tagStack.peek(), null);
                    tagStack.pop();
                }
                done = true;
                break;
            } else if (tsi.tag.endKind != HtmlTag.EndKind.OPTIONAL) {
                done = true;
                break;
            }
        }
        if (!done && HtmlTag.BODY.accepts(t)) {
            while (!tagStack.isEmpty()) {
                warnIfEmpty(tagStack.peek(), null);
                tagStack.pop();
            }
        }

        markEnclosingTag(Flag.HAS_ELEMENT);
        checkStructure(tree, t);

        // tag specific checks
        switch (t) {
            // check for out of sequence headers, such as <h1>...</h1>  <h3>...</h3>
            case H1: case H2: case H3: case H4: case H5: case H6:
                checkHeader(tree, t);
                break;
        }

        if (t.flags.contains(HtmlTag.Flag.NO_NEST)) {
            for (TagStackItem i: tagStack) {
                if (t == i.tag) {
                    env.messages.warning(HTML, tree, "dc.tag.nested.not.allowed", treeName);
                    break;
                }
            }
        }
    }

    // check for self closing tags, such as <a id="name"/>
    if (tree.isSelfClosing()) {
        env.messages.error(HTML, tree, "dc.tag.self.closing", treeName);
    }

    try {
        TagStackItem parent = tagStack.peek();
        TagStackItem top = new TagStackItem(tree, t);
        tagStack.push(top);

        super.visitStartElement(tree, ignore);

        // handle attributes that may or may not have been found in start element
        if (t != null) {
            switch (t) {
                case CAPTION:
                    if (parent != null && parent.tag == HtmlTag.TABLE)
                        parent.flags.add(Flag.TABLE_HAS_CAPTION);
                    break;

                case IMG:
                    if (!top.attrs.contains(HtmlTag.Attr.ALT))
                        env.messages.error(ACCESSIBILITY, tree, "dc.no.alt.attr.for.image");
                    break;
            }
        }

        return null;
    } finally {

        if (t == null || t.endKind == HtmlTag.EndKind.NONE)
            tagStack.pop();
    }
}
 
Example 9
Source File: Checker.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Void visitStartElement(StartElementTree tree, Void ignore) {
    final Name treeName = tree.getName();
    final HtmlTag t = HtmlTag.get(treeName);
    if (t == null) {
        env.messages.error(HTML, tree, "dc.tag.unknown", treeName);
    } else {
        boolean done = false;
        for (TagStackItem tsi: tagStack) {
            if (tsi.tag.accepts(t)) {
                while (tagStack.peek() != tsi) {
                    warnIfEmpty(tagStack.peek(), null);
                    tagStack.pop();
                }
                done = true;
                break;
            } else if (tsi.tag.endKind != HtmlTag.EndKind.OPTIONAL) {
                done = true;
                break;
            }
        }
        if (!done && HtmlTag.BODY.accepts(t)) {
            while (!tagStack.isEmpty()) {
                warnIfEmpty(tagStack.peek(), null);
                tagStack.pop();
            }
        }

        markEnclosingTag(Flag.HAS_ELEMENT);
        checkStructure(tree, t);

        // tag specific checks
        switch (t) {
            // check for out of sequence headers, such as <h1>...</h1>  <h3>...</h3>
            case H1: case H2: case H3: case H4: case H5: case H6:
                checkHeader(tree, t);
                break;
        }

        if (t.flags.contains(HtmlTag.Flag.NO_NEST)) {
            for (TagStackItem i: tagStack) {
                if (t == i.tag) {
                    env.messages.warning(HTML, tree, "dc.tag.nested.not.allowed", treeName);
                    break;
                }
            }
        }
    }

    // check for self closing tags, such as <a id="name"/>
    if (tree.isSelfClosing()) {
        env.messages.error(HTML, tree, "dc.tag.self.closing", treeName);
    }

    try {
        TagStackItem parent = tagStack.peek();
        TagStackItem top = new TagStackItem(tree, t);
        tagStack.push(top);

        super.visitStartElement(tree, ignore);

        // handle attributes that may or may not have been found in start element
        if (t != null) {
            switch (t) {
                case CAPTION:
                    if (parent != null && parent.tag == HtmlTag.TABLE)
                        parent.flags.add(Flag.TABLE_HAS_CAPTION);
                    break;

                case IMG:
                    if (!top.attrs.contains(HtmlTag.Attr.ALT))
                        env.messages.error(ACCESSIBILITY, tree, "dc.no.alt.attr.for.image");
                    break;
            }
        }

        return null;
    } finally {

        if (t == null || t.endKind == HtmlTag.EndKind.NONE)
            tagStack.pop();
    }
}
 
Example 10
Source File: Checker.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Void visitStartElement(StartElementTree tree, Void ignore) {
    final Name treeName = tree.getName();
    final HtmlTag t = HtmlTag.get(treeName);
    if (t == null) {
        env.messages.error(HTML, tree, "dc.tag.unknown", treeName);
    } else {
        boolean done = false;
        for (TagStackItem tsi: tagStack) {
            if (tsi.tag.accepts(t)) {
                while (tagStack.peek() != tsi) {
                    warnIfEmpty(tagStack.peek(), null);
                    tagStack.pop();
                }
                done = true;
                break;
            } else if (tsi.tag.endKind != HtmlTag.EndKind.OPTIONAL) {
                done = true;
                break;
            }
        }
        if (!done && HtmlTag.BODY.accepts(t)) {
            while (!tagStack.isEmpty()) {
                warnIfEmpty(tagStack.peek(), null);
                tagStack.pop();
            }
        }

        markEnclosingTag(Flag.HAS_ELEMENT);
        checkStructure(tree, t);

        // tag specific checks
        switch (t) {
            // check for out of sequence headers, such as <h1>...</h1>  <h3>...</h3>
            case H1: case H2: case H3: case H4: case H5: case H6:
                checkHeader(tree, t);
                break;
        }

        if (t.flags.contains(HtmlTag.Flag.NO_NEST)) {
            for (TagStackItem i: tagStack) {
                if (t == i.tag) {
                    env.messages.warning(HTML, tree, "dc.tag.nested.not.allowed", treeName);
                    break;
                }
            }
        }
    }

    // check for self closing tags, such as <a id="name"/>
    if (tree.isSelfClosing()) {
        env.messages.error(HTML, tree, "dc.tag.self.closing", treeName);
    }

    try {
        TagStackItem parent = tagStack.peek();
        TagStackItem top = new TagStackItem(tree, t);
        tagStack.push(top);

        super.visitStartElement(tree, ignore);

        // handle attributes that may or may not have been found in start element
        if (t != null) {
            switch (t) {
                case CAPTION:
                    if (parent != null && parent.tag == HtmlTag.TABLE)
                        parent.flags.add(Flag.TABLE_HAS_CAPTION);
                    break;

                case IMG:
                    if (!top.attrs.contains(HtmlTag.Attr.ALT))
                        env.messages.error(ACCESSIBILITY, tree, "dc.no.alt.attr.for.image");
                    break;
            }
        }

        return null;
    } finally {

        if (t == null || t.endKind == HtmlTag.EndKind.NONE)
            tagStack.pop();
    }
}