• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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 package com.android.cts.verifier.libs.ui;
18 
19 import android.util.Log;
20 import android.view.View;
21 
22 import java.io.File;
23 import java.io.FileNotFoundException;
24 import java.io.FileOutputStream;
25 import java.io.IOException;
26 
27 /**
28  * An interface for formatting text out for various View Types.
29  * Concrete implementations include PlainTextFormatter and HtmlFormatter.
30  */
31 public abstract class TextFormatter {
32     protected StringBuilder mSB = new StringBuilder();
33     private static final String TAG = "TextFormatter";
34 
35     /**
36      * Clear any accumulated text
37      * @return this TextFormatter to allow for cascading calls.
38      */
clear()39     public TextFormatter clear() {
40         mSB = new StringBuilder();
41         return this;
42     }
43 
44     /**
45      * Starts a document block
46      * @return this TextFormatter to allow for cascading calls.
47      */
openDocument()48     public TextFormatter openDocument() {
49         return this;
50     }
51 
52     /**
53      * Closes the document block
54      * @return this TextFormatter to allow for cascading calls.
55      */
closeDocument()56     public TextFormatter closeDocument() {
57         return this;
58     }
59 
60     /**
61      * Starts a Heading block
62      * @param level The desired heading level. Should be between 1 and 6 inclusive.
63      * @return this TextFormatter to allow for cascading calls.
64      */
openHeading(int level)65     public TextFormatter openHeading(int level) {
66         return this;
67     }
68 
69     /**
70      * Ends the Heading block
71      * @param level The heading level associated with the corresponding openHeading() call.
72      * @return this TextFormatter to allow for cascading calls.
73      */
closeHeading(int level)74     public TextFormatter closeHeading(int level) {
75         return this;
76     }
77 
78     /**
79      * Opens a paragraph block.
80      * @return this TextFormatter to allow for cascading calls.
81      */
openParagraph()82     public TextFormatter openParagraph() {
83         return this;
84     }
85 
86     /**
87      * Closes a paragraph block.
88      * @return this TextFormatter to allow for cascading calls.
89      */
closeParagraph()90     public TextFormatter closeParagraph() {
91         return this;
92     }
93 
94     /**
95      * Opens a bold block.
96      * @return this TextFormatter to allow for cascading calls.
97      */
openBold()98     public TextFormatter openBold() {
99         return this;
100     }
101 
102     /**
103      * Closes a bold block.
104      * @return this TextFormatter to allow for cascading calls.
105      */
closeBold()106     public TextFormatter closeBold() {
107         return this;
108     }
109 
110     /**
111      * Opens an italic block.
112      * @return this TextFormatter to allow for cascading calls.
113      */
openItalic()114     public TextFormatter openItalic() {
115         return this;
116     }
117 
118     /**
119      * Closes an italic block.
120      * @return this TextFormatter to allow for cascading calls.
121      */
closeItalic()122     public TextFormatter closeItalic() {
123         return this;
124     }
125 
126     /**
127      * Inserts a 'break' in the text
128      * @return this TextFormatter to allow for cascading calls.
129      */
appendBreak()130     public TextFormatter appendBreak() {
131         return this;
132     }
133 
134     /**
135      * Opens a text color block
136      * @param color The desired color, i.e. "red", "blue"...
137      * @return this TextFormatter to allow for cascading calls.
138      */
openTextColor(String color)139     public TextFormatter openTextColor(String color) {
140         return this;
141     }
142 
143     /**
144      * Closes a color block
145      * @return this TextFormatter to allow for cascading calls.
146      */
closeTextColor()147     public TextFormatter closeTextColor() {
148         return this;
149     }
150 
151     /**
152      * Starts a bullets list.
153      * @return This TextFormatter to allow for cascading calls.
154      */
openBulletList()155     public TextFormatter openBulletList() {
156         return this;
157     }
158 
159     /**
160      * Ends a bullets list.
161      * @return This TextFormatter to allow for cascading calls.
162      */
closeBulletList()163     public TextFormatter closeBulletList() {
164         return this;
165     }
166 
167     /**
168      * Opens a list item in an enclosing bulleted list.
169      * @return This TextFormatter to allow for cascading calls.
170      */
openListItem()171     public TextFormatter openListItem() {
172         return this;
173     }
174 
175     /**
176      * Closes a list item in an enclosing bulleted list.
177      * @return This TextFormatter to allow for cascading calls.
178      */
closeListItem()179     public TextFormatter closeListItem() {
180         return this;
181     }
182 
183     /**
184      * Appends a link tag with the specified link target URL
185      * @param url The url for the link.
186      * @return This TextFormatter to allow for cascading calls.
187      */
openLink(String url)188     public TextFormatter openLink(String url) {
189         return this;
190     }
191 
192     /**
193      * Closes a link tag.
194      * @return This TextFormatter to allow for cascading calls.
195      */
closeLink()196     public TextFormatter closeLink() {
197         return this;
198     }
199 
200     /**
201      * Appends the specified text to the stream.
202      * @return this TextFormatter to allow for cascading calls.
203      */
appendText(String text)204     public TextFormatter appendText(String text) {
205         return this;
206     }
207 
208     @Override
toString()209     public String toString() {
210         return mSB.toString();
211     }
212 
213     /**
214      * Loads the formatted text into a view.
215      *
216      * @param view The View into which the formatted text will is to be displayed.
217      */
put(View view)218     public abstract void put(View view);
219 
220     /**
221      *
222      */
put(File file)223     public void put(File file) {
224         try {
225             FileOutputStream stream = new FileOutputStream(file);
226             try {
227                 stream.write(toString().getBytes());
228             } finally {
229                 stream.close();
230             }
231         } catch (FileNotFoundException e) {
232             Log.e(TAG, "FileNotFoundException: " + e);
233         } catch (IOException e) {
234             Log.e(TAG, "IOException: " + e);
235         }
236     }
237 }
238