aboutsummaryrefslogtreecommitdiff
path: root/libjava/classpath/tools/gnu/classpath/tools/gjdoc/DocImpl.java
blob: dfa1a7a3c6175aebe652095a3bba8477a30494f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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
95
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
141
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
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
239
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
/* gnu.classpath.tools.gjdoc.DocImpl
   Copyright (C) 2001, 2012 Free Software Foundation, Inc.

This file is part of GNU Classpath.

GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

GNU Classpath 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 for more details.

You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.

Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version. */

package gnu.classpath.tools.gjdoc;

import com.sun.javadoc.*;
import java.util.*;
import java.text.*;
import java.io.File;
import javax.swing.text.Segment;

/**
 *  Represents the least common denominator of all Javadoc
 *  comment classes.
 */
public abstract class DocImpl implements Doc, TagContainer {

   protected static Tag[] seeTagEmptyArr = new SeeTagImpl[0];
   protected static Tag[] linkTagEmptyArr = new LinkTagImpl[0];
   protected static Tag[] paramTagEmptyArr = new ParamTagImpl[0];
   protected static Tag[] throwsTagEmptyArr = new ThrowsTagImpl[0];
   protected SourcePosition position;
   private String boilerplateComment;

   // Return the text of the comment for this doc item.
   public String commentText() {

      StringBuffer rc=new StringBuffer();

      Tag[] textTags=(Tag[])tagMap.get("text");
      if (textTags!=null) {
         for (int i=0; i<textTags.length; ++i) {
            rc.append(textTags[i].text());
         }
      }
      return rc.toString();
   }

   // Compares this Object with the specified Object for order.
   public int compareTo(Doc d) {
      return Main.getInstance().getCollator().compare(name(), d.name());
   }

   // Return the first sentence of the comment as tags.
   public Tag[] firstSentenceTags() {

      Tag[] rc=(Tag[])tagMap.get("first");
      if (rc==null) rc=new Tag[0];
      return rc;
   }

   // Return the full unprocessed text of the comment.
   public String getRawCommentText() {
      if (rawDocumentation!=null)
          return rawDocumentation;
      else if (rawDocOffset>=0)
         return Main.getRootDoc().readRawComment(rawDocOffset);
      else
         return null;
   }

   // Return comment as tags.
   public Tag[] inlineTags() {

      Tag[] rc=(Tag[])tagMap.get("inline");
      if (rc==null) rc=new Tag[0];
      return rc;
   }

   // Is this Doc item a class.
   public boolean isClass() {
      return false;
   }

   // Is this Doc item a constructor? False until overridden.
   public boolean isConstructor() {
      return false;
   }

   // Is this Doc item a error class? False until overridden.
   public boolean isError() {
      return false;
   }

   // Is this Doc item a exception class? False until overridden.
   public boolean isException() {
      return false;
   }

   // Is this Doc item a field? False until overridden.
   public boolean isField() {
      return false;
   }

   // return true if this Doc is include in the active set.
   public boolean isIncluded() {
      return false;
   }

   // Is this Doc item a interface? False until overridden.
   public boolean isInterface() {
      return false;
   }

   // Is this Doc item a simple method (i.e.
   public boolean isMethod() {
      return false;
   }

   public boolean isPackage() {
      return false;
   }

   // Is this Doc item a ordinary class (i.e.
   public boolean isOrdinaryClass() {
      return false;
   }

   // Return the see also tags in this Doc item.
   public SeeTag[] seeTags() {
      return (SeeTag[])getTagArr("see", seeTagEmptyArr);
   }

   protected Tag[] getTagArr(String kindOfTag, Tag[] defaultRc) {
      Tag[] rc=(Tag[])tagMap.get(kindOfTag);
      if (rc==null) rc=defaultRc;
      return rc;
   }

   // Set the full unprocessed text of the comment.
   public void setRawCommentText(String rawDocumentation) {
      this.rawDocumentation=rawDocumentation;
   }

