From: shingil.kang Date: Wed, 29 Jan 2014 06:32:11 +0000 (+0900) Subject: TOOLS: SDBLIB: Supported character set encoding in the Log View user can manually... X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=040f822a176a2087c4e3a42778be7a5696ac25f2;p=sdk%2Fide%2Fcommon-eplugin.git TOOLS: SDBLIB: Supported character set encoding in the Log View user can manually apply In order to decode result bytes which the sdblib module generates, added the functions that support specific character set decoding. Change-Id: Ie8e1fc8c6c8cfc5ea0f966ebb1418e2781dec322 Signed-off-by: shingil.kang --- diff --git a/org.tizen.common.connection/src/org/tizen/common/connection/log/EncodingChar.java b/org.tizen.common.connection/src/org/tizen/common/connection/log/EncodingChar.java new file mode 100644 index 0000000..6f5652a --- /dev/null +++ b/org.tizen.common.connection/src/org/tizen/common/connection/log/EncodingChar.java @@ -0,0 +1,135 @@ +/* + * Common + * + * Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: + * Kangho Kim + * Shingil kang + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * - S-Core Co., Ltd + * + */ +package org.tizen.common.connection.log; + +public enum EncodingChar +{ + AUTO("AUTO"), + GB18030("GB18030", Region.EAST_ASAIN, "Chiness Simplified"), + GB2312("EUC_CN", Region.EAST_ASAIN, "Chiness Simplified"), + GBK("GBK", Region.EAST_ASAIN, "Chiness Simplified"), + ISO_2022_CN("ISO2022CN", Region.EAST_ASAIN, "Chiness Simplified"), + BIG5("Big5", Region.EAST_ASAIN, "Chiness Traditional"), + BIG5_HKSCS("Big5_HKSCS", Region.EAST_ASAIN, "Chiness Traditional"), + X_EUC_TW("EUC_TW", Region.EAST_ASAIN, "Chiness Traditional"), + EUC_JP("EUC_JP", Region.EAST_ASAIN, "Japanese"), + ISO_2022_JP("ISO2022JP", Region.EAST_ASAIN, "Japanese"), + SHIFT_JIS("SJIS", Region.EAST_ASAIN, "Japanese"), + EUC_KR("EUC_KR", Region.EAST_ASAIN, "Korean"), + ISO_2022_KR("ISO2022KR", Region.EAST_ASAIN, "Korean"), + X_JOHAB("x-Johab", Region.EAST_ASAIN, "Korean"), + ISO_8859_13("ISO8859_13", Region.EAST_EUROPEAN, "Baltic"), + ISO_8859_4("ISO8859_4", Region.EAST_EUROPEAN, "Baltic"), + WINDOWS_1257("Cp1257", Region.EAST_EUROPEAN, "Baltic"), + IBM852("Cp852", Region.EAST_EUROPEAN, "Central European"), + ISO_8859_2("ISO8859_2", Region.EAST_EUROPEAN, "Central European"), + X_MACCENTRALEUROPE("MacCentralEurope", Region.EAST_EUROPEAN, "Central European"), + WINDOWS_250("Cp1250", Region.EAST_EUROPEAN, "Central European"), + X_MACCROATIAN("MacCroatian", Region.EAST_EUROPEAN, "Croatian"), + IBM855("Cp855", Region.EAST_EUROPEAN, "Cyrillic"), + ISO_8859_5("ISO8859_5", Region.EAST_EUROPEAN, "Cyrillic"), + KOI8_R("KOI8_R", Region.EAST_EUROPEAN, "Cyrillic"), + X_MACCYRILLIC("MacCyrillic", Region.EAST_EUROPEAN, "Cyrillic"), + WINDOWS_1251("Cp1251", Region.EAST_EUROPEAN, "Cyrillic"), + IBM866("Cp866", Region.EAST_EUROPEAN, "Cyrillic/Russian"), + KOI8_U("KOI8_U", Region.EAST_EUROPEAN, "Cyrillic/Ukrainian"), + X_MACROMANIA("MacRomania", Region.EAST_EUROPEAN, "Romanian"), + ISO_8859_6("ISO8859_6", Region.MIDDLE_EASTERN, "Arabic"), + WINDOWS_1256("Cp1256", Region.MIDDLE_EASTERN, "Arabic"), + IBM862("Cp862", Region.MIDDLE_EASTERN, "Hebrew"), + WINDOWS_1255("Cp1255", Region.MIDDLE_EASTERN, "Hebrew"), + ISO_8859_8("ISO8859_8", Region.MIDDLE_EASTERN, "Hebrew Visual"), + X_ISO_8859_11("x-iso-8859-11", Region.SE_SW_ASIAN, "Thai"), + TIS_620("TIS620", Region.SE_SW_ASIAN, "Thai"), + X_WINDOWS_874("MS874", Region.SE_SW_ASIAN, "Thai"), + IBM857("Cp857", Region.SE_SW_ASIAN, "Turkish"), + ISO_8859_9("ISO8859_9", Region.SE_SW_ASIAN, "Turkish"), + X_MACTURKISH("MacTurkish", Region.SE_SW_ASIAN, "Turkish"), + WINDOWS_1254("Cp1254", Region.SE_SW_ASIAN, "Turkish"), + WINDOWS_1258("Cp1258", Region.SE_SW_ASIAN, "Vietnamese"), + ISO_8859_7("ISO8859_7", Region.WEST_EUROPEAN, "Greek"), + X_MACGREEK("MacGreek", Region.WEST_EUROPEAN, "Greek"), + WINDOWS_1253("Cp1253", Region.WEST_EUROPEAN, "Greek"), + X_MACLCELAND("MacIceland", Region.WEST_EUROPEAN, "Icelandic"), + ISO_8859_3("ISO8859_3", Region.WEST_EUROPEAN, "South Enropean"), + IBM850("Cp850", Region.WEST_EUROPEAN, "Western"), + ISO_8859_1("ISO8859_1", Region.WEST_EUROPEAN, "Western"), + ISO_8859_15("ISO8859_15", Region.WEST_EUROPEAN, "Western"), + X_MACROMAN("MacRoman", Region.WEST_EUROPEAN, "Western"), + WINDOWS_1252("Cp1252", Region.WEST_EUROPEAN, "Western"); + + private String name; + private Region region; + private String nation; + + EncodingChar(String name) + { + this.name = name; + } + + EncodingChar(String name, Region region, String nation) + { + this.name = name; + this.region = region; + this.nation = nation; + } + + public String getName() + { + return name; + } + + public Region getRegion() + { + return region; + } + + public String getNation() + { + return nation; + } +} + +enum Region +{ + EAST_ASAIN("East Asian"), + SE_SW_ASIAN("Southeast and Southwest Asian"), + MIDDLE_EASTERN("Middle Eastern"), + EAST_EUROPEAN("East European"), + WEST_EUROPEAN("West European"); + + private String name; + + Region(String name) + { + this.name = name; + } + + public String getName() + { + return name; + } +} \ No newline at end of file diff --git a/org.tizen.common.connection/src/org/tizen/common/connection/log/LogPanel.java b/org.tizen.common.connection/src/org/tizen/common/connection/log/LogPanel.java index 068ff00..aa6c10e 100755 --- a/org.tizen.common.connection/src/org/tizen/common/connection/log/LogPanel.java +++ b/org.tizen.common.connection/src/org/tizen/common/connection/log/LogPanel.java @@ -86,15 +86,16 @@ public class LogPanel implements IDeviceChangeListener // Only add, edit disabled public static final int DISCONNECTED_TAB = 3; - private final int ENABLE_ADD = 0x0000001; - private final int ENABLE_REMOVE = 0x0000010; - private final int ENABLE_EDIT = 0x0000100; - private final int ENABLE_SCROLL_LOCK = 0x0001000; - private final int ENABLE_EXPORT = 0x0010000; - private final int ENABLE_CLEAR = 0x0100000; - private final int ENABLE_LEVEL = 0x1000000; + private final int ENABLE_ADD = 0x00000001; + private final int ENABLE_REMOVE = 0x00000010; + private final int ENABLE_EDIT = 0x00000100; + private final int ENABLE_SCROLL_LOCK = 0x00001000; + private final int ENABLE_EXPORT = 0x00010000; + private final int ENABLE_CLEAR = 0x00100000; + private final int ENABLE_LEVEL = 0x01000000; + private final int ENABLE_CONVERT = 0x10000000; - private final int[] ENABLE_VIEW_ACTIONS = { ENABLE_ADD, ENABLE_REMOVE, ENABLE_EDIT, ENABLE_SCROLL_LOCK, ENABLE_EXPORT, ENABLE_CLEAR }; + private final int[] ENABLE_VIEW_ACTIONS = { ENABLE_ADD, ENABLE_REMOVE, ENABLE_EDIT, ENABLE_SCROLL_LOCK, ENABLE_EXPORT, ENABLE_CLEAR, ENABLE_CONVERT}; private String defaultLogSave; @@ -111,7 +112,7 @@ public class LogPanel implements IDeviceChangeListener private StackLayout stackLayout; public static List logTabList = new ArrayList(); - + public static final LogLevel DEFAULT_LOG_LEVEL = LogLevel.VERBOSE; /** @@ -856,7 +857,7 @@ public class LogPanel implements IDeviceChangeListener } boolean check = getEnabled(state, ENABLE_LEVEL); - boolean[] bEnable = { false, false, false, false, false, false }; + boolean[] bEnable = { false, false, false, false, false, false, false }; // if level is enabled, if (check) { @@ -911,23 +912,23 @@ public class LogPanel implements IDeviceChangeListener */ public int getEnabledStateForActions(int tabType) { - int enable = 0x0000000; + int enable = 0x00000000; if (tabType == NO_TAB) { - enable = 0x0000000; + enable = 0x00000000; } else if (tabType == DEVICE_TAB) { - enable = ENABLE_ADD | ENABLE_SCROLL_LOCK | ENABLE_EXPORT | ENABLE_CLEAR | ENABLE_LEVEL; + enable = ENABLE_ADD | ENABLE_SCROLL_LOCK | ENABLE_EXPORT | ENABLE_CLEAR | ENABLE_LEVEL | ENABLE_CONVERT; } else if (tabType == ADDITIONAL_TAB) { - enable = ENABLE_ADD | ENABLE_REMOVE | ENABLE_EDIT | ENABLE_SCROLL_LOCK | ENABLE_EXPORT | ENABLE_CLEAR | ENABLE_LEVEL; + enable = ENABLE_ADD | ENABLE_REMOVE | ENABLE_EDIT | ENABLE_SCROLL_LOCK | ENABLE_EXPORT | ENABLE_CLEAR | ENABLE_LEVEL | ENABLE_CONVERT; } else if (tabType == DISCONNECTED_TAB) { - enable = ENABLE_REMOVE | ENABLE_EXPORT | ENABLE_LEVEL; + enable = ENABLE_REMOVE | ENABLE_EXPORT | ENABLE_LEVEL | ENABLE_CONVERT; } return enable; @@ -954,4 +955,10 @@ public class LogPanel implements IDeviceChangeListener { getSelectionLogTab().setScrollLock(lock); } + + public void restart() + { + LogTab logTab = getSelectionLogTab(); + logTab.restart(); + } } diff --git a/org.tizen.common.connection/src/org/tizen/common/connection/log/LogTab.java b/org.tizen.common.connection/src/org/tizen/common/connection/log/LogTab.java index 8734582..33d0556 100755 --- a/org.tizen.common.connection/src/org/tizen/common/connection/log/LogTab.java +++ b/org.tizen.common.connection/src/org/tizen/common/connection/log/LogTab.java @@ -92,6 +92,9 @@ public class LogTab private UniversalDetector detector = new UniversalDetector(null); + // the selected encoding character that a log tab has + private EncodingChar selectedEncoding = EncodingChar.AUTO; + public LogFilter getLogFilter() { return logFilter; @@ -166,6 +169,16 @@ public class LogTab return table; } + public void setEncoding(EncodingChar encodingChar) + { + selectedEncoding = encodingChar; + } + + public EncodingChar getEncoding() + { + return selectedEncoding; + } + public void start() { LogLevel level = LogPanel.DEFAULT_LOG_LEVEL; @@ -203,7 +216,7 @@ public class LogTab try { - device.executeShellCommand(String.format(TizenPlatformConstants.DLOGUTIL_CMD, "-v long *:" + level.getLetter()), logReceiver, 0 /* timeout */); //$NON-NLS-1$ + device.executeShellCommand(String.format(TizenPlatformConstants.DLOGUTIL_CMD, "-v long *:" + level.getLetter()), logReceiver, 0 /* timeout */, SdbHelper.DEFAULT_ENCODING); //$NON-NLS-1$ } catch (Exception e) { logger.error("LogTab", e); @@ -233,15 +246,13 @@ public class LogTab * Stop current logger and start with new level It can be lost some log * messages in time interval between stop and restart * - * @param level - * start logger with level */ - public void restart(LogLevel level) + public void restart() { IDevice old = device; stop(); setDevice(old); - start(level); + start(); } /** @@ -418,7 +429,7 @@ public class LogTab try { // get bytes from string - lineBytes = line.getBytes(SdbHelper.UTF_CHARSET); + lineBytes = line.getBytes(SdbHelper.DEFAULT_ENCODING); // input byte data to detector detector.handleData(lineBytes, 0, lineBytes.length); @@ -443,6 +454,36 @@ public class LogTab } /** + * Get decoded line. + * + * @param line + * the encoded line + * @param encodingChar + * the encoding character + * @return the decoded line + */ + protected String getDecodedLineManual(String line, EncodingChar encodingChar) + { + byte[] lineBytes = null; + + // set default string value + String encodedString = line; + + try + { + // get bytes from string + lineBytes = line.getBytes(SdbHelper.DEFAULT_ENCODING); + + encodedString = new String(lineBytes, encodingChar.getName()); + + } catch (UnsupportedEncodingException e) + { + logger.error("encoding format is not supported", e.getMessage()); + } + return encodedString; + } + + /** * Process new Log lines coming from {@link LogTabOuputReceiver}. * * @param lines @@ -462,7 +503,11 @@ public class LogTab { for (String line : lines) { - line = getDecodedLine(line); + if(selectedEncoding == EncodingChar.AUTO) + line = getDecodedLine(line); + else + line = getDecodedLineManual(line, selectedEncoding); + line = AnsicodeAdapter.getStripAnsiString(line); // ignore empty line. diff --git a/org.tizen.common.connection/src/org/tizen/common/connection/log/LogView.java b/org.tizen.common.connection/src/org/tizen/common/connection/log/LogView.java index 4d5fc92..38bf248 100755 --- a/org.tizen.common.connection/src/org/tizen/common/connection/log/LogView.java +++ b/org.tizen.common.connection/src/org/tizen/common/connection/log/LogView.java @@ -25,22 +25,32 @@ */ package org.tizen.common.connection.log; +import java.util.ArrayList; + import org.eclipse.jface.action.Action; import org.eclipse.jface.action.IAction; +import org.eclipse.jface.action.IMenuCreator; import org.eclipse.jface.action.IMenuManager; import org.eclipse.jface.action.IToolBarManager; import org.eclipse.jface.action.Separator; +import org.eclipse.swt.SWT; import org.eclipse.swt.dnd.Clipboard; +import org.eclipse.swt.events.MenuEvent; +import org.eclipse.swt.events.MenuListener; +import org.eclipse.swt.events.SelectionEvent; +import org.eclipse.swt.events.SelectionListener; import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.Display; +import org.eclipse.swt.widgets.List; +import org.eclipse.swt.widgets.Menu; +import org.eclipse.swt.widgets.MenuItem; import org.eclipse.ui.IActionBars; import org.eclipse.ui.PlatformUI; import org.eclipse.ui.actions.ActionFactory; import org.eclipse.ui.part.ViewPart; import org.tizen.common.TizenHelpContextIds; import org.tizen.common.connection.ConnectionPlugin; -import org.tizen.common.connection.log.LogColors; -import org.tizen.common.connection.log.LogPanel; import org.tizen.sdblib.util.LogLevel; /** @@ -61,6 +71,7 @@ public final class LogView extends ViewPart private Action scrollLockAction; private Action exportAction; private Action clearAction; + private Action encodingAction; private Action[] viewActions; private Action[] levelFilterActions; @@ -76,6 +87,35 @@ public final class LogView extends ViewPart private Clipboard clipboard; private LogColors colors; + private LogTab currentLogTab; + + // encoding main menu + private Menu encodingListMenu; + + // menu items (each menu item express a character encoding) + private static ArrayList allEncodingMenuItems = new ArrayList(); + + // get the menu item matched with character encoding + public MenuItem findMatchedMenuItem(EncodingChar selectedEncodingChar) + { + for (MenuItem menuItem : allEncodingMenuItems) + { + if (((EncodingChar) menuItem.getData()).getName().equals(selectedEncodingChar.getName())) + { + return menuItem; + } + } + return null; + } + + // uncheck all menu items + public void uncheckAllMenuItem() + { + for (MenuItem menuItem : allEncodingMenuItems) + { + menuItem.setSelection(false); + } + } @Override public void createPartControl(Composite p) @@ -153,6 +193,145 @@ public final class LogView extends ViewPart clearAction.setToolTipText("Clear the log"); clearAction.setImageDescriptor(ConnectionPlugin.getImageDescriptorFromPlugin("icons/log/clear_log.png")); //$NON-NLS-1$ + encodingAction = new Action("Encoding", SWT.DROP_DOWN) + { + @Override + public void run() + { + } + }; + encodingAction.setToolTipText("Convert the log"); + + // register menu to action + encodingAction.setMenuCreator(new IMenuCreator() + { + @Override + public void dispose() + { + // TODO Auto-generated method stub + } + + @Override + public Menu getMenu(Control parent) + { + return null; + } + + @Override + public Menu getMenu(Menu parent) + { + encodingListMenu = new Menu(parent); + encodingListMenu.addMenuListener(new MenuListener() + { + public void menuShown(MenuEvent e) + { + LogTab selectedLogTab = logPanel.getSelectionLogTab(); + + if(selectedLogTab != null) + { + // check if the currently selected log tab was selected before when opening meun. + if(currentLogTab == null || (currentLogTab != selectedLogTab)) + { + // unckeck all encoding menu items if checked before. + uncheckAllMenuItem(); + + // find and check the encoding menu item. + MenuItem matchedMenuItem = findMatchedMenuItem(selectedLogTab.getEncoding()); + if (matchedMenuItem != null) + { + matchedMenuItem.setSelection(true); + } + currentLogTab = selectedLogTab; + } + } + } + + public void menuHidden(MenuEvent e) + { + } + }); + + // create auto encoding menu + MenuItem autoEncodingItem = new MenuItem(encodingListMenu, SWT.CHECK); + autoEncodingItem.setText("Auto"); + autoEncodingItem.setData(EncodingChar.AUTO); + allEncodingMenuItems.add(autoEncodingItem); + + autoEncodingItem.addSelectionListener(new SelectionListener() + { + @Override + public void widgetSelected(SelectionEvent e) + { + MenuItem menuItem = (MenuItem) e.getSource(); + setMenuItemtoLogTab(menuItem, currentLogTab); + } + + @Override + public void widgetDefaultSelected(SelectionEvent e) + { + // TODO Auto-generated method stub + } + + }); + + MenuItem manualEncodingItem = new MenuItem(encodingListMenu, SWT.CASCADE); + manualEncodingItem.setText("Manual"); + + // create manual encoding menu + Menu encodingListManualMenu = new Menu(encodingListMenu); + manualEncodingItem.setMenu(encodingListManualMenu); + + // create all encoding format menu items + Region[] regions = Region.values(); + MenuItem encodingRegionMenuItems[] = new MenuItem[regions.length]; + Menu encodingRegionMenus[] = new Menu[regions.length]; + for (int i = 0; i < regions.length; i++) + { + encodingRegionMenuItems[i] = new MenuItem(encodingListManualMenu, SWT.CASCADE); + encodingRegionMenuItems[i].setText(regions[i].getName()); + encodingRegionMenus[i] = new Menu(encodingListManualMenu); + encodingRegionMenuItems[i].setMenu(encodingRegionMenus[i]); + } + + // add all encoding formats + for (final EncodingChar encodingChar : EncodingChar.values()) + { + // skip auto encoding option + if (encodingChar == EncodingChar.AUTO) + continue; + + for (int i = 0; i < regions.length; i++) + { + if (encodingChar.getRegion().equals(regions[i])) + { + final MenuItem encodingItem = new MenuItem(encodingRegionMenus[i], SWT.CHECK); + encodingItem.setText(encodingChar.getNation() + " (" + encodingChar.getName() + ")"); + encodingItem.setData(encodingChar); + allEncodingMenuItems.add(encodingItem); + + encodingItem.addSelectionListener(new SelectionListener() + { + @Override + public void widgetSelected(SelectionEvent e) + { + MenuItem menuItem = (MenuItem) e.getSource(); + setMenuItemtoLogTab(menuItem, currentLogTab); + } + + @Override + public void widgetDefaultSelected(SelectionEvent e) + { + // TODO Auto-generated method stub + } + }); + break; + } + } + } + return encodingListMenu; + } + }); + final LogLevel[] levels = LogLevel.values(); levelFilterActions = new Action[logLevelIcons.length]; for (int i = 0; i < levelFilterActions.length; i++) @@ -190,7 +369,7 @@ public final class LogView extends ViewPart IActionBars actionBars = getViewSite().getActionBars(); // now create the log view logPanel = new LogPanel(colors, actionBars); - viewActions = new Action[] { addAction, removeAction, editAction, scrollLockAction, exportAction, clearAction }; + viewActions = new Action[] { addAction, removeAction, editAction, scrollLockAction, exportAction, clearAction, encodingAction}; logPanel.setActions(levelFilterActions, viewActions); placeActions(); logPanel.setEnableState(LogPanel.NO_TAB, null); @@ -218,6 +397,35 @@ public final class LogView extends ViewPart }); } + // set encoding format got by menu item to the currently selected log tab + public void setMenuItemtoLogTab(MenuItem menuItem, LogTab logTab) + { + if (menuItem == null || logTab == null) + { + return; + } + + EncodingChar selectedEncoding = logTab.getEncoding(); + if (menuItem.getData() == selectedEncoding) + { + return; + } + + // uncheck recently checked encoding format + MenuItem selectedMenuItem = findMatchedMenuItem(selectedEncoding); + if (selectedMenuItem != null) + selectedMenuItem.setSelection(false); + + // check encoding format + menuItem.setSelection(true); + + // set encoding format + logTab.setEncoding((EncodingChar) menuItem.getData()); + + // restart log tab + logTab.restart(); + } + @Override public void dispose() { @@ -253,6 +461,8 @@ public final class LogView extends ViewPart menuManager.add(new Separator()); menuManager.add(exportAction); menuManager.add(clearAction); + menuManager.add(new Separator()); + menuManager.add(encodingAction); // and then in the toolbar IToolBarManager toolBarManager = actionBars.getToolBarManager(); diff --git a/org.tizen.common.connection/test/src/org/tizen/common/connection/log/LogTabTest.java b/org.tizen.common.connection/test/src/org/tizen/common/connection/log/LogTabTest.java new file mode 100644 index 0000000..b7a7b6b --- /dev/null +++ b/org.tizen.common.connection/test/src/org/tizen/common/connection/log/LogTabTest.java @@ -0,0 +1,110 @@ +/* + * Common + * + * Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: + * Shingil Kang + * Kangho Kim + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * - S-Core Co., Ltd + * + */ +package org.tizen.common.connection.log; + +import static org.junit.Assert.assertEquals; + +import java.io.UnsupportedEncodingException; + +import org.junit.Test; +import org.tizen.common.connection.log.LogTab; +import org.tizen.sdblib.SdbHelper; + +/** + * LogTabTest + * + * Testcase for {@link LogTab} + * + * @author Shingil Kang{@literal } (S-Core) + */ +public class LogTabTest +{ + private LogTab logTab = new LogTab(); + + @Test + public void test_getDecodedLineManual() + { + final Language[] TEST_CASES = new Language[] + { + new Language(new byte[] {(byte) 0xB0, (byte) 0xA1}, EncodingChar.EUC_KR), + new Language(new byte[] {(byte) 0xA4, (byte) 0x40}, EncodingChar.BIG5), + new Language(new byte[] {(byte) 0x82, (byte) 0x9F, (byte) 0x92, (byte) 0xA0}, EncodingChar.SHIFT_JIS), + new Language(new byte[] {(byte) 0x80, (byte) 0x81, (byte) 0x82}, EncodingChar.IBM852), + new Language(new byte[] {(byte) 0xAA, (byte) 0xB5}, EncodingChar.TIS_620), + new Language(new byte[] {(byte) 0x65, (byte) 0xAE}, EncodingChar.X_MACROMAN) + }; + + for (final Language TEST_CASE : TEST_CASES) + { + byte[] byteValue = TEST_CASE.getByteValue(); + EncodingChar encodingChar = TEST_CASE.getEncoding(); + String defaultEncodedString = null; + + try + { + defaultEncodedString = new String(byteValue, SdbHelper.DEFAULT_ENCODING); + String encodedLine = logTab.getDecodedLineManual(defaultEncodedString, encodingChar); + + System.out.println("encoded line : " + encodedLine); + + byte[] encodedLineByte = encodedLine.getBytes(encodingChar.getName()); + + assertEquals(byteValue.length, encodedLineByte.length); + + for(int i = 0; i < byteValue.length; i++) + { + assertEquals(byteValue[i], encodedLineByte[i]); + } + + } catch (UnsupportedEncodingException e) + { + continue; + } + } + } + + class Language + { + private byte[] byteValue; + private EncodingChar encoding; + + Language(byte[] byteValue, EncodingChar encoding) + { + this.byteValue = byteValue; + this.encoding = encoding; + } + + public byte[] getByteValue() + { + return byteValue; + } + + public EncodingChar getEncoding() + { + return encoding; + } + } +} diff --git a/org.tizen.common.sdblib/src/org/tizen/sdblib/Device.java b/org.tizen.common.sdblib/src/org/tizen/sdblib/Device.java index a27459e..beffd55 100644 --- a/org.tizen.common.sdblib/src/org/tizen/sdblib/Device.java +++ b/org.tizen.common.sdblib/src/org/tizen/sdblib/Device.java @@ -456,6 +456,22 @@ implements IDevice } /* (non-Javadoc) + * @see org.tizen.sdblib.IDevice#executeShellCommand(java.lang.String, org.tizen.sdblib.IShellOutputReceiver, int, java.lang.String) + */ + public + void + executeShellCommand( + final String command, + final IShellOutputReceiver receiver, + final int maxTimeToOutputResponse, + final String characterSet + ) + throws IOException + { + execute( command ).with( MaxTimeToRespond, maxTimeToOutputResponse ).in( this, receiver, characterSet ); + } + + /* (non-Javadoc) * @see org.tizen.sdblib.IDevice#createForward(int, int) */ public diff --git a/org.tizen.common.sdblib/src/org/tizen/sdblib/IDevice.java b/org.tizen.common.sdblib/src/org/tizen/sdblib/IDevice.java index 19a0de8..4183c52 100755 --- a/org.tizen.common.sdblib/src/org/tizen/sdblib/IDevice.java +++ b/org.tizen.common.sdblib/src/org/tizen/sdblib/IDevice.java @@ -197,7 +197,30 @@ IDevice int maxTimeToOutputResponse ) throws IOException; - + + /** + * Executes a shell command on the device, and sends the result to a {@code receiver} + * + * @param command the shell command to execute + * @param receiver object to process result + * @param maxTimeToOutputResponse time for waiting + * @param characterSet the character set used to encode result bytes + * + * @throws TimeoutException if it timeout + * @throws SdbCommandRejectedException if sdb rejects the command + * @throws ShellCommandUnresponsiveException if the shell command doesn't send output for a given time. + * @throws IOException in case of I/O error on the connection. + * + */ + void + executeShellCommand( + String command, + IShellOutputReceiver receiver, + int maxTimeToOutputResponse, + String characterSet + ) + throws IOException; + /** * Creates a port forwarding between a local and a remote port. * diff --git a/org.tizen.common.sdblib/src/org/tizen/sdblib/SdbHelper.java b/org.tizen.common.sdblib/src/org/tizen/sdblib/SdbHelper.java index 778176a..02c6d0d 100755 --- a/org.tizen.common.sdblib/src/org/tizen/sdblib/SdbHelper.java +++ b/org.tizen.common.sdblib/src/org/tizen/sdblib/SdbHelper.java @@ -76,7 +76,7 @@ public class SdbHelper { { return str.getBytes( UTF_CHARSET ); } - + /** * convert bytes to string * @@ -92,7 +92,51 @@ public class SdbHelper { { return new String( bytes, UTF_CHARSET ); } - + + /** + * convert string to bytes + * + * @param str string to convert + * + * @param str characterSet character set used to convert + * + * @return bytes to be converted + * + * @throws UnsupportedEncodingException If decoding fail + */ + public static + byte[] + string2bytes( + final String str, + final String characterSet + ) + throws UnsupportedEncodingException + { + return str.getBytes( characterSet ); + } + + /** + * convert bytes to string + * + * @param bytes byte[] to convert + * + * @param str characterSet character set used to convert + * + * @return string to be converted + * + * @throws UnsupportedEncodingException If encoding fail + */ + public static + String + bytes2string( + final byte[] bytes, + final String characterSet + ) + throws UnsupportedEncodingException + { + return new String( bytes, characterSet ); + } + /** * Return {@code true} if subsequence is equal * diff --git a/org.tizen.common.sdblib/src/org/tizen/sdblib/command/Command.java b/org.tizen.common.sdblib/src/org/tizen/sdblib/command/Command.java index 54e694a..547856d 100755 --- a/org.tizen.common.sdblib/src/org/tizen/sdblib/command/Command.java +++ b/org.tizen.common.sdblib/src/org/tizen/sdblib/command/Command.java @@ -101,6 +101,22 @@ implements ICommand, ICommandWithArgument, ICommandWithArgument2 ) throws IOException { + in(device, receiver, SdbHelper.UTF_DECODING); + } + + /* (non-Javadoc) + * @see org.tizen.sdblib.command.ICommand#in(org.tizen.sdblib.IDevice, org.tizen.sdblib.IShellOutputReceiver, java.lang.String) + */ + @Override + public + void + in( + final IDevice device, + final IShellOutputReceiver receiver, + final String characterSet + ) + throws IOException + { notNull( receiver ); final SocketChannel channel = sdb.openChannel(); @@ -148,7 +164,7 @@ implements ICommand, ICommandWithArgument, ICommandWithArgument2 byteOut.write( buf.array(), buf.arrayOffset(), buf.position() ); try { - receiver.append( SdbHelper.bytes2string( byteOut.toByteArray() ) ); + receiver.append( SdbHelper.bytes2string( byteOut.toByteArray(), characterSet ) ); } catch (Exception e) { receiver.append( new String( byteOut.toByteArray() ) ); diff --git a/org.tizen.common.sdblib/src/org/tizen/sdblib/command/ICommand.java b/org.tizen.common.sdblib/src/org/tizen/sdblib/command/ICommand.java index 95dc644..51a3d55 100644 --- a/org.tizen.common.sdblib/src/org/tizen/sdblib/command/ICommand.java +++ b/org.tizen.common.sdblib/src/org/tizen/sdblib/command/ICommand.java @@ -58,7 +58,19 @@ ICommand * @throws IOException If command can't be executed in device */ void in( IDevice device, IShellOutputReceiver receiver ) throws IOException; - + + /** + * Execute in device and pass result to receiver + * + * @param device device to execute in + * @param receiver receiver to process result + * @param characterSet the character set used to encode result bytes + * + * @throws IOException If command can't be executed in device + + */ + void in( IDevice device, IShellOutputReceiver receiver, String characterSet ) throws IOException; + /** * Execute in device and check to match result *