@Test void mapSimpleTableMissingValuesInFront() { String raw = "Port Name Status Vlan Duplex Speed Type\n" + " connected routed a-full a-100 10/100/1000BaseTX"; String[] colNames = {"Port", "Name", "Status", "Vlan", "Duplex", "Speed", "Type"}; String[] mapNames = {"interface", "name", "status", "vlan", "duplex", "speed", "type"}; Map<String, String> expected = Map.of("interface", "", "name", "", "status", "connected", "vlan", "routed", "duplex", "a-full", "speed", "a-100", "type", "10/100/1000BaseTX"); Map<String, String> response = BaseSwitchController.mapSimpleTable(raw, colNames, mapNames); for (String key : response.keySet()) { assertEquals(expected.get(key), response.get(key)); } }
protected static HashMap<String, String> mapSimpleTable( String rawPacket, String[] colNames, String[] mapNames) { HashMap<String, String> colMap = new HashMap<>(); String[] lines = rawPacket.split("\n"); int headerLine = 0; if (lines.length >= headerLine + 2) { String header = lines[headerLine].trim(); String values = lines[headerLine + 1].trim(); int lastSectionEnd = 0; for (int i = 0; i < colNames.length; i++) { int secStart = lastSectionEnd; int secEnd; if ((i + 1) >= colNames.length) { secEnd = values.length(); } else { int nextHeaderStart = header.indexOf(colNames[i + 1]); if (Character.isWhitespace(values.charAt(lastSectionEnd)) && lastSectionEnd < nextHeaderStart) { lastSectionEnd++; } int firstWhiteSpace = getFirstWhiteSpace(values.substring(lastSectionEnd)) + lastSectionEnd; if (firstWhiteSpace < 0) { headerLine++; break; } int lastWhiteSpace = getIndexOfNonWhitespaceAfterWhitespace(values.substring(firstWhiteSpace)) + firstWhiteSpace; if (nextHeaderStart >= firstWhiteSpace && nextHeaderStart <= lastWhiteSpace) { secEnd = nextHeaderStart; } else { char beforeHead = values.charAt(nextHeaderStart - 1); if (Character.isWhitespace(beforeHead)) { secEnd = Math.max(lastWhiteSpace, nextHeaderStart); } else { secEnd = lastWhiteSpace; } } } lastSectionEnd = secEnd; String rawString = values.substring(secStart, secEnd) .replace('\u00A0', ' ').trim(); colMap.put(mapNames[i], rawString); } } return colMap; }
BaseSwitchController implements SwitchController { protected static HashMap<String, String> mapSimpleTable( String rawPacket, String[] colNames, String[] mapNames) { HashMap<String, String> colMap = new HashMap<>(); String[] lines = rawPacket.split("\n"); int headerLine = 0; if (lines.length >= headerLine + 2) { String header = lines[headerLine].trim(); String values = lines[headerLine + 1].trim(); int lastSectionEnd = 0; for (int i = 0; i < colNames.length; i++) { int secStart = lastSectionEnd; int secEnd; if ((i + 1) >= colNames.length) { secEnd = values.length(); } else { int nextHeaderStart = header.indexOf(colNames[i + 1]); if (Character.isWhitespace(values.charAt(lastSectionEnd)) && lastSectionEnd < nextHeaderStart) { lastSectionEnd++; } int firstWhiteSpace = getFirstWhiteSpace(values.substring(lastSectionEnd)) + lastSectionEnd; if (firstWhiteSpace < 0) { headerLine++; break; } int lastWhiteSpace = getIndexOfNonWhitespaceAfterWhitespace(values.substring(firstWhiteSpace)) + firstWhiteSpace; if (nextHeaderStart >= firstWhiteSpace && nextHeaderStart <= lastWhiteSpace) { secEnd = nextHeaderStart; } else { char beforeHead = values.charAt(nextHeaderStart - 1); if (Character.isWhitespace(beforeHead)) { secEnd = Math.max(lastWhiteSpace, nextHeaderStart); } else { secEnd = lastWhiteSpace; } } } lastSectionEnd = secEnd; String rawString = values.substring(secStart, secEnd) .replace('\u00A0', ' ').trim(); colMap.put(mapNames[i], rawString); } } return colMap; } }
BaseSwitchController implements SwitchController { protected static HashMap<String, String> mapSimpleTable( String rawPacket, String[] colNames, String[] mapNames) { HashMap<String, String> colMap = new HashMap<>(); String[] lines = rawPacket.split("\n"); int headerLine = 0; if (lines.length >= headerLine + 2) { String header = lines[headerLine].trim(); String values = lines[headerLine + 1].trim(); int lastSectionEnd = 0; for (int i = 0; i < colNames.length; i++) { int secStart = lastSectionEnd; int secEnd; if ((i + 1) >= colNames.length) { secEnd = values.length(); } else { int nextHeaderStart = header.indexOf(colNames[i + 1]); if (Character.isWhitespace(values.charAt(lastSectionEnd)) && lastSectionEnd < nextHeaderStart) { lastSectionEnd++; } int firstWhiteSpace = getFirstWhiteSpace(values.substring(lastSectionEnd)) + lastSectionEnd; if (firstWhiteSpace < 0) { headerLine++; break; } int lastWhiteSpace = getIndexOfNonWhitespaceAfterWhitespace(values.substring(firstWhiteSpace)) + firstWhiteSpace; if (nextHeaderStart >= firstWhiteSpace && nextHeaderStart <= lastWhiteSpace) { secEnd = nextHeaderStart; } else { char beforeHead = values.charAt(nextHeaderStart - 1); if (Character.isWhitespace(beforeHead)) { secEnd = Math.max(lastWhiteSpace, nextHeaderStart); } else { secEnd = lastWhiteSpace; } } } lastSectionEnd = secEnd; String rawString = values.substring(secStart, secEnd) .replace('\u00A0', ' ').trim(); colMap.put(mapNames[i], rawString); } } return colMap; } BaseSwitchController(String remoteIpAddress, String username,
                              String password);  BaseSwitchController(
      String remoteIpAddress, String username, String password, boolean debug); }