   public void resolveComments() {

      if (rawDocumentation!=null && tagMap.isEmpty()) {
         char[] charArray = rawDocumentation.toCharArray();
         int length = rawDocumentation.length();
         int startOffset = 0;
         int endOffset = 0;
         if (charArray[0] == '/'
             && charArray[1] == '*'
             && charArray[2] == '*'
             && charArray[length - 2] == '*'
             && charArray[length - 1] == '/') {

            startOffset = 3;
            endOffset = 2;
         }

         this.tagMap=parseCommentTags(charArray,
                                      startOffset,
                                      length - endOffset,
                                      getContextClass(),
                                      getContextMember(),
                                      null,
                                      boilerplateComment);

         if (Main.getInstance().isCacheRawComments()) {
            rawDocOffset=Main.getRootDoc().writeRawComment(rawDocumentation);
            rawDocumentation=null;
         }

         resolveTags();
      }
      else if (tagMap.isEmpty() && null != boilerplateComment) {
         tagMap.put("all", new Tag[] { new TagImpl("@boilerplate", boilerplateComment,getContextClass(),null) });
         tagMap.put("@boilerplate", new Tag[] { new TagImpl("@boilerplate", boilerplateComment,getContextClass(),null) });
      }
   }

   public static int skipHtmlWhitespace(char[] buffer, int startIndex) {
      while (startIndex < buffer.length) {
         char c=buffer[startIndex];
         if (!Parser.isWhitespace(c)) {
            break;
         }
         else {
            ++ startIndex;
         }
      }
      return startIndex;
   }

   /**
    *  Looks for an end-of-sentence marker in <code>text</code>,
    *  starting at <code>startIndex</code> and stopping at
    *  <code>endIndex</code>.
    *
    *  @param text  the text to be searched
    *  @param startIndex  index in <code>text</code> at which to start
    *  @param endIndex  index in <code>text</code> at which to stop
    *
    *  @return the index of the character following the end-of-sentence
    *    marker, <code>endIndex</code> if no end-of-sentence
    *    marker could be found, or -1 if not implemented.
    */
   private static int findEndOfSentence(char[] text, int startIndex,
                                        int endIndex)
   {
      if (Main.getInstance().isUseBreakIterator()) {
         Segment segment = new Segment(text, startIndex, endIndex - startIndex);
         BreakIterator breakIterator = BreakIterator.getSentenceInstance(Main.getInstance().getLocale());
         breakIterator.setText(segment);
         int result = breakIterator.next();
         if (BreakIterator.DONE == result) {
            return endIndex;
         }
         else {
            return result;
         }
      }
      else {
         while (startIndex < endIndex) {
            if (text[startIndex] == '.'
                && (startIndex+1 == endIndex
                    || Character.isWhitespace(text[startIndex+1])
                    || isHTMLBreakTag(text, startIndex+1, endIndex)
                    )) {
               return startIndex;
            }

            startIndex++;
         }
         return endIndex;
      }
   }

   /**
    * Returns true is the text from start to end begins with a 'p' or 'br' tag.
    */
   private static boolean isHTMLBreakTag(char[] text, int start, int end)
   {
      String[] breakTags = {
         "p>", "/p>", "h1>", "h2>", "h3>", "h4>", "h5>", "h6>", "hr>",
         "pre>", "/pre>"
      };

      if (text[start] == '<') {

      outer:
         for (int i=0; i<breakTags.length; ++i) {
            String tag = breakTags[i];
            int len = tag.length();
            if (start + len < end) {
               for (int j=0; j<len; ++j) {
                  char c = tag.charAt(j);
                  if (Character.toLowerCase(text[start + 1 + j]) != c) {
                     continue outer;
                  }
               }
               return true;
            }
         }
      }
      return false;
   }

   //private static final StringBuffer buf=new StringBuffer(32768);
   private static final StringBuffer whitespaceBuf=new StringBuffer();
   private static char[] charBuf = new char[60000];
   private static int bufPos = 0;

   private static void appendToBuf(char c)
   {
      if (bufPos < charBuf.length) {
         charBuf[bufPos++] = c;
      }
      else {
         //
      }
   }

   private static void appendToBuf(StringBuffer s)
   {
      if (bufPos + s.length() <= charBuf.length) {
         s.getChars(0, s.length(), charBuf, bufPos);
         bufPos += s.length();
      }
      else {
         //
      }
   }

   private static void setBufLength(int length)
   {
      bufPos = 0;
   }

   private static String bufToString()
   {
      return new String(charBuf, 0, bufPos);
   }

   private static int bufLength()
   {
      return bufPos;
   }

