alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Java example source code file (DocImpl.java)

This example Java source code file (DocImpl.java) is included in the alvinalexander.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Learn more about this Java project at its project page.

Java - Java tags/keywords

collationkey, comment, datainputstream, docenv, docimpl, ioexception, javadoc, jccompilationunit, jctree, override, packagedocimpl, regex, sourceposition, string, tag, text, treepath

The DocImpl.java Java example source code

/*
 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

package com.sun.tools.javadoc;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.CollationKey;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.tools.FileObject;

import com.sun.javadoc.*;
import com.sun.source.util.TreePath;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
import com.sun.tools.javac.util.Position;

/**
 * abstract base class of all Doc classes.  Doc item's are representations
 * of java language constructs (class, package, method,...) which have
 * comments and have been processed by this run of javadoc.  All Doc items
 * are unique, that is, they are == comparable.
 *
 *  <p>This is NOT part of any supported API.
 *  If you write code that depends on this, you do so at your own risk.
 *  This code and its internal interfaces are subject to change or
 *  deletion without notice.</b>
 *
 * @since 1.2
 * @author Robert Field
 * @author Atul M Dambalkar
 * @author Neal Gafter (rewrite)
 */
public abstract class DocImpl implements Doc, Comparable<Object> {

    /**
     * Doc environment
     */
    protected final DocEnv env;   //### Rename this everywhere to 'docenv' ?

    /**
     * Back pointer to the tree node for this doc item.
     * May be null if there is no associated tree.
     */
    protected TreePath treePath;

    /**
     *  The complex comment object, lazily initialized.
     */
    private Comment comment;

    /**
     * The cached sort key, to take care of Natural Language Text sorting.
     */
    private CollationKey collationkey = null;

    /**
     *  Raw documentation string.
     */
    protected String documentation;  // Accessed in PackageDocImpl, RootDocImpl

    /**
     * Cached first sentence.
     */
    private Tag[] firstSentence;

    /**
     * Cached inline tags.
     */
    private Tag[] inlineTags;

    /**
     * Constructor.
     */
    DocImpl(DocEnv env, TreePath treePath) {
        this.treePath = treePath;
        this.documentation = getCommentText(treePath);
        this.env = env;
    }

    private static String getCommentText(TreePath p) {
        if (p == null)
            return null;

        JCCompilationUnit topLevel = (JCCompilationUnit) p.getCompilationUnit();
        JCTree tree = (JCTree) p.getLeaf();
        return topLevel.docComments.getCommentText(tree);
    }

    /**
     * So subclasses have the option to do lazy initialization of
     * "documentation" string.
     */
    protected String documentation() {
        if (documentation == null) documentation = "";
        return documentation;
    }

    /**
     * For lazy initialization of comment.
     */
    Comment comment() {
        if (comment == null) {
            String d = documentation();
            if (env.doclint != null
                    && treePath != null
                    && d.equals(getCommentText(treePath))) {
                env.doclint.scan(treePath);
            }
            comment = new Comment(this, d);
        }
        return comment;
    }

    /**
     * Return the text of the comment for this doc item.
     * TagImpls have been removed.
     */
    public String commentText() {
        return comment().commentText();
    }

    /**
     * Return all tags in this Doc item.
     *
     * @return an array of TagImpl containing all tags on this Doc item.
     */
    public Tag[] tags() {
        return comment().tags();
    }

    /**
     * Return tags of the specified kind in this Doc item.
     *
     * @param tagname name of the tag kind to search for.
     * @return an array of TagImpl containing all tags whose 'kind()'
     * matches 'tagname'.
     */
    public Tag[] tags(String tagname) {
        return comment().tags(tagname);
    }

    /**
     * Return the see also tags in this Doc item.
     *
     * @return an array of SeeTag containing all @see tags.
     */
    public SeeTag[] seeTags() {
        return comment().seeTags();
    }

    public Tag[] inlineTags() {
        if (inlineTags == null) {
            inlineTags = Comment.getInlineTags(this, commentText());
        }
        return inlineTags;
    }

    public Tag[] firstSentenceTags() {
        if (firstSentence == null) {
            //Parse all sentences first to avoid duplicate warnings.
            inlineTags();
            try {
                env.setSilent(true);
                firstSentence = Comment.firstSentenceTags(this, commentText());
            } finally {
                env.setSilent(false);
            }
        }
        return firstSentence;
    }

    /**
     * Utility for subclasses which read HTML documentation files.
     */
    String readHTMLDocumentation(InputStream input, FileObject filename) throws IOException {
        byte[] filecontents = new byte[input.available()];
        try {
            DataInputStream dataIn = new DataInputStream(input);
            dataIn.readFully(filecontents);
        } finally {
            input.close();
        }
        String encoding = env.getEncoding();
        String rawDoc = (encoding!=null)
            ? new String(filecontents, encoding)
            : new String(filecontents);
        Pattern bodyPat = Pattern.compile("(?is).*<body\\b[^>]*>(.*)

Other Java examples (source code examples)

Here is a short list of links related to this Java DocImpl.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.