BaseSwitchController implements SwitchController { protected static HashMap<String, String> mapSimpleTable( String rawPacket, String[] colNames, String[] mapNames) { HashMap<String, String> colMap = new HashMap<>(); String[] lines = rawPacket.split("\n"); int headerLine = 0; if (lines.length >= headerLine + 2) { String header = lines[headerLine].trim(); String values = lines[headerLine + 1].trim(); int lastSectionEnd = 0; for (int i = 0; i < colNames.length; i++) { int secStart = lastSectionEnd; int secEnd; if ((i + 1) >= colNames.length) { secEnd = values.length(); } else { int nextHeaderStart = header.indexOf(colNames[i + 1]); if (Character.isWhitespace(values.charAt(lastSectionEnd)) && lastSectionEnd < nextHeaderStart) { lastSectionEnd++; } int firstWhiteSpace = getFirstWhiteSpace(values.substring(lastSectionEnd)) + lastSectionEnd; if (firstWhiteSpace < 0) { headerLine++; break; } int lastWhiteSpace = getIndexOfNonWhitespaceAfterWhitespace(values.substring(firstWhiteSpace)) + firstWhiteSpace; if (nextHeaderStart >= firstWhiteSpace && nextHeaderStart <= lastWhiteSpace) { secEnd = nextHeaderStart; } else { char beforeHead = values.charAt(nextHeaderStart - 1); if (Character.isWhitespace(beforeHead)) { secEnd = Math.max(lastWhiteSpace, nextHeaderStart); } else { secEnd = lastWhiteSpace; } } } lastSectionEnd = secEnd; String rawString = values.substring(secStart, secEnd) .replace('\u00A0', ' ').trim(); colMap.put(mapNames[i], rawString); } } return colMap; } BaseSwitchController(String remoteIpAddress, String username,
                              String password);  BaseSwitchController(
      String remoteIpAddress, String username, String password, boolean debug); void receiveData(String consoleData); @Override void start(); }
BaseSwitchController implements SwitchController { protected static HashMap<String, String> mapSimpleTable( String rawPacket, String[] colNames, String[] mapNames) { HashMap<String, String> colMap = new HashMap<>(); String[] lines = rawPacket.split("\n"); int headerLine = 0; if (lines.length >= headerLine + 2) { String header = lines[headerLine].trim(); String values = lines[headerLine + 1].trim(); int lastSectionEnd = 0; for (int i = 0; i < colNames.length; i++) { int secStart = lastSectionEnd; int secEnd; if ((i + 1) >= colNames.length) { secEnd = values.length(); } else { int nextHeaderStart = header.indexOf(colNames[i + 1]); if (Character.isWhitespace(values.charAt(lastSectionEnd)) && lastSectionEnd < nextHeaderStart) { lastSectionEnd++; } int firstWhiteSpace = getFirstWhiteSpace(values.substring(lastSectionEnd)) + lastSectionEnd; if (firstWhiteSpace < 0) { headerLine++; break; } int lastWhiteSpace = getIndexOfNonWhitespaceAfterWhitespace(values.substring(firstWhiteSpace)) + firstWhiteSpace; if (nextHeaderStart >= firstWhiteSpace && nextHeaderStart <= lastWhiteSpace) { secEnd = nextHeaderStart; } else { char beforeHead = values.charAt(nextHeaderStart - 1); if (Character.isWhitespace(beforeHead)) { secEnd = Math.max(lastWhiteSpace, nextHeaderStart); } else { secEnd = lastWhiteSpace; } } } lastSectionEnd = secEnd; String rawString = values.substring(secStart, secEnd) .replace('\u00A0', ' ').trim(); colMap.put(mapNames[i], rawString); } } return colMap; } BaseSwitchController(String remoteIpAddress, String username,
                              String password);  BaseSwitchController(
      String remoteIpAddress, String username, String password, boolean debug); void receiveData(String consoleData); @Override void start(); static final String CONSOLE_PROMPT_ENDING_ENABLED; static final String CONSOLE_PROMPT_ENDING_LOGIN; static final int TELNET_PORT; }