   public static Map parseCommentTags(char[] comment, int startIndex, int endIndex,
                                      ClassDocImpl contextClass, MemberDocImpl contextMember,
                                      AbstractTagImpl contextTag, String boilerplateComment) {

      int rawDocStart=skipHtmlWhitespace(comment, startIndex);

      int firstSentenceEnd = 0;

      if (comment.length>rawDocStart) {

         firstSentenceEnd = findEndOfSentence(comment, rawDocStart, comment.length);

         if (firstSentenceEnd < 0) {
            BreakIterator boundary = BreakIterator.getSentenceInstance(Locale.ENGLISH);
            boundary.setText(new ArrayCharacterIterator(comment, rawDocStart));
            boundary.first();
            boundary.next();
            firstSentenceEnd = boundary.current();
         }

         // Always include period at end of sentence if there is one.
         if (firstSentenceEnd < comment.length
                         && '.' == comment[firstSentenceEnd]) {
            ++ firstSentenceEnd;
         }
      }

      final int STATE_BEGOFLINE            = 1;
      final int STATE_TEXT                 = 2;
      final int STATE_PARAM                = 3;
      final int STATE_PARAMVALUE           = 4;
      final int STATE_PARAMWRAP            = 5;
      final int STATE_INLINEPARAM          = 6;
      final int STATE_INLINEPARAMVALUE     = 7;
      final int STATE_WHITESPACE           = 8;
      final int STATE_INLINEPARAMVALUE_BOL = 9;
      final int STATE_IPV_WHITESPACE       = 10;

      int state=STATE_BEGOFLINE;
      int prevState=STATE_TEXT;

      setBufLength(0);
      whitespaceBuf.setLength(0);

      String paramName="", paramValue="";

      Map tags=new HashMap();
      tags.put("inline", new LinkedList());
      tags.put("first", new LinkedList());
      tags.put("all", new LinkedList());

      final char EOL=(char)-1;

      for (int i=rawDocStart; i<=endIndex; ++i) {
         char c=(i<endIndex)?comment[i]:EOL;
         char peek=(i<endIndex-1)?comment[i+1]:EOL;

         switch (state){

         case STATE_BEGOFLINE:
            if (i==firstSentenceEnd) {
               AbstractTagImpl newTag = addTag(tags, "text", bufToString(), true, contextClass, contextMember, contextTag, false);
               if (null != newTag) {
                  contextTag = newTag;
               }
               setBufLength(0);
            }

            if (Parser.isWhitespace(c)) {
               // ignore
            }
            else if (c=='*') {
               // ignore, but go to STATE_TEXT
               if (peek!='*' && peek!='@' && peek!=EOL) {
                  state=STATE_WHITESPACE;
               }
            }
            else if (c=='@' || (c=='{' && peek=='@') || c==EOL) {
               if (bufLength()>0) {
                  addTag(tags, "text", bufToString(), i<firstSentenceEnd, contextClass, contextMember, contextTag, false);
                  setBufLength(0);
               }
               if (c=='{') {
                  ++i;
                  state=STATE_INLINEPARAM;
               }
               else {
                  state=STATE_PARAM;
               }
            }
            else {
               state=STATE_TEXT;
               appendToBuf(whitespaceBuf);
               whitespaceBuf.setLength(0);
               appendToBuf(c);
            }
            break;

         case STATE_WHITESPACE:
            if (i==firstSentenceEnd) {
               AbstractTagImpl newTag = addTag(tags, "text", bufToString(), true, contextClass, contextMember, contextTag, false);
               if (null != newTag) {
                  contextTag = newTag;
               }
               setBufLength(0);
            }

            if (c=='\n') {
               whitespaceBuf.append(c);
               state=STATE_BEGOFLINE;
            }
            else if (Parser.isWhitespace(c)) {
               whitespaceBuf.append(c);
            }
            else if (c=='@' || (c=='{' && peek=='@') || c==EOL) {
               if (bufLength()>0) {
                  AbstractTagImpl newTag = addTag(tags, "text", bufToString(), i<firstSentenceEnd, contextClass, contextMember, contextTag, false);
                  if (null != newTag) {
                     contextTag = newTag;
                  }
                  setBufLength(0);
               }
               if (c=='{') {
                  ++i;
                  state=STATE_INLINEPARAM;
               }
               else {
                  state=STATE_PARAM;
               }
            }
            else {
               appendToBuf(whitespaceBuf);
               whitespaceBuf.setLength(0);
               appendToBuf(c);
               state=STATE_TEXT;
            }
            break;

         case STATE_PARAMWRAP:
            if (c=='\n') {
               appendToBuf(c);
            }
            else if (Parser.isWhitespace(c)) {
               // ignore
            }
            else if (c=='*') {
               // ignore, but go to STATE_TEXT
               /*
               if (i<endIndex && comment[i+1]!='*' && comment[i+1]!='@') {
                  state=STATE_PARAMVALUE;
               }
               */
            }
            else if (c=='@' || c==EOL) {
               paramValue=bufToString();
               AbstractTagImpl newTag = addTag(tags, paramName, paramValue, i<firstSentenceEnd, contextClass, contextMember, contextTag, false);
               if (null != newTag) {
                  contextTag = newTag;
               }
               setBufLength(0);
               if (c=='{') {
                  ++i;
                  state=STATE_INLINEPARAM;
               }
               else {
                  state=STATE_PARAM;
               }
            }
            else {
               state=STATE_PARAMVALUE;
               appendToBuf(c);
            }
            break;

         case STATE_PARAM:
            if (!(c==EOL || Parser.isWhitespace(c))) {
               appendToBuf(c);
            }
            else if (c=='\n') {
               paramName=bufToString();
               setBufLength(0);
               state=STATE_PARAMWRAP;
            }
            else {
               paramName=bufToString();
               setBufLength(0);
               state=STATE_PARAMVALUE;
            }
            break;

         case STATE_INLINEPARAM:
            if (c=='}') {
               // tag without value
               paramName=bufToString();
               AbstractTagImpl newTag = addTag(tags, paramName, "", i<firstSentenceEnd, contextClass, contextMember, contextTag, true);
               if (null != newTag) {
                  contextTag = newTag;
               }
               state=prevState;
               setBufLength(0);
            }
            else if (!(c==EOL || Parser.isWhitespace(c))) {
               appendToBuf(c);
            }
            else if (c=='\n') {
               paramName=bufToString();
               setBufLength(0);
               state=STATE_INLINEPARAMVALUE_BOL;
            }
            else {
               paramName=bufToString();
               setBufLength(0);
               state=STATE_INLINEPARAMVALUE;
            }
            break;

         case STATE_PARAMVALUE:
            if (c==EOL) {
               paramValue=bufToString();
               AbstractTagImpl newTag = addTag(tags, paramName, paramValue, i<firstSentenceEnd, contextClass, contextMember, contextTag, false);
               if (null != newTag) {
                  contextTag = newTag;
               }
            }
            else if (c=='\n') {
               appendToBuf(c);
               state=STATE_PARAMWRAP;
            }
            else {
               appendToBuf(c);
            }
            break;

         case STATE_INLINEPARAMVALUE:
            if (c=='\n') {
               appendToBuf(c);
               state=STATE_INLINEPARAMVALUE_BOL;
            }
            else if (c==EOL || c=='}') {
               paramValue=bufToString();
               AbstractTagImpl newTag = addTag(tags, paramName, paramValue, i<firstSentenceEnd, contextClass, contextMember, contextTag, true);
               if (null != newTag) {
                  contextTag = newTag;
               }
               state=prevState;
               setBufLength(0);
            }
            else {
               appendToBuf(c);
            }
            break;

         case STATE_INLINEPARAMVALUE_BOL:
            if (Parser.isWhitespace(c)) {
               // ignore
            }
            else if (c=='*') {
               // ignore, but go to STATE_TEXT
               if (i<endIndex && peek!='*') {
                  state=STATE_IPV_WHITESPACE;
               }
            }
            else if (c==EOL) {
               if (bufLength()>0) {
                  AbstractTagImpl newTag = addTag(tags, "text", bufToString(), i<firstSentenceEnd, contextClass, contextMember, contextTag, false);
                  if (null != newTag) {
                     contextTag = newTag;
                  }
               }
            }
            else {
               state=STATE_INLINEPARAMVALUE;
               appendToBuf(whitespaceBuf);
               whitespaceBuf.setLength(0);
               appendToBuf(c);
            }
            break;

         case STATE_IPV_WHITESPACE:
            if (c=='\n') {
               whitespaceBuf.append(c);
               state=STATE_INLINEPARAMVALUE_BOL;
            }
            else if (Parser.isWhitespace(c)) {
               whitespaceBuf.append(c);
            }
            else if (c==EOL) {
               if (bufLength()>0) {
                  AbstractTagImpl newTag = addTag(tags, "text", bufToString(), i<firstSentenceEnd, contextClass, contextMember, contextTag, false);
                  if (null != newTag) {
                     contextTag = newTag;
                  }
                  setBufLength(0);
               }
            }
            else {
               appendToBuf(whitespaceBuf);
               whitespaceBuf.setLength(0);
               appendToBuf(c);
               state=STATE_INLINEPARAMVALUE;
            }
            break;

         case STATE_TEXT:
            if (i==firstSentenceEnd) {
               AbstractTagImpl newTag = addTag(tags, "text", bufToString(), true, contextClass, contextMember, contextTag, false);
               if (null != newTag) {
                  contextTag = newTag;
               }
               setBufLength(0);
            }

            if (c==EOL) {
               paramValue=bufToString();
               AbstractTagImpl newTag = addTag(tags, "text", paramValue, i<firstSentenceEnd, contextClass, contextMember, contextTag, false);
               if (null != newTag) {
                  contextTag = newTag;
               }
            }
            else if (c=='\n') {
               appendToBuf(c);
               state=STATE_BEGOFLINE;
            }
            else if (c=='{' && peek=='@') {
               paramValue=bufToString();
               AbstractTagImpl newTag = addTag(tags, "text", paramValue, i<firstSentenceEnd, contextClass, contextMember, contextTag, false);
               if (null != newTag) {
                  contextTag = newTag;
               }
               ++i;
               setBufLength(0);
               state=STATE_INLINEPARAM;
            }
            else {
               appendToBuf(c);
            }
            break;

         default:
            throw new Error("illegal state "+state);
         }
      }


      if (null == contextMember && null != boilerplateComment && Main.getInstance().isCopyLicenseText()) {
         addTag(tags, "@boilerplate", boilerplateComment, false, contextClass, null, null, false);
      }

      Map rc=new HashMap();

      for (Iterator it=tags.keySet().iterator(); it.hasNext(); ) {
         String key=(String)it.next();
         Tag[] templateArr;
         List list=(List)tags.get(key);

         if ("see".equals(key))
            templateArr=new SeeTag[list.size()];
         else if ("param".equals(key))
            templateArr=new ParamTag[list.size()];
         else if ("serialField".equals(key))
            templateArr=new SerialFieldTag[list.size()];
         else if ("throws".equals(key) || "exception".equals(key))
            templateArr=new ThrowsTag[list.size()];
         else {
            templateArr=new Tag[list.size()];
         }

         rc.put(key, list.toArray(templateArr));
      }

      return rc;
   }

