DocImpl.java 11.0 KB
Newer Older
D
duke 已提交
1
/*
2
 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
D
duke 已提交
3 4 5 6
 * 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
7
 * published by the Free Software Foundation.  Oracle designates this
D
duke 已提交
8
 * particular file as subject to the "Classpath" exception as provided
9
 * by Oracle in the LICENSE file that accompanied this code.
D
duke 已提交
10 11 12 13 14 15 16 17 18 19 20
 *
 * 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.
 *
21 22 23
 * 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.
D
duke 已提交
24 25 26 27
 */

package com.sun.tools.javadoc;

28
import java.io.DataInputStream;
D
duke 已提交
29
import java.io.IOException;
J
jjg 已提交
30
import java.io.InputStream;
D
duke 已提交
31
import java.text.CollationKey;
J
jjg 已提交
32 33 34
import java.util.regex.Matcher;
import java.util.regex.Pattern;

35 36 37
import javax.tools.FileObject;

import com.sun.javadoc.*;
D
duke 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50
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.
 *
 * @since 1.2
 * @author Robert Field
 * @author Atul M Dambalkar
 * @author Neal Gafter (rewrite)
 */
51
public abstract class DocImpl implements Doc, Comparable<Object> {
D
duke 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94

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

    /**
     *  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, String documentation) {
        this.documentation = documentation;
        this.env = env;
    }

    /**
     * So subclasses have the option to do lazy initialization of
     * "documentation" string.
     */
95
    protected String documentation() {
D
duke 已提交
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
        if (documentation == null) documentation = "";
        return documentation;
    }

    /**
     * For lazy initialization of comment.
     */
    Comment comment() {
        if (comment == null) {
            comment = new Comment(this, documentation());
        }
        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.
     *
141
     * @return an array of SeeTag containing all &#64;see tags.
D
duke 已提交
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
     */
    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.
     */
171
    String readHTMLDocumentation(InputStream input, FileObject filename) throws IOException {
172 173 174 175 176 177 178
        byte[] filecontents = new byte[input.available()];
        try {
            DataInputStream dataIn = new DataInputStream(input);
            dataIn.readFully(filecontents);
        } finally {
            input.close();
        }
D
duke 已提交
179 180 181 182
        String encoding = env.getEncoding();
        String rawDoc = (encoding!=null)
            ? new String(filecontents, encoding)
            : new String(filecontents);
183 184 185 186 187 188 189 190 191
        Pattern bodyPat = Pattern.compile("(?is).*<body\\b[^>]*>(.*)</body\\b.*");
        Matcher m = bodyPat.matcher(rawDoc);
        if (m.matches()) {
            return m.group(1);
        } else {
            String key = rawDoc.matches("(?is).*<body\\b.*")
                    ? "javadoc.End_body_missing_from_html_file"
                    : "javadoc.Body_missing_from_html_file";
            env.error(SourcePositionImpl.make(filename, Position.NOPOS, null), key);
D
duke 已提交
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
            return "";
        }
    }

    /**
     * Return the full unprocessed text of the comment.  Tags
     * are included as text.  Used mainly for store and retrieve
     * operations like internalization.
     */
    public String getRawCommentText() {
        return documentation();
    }

    /**
     * Set the full unprocessed text of the comment.  Tags
     * are included as text.  Used mainly for store and retrieve
     * operations like internalization.
     */
    public void setRawCommentText(String rawDocumentation) {
        documentation = rawDocumentation;
        comment = null;
    }

    /**
     * return a key for sorting.
     */
    CollationKey key() {
        if (collationkey == null) {
            collationkey = generateKey();
        }
        return collationkey;
    }

    /**
     * Generate a key for sorting.
     * <p>
     * Default is name().
     */
    CollationKey generateKey() {
        String k = name();
        // System.out.println("COLLATION KEY FOR " + this + " is \"" + k + "\"");
        return env.doclocale.collator.getCollationKey(k);
    }

    /**
     * Returns a string representation of this Doc item.
     */
239
    @Override
D
duke 已提交
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
    public String toString() {
        return qualifiedName();
    }

    /**
     * Returns the name of this Doc item.
     *
     * @return  the name
     */
    public abstract String name();

    /**
     * Returns the qualified name of this Doc item.
     *
     * @return  the name
     */
    public abstract String qualifiedName();

    /**
     * Compares this Object with the specified Object for order.  Returns a
     * negative integer, zero, or a positive integer as this Object is less
     * than, equal to, or greater than the given Object.
     * <p>
     * Included so that Doc item are java.lang.Comparable.
     *
     * @param   o the <code>Object</code> to be compared.
     * @return  a negative integer, zero, or a positive integer as this Object
     *          is less than, equal to, or greater than the given Object.
     * @exception ClassCastException the specified Object's type prevents it
     *            from being compared to this Object.
     */
    public int compareTo(Object obj) {
        // System.out.println("COMPARE \"" + this + "\" to \"" + obj + "\" = " + key().compareTo(((DocImpl)obj).key()));
        return key().compareTo(((DocImpl)obj).key());
    }

    /**
     * Is this Doc item a field?  False until overridden.
     *
     * @return true if it represents a field
     */
    public boolean isField() {
        return false;
    }

    /**
     * Is this Doc item an enum constant?  False until overridden.
     *
     * @return true if it represents an enum constant
     */
    public boolean isEnumConstant() {
        return false;
    }

    /**
     * Is this Doc item a constructor?  False until overridden.
     *
     * @return true if it represents a constructor
     */
    public boolean isConstructor() {
        return false;
    }

    /**
     * Is this Doc item a method (but not a constructor or annotation
     * type element)?
     * False until overridden.
     *
     * @return true if it represents a method
     */
    public boolean isMethod() {
        return false;
    }

    /**
     * Is this Doc item an annotation type element?
     * False until overridden.
     *
     * @return true if it represents an annotation type element
     */
    public boolean isAnnotationTypeElement() {
        return false;
    }

    /**
     * Is this Doc item a interface (but not an annotation type)?
     * False until overridden.
     *
     * @return true if it represents a interface
     */
    public boolean isInterface() {
        return false;
    }

    /**
     * Is this Doc item a exception class?  False until overridden.
     *
     * @return true if it represents a exception
     */
    public boolean isException() {
        return false;
    }

    /**
     * Is this Doc item a error class?  False until overridden.
     *
     * @return true if it represents a error
     */
    public boolean isError() {
        return false;
    }

    /**
     * Is this Doc item an enum type?  False until overridden.
     *
     * @return true if it represents an enum type
     */
    public boolean isEnum() {
        return false;
    }

    /**
     * Is this Doc item an annotation type?  False until overridden.
     *
     * @return true if it represents an annotation type
     */
    public boolean isAnnotationType() {
        return false;
    }

    /**
     * Is this Doc item an ordinary class (i.e. not an interface,
     * annotation type, enumeration, exception, or error)?
     * False until overridden.
     *
     * @return true if it represents an ordinary class
     */
    public boolean isOrdinaryClass() {
        return false;
    }

    /**
     * Is this Doc item a class
     * (and not an interface or annotation type)?
     * This includes ordinary classes, enums, errors and exceptions.
     * False until overridden.
     *
     * @return true if it represents a class
     */
    public boolean isClass() {
        return false;
    }

    /**
     * return true if this Doc is include in the active set.
     */
    public abstract boolean isIncluded();

    /**
     * Return the source position of the entity, or null if
     * no position is available.
     */
    public SourcePosition position() { return null; }
}