DocTreeMaker.java revision 3692:87b48a8fb3cf
1/*
2 * Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package com.sun.tools.javac.tree;
27
28import java.text.BreakIterator;
29import java.util.ArrayList;
30import java.util.Collection;
31import java.util.EnumSet;
32import java.util.List;
33import java.util.ListIterator;
34
35import javax.lang.model.element.Name;
36import javax.tools.Diagnostic;
37import javax.tools.JavaFileObject;
38
39import com.sun.source.doctree.AttributeTree.ValueKind;
40import com.sun.source.doctree.DocTree;
41import com.sun.source.doctree.DocTree.Kind;
42import com.sun.source.doctree.EndElementTree;
43import com.sun.source.doctree.IdentifierTree;
44import com.sun.source.doctree.ReferenceTree;
45import com.sun.source.doctree.StartElementTree;
46import com.sun.source.doctree.TextTree;
47import com.sun.source.util.DocTreeFactory;
48import com.sun.tools.doclint.HtmlTag;
49import com.sun.tools.javac.api.JavacTrees;
50import com.sun.tools.javac.parser.ParserFactory;
51import com.sun.tools.javac.parser.ReferenceParser;
52import com.sun.tools.javac.parser.Tokens.Comment;
53import com.sun.tools.javac.parser.Tokens.Comment.CommentStyle;
54import com.sun.tools.javac.tree.DCTree.DCAttribute;
55import com.sun.tools.javac.tree.DCTree.DCAuthor;
56import com.sun.tools.javac.tree.DCTree.DCComment;
57import com.sun.tools.javac.tree.DCTree.DCDeprecated;
58import com.sun.tools.javac.tree.DCTree.DCDocComment;
59import com.sun.tools.javac.tree.DCTree.DCDocRoot;
60import com.sun.tools.javac.tree.DCTree.DCEndElement;
61import com.sun.tools.javac.tree.DCTree.DCEntity;
62import com.sun.tools.javac.tree.DCTree.DCErroneous;
63import com.sun.tools.javac.tree.DCTree.DCHidden;
64import com.sun.tools.javac.tree.DCTree.DCIdentifier;
65import com.sun.tools.javac.tree.DCTree.DCIndex;
66import com.sun.tools.javac.tree.DCTree.DCInheritDoc;
67import com.sun.tools.javac.tree.DCTree.DCLink;
68import com.sun.tools.javac.tree.DCTree.DCLiteral;
69import com.sun.tools.javac.tree.DCTree.DCParam;
70import com.sun.tools.javac.tree.DCTree.DCReference;
71import com.sun.tools.javac.tree.DCTree.DCReturn;
72import com.sun.tools.javac.tree.DCTree.DCSee;
73import com.sun.tools.javac.tree.DCTree.DCSerial;
74import com.sun.tools.javac.tree.DCTree.DCSerialData;
75import com.sun.tools.javac.tree.DCTree.DCSerialField;
76import com.sun.tools.javac.tree.DCTree.DCSince;
77import com.sun.tools.javac.tree.DCTree.DCStartElement;
78import com.sun.tools.javac.tree.DCTree.DCText;
79import com.sun.tools.javac.tree.DCTree.DCThrows;
80import com.sun.tools.javac.tree.DCTree.DCUnknownBlockTag;
81import com.sun.tools.javac.tree.DCTree.DCUnknownInlineTag;
82import com.sun.tools.javac.tree.DCTree.DCValue;
83import com.sun.tools.javac.tree.DCTree.DCVersion;
84import com.sun.tools.javac.util.Context;
85import com.sun.tools.javac.util.DefinedBy;
86import com.sun.tools.javac.util.DefinedBy.Api;
87import com.sun.tools.javac.util.DiagnosticSource;
88import com.sun.tools.javac.util.JCDiagnostic;
89import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
90import com.sun.tools.javac.util.ListBuffer;
91import com.sun.tools.javac.util.Pair;
92import com.sun.tools.javac.util.Position;
93
94import static com.sun.tools.doclint.HtmlTag.*;
95
96/**
97 *
98 *  <p><b>This is NOT part of any supported API.
99 *  If you write code that depends on this, you do so at your own risk.
100 *  This code and its internal interfaces are subject to change or
101 *  deletion without notice.</b>
102 */
103public class DocTreeMaker implements DocTreeFactory {
104
105    /** The context key for the tree factory. */
106    protected static final Context.Key<DocTreeMaker> treeMakerKey = new Context.Key<>();
107
108    // A subset of block tags, which acts as sentence breakers, appearing
109    // anywhere but the zero'th position in the first sentence.
110    final EnumSet<HtmlTag> sentenceBreakTags;
111
112    /** Get the TreeMaker instance. */
113    public static DocTreeMaker instance(Context context) {
114        DocTreeMaker instance = context.get(treeMakerKey);
115        if (instance == null)
116            instance = new DocTreeMaker(context);
117        return instance;
118    }
119
120    /** The position at which subsequent trees will be created.
121     */
122    public int pos = Position.NOPOS;
123
124    /** Access to diag factory for ErroneousTrees. */
125    private final JCDiagnostic.Factory diags;
126
127    private final JavacTrees trees;
128
129    /** Utility class to parse reference signatures. */
130    private final ReferenceParser referenceParser;
131
132    /** Create a tree maker with NOPOS as initial position.
133     */
134    protected DocTreeMaker(Context context) {
135        context.put(treeMakerKey, this);
136        diags = JCDiagnostic.Factory.instance(context);
137        this.pos = Position.NOPOS;
138        trees = JavacTrees.instance(context);
139        referenceParser = new ReferenceParser(ParserFactory.instance(context));
140        sentenceBreakTags = EnumSet.of(H1, H2, H3, H4, H5, H6, PRE, P);
141    }
142
143    /** Reassign current position.
144     */
145    @Override @DefinedBy(Api.COMPILER_TREE)
146    public DocTreeMaker at(int pos) {
147        this.pos = pos;
148        return this;
149    }
150
151    /** Reassign current position.
152     */
153    public DocTreeMaker at(DiagnosticPosition pos) {
154        this.pos = (pos == null ? Position.NOPOS : pos.getStartPosition());
155        return this;
156    }
157
158    @Override @DefinedBy(Api.COMPILER_TREE)
159    public DCAttribute newAttributeTree(javax.lang.model.element.Name name, ValueKind vkind, java.util.List<? extends DocTree> value) {
160        DCAttribute tree = new DCAttribute(name, vkind, cast(value));
161        tree.pos = pos;
162        return tree;
163    }
164
165    @Override @DefinedBy(Api.COMPILER_TREE)
166    public DCAuthor newAuthorTree(java.util.List<? extends DocTree> name) {
167        DCAuthor tree = new DCAuthor(cast(name));
168        tree.pos = pos;
169        return tree;
170    }
171
172    @Override @DefinedBy(Api.COMPILER_TREE)
173    public DCLiteral newCodeTree(TextTree text) {
174        DCLiteral tree = new DCLiteral(Kind.CODE, (DCText) text);
175        tree.pos = pos;
176        return tree;
177    }
178
179    @Override @DefinedBy(Api.COMPILER_TREE)
180    public DCComment newCommentTree(String text) {
181        DCComment tree = new DCComment(text);
182        tree.pos = pos;
183        return tree;
184    }
185
186    @Override @DefinedBy(Api.COMPILER_TREE)
187    public DCDeprecated newDeprecatedTree(List<? extends DocTree> text) {
188        DCDeprecated tree = new DCDeprecated(cast(text));
189        tree.pos = pos;
190        return tree;
191    }
192
193    public DCDocComment newDocCommentTree(Comment comment, List<? extends DocTree> fullBody, List<? extends DocTree> tags) {
194        Pair<List<DCTree>, List<DCTree>> pair = splitBody(fullBody);
195        DCDocComment tree = new DCDocComment(comment, cast(fullBody), pair.fst, pair.snd, cast(tags));
196        tree.pos = pos;
197        return tree;
198    }
199
200    /*
201     * Primarily to produce a DocCommenTree when given a
202     * first sentence and a body, this is useful, in cases
203     * where the trees are being synthesized by a tool.
204     */
205    @Override @DefinedBy(Api.COMPILER_TREE)
206    public DCDocComment newDocCommentTree(List<? extends DocTree> fullBody, List<? extends DocTree> tags) {
207        ListBuffer<DCTree> lb = new ListBuffer<>();
208        lb.addAll(cast(fullBody));
209        List<DCTree> fBody = lb.toList();
210
211        // A dummy comment to keep the diagnostics logic happy.
212        Comment c = new Comment() {
213            @Override
214            public String getText() {
215                return null;
216            }
217
218            @Override
219            public int getSourcePos(int index) {
220                return Position.NOPOS;
221            }
222
223            @Override
224            public CommentStyle getStyle() {
225                return CommentStyle.JAVADOC;
226            }
227
228            @Override
229            public boolean isDeprecated() {
230                return false;
231            }
232        };
233        Pair<List<DCTree>, List<DCTree>> pair = splitBody(fullBody);
234        DCDocComment tree = new DCDocComment(c, fBody, pair.fst, pair.snd, cast(tags));
235        return tree;
236    }
237
238    @Override @DefinedBy(Api.COMPILER_TREE)
239    public DCDocRoot newDocRootTree() {
240        DCDocRoot tree = new DCDocRoot();
241        tree.pos = pos;
242        return tree;
243    }
244
245    @Override @DefinedBy(Api.COMPILER_TREE)
246    public DCEndElement newEndElementTree(Name name) {
247        DCEndElement tree = new DCEndElement(name);
248        tree.pos = pos;
249        return tree;
250    }
251
252    @Override @DefinedBy(Api.COMPILER_TREE)
253    public DCEntity newEntityTree(Name name) {
254        DCEntity tree = new DCEntity(name);
255        tree.pos = pos;
256        return tree;
257    }
258
259    @Override @DefinedBy(Api.COMPILER_TREE)
260    public DCErroneous newErroneousTree(String text, Diagnostic<JavaFileObject> diag) {
261        DCErroneous tree = new DCErroneous(text, (JCDiagnostic) diag);
262        tree.pos = pos;
263        return tree;
264    }
265
266    public DCErroneous newErroneousTree(String text, DiagnosticSource diagSource, String code, Object... args) {
267        DCErroneous tree = new DCErroneous(text, diags, diagSource, code, args);
268        tree.pos = pos;
269        return tree;
270    }
271
272    @Override @DefinedBy(Api.COMPILER_TREE)
273    public DCThrows newExceptionTree(ReferenceTree name, List<? extends DocTree> description) {
274        // TODO: verify the reference is just to a type (not a field or method)
275        DCThrows tree = new DCThrows(Kind.EXCEPTION, (DCReference) name, cast(description));
276        tree.pos = pos;
277        return tree;
278    }
279
280    @Override @DefinedBy(Api.COMPILER_TREE)
281    public DCHidden newHiddenTree(List<? extends DocTree> text) {
282        DCHidden tree = new DCHidden(cast(text));
283        tree.pos = pos;
284        return tree;
285    }
286
287    @Override @DefinedBy(Api.COMPILER_TREE)
288    public DCIdentifier newIdentifierTree(Name name) {
289        DCIdentifier tree = new DCIdentifier(name);
290        tree.pos = pos;
291        return tree;
292    }
293
294    @Override @DefinedBy(Api.COMPILER_TREE)
295    public DCIndex newIndexTree(DocTree term, List<? extends DocTree> description) {
296        DCIndex tree = new DCIndex((DCTree) term, cast(description));
297        tree.pos = pos;
298        return tree;
299    }
300
301    @Override @DefinedBy(Api.COMPILER_TREE)
302    public DCInheritDoc newInheritDocTree() {
303        DCInheritDoc tree = new DCInheritDoc();
304        tree.pos = pos;
305        return tree;
306    }
307
308    @Override @DefinedBy(Api.COMPILER_TREE)
309    public DCLink newLinkTree(ReferenceTree ref, List<? extends DocTree> label) {
310        DCLink tree = new DCLink(Kind.LINK, (DCReference) ref, cast(label));
311        tree.pos = pos;
312        return tree;
313    }
314
315    @Override @DefinedBy(Api.COMPILER_TREE)
316    public DCLink newLinkPlainTree(ReferenceTree ref, List<? extends DocTree> label) {
317        DCLink tree = new DCLink(Kind.LINK_PLAIN, (DCReference) ref, cast(label));
318        tree.pos = pos;
319        return tree;
320    }
321
322    @Override @DefinedBy(Api.COMPILER_TREE)
323    public DCLiteral newLiteralTree(TextTree text) {
324        DCLiteral tree = new DCLiteral(Kind.LITERAL, (DCText) text);
325        tree.pos = pos;
326        return tree;
327    }
328
329    @Override @DefinedBy(Api.COMPILER_TREE)
330    public DCParam newParamTree(boolean isTypeParameter, IdentifierTree name, List<? extends DocTree> description) {
331        DCParam tree = new DCParam(isTypeParameter, (DCIdentifier) name, cast(description));
332        tree.pos = pos;
333        return tree;
334    }
335
336    @Override @DefinedBy(Api.COMPILER_TREE)
337    public DCReference newReferenceTree(String signature) {
338        try {
339            ReferenceParser.Reference ref = referenceParser.parse(signature);
340            DCReference tree = new DCReference(signature, ref.qualExpr, ref.member, ref.paramTypes);
341            tree.pos = pos;
342            return tree;
343        } catch (ReferenceParser.ParseException e) {
344            throw new IllegalArgumentException("invalid signature", e);
345        }
346    }
347
348    public DCReference newReferenceTree(String signature, JCTree qualExpr, Name member, List<JCTree> paramTypes) {
349        DCReference tree = new DCReference(signature, qualExpr, member, paramTypes);
350        tree.pos = pos;
351        return tree;
352    }
353
354    @Override @DefinedBy(Api.COMPILER_TREE)
355    public DCReturn newReturnTree(List<? extends DocTree> description) {
356        DCReturn tree = new DCReturn(cast(description));
357        tree.pos = pos;
358        return tree;
359    }
360
361    @Override @DefinedBy(Api.COMPILER_TREE)
362    public DCSee newSeeTree(List<? extends DocTree> reference) {
363        DCSee tree = new DCSee(cast(reference));
364        tree.pos = pos;
365        return tree;
366    }
367
368    @Override @DefinedBy(Api.COMPILER_TREE)
369    public DCSerial newSerialTree(List<? extends DocTree> description) {
370        DCSerial tree = new DCSerial(cast(description));
371        tree.pos = pos;
372        return tree;
373    }
374
375    @Override @DefinedBy(Api.COMPILER_TREE)
376    public DCSerialData newSerialDataTree(List<? extends DocTree> description) {
377        DCSerialData tree = new DCSerialData(cast(description));
378        tree.pos = pos;
379        return tree;
380    }
381
382    @Override @DefinedBy(Api.COMPILER_TREE)
383    public DCSerialField newSerialFieldTree(IdentifierTree name, ReferenceTree type, List<? extends DocTree> description) {
384        DCSerialField tree = new DCSerialField((DCIdentifier) name, (DCReference) type, cast(description));
385        tree.pos = pos;
386        return tree;
387    }
388
389    @Override @DefinedBy(Api.COMPILER_TREE)
390    public DCSince newSinceTree(List<? extends DocTree> text) {
391        DCSince tree = new DCSince(cast(text));
392        tree.pos = pos;
393        return tree;
394    }
395
396    @Override @DefinedBy(Api.COMPILER_TREE)
397    public DCStartElement newStartElementTree(Name name, List<? extends DocTree> attrs, boolean selfClosing) {
398        DCStartElement tree = new DCStartElement(name, cast(attrs), selfClosing);
399        tree.pos = pos;
400        return tree;
401    }
402
403    @Override @DefinedBy(Api.COMPILER_TREE)
404    public DCText newTextTree(String text) {
405        DCText tree = new DCText(text);
406        tree.pos = pos;
407        return tree;
408    }
409
410    @Override @DefinedBy(Api.COMPILER_TREE)
411    public DCThrows newThrowsTree(ReferenceTree name, List<? extends DocTree> description) {
412        // TODO: verify the reference is just to a type (not a field or method)
413        DCThrows tree = new DCThrows(Kind.THROWS, (DCReference) name, cast(description));
414        tree.pos = pos;
415        return tree;
416    }
417
418    @Override @DefinedBy(Api.COMPILER_TREE)
419    public DCUnknownBlockTag newUnknownBlockTagTree(Name name, List<? extends DocTree> content) {
420        DCUnknownBlockTag tree = new DCUnknownBlockTag(name, cast(content));
421        tree.pos = pos;
422        return tree;
423    }
424
425    @Override @DefinedBy(Api.COMPILER_TREE)
426    public DCUnknownInlineTag newUnknownInlineTagTree(Name name, List<? extends DocTree> content) {
427        DCUnknownInlineTag tree = new DCUnknownInlineTag(name, cast(content));
428        tree.pos = pos;
429        return tree;
430    }
431
432    @Override @DefinedBy(Api.COMPILER_TREE)
433    public DCValue newValueTree(ReferenceTree ref) {
434        // TODO: verify the reference is to a constant value
435        DCValue tree = new DCValue((DCReference) ref);
436        tree.pos = pos;
437        return tree;
438    }
439
440    @Override @DefinedBy(Api.COMPILER_TREE)
441    public DCVersion newVersionTree(List<? extends DocTree> text) {
442        DCVersion tree = new DCVersion(cast(text));
443        tree.pos = pos;
444        return tree;
445    }
446
447    @Override @DefinedBy(Api.COMPILER_TREE)
448    public java.util.List<DocTree> getFirstSentence(java.util.List<? extends DocTree> list) {
449        Pair<List<DCTree>, List<DCTree>> pair = splitBody(list);
450        return new ArrayList<>(pair.fst);
451    }
452
453    /*
454     * Breaks up the body tags into the first sentence and its successors.
455     * The first sentence is determined with the presence of a period,
456     * block tag, or a sentence break, as returned by the BreakIterator.
457     * Trailing whitespaces are trimmed.
458     */
459    private Pair<List<DCTree>, List<DCTree>> splitBody(Collection<? extends DocTree> list) {
460        // pos is modified as we create trees, therefore
461        // we save the pos and restore it later.
462        final int savedpos = this.pos;
463        try {
464            ListBuffer<DCTree> body = new ListBuffer<>();
465            // split body into first sentence and body
466            ListBuffer<DCTree> fs = new ListBuffer<>();
467            if (list.isEmpty()) {
468                return new Pair<>(fs.toList(), body.toList());
469            }
470            boolean foundFirstSentence = false;
471            ArrayList<DocTree> alist = new ArrayList<>(list);
472            ListIterator<DocTree> itr = alist.listIterator();
473            while (itr.hasNext()) {
474                boolean isFirst = !itr.hasPrevious();
475                DocTree dt = itr.next();
476                int spos = ((DCTree) dt).pos;
477                if (foundFirstSentence) {
478                    body.add((DCTree) dt);
479                    continue;
480                }
481                switch (dt.getKind()) {
482                    case TEXT:
483                        DCText tt = (DCText) dt;
484                        String s = tt.getBody();
485                        DocTree peekedNext = itr.hasNext()
486                                ? alist.get(itr.nextIndex())
487                                : null;
488                        int sbreak = getSentenceBreak(s, peekedNext);
489                        if (sbreak > 0) {
490                            s = removeTrailingWhitespace(s.substring(0, sbreak));
491                            DCText text = this.at(spos).newTextTree(s);
492                            fs.add(text);
493                            foundFirstSentence = true;
494                            int nwPos = skipWhiteSpace(tt.getBody(), sbreak);
495                            if (nwPos > 0) {
496                                DCText text2 = this.at(spos + nwPos).newTextTree(tt.getBody().substring(nwPos));
497                                body.add(text2);
498                            }
499                            continue;
500                        } else if (itr.hasNext()) {
501                            // if the next doctree is a break, remove trailing spaces
502                            peekedNext = alist.get(itr.nextIndex());
503                            boolean sbrk = isSentenceBreak(peekedNext, false);
504                            if (sbrk) {
505                                DocTree next = itr.next();
506                                s = removeTrailingWhitespace(s);
507                                DCText text = this.at(spos).newTextTree(s);
508                                fs.add(text);
509                                body.add((DCTree) next);
510                                foundFirstSentence = true;
511                                continue;
512                            }
513                        }
514                        break;
515                    default:
516                        if (isSentenceBreak(dt, isFirst)) {
517                            body.add((DCTree) dt);
518                            foundFirstSentence = true;
519                            continue;
520                        }
521                        break;
522                }
523                fs.add((DCTree) dt);
524            }
525            return new Pair<>(fs.toList(), body.toList());
526        } finally {
527            this.pos = savedpos;
528        }
529    }
530
531    private boolean isTextTree(DocTree tree) {
532        return tree.getKind() == Kind.TEXT;
533    }
534
535    /*
536     * Computes the first sentence break, a simple dot-space algorithm.
537     */
538    private int defaultSentenceBreak(String s) {
539        // scan for period followed by whitespace
540        int period = -1;
541        for (int i = 0; i < s.length(); i++) {
542            switch (s.charAt(i)) {
543                case '.':
544                    period = i;
545                    break;
546
547                case ' ':
548                case '\f':
549                case '\n':
550                case '\r':
551                case '\t':
552                    if (period >= 0) {
553                        return i;
554                    }
555                    break;
556
557                default:
558                    period = -1;
559                    break;
560            }
561        }
562        return -1;
563    }
564
565    /*
566     * Computes the first sentence, if using a default breaker,
567     * the break is returned, if not then a -1, indicating that
568     * more doctree elements are required to be examined.
569     *
570     * BreakIterator.next points to the the start of the following sentence,
571     * and does not provide an easy way to disambiguate between "sentence break",
572     * "possible sentence break" and "not a sentence break" at the end of the input.
573     * For example, BreakIterator.next returns the index for the end
574     * of the string for all of these examples,
575     * using vertical bars to delimit the bounds of the example text
576     * |Abc|        (not a valid end of sentence break, if followed by more text)
577     * |Abc.|       (maybe a valid end of sentence break, depending on the following text)
578     * |Abc. |      (maybe a valid end of sentence break, depending on the following text)
579     * |"Abc." |    (maybe a valid end of sentence break, depending on the following text)
580     * |Abc.  |     (definitely a valid end of sentence break)
581     * |"Abc."  |   (definitely a valid end of sentence break)
582     * Therefore, we have to probe further to determine whether
583     * there really is a sentence break or not at the end of this run of text.
584     */
585    private int getSentenceBreak(String s, DocTree dt) {
586        BreakIterator breakIterator = trees.getBreakIterator();
587        if (breakIterator == null) {
588            return defaultSentenceBreak(s);
589        }
590        breakIterator.setText(s);
591        final int sbrk = breakIterator.next();
592        // This is the last doctree, found the droid we are looking for
593        if (dt == null) {
594            return sbrk;
595        }
596
597        // If the break is well within the span of the string ie. not
598        // at EOL, then we have a clear break.
599        if (sbrk < s.length() - 1) {
600            return sbrk;
601        }
602
603        if (isTextTree(dt)) {
604            // Two adjacent text trees, a corner case, perhaps
605            // produced by a tool synthesizing a doctree. In
606            // this case, does the break lie within the first span,
607            // then we have the droid, otherwise allow the callers
608            // logic to handle the break in the adjacent doctree.
609            TextTree ttnext = (TextTree) dt;
610            String combined = s + ttnext.getBody();
611            breakIterator.setText(combined);
612            int sbrk2 = breakIterator.next();
613            if (sbrk < sbrk2) {
614                return sbrk;
615            }
616        }
617
618        // Is the adjacent tree a sentence breaker ?
619        if (isSentenceBreak(dt, false)) {
620            return sbrk;
621        }
622
623        // At this point the adjacent tree is either a javadoc tag ({@..),
624        // html tag (<..) or an entity (&..). Perform a litmus test, by
625        // concatenating a sentence, to validate the break earlier identified.
626        String combined = s + "Dummy Sentence.";
627        breakIterator.setText(combined);
628        int sbrk2 = breakIterator.next();
629        if (sbrk2 <= sbrk) {
630            return sbrk2;
631        }
632        return -1; // indeterminate at this time
633    }
634
635    private boolean isSentenceBreak(javax.lang.model.element.Name tagName) {
636        return sentenceBreakTags.contains(get(tagName));
637    }
638
639    private boolean isSentenceBreak(DocTree dt, boolean isFirstDocTree) {
640        switch (dt.getKind()) {
641            case START_ELEMENT:
642                    StartElementTree set = (StartElementTree)dt;
643                    return !isFirstDocTree && ((DCTree) dt).pos > 1 && isSentenceBreak(set.getName());
644            case END_ELEMENT:
645                    EndElementTree eet = (EndElementTree)dt;
646                    return !isFirstDocTree && ((DCTree) dt).pos > 1 && isSentenceBreak(eet.getName());
647            default:
648                return false;
649        }
650    }
651
652    /*
653     * Returns the position of the the first non-white space
654     */
655    private int skipWhiteSpace(String s, int start) {
656        for (int i = start; i < s.length(); i++) {
657            char c = s.charAt(i);
658            if (!Character.isWhitespace(c)) {
659                return i;
660            }
661        }
662        return -1;
663    }
664
665    private String removeTrailingWhitespace(String s) {
666        for (int i = s.length() - 1 ; i >= 0 ; i--) {
667            char ch = s.charAt(i);
668            if (!Character.isWhitespace(ch)) {
669                return s.substring(0, i + 1);
670            }
671        }
672        return s;
673    }
674
675    @SuppressWarnings("unchecked")
676    private List<DCTree> cast(List<? extends DocTree> list) {
677        return (List<DCTree>) list;
678    }
679}
680