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 #ifndef AAPT_JAVA_ANNOTATIONPROCESSOR_H
18 #define AAPT_JAVA_ANNOTATIONPROCESSOR_H
19 
20 #include <sstream>
21 #include <string>
22 #include <unordered_map>
23 
24 #include "androidfw/StringPiece.h"
25 
26 #include "text/Printer.h"
27 
28 namespace aapt {
29 
30 // Builds a JavaDoc comment from a set of XML comments.
31 // This will also look for instances of @SystemApi and convert them to
32 // actual Java annotations.
33 //
34 // Example:
35 //
36 // Input XML:
37 //
38 // <!-- This is meant to be hidden because
39 //      It is system api. Also it is @deprecated
40 //      @SystemApi
41 //      -->
42 //
43 // Output JavaDoc:
44 //
45 //  /**
46 //   * This is meant to be hidden because
47 //   * It is system api. Also it is @deprecated
48 //   */
49 //
50 // Output Annotations:
51 //
52 // @Deprecated
53 // @android.annotation.SystemApi
54 class AnnotationProcessor {
55  public:
56   // Extracts the first sentence of a comment. The algorithm selects the substring starting from
57   // the beginning of the string, and ending at the first '.' character that is followed by a
58   // whitespace character. If these requirements are not met, the whole string is returned.
59   static android::StringPiece ExtractFirstSentence(android::StringPiece comment);
60 
61   // Adds more comments. Resources can have value definitions for various configurations, and
62   // each of the definitions may have comments that need to be processed.
63   //
64   // If add_api_annotations is false, annotations found in the comment (e.g., "@SystemApi")
65   // will NOT be converted to Java annotations.
66   void AppendComment(android::StringPiece comment, bool add_api_annotations = true);
67 
68   void AppendNewLine();
69 
70   // Writes the comments and annotations to the Printer.
71   void Print(text::Printer* printer, bool strip_api_annotations = false) const;
72 
73  private:
74   std::stringstream comment_;
75   std::stringstream mAnnotations;
76   bool has_comments_ = false;
77   std::unordered_map<uint32_t, std::string> annotation_parameter_map_;
78 
79   void AppendCommentLine(std::string line, bool add_api_annotations);
80 };
81 
82 }  // namespace aapt
83 
84 #endif /* AAPT_JAVA_ANNOTATIONPROCESSOR_H */
85