   private ClassDocImpl getContextClass() {
      if (isClass() || isInterface()) {
         return (ClassDocImpl)this;
      }
      else if (isField() || isMethod() || isConstructor()) {
         return (ClassDocImpl)((MemberDocImpl)this).containingClass();
      }
      else {
         return null;
      }
   }

   private MemberDocImpl getContextMember() {
      if (isField() || isMethod() || isConstructor()) {
         return (MemberDocImpl)this;
      }
      else {
         return null;
      }
   }

   protected static AbstractTagImpl addTag(Map tags, String name,
                                           String value, boolean isFirstSentence,
                                           ClassDocImpl contextClass,
                                           MemberDocImpl contextMember,
                                           AbstractTagImpl contextTag,
                                           boolean isInline) {

      AbstractTagImpl tag = null;

      boolean haveValue = (0 != value.trim().length());

      String emptyWarning = "Empty @" + name + " tag.";

      if (name.equals("param")) {
         if (haveValue) {
            tag=new ParamTagImpl(value, contextClass, contextMember);
         }
         else {
            //printWarning(emptyWarning);
         }
      }
      else if (name.equals("see")) {
         if (haveValue) {
            tag=new SeeTagImpl(value, contextClass);
         }
         else {
            //printWarning(emptyWarning);
         }
      }
      else if (name.equals("link") || name.equals("linkplain")) {
         if (haveValue) {
            tag=new LinkTagImpl("@" + name, value, contextClass);
            isInline = true;
         }
         else {
            //printWarning(emptyWarning);
         }
      }
      else if (name.equals("value")) {
         if (haveValue) {
            tag=new ValueTagImpl(value, contextClass);
            isInline = true;
         }
         else {
            //printWarning(emptyWarning);
         }
      }
      else if (name.equals("inheritDoc")) {
         if (haveValue) {
            //printWarning("@inheritDoc tags are not supposed to have any content.");
         }
         tag=new InheritDocTagImpl(contextClass, contextMember, contextTag);
         isInline = true;
      }
      else if (name.equals("serialField")) {
         if (haveValue) {
            tag=new SerialFieldTagImpl(value, contextClass, contextMember);
         }
         else {
            //printWarning(emptyWarning);
         }
      }
      else if (name.equals("throws") || name.equals("exception")) {
         if (haveValue) {
            tag=new ThrowsTagImpl(value, contextClass, contextMember);
         }
         else {
            //printWarning(emptyWarning);
         }
         name="throws";
      }
      else if (name.equals("text")) {
         tag=new TextTagImpl(value);
         isInline = true;
      }
      else {
         tag=new TagImpl("@"+name, value.trim(), contextClass, contextMember);
         // FIXME: consider taglets
      }

      if (tag != null) {
         if (isInline) {
            ((List)tags.get("inline")).add(tag);
            if (isFirstSentence) {
               if (name.equals("text")) {
                  String txt = ((TextTagImpl)tag).getText();
                  Tag newTag;
                  if (txt.startsWith("<p>")) {
                     newTag = new TextTagImpl(txt.substring(3));
                  }
                  else if (txt.endsWith("</p>")) {
                     newTag = new TextTagImpl(txt.substring(0, txt.length() - 4));
                  }
                  else {
                     newTag = tag;
                  }
                  ((List)tags.get("first")).add(newTag);

               }
               else {
                  ((List)tags.get("first")).add(tag);
               }
            }
         }
         else {
            ((List)tags.get("all")).add(tag);
         }

         List l=((List)tags.get(name));
         if (l==null) {
            l=new LinkedList();
            tags.put(name,l);
         }
         l.add(tag);

         return isInline ? tag : contextTag;
      }
      else {
         return null;
      }
   }

