OSDN Git Service

Cherry-pick from master: AAPT2: Fix JavaDoc first sentence extraction.
[android-x86/frameworks-base.git] / tools / aapt2 / java / AnnotationProcessor.cpp
1 /*
2  * Copyright (C) 2015 The Android Open Source Project
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
17 #include "java/AnnotationProcessor.h"
18
19 #include <algorithm>
20
21 #include "text/Unicode.h"
22 #include "text/Utf8Iterator.h"
23 #include "util/Util.h"
24
25 using ::aapt::text::Utf8Iterator;
26 using ::android::StringPiece;
27
28 namespace aapt {
29
30 StringPiece AnnotationProcessor::ExtractFirstSentence(const StringPiece& comment) {
31   Utf8Iterator iter(comment);
32   while (iter.HasNext()) {
33     const char32_t codepoint = iter.Next();
34     if (codepoint == U'.') {
35       const size_t current_position = iter.Position();
36       if (!iter.HasNext() || text::IsWhitespace(iter.Next())) {
37         return comment.substr(0, current_position);
38       }
39     }
40   }
41   return comment;
42 }
43
44 void AnnotationProcessor::AppendCommentLine(std::string& comment) {
45   static const std::string sDeprecated = "@deprecated";
46   static const std::string sSystemApi = "@SystemApi";
47
48   if (comment.find(sDeprecated) != std::string::npos) {
49     annotation_bit_mask_ |= kDeprecated;
50   }
51
52   std::string::size_type idx = comment.find(sSystemApi);
53   if (idx != std::string::npos) {
54     annotation_bit_mask_ |= kSystemApi;
55     comment.erase(comment.begin() + idx,
56                   comment.begin() + idx + sSystemApi.size());
57   }
58
59   if (util::TrimWhitespace(comment).empty()) {
60     return;
61   }
62
63   if (!has_comments_) {
64     has_comments_ = true;
65     comment_ << "/**";
66   }
67
68   comment_ << "\n * " << std::move(comment);
69 }
70
71 void AnnotationProcessor::AppendComment(const StringPiece& comment) {
72   // We need to process line by line to clean-up whitespace and append prefixes.
73   for (StringPiece line : util::Tokenize(comment, '\n')) {
74     line = util::TrimWhitespace(line);
75     if (!line.empty()) {
76       std::string lineCopy = line.to_string();
77       AppendCommentLine(lineCopy);
78     }
79   }
80 }
81
82 void AnnotationProcessor::AppendNewLine() { comment_ << "\n *"; }
83
84 void AnnotationProcessor::WriteToStream(std::ostream* out,
85                                         const StringPiece& prefix) const {
86   if (has_comments_) {
87     std::string result = comment_.str();
88     for (StringPiece line : util::Tokenize(result, '\n')) {
89       *out << prefix << line << "\n";
90     }
91     *out << prefix << " */"
92          << "\n";
93   }
94
95   if (annotation_bit_mask_ & kDeprecated) {
96     *out << prefix << "@Deprecated\n";
97   }
98
99   if (annotation_bit_mask_ & kSystemApi) {
100     *out << prefix << "@android.annotation.SystemApi\n";
101   }
102 }
103
104 }  // namespace aapt