michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko.background.db; michael@0: michael@0: import android.database.Cursor; michael@0: michael@0: /** michael@0: * A utility for dumping a cursor the debug log. michael@0: *

michael@0: * For debugging only!

michael@0: */ michael@0: public class CursorDumper { michael@0: protected static String fixedWidth(int width, String s) { michael@0: if (s == null) { michael@0: return spaces(width); michael@0: } michael@0: int length = s.length(); michael@0: if (width == length) { michael@0: return s; michael@0: } michael@0: if (width > length) { michael@0: return s + spaces(width - length); michael@0: } michael@0: return s.substring(0, width); michael@0: } michael@0: michael@0: protected static String spaces(int i) { michael@0: return " ".substring(0, i); michael@0: } michael@0: michael@0: protected static String dashes(int i) { michael@0: return "-------------------------------------".substring(0, i); michael@0: } michael@0: michael@0: /** michael@0: * Dump a cursor to the debug log, ignoring any log level settings. michael@0: *

michael@0: * The position in the cursor is maintained. Caller is responsible for opening michael@0: * and closing cursor. michael@0: * michael@0: * @param cursor michael@0: * to dump. michael@0: */ michael@0: public static void dumpCursor(Cursor cursor) { michael@0: dumpCursor(cursor, 18, "records"); michael@0: } michael@0: michael@0: /** michael@0: * Dump a cursor to the debug log, ignoring any log level settings. michael@0: *

michael@0: * The position in the cursor is maintained. Caller is responsible for opening michael@0: * and closing cursor. michael@0: * michael@0: * @param cursor michael@0: * to dump. michael@0: * @param columnWidth michael@0: * how many characters per cursor column. michael@0: * @param tags michael@0: * a descriptor, printed like "(10 tags)", in the header row. michael@0: */ michael@0: protected static void dumpCursor(Cursor cursor, int columnWidth, String tags) { michael@0: int originalPosition = cursor.getPosition(); michael@0: try { michael@0: String[] columnNames = cursor.getColumnNames(); michael@0: int columnCount = cursor.getColumnCount(); michael@0: michael@0: for (int i = 0; i < columnCount; ++i) { michael@0: System.out.print(fixedWidth(columnWidth, columnNames[i]) + " | "); michael@0: } michael@0: System.out.println("(" + cursor.getCount() + " " + tags + ")"); michael@0: for (int i = 0; i < columnCount; ++i) { michael@0: System.out.print(dashes(columnWidth) + " | "); michael@0: } michael@0: System.out.println(""); michael@0: if (!cursor.moveToFirst()) { michael@0: System.out.println("EMPTY"); michael@0: return; michael@0: } michael@0: michael@0: cursor.moveToFirst(); michael@0: while (!cursor.isAfterLast()) { michael@0: for (int i = 0; i < columnCount; ++i) { michael@0: System.out.print(fixedWidth(columnWidth, cursor.getString(i)) + " | "); michael@0: } michael@0: System.out.println(""); michael@0: cursor.moveToNext(); michael@0: } michael@0: for (int i = 0; i < columnCount-1; ++i) { michael@0: System.out.print(dashes(columnWidth + 3)); michael@0: } michael@0: System.out.print(dashes(columnWidth + 3 - 1)); michael@0: System.out.println(""); michael@0: } finally { michael@0: cursor.moveToPosition(originalPosition); michael@0: } michael@0: } michael@0: }