jjg@1409: /*
jjg@1409: * Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved.
jjg@1409: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jjg@1409: *
jjg@1409: * This code is free software; you can redistribute it and/or modify it
jjg@1409: * under the terms of the GNU General Public License version 2 only, as
jjg@1409: * published by the Free Software Foundation. Oracle designates this
jjg@1409: * particular file as subject to the "Classpath" exception as provided
jjg@1409: * by Oracle in the LICENSE file that accompanied this code.
jjg@1409: *
jjg@1409: * This code is distributed in the hope that it will be useful, but WITHOUT
jjg@1409: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jjg@1409: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jjg@1409: * version 2 for more details (a copy is included in the LICENSE file that
jjg@1409: * accompanied this code).
jjg@1409: *
jjg@1409: * You should have received a copy of the GNU General Public License version
jjg@1409: * 2 along with this work; if not, write to the Free Software Foundation,
jjg@1409: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jjg@1409: *
jjg@1409: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jjg@1409: * or visit www.oracle.com if you need additional information or have any
jjg@1409: * questions.
jjg@1409: */
jjg@1409:
jjg@1409: package com.sun.source.util;
jjg@1409:
jjg@1409: import com.sun.source.doctree.*;
jjg@1409:
jjg@1409:
jjg@1409: /**
jjg@1409: * A TreeVisitor that visits all the child tree nodes.
jjg@1409: * To visit nodes of a particular type, just override the
jjg@1409: * corresponding visitXYZ method.
jjg@1409: * Inside your method, call super.visitXYZ to visit descendant
jjg@1409: * nodes.
jjg@1409: *
jjg@1409: *
The default implementation of the visitXYZ methods will determine
jjg@1409: * a result as follows:
jjg@1409: *
jjg@1409: * - If the node being visited has no children, the result will be null.
jjg@1409: *
- If the node being visited has one child, the result will be the
jjg@1409: * result of calling {@code scan} on that child. The child may be a simple node
jjg@1409: * or itself a list of nodes.
jjg@1409: *
- If the node being visited has more than one child, the result will
jjg@1409: * be determined by calling {@code scan} each child in turn, and then combining the
jjg@1409: * result of each scan after the first with the cumulative result
jjg@1409: * so far, as determined by the {@link #reduce} method. Each child may be either
jjg@1409: * a simple node of a list of nodes. The default behavior of the {@code reduce}
jjg@1409: * method is such that the result of the visitXYZ method will be the result of
jjg@1409: * the last child scanned.
jjg@1409: *
jjg@1409: *
jjg@1409: * Here is an example to count the number of erroneous nodes in a tree:
jjg@1409: *
jjg@1409: * class CountErrors extends DocTreeScanner {
jjg@1409: * {@literal @}Override
jjg@1409: * public Integer visitErroneous(ErroneousTree node, Void p) {
jjg@1409: * return 1;
jjg@1409: * }
jjg@1409: * {@literal @}Override
jjg@1409: * public Integer reduce(Integer r1, Integer r2) {
jjg@1409: * return (r1 == null ? 0 : r1) + (r2 == null ? 0 : r2);
jjg@1409: * }
jjg@1409: * }
jjg@1409: *
jjg@1409: *
jjg@1409: * @since 1.8
jjg@1409: */
jjg@1409: public class DocTreeScanner implements DocTreeVisitor {
jjg@1409:
jjg@1409: /**
jjg@1409: * Scan a single node.
jjg@1409: */
jjg@1409: public R scan(DocTree node, P p) {
jjg@1409: return (node == null) ? null : node.accept(this, p);
jjg@1409: }
jjg@1409:
jjg@1409: private R scanAndReduce(DocTree node, P p, R r) {
jjg@1409: return reduce(scan(node, p), r);
jjg@1409: }
jjg@1409:
jjg@1409: /**
jjg@1409: * Scan a list of nodes.
jjg@1409: */
jjg@1409: public R scan(Iterable extends DocTree> nodes, P p) {
jjg@1409: R r = null;
jjg@1409: if (nodes != null) {
jjg@1409: boolean first = true;
jjg@1409: for (DocTree node : nodes) {
jjg@1409: r = (first ? scan(node, p) : scanAndReduce(node, p, r));
jjg@1409: first = false;
jjg@1409: }
jjg@1409: }
jjg@1409: return r;
jjg@1409: }
jjg@1409:
jjg@1409: private R scanAndReduce(Iterable extends DocTree> nodes, P p, R r) {
jjg@1409: return reduce(scan(nodes, p), r);
jjg@1409: }
jjg@1409:
jjg@1409: /**
jjg@1409: * Reduces two results into a combined result.
jjg@1409: * The default implementation is to return the first parameter.
jjg@1409: * The general contract of the method is that it may take any action whatsoever.
jjg@1409: */
jjg@1409: public R reduce(R r1, R r2) {
jjg@1409: return r1;
jjg@1409: }
jjg@1409:
jjg@1409:
jjg@1409: /* ***************************************************************************
jjg@1409: * Visitor methods
jjg@1409: ****************************************************************************/
jjg@1409:
jjg@1409: @Override
jjg@1409: public R visitAttribute(AttributeTree node, P p) {
jjg@1409: return null;
jjg@1409: }
jjg@1409:
jjg@1409: @Override
jjg@1409: public R visitAuthor(AuthorTree node, P p) {
jjg@1409: return scan(node.getName(), p);
jjg@1409: }
jjg@1409:
jjg@1409: @Override
jjg@1409: public R visitComment(CommentTree node, P p) {
jjg@1409: return null;
jjg@1409: }
jjg@1409:
jjg@1409: @Override
jjg@1409: public R visitDeprecated(DeprecatedTree node, P p) {
jjg@1409: return scan(node.getBody(), p);
jjg@1409: }
jjg@1409:
jjg@1409: @Override
jjg@1409: public R visitDocComment(DocCommentTree node, P p) {
jjg@1409: R r = scan(node.getFirstSentence(), p);
jjg@1409: r = scanAndReduce(node.getBody(), p, r);
jjg@1409: r = scanAndReduce(node.getBlockTags(), p, r);
jjg@1409: return r;
jjg@1409: }
jjg@1409:
jjg@1409: @Override
jjg@1409: public R visitDocRoot(DocRootTree node, P p) {
jjg@1409: return null;
jjg@1409: }
jjg@1409:
jjg@1409: @Override
jjg@1409: public R visitEndElement(EndElementTree node, P p) {
jjg@1409: return null;
jjg@1409: }
jjg@1409:
jjg@1409: @Override
jjg@1409: public R visitEntity(EntityTree node, P p) {
jjg@1409: return null;
jjg@1409: }
jjg@1409:
jjg@1409: @Override
jjg@1409: public R visitErroneous(ErroneousTree node, P p) {
jjg@1409: return null;
jjg@1409: }
jjg@1409:
jjg@1409: @Override
jjg@1409: public R visitIdentifier(IdentifierTree node, P p) {
jjg@1409: return null;
jjg@1409: }
jjg@1409:
jjg@1409: @Override
jjg@1409: public R visitInheritDoc(InheritDocTree node, P p) {
jjg@1409: return null;
jjg@1409: }
jjg@1409:
jjg@1409: @Override
jjg@1409: public R visitLink(LinkTree node, P p) {
jjg@1409: R r = scan(node.getReference(), p);
jjg@1409: r = scanAndReduce(node.getLabel(), p, r);
jjg@1409: return r;
jjg@1409: }
jjg@1409:
jjg@1409: @Override
jjg@1409: public R visitLiteral(LiteralTree node, P p) {
jjg@1409: return null;
jjg@1409: }
jjg@1409:
jjg@1409: @Override
jjg@1409: public R visitParam(ParamTree node, P p) {
jjg@1409: R r = scan(node.getName(), p);
jjg@1409: r = scanAndReduce(node.getDescription(), p, r);
jjg@1409: return r;
jjg@1409: }
jjg@1409:
jjg@1409: @Override
jjg@1409: public R visitReference(ReferenceTree node, P p) {
jjg@1409: return null;
jjg@1409: }
jjg@1409:
jjg@1409: @Override
jjg@1409: public R visitReturn(ReturnTree node, P p) {
jjg@1409: return scan(node.getDescription(), p);
jjg@1409: }
jjg@1409:
jjg@1409: @Override
jjg@1409: public R visitSee(SeeTree node, P p) {
jjg@1409: return scan(node.getReference(), p);
jjg@1409: }
jjg@1409:
jjg@1409: @Override
jjg@1409: public R visitSerial(SerialTree node, P p) {
jjg@1409: return scan(node.getDescription(), p);
jjg@1409: }
jjg@1409:
jjg@1409: @Override
jjg@1409: public R visitSerialData(SerialDataTree node, P p) {
jjg@1409: return scan(node.getDescription(), p);
jjg@1409: }
jjg@1409:
jjg@1409: @Override
jjg@1409: public R visitSerialField(SerialFieldTree node, P p) {
jjg@1409: R r = scan(node.getName(), p);
jjg@1409: r = scanAndReduce(node.getType(), p, r);
jjg@1409: r = scanAndReduce(node.getDescription(), p, r);
jjg@1409: return r;
jjg@1409: }
jjg@1409:
jjg@1409: @Override
jjg@1409: public R visitSince(SinceTree node, P p) {
jjg@1409: return scan(node.getBody(), p);
jjg@1409: }
jjg@1409:
jjg@1409: @Override
jjg@1409: public R visitStartElement(StartElementTree node, P p) {
jjg@1409: return scan(node.getAttributes(), p);
jjg@1409: }
jjg@1409:
jjg@1409: @Override
jjg@1409: public R visitText(TextTree node, P p) {
jjg@1409: return null;
jjg@1409: }
jjg@1409:
jjg@1409: @Override
jjg@1409: public R visitThrows(ThrowsTree node, P p) {
jjg@1409: R r = scan(node.getExceptionName(), p);
jjg@1409: r = scanAndReduce(node.getDescription(), p, r);
jjg@1409: return r;
jjg@1409: }
jjg@1409:
jjg@1409: @Override
jjg@1409: public R visitUnknownBlockTag(UnknownBlockTagTree node, P p) {
jjg@1409: return scan(node.getContent(), p);
jjg@1409: }
jjg@1409:
jjg@1409: @Override
jjg@1409: public R visitUnknownInlineTag(UnknownInlineTagTree node, P p) {
jjg@1409: return scan(node.getContent(), p);
jjg@1409: }
jjg@1409:
jjg@1409: @Override
jjg@1409: public R visitValue(ValueTree node, P p) {
jjg@1409: return scan(node.getReference(), p);
jjg@1409: }
jjg@1409:
jjg@1409: @Override
jjg@1409: public R visitVersion(VersionTree node, P p) {
jjg@1409: return scan(node.getBody(), p);
jjg@1409: }
jjg@1409:
jjg@1409: @Override
jjg@1409: public R visitOther(DocTree node, P p) {
jjg@1409: return null;
jjg@1409: }
jjg@1409:
jjg@1409: }