AdditionalTextOutput.java revision 2e5982a55ac031110ed39515a76f7a5ec9ff2c14
1/*
2 * Copyright (C) 2010 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
17package com.android.dumprendertree2;
18
19import android.util.Log;
20import android.webkit.ConsoleMessage;
21
22import java.net.MalformedURLException;
23import java.net.URL;
24
25/**
26 * A class that stores consoles messages, database callbacks, alert messages, etc.
27 */
28public class AdditionalTextOutput {
29    private static final String LOG_TAG = "AdditionalTextOutput";
30
31    /**
32     * Ordering of enums is important as it determines ordering of the toString method!
33     * StringBuilders will be printed in the order the corresponding types appear here.
34     */
35    private enum OutputType {
36        JS_DIALOG,
37        EXCEEDED_DB_QUOTA_MESSAGE,
38        CONSOLE_MESSAGE;
39    }
40
41    StringBuilder[] mOutputs = new StringBuilder[OutputType.values().length];
42
43    private StringBuilder getStringBuilderForType(OutputType outputType) {
44        int index = outputType.ordinal();
45        if (mOutputs[index] == null) {
46            mOutputs[index] = new StringBuilder();
47        }
48        return mOutputs[index];
49    }
50
51    public void appendExceededDbQuotaMessage(String urlString, String databaseIdentifier) {
52        StringBuilder output = getStringBuilderForType(OutputType.EXCEEDED_DB_QUOTA_MESSAGE);
53
54        String protocol = "";
55        String host = "";
56        int port = 0;
57
58        try {
59            URL url = new URL(urlString);
60            protocol = url.getProtocol();
61            host = url.getHost();
62            if (url.getPort() > -1) {
63                port = url.getPort();
64            }
65        } catch (MalformedURLException e) {
66            Log.e(LOG_TAG, "urlString=" + urlString + " databaseIdentifier=" + databaseIdentifier,
67                    e);
68        }
69
70        output.append("UI DELEGATE DATABASE CALLBACK: ");
71        output.append("exceededDatabaseQuotaForSecurityOrigin:{");
72        output.append(protocol + ", " + host + ", " + port + "} ");
73        output.append("database:" + databaseIdentifier + "\n");
74    }
75
76    public void appendConsoleMessage(ConsoleMessage consoleMessage) {
77        StringBuilder output = getStringBuilderForType(OutputType.CONSOLE_MESSAGE);
78
79        output.append("CONSOLE MESSAGE: line " + consoleMessage.lineNumber());
80        output.append(": " + consoleMessage.message() + "\n");
81    }
82
83    public void appendJsAlert(String message) {
84        StringBuilder output = getStringBuilderForType(OutputType.JS_DIALOG);
85
86        output.append("ALERT: ");
87        output.append(message);
88        output.append('\n');
89    }
90
91    public void appendJsConfirm(String message) {
92        StringBuilder output = getStringBuilderForType(OutputType.JS_DIALOG);
93
94        output.append("CONFIRM: ");
95        output.append(message);
96        output.append('\n');
97    }
98
99    public void appendJsPrompt(String message, String defaultValue) {
100        StringBuilder output = getStringBuilderForType(OutputType.JS_DIALOG);
101
102        output.append("PROMPT: ");
103        output.append(message);
104        output.append(", default text: ");
105        output.append(defaultValue);
106        output.append('\n');
107    }
108
109    @Override
110    public String toString() {
111        StringBuilder result = new StringBuilder();
112        for (int i = 0; i < mOutputs.length; i++) {
113            if (mOutputs[i] != null) {
114                result.append(mOutputs[i].toString());
115            }
116        }
117        return result.toString();
118    }
119}