   // Return all tags in this Doc item.
   public Tag[] tags() {
      Tag[] rc=(Tag[])tagMap.get("all");
      if (rc==null) rc=new Tag[0];
      return rc;
   }

   // Return tags of the specified kind in this Doc item.
   public Tag[] tags(java.lang.String tagname) {
      Tag[] rc=(Tag[])tagMap.get(tagname);
      if (rc==null) rc=new Tag[0];
      return rc;
   }

   protected String rawDocumentation;
   protected long rawDocOffset=-1;

   protected Map tagMap = new HashMap();

   public Map getTagMap() { return tagMap; }

   protected void resolveTags() {

      Tag[] tags=tags();
      for (int i=0; i<tags.length; ++i) {
         ((AbstractTagImpl)tags[i]).resolve();
      }

      Tag[] inlineTags=inlineTags();
      for (int i=0; i<inlineTags.length; ++i) {
         ((AbstractTagImpl)inlineTags[i]).resolve();
      }
   }

   private static Map classDocToFileMap = new HashMap();

   private static File getFile(ClassDoc classDoc) {
      File result = (File)classDocToFileMap.get(classDoc);
      if (null == result) {
         result = new File(((GjdocPackageDoc)classDoc.containingPackage()).packageDirectory(),
                           classDoc.name() + ".java");
         classDocToFileMap.put(classDoc, result);
      }
      return result;
   }

