2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 import java.util.regex.Pattern;
18 import java.util.regex.Matcher;
19 import java.util.ArrayList;
23 static final Pattern LEADING_WHITESPACE = Pattern.compile(
27 static final Pattern TAG_BEGIN = Pattern.compile(
31 static final Pattern TAG = Pattern.compile(
32 "(@[^ \t\r\n]+)[ \t\r\n]+(.*)",
35 static final Pattern INLINE_TAG = Pattern.compile(
36 "(.*?)\\{(@[^ \t\r\n\\}]+)[ \t\r\n]*(.*?)\\}",
39 static final Pattern FIRST_SENTENCE = Pattern.compile(
40 "((.*?)\\.)[ \t\r\n\\<](.*)",
43 private static final String[] KNOWN_TAGS = new String[] {
58 "@com.intel.drl.spec_ref",
59 "@ar.org.fitc.spec_ref",
62 public Comment(String text, ContainerInfo base, SourcePositionInfo sp)
66 // sp now points to the end of the text, not the beginning!
67 mPosition = SourcePositionInfo.findBeginning(sp, text);
70 private void parseRegex(String text)
74 m = LEADING_WHITESPACE.matcher(text);
78 m = TAG_BEGIN.matcher(text);
85 tag(text, start, end);
87 start = m.end()-1; // -1 is the @
90 tag(text, start, end);
93 private void tag(String text, int start, int end)
95 SourcePositionInfo pos = SourcePositionInfo.add(mPosition, mText, start);
97 if (start >= 0 && end > 0 && (end-start) > 0) {
98 text = text.substring(start, end);
100 Matcher m = TAG.matcher(text);
103 tag(m.group(1), m.group(2), false, pos);
105 // look for inline tags
106 m = INLINE_TAG.matcher(text);
109 String str = m.group(1);
110 String tagname = m.group(2);
111 String tagvalue = m.group(3);
112 tag(null, m.group(1), true, pos);
113 tag(tagname, tagvalue, true, pos);
116 int len = text.length();
118 tag(null, text.substring(start), true, pos);
124 private void tag(String name, String text, boolean isInline, SourcePositionInfo pos)
127 String s = isInline ? "inline" : "outofline";
128 System.out.println("---> " + s
129 + " name=[" + name + "] text=[" + text + "]");
132 mInlineTagsList.add(new TextTagInfo("Text", "Text", text, pos));
134 else if (name.equals("@param")) {
135 mParamTagsList.add(new ParamTagInfo("@param", "@param", text, mBase, pos));
137 else if (name.equals("@see")) {
138 mSeeTagsList.add(new SeeTagInfo("@see", "@see", text, mBase, pos));
140 else if (name.equals("@link") || name.equals("@linkplain")) {
141 mInlineTagsList.add(new SeeTagInfo(name, "@see", text, mBase, pos));
143 else if (name.equals("@throws") || name.equals("@exception")) {
144 mThrowsTagsList.add(new ThrowsTagInfo("@throws", "@throws", text, mBase, pos));
146 else if (name.equals("@return")) {
147 mReturnTagsList.add(new ParsedTagInfo("@return", "@return", text, mBase, pos));
149 else if (name.equals("@deprecated")) {
150 if (text.length() == 0) {
151 Errors.error(Errors.MISSING_COMMENT, pos,
152 "@deprecated tag with no explanatory comment");
153 text = "No replacement.";
155 mDeprecatedTagsList.add(new ParsedTagInfo("@deprecated", "@deprecated", text, mBase, pos));
157 else if (name.equals("@literal")) {
158 mInlineTagsList.add(new LiteralTagInfo(name, name, text, pos));
160 else if (name.equals("@hide") || name.equals("@pending") || name.equals("@doconly")) {
163 else if (name.equals("@attr")) {
164 AttrTagInfo tag = new AttrTagInfo("@attr", "@attr", text, mBase, pos);
165 mAttrTagsList.add(tag);
166 Comment c = tag.description();
168 for (TagInfo t: c.tags()) {
169 mInlineTagsList.add(t);
173 else if (name.equals("@undeprecate")) {
174 mUndeprecateTagsList.add(new TextTagInfo("@undeprecate", "@undeprecate", text, pos));
176 else if (name.equals("@include") || name.equals("@sample")) {
177 mInlineTagsList.add(new SampleTagInfo(name, "@include", text, mBase, pos));
180 boolean known = false;
181 for (String s: KNOWN_TAGS) {
182 if (s.equals(name)) {
188 Errors.error(Errors.UNKNOWN_TAG, pos == null ? null : new SourcePositionInfo(pos),
189 "Unknown tag: " + name);
191 TagInfo t = new TextTagInfo(name, name, text, pos);
193 mInlineTagsList.add(t);
200 private void parseBriefTags()
202 int N = mInlineTagsList.size();
204 // look for "@more" tag, which means that we might go past the first sentence.
206 for (int i=0; i<N; i++) {
207 if (mInlineTagsList.get(i).name().equals("@more")) {
212 for (int i=0; i<more; i++) {
213 mBriefTagsList.add(mInlineTagsList.get(i));
216 for (int i=0; i<N; i++) {
217 TagInfo t = mInlineTagsList.get(i);
218 if (t.name().equals("Text")) {
219 Matcher m = FIRST_SENTENCE.matcher(t.text());
221 String text = m.group(1);
222 TagInfo firstSentenceTag = new TagInfo(t.name(), t.kind(), text, t.position());
223 mBriefTagsList.add(firstSentenceTag);
227 mBriefTagsList.add(t);
233 public TagInfo[] tags()
239 public TagInfo[] tags(String name)
242 ArrayList<TagInfo> results = new ArrayList<TagInfo>();
243 int N = mInlineTagsList.size();
244 for (int i=0; i<N; i++) {
245 TagInfo t = mInlineTagsList.get(i);
246 if (t.name().equals(name)) {
250 return results.toArray(new TagInfo[results.size()]);
253 public ParamTagInfo[] paramTags()
259 public SeeTagInfo[] seeTags()
265 public ThrowsTagInfo[] throwsTags()
271 public TagInfo[] returnTags()
277 public TagInfo[] deprecatedTags()
280 return mDeprecatedTags;
283 public TagInfo[] undeprecateTags()
286 return mUndeprecateTags;
289 public AttrTagInfo[] attrTags()
295 public TagInfo[] briefTags()
301 public boolean isHidden()
306 if (DroidDoc.checkLevel(DroidDoc.SHOW_HIDDEN)) {
310 boolean b = mText.indexOf("@hide") >= 0 || mText.indexOf("@pending") >= 0;
316 public boolean isDocOnly() {
318 return mDocOnly != 0;
320 boolean b = (mText != null) && (mText.indexOf("@doconly") >= 0);
321 mDocOnly = b ? 1 : 0;
333 private void initImpl()
342 mInlineTags = mInlineTagsList.toArray(new TagInfo[mInlineTagsList.size()]);
343 mParamTags = mParamTagsList.toArray(new ParamTagInfo[mParamTagsList.size()]);
344 mSeeTags = mSeeTagsList.toArray(new SeeTagInfo[mSeeTagsList.size()]);
345 mThrowsTags = mThrowsTagsList.toArray(new ThrowsTagInfo[mThrowsTagsList.size()]);
346 mReturnTags = ParsedTagInfo.joinTags(mReturnTagsList.toArray(
347 new ParsedTagInfo[mReturnTagsList.size()]));
348 mDeprecatedTags = ParsedTagInfo.joinTags(mDeprecatedTagsList.toArray(
349 new ParsedTagInfo[mDeprecatedTagsList.size()]));
350 mUndeprecateTags = mUndeprecateTagsList.toArray(new TagInfo[mUndeprecateTagsList.size()]);
351 mAttrTags = mAttrTagsList.toArray(new AttrTagInfo[mAttrTagsList.size()]);
352 mBriefTags = mBriefTagsList.toArray(new TagInfo[mBriefTagsList.size()]);
354 mParamTagsList = null;
356 mThrowsTagsList = null;
357 mReturnTagsList = null;
358 mDeprecatedTagsList = null;
359 mUndeprecateTagsList = null;
360 mAttrTagsList = null;
361 mBriefTagsList = null;
364 boolean mInitialized;
369 SourcePositionInfo mPosition;
372 TagInfo[] mInlineTags;
374 ParamTagInfo[] mParamTags;
375 SeeTagInfo[] mSeeTags;
376 ThrowsTagInfo[] mThrowsTags;
377 TagInfo[] mBriefTags;
378 TagInfo[] mReturnTags;
379 TagInfo[] mDeprecatedTags;
380 TagInfo[] mUndeprecateTags;
381 AttrTagInfo[] mAttrTags;
383 ArrayList<TagInfo> mInlineTagsList = new ArrayList<TagInfo>();
384 ArrayList<TagInfo> mTagsList = new ArrayList<TagInfo>();
385 ArrayList<ParamTagInfo> mParamTagsList = new ArrayList<ParamTagInfo>();
386 ArrayList<SeeTagInfo> mSeeTagsList = new ArrayList<SeeTagInfo>();
387 ArrayList<ThrowsTagInfo> mThrowsTagsList = new ArrayList<ThrowsTagInfo>();
388 ArrayList<TagInfo> mBriefTagsList = new ArrayList<TagInfo>();
389 ArrayList<ParsedTagInfo> mReturnTagsList = new ArrayList<ParsedTagInfo>();
390 ArrayList<ParsedTagInfo> mDeprecatedTagsList = new ArrayList<ParsedTagInfo>();
391 ArrayList<TagInfo> mUndeprecateTagsList = new ArrayList<TagInfo>();
392 ArrayList<AttrTagInfo> mAttrTagsList = new ArrayList<AttrTagInfo>();