1/*
2 * Copyright (C) 2010 Google Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.google.doclava;
18
19import com.google.clearsilver.jsilver.data.Data;
20
21import java.util.regex.Matcher;
22import java.util.regex.Pattern;
23
24public class ParamTagInfo extends ParsedTagInfo {
25  public static final ParamTagInfo[] EMPTY_ARRAY = new ParamTagInfo[0];
26
27  public static ParamTagInfo[] getArray(int size) {
28      return size == 0 ? EMPTY_ARRAY : new ParamTagInfo[size];
29  }
30
31  static final Pattern PATTERN = Pattern.compile("([^ \t\r\n]+)[ \t\r\n]+(.*)", Pattern.DOTALL);
32
33  private boolean mIsTypeParameter;
34  private String mParameterComment;
35  private String mParameterName;
36  private TagInfo[] mAuxTags;
37
38  ParamTagInfo(String name, String kind, String text, ContainerInfo base, SourcePositionInfo sp) {
39      this(name, kind, text, base, sp, null);
40  }
41
42  ParamTagInfo(String name, String kind, String text, ContainerInfo base, SourcePositionInfo sp,
43      TagInfo[] auxTags) {
44    super(name, kind, text, base, sp);
45    mAuxTags = auxTags;
46
47    Matcher m = PATTERN.matcher(text);
48    if (m.matches()) {
49      mParameterName = m.group(1);
50      mParameterComment = m.group(2);
51      int len = mParameterName.length();
52      mIsTypeParameter =
53          len > 2 && mParameterName.charAt(0) == '<' && mParameterName.charAt(len - 1) == '>';
54    } else {
55      mParameterName = text.trim();
56      mParameterComment = "";
57      mIsTypeParameter = false;
58    }
59    setCommentText(mParameterComment);
60  }
61
62  ParamTagInfo(String name, String kind, String text, boolean isTypeParameter,
63      String parameterComment, String parameterName, ContainerInfo base, SourcePositionInfo sp) {
64    super(name, kind, text, base, sp);
65    mIsTypeParameter = isTypeParameter;
66    mParameterComment = parameterComment;
67    mParameterName = parameterName;
68  }
69
70  public boolean isTypeParameter() {
71    return mIsTypeParameter;
72  }
73
74  public String parameterComment() {
75    return mParameterComment;
76  }
77
78  public String parameterName() {
79    return mParameterName;
80  }
81
82  public TagInfo[] auxTags() {
83    return mAuxTags;
84  }
85
86  @Override
87  public void makeHDF(Data data, String base) {
88    data.setValue(base + ".name", parameterName());
89    data.setValue(base + ".kind", kind());
90    data.setValue(base + ".isTypeParameter", isTypeParameter() ? "1" : "0");
91    TagInfo.makeHDF(data, base + ".comment", commentTags());
92    TagInfo.makeHDF(data, base + ".commentAux", auxTags());
93  }
94
95  public static void makeHDF(Data data, String base, ParamTagInfo[] tags) {
96    for (int i = 0; i < tags.length; i++) {
97      // don't output if the comment is ""
98      if (!"".equals(tags[i].parameterComment())) {
99        tags[i].makeHDF(data, base + "." + i);
100      }
101    }
102  }
103}
104