   public static SourcePosition getPosition(ClassDoc classDoc)
   {
      return new SourcePositionImpl(getFile(classDoc), 0, 0);
   }

   public static SourcePosition getPosition(ClassDoc classDoc, char[] source, int startIndex)
   {
      int column = 0;
      int line = 0;
      for (int i=0; i<startIndex; ++i) {
         if (10 == source[i]) {
            ++ line;
            column = 0;
         }
         else if (13 != source[i]) {
            ++ column;
         }
      }
      while (true) {
         ClassDoc containingClassDoc = classDoc.containingClass();
         if (null != containingClassDoc) {
            classDoc = containingClassDoc;
         }
         else {
            break;
         }
      }

      File file = getFile(classDoc);

      return new SourcePositionImpl(file, line + 1, column + 1);
   }

   public SourcePosition position()
   {
      return this.position;
   }

   public DocImpl(SourcePosition position)
   {
      this.position = position;
   }

   public void setPosition(SourcePosition position)
   {
      this.position = position;
   }

   private static TagContainer checkForInheritedDoc(ClassDoc classDoc,
                                                    MemberDocImpl memberDoc,
                                                    AbstractTagImpl tag)
   {
      DocImpl result;

      if (!(classDoc instanceof ClassDocImpl)) {
         result = null;
      }
      else if (null == memberDoc) {
         result = (DocImpl)classDoc;
      }
      else if (memberDoc.isField()) {
         result = (DocImpl)((ClassDocImpl)classDoc).getFieldDoc(memberDoc.name());
      }
      else if (memberDoc.isMethod()) {
         result = (DocImpl)((ClassDocImpl)classDoc).getMethodDoc(memberDoc.name(),
                                                                 ((MethodDoc)memberDoc).signature());
      }
      else if (memberDoc.isConstructor()) {
         result = (DocImpl)((ClassDocImpl)classDoc).getConstructorDoc(((ConstructorDoc)memberDoc).signature());
      }
      else {
         //assert(false);
         throw new RuntimeException("memberDoc is supposed to be field, method or constructor");
      }

      if (null != result
          && null != memberDoc
          && null != tag) {

         TagContainer tagDoc = null;

         Tag[] tags = result.tags();
         for (int i=0; i<tags.length; ++i) {
            if (tags[i].kind().equals(tag.kind())) {
               if ("@param".equals(tag.kind())) {
                  if (((ParamTagImpl)tags[i]).parameterName().equals(((ParamTagImpl)tag).parameterName())) {
                     tagDoc = (TagContainer)tags[i];
                     break;
                  }
               }
               else if ("@throws".equals(tag.kind())) {
                  if (((ThrowsTagImpl)tags[i]).exceptionName().equals(((ThrowsTagImpl)tag).exceptionName())) {
                     tagDoc = (TagContainer)tags[i];
                     break;
                  }
               }
               else if ("@return".equals(tag.kind())) {
                  tagDoc = (TagContainer)tags[i];
               }
            }
         }

         return tagDoc;
      }

      if (null == result || result.isEmptyDoc()) {
         return null;
      }
      else {
         return result;
      }
   }

   public static TagContainer findInheritedDoc(ClassDoc classDoc,
                                               MemberDocImpl memberDoc,
                                               AbstractTagImpl tag)
   {
      TagContainer result;

      // (Taken from Javadoc Solaris Tool documentation 1.5,
      // section "Automatic Copying of Method Comments")

      // Algorithm for Inheriting Method Comments - If a method does
      // not have a doc comment, or has an {@inheritDoc} tag, the
      // Javadoc tool searches for an applicable comment using the
      // following algorithm, which is designed to find the most
      // specific applicable doc comment, giving preference to
      // interfaces over superclasses:

      // 1. Look in each directly implemented (or extended) interface
      // in the order they appear following the word implements (or
      // extends) in the method declaration. Use the first doc comment
      // found for this method.

      ClassDoc[] interfaces = classDoc.interfaces();
      if (null != interfaces) {
         for (int i=0; i<interfaces.length; ++i) {
            result = checkForInheritedDoc(interfaces[i], memberDoc, tag);
            if (null != result) {
               return result;
            }
         }
      }

      // 2. If step 1 failed to find a doc comment, recursively apply
      // this entire algorithm to each directly implemented (or
      // extended) interface, in the same order they were examined
      // in step 1.

      if (null != interfaces) {
         for (int i=0; i<interfaces.length; ++i) {
            result = findInheritedDoc(interfaces[i], memberDoc, tag);
            if (null != result) {
               return result;
            }
         }
      }

      ClassDoc superclassDoc = classDoc.superclass();

      // 3. If step 2 failed to find a doc comment and this is a class
      // other than Object (not an interface):
      if (!classDoc.isInterface()
          && null != superclassDoc
          && !"java.lang.Object".equals(classDoc.qualifiedTypeName())) {

         // 3a. If the superclass has a doc comment for this method, use it.

         result = checkForInheritedDoc(superclassDoc, memberDoc, tag);
         if (null != result) {
            return result;
         }

         // 3b. If step 3a failed to find a doc comment, recursively
         // apply this entire algorithm to the superclass.

         return findInheritedDoc(superclassDoc,
                                 memberDoc, tag);
      }
      else {
         return null;
      }
   }

   public boolean isEmptyDoc()
   {
      return tagMap.isEmpty();
   }

   void setBoilerplateComment(String boilerplateComment)
   {
      this.boilerplateComment = boilerplateComment;
   }
}