parse original columns properly and handle spaces in column values

This commit is contained in:
Anton Keks 2016-01-12 00:06:08 +02:00
parent 234c2874dd
commit 4527cb3f54
3 changed files with 36 additions and 4 deletions

View File

@ -23,6 +23,7 @@ import java.util.Date;
import java.util.List;
import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
import static net.azib.ipscan.core.ScanningResult.ResultType.*;
import static net.azib.ipscan.util.IOUtils.closeQuietly;
import static net.azib.ipscan.util.InetAddressUtils.increment;
@ -115,6 +116,7 @@ public class TXTExporter extends AbstractExporter {
String startIP = null;
String endIP = null;
String lastLoadedIP = null;
List<String> columns = emptyList();
int ipIndex = 0, pingIndex = 1, portsIndex = 3;
@ -126,9 +128,11 @@ public class TXTExporter extends AbstractExporter {
String line;
while ((line = reader.readLine()) != null) {
String[] sp = line.split("\\s+");
String[] sp;
if (lookingForIndex < lookingFor.length) {
sp = line.split("\\s");
if (lookingFor[lookingForIndex].equals(sp[0])) {
if (lookingForIndex == 0) {
startIP = sp[1];
@ -136,15 +140,18 @@ public class TXTExporter extends AbstractExporter {
lookingForIndex++;
}
else if (lookingForIndex == 1) {
pingIndex = asList(sp).indexOf(Labels.getLabel(PingFetcher.ID));
portsIndex = asList(sp).indexOf(Labels.getLabel(PortsFetcher.ID));
sp = line.split("\\s{2,}");
columns = asList(sp);
pingIndex = columns.indexOf(Labels.getLabel(PingFetcher.ID));
portsIndex = columns.indexOf(Labels.getLabel(PortsFetcher.ID));
lookingForIndex++;
}
}
continue;
}
if (sp.length < 2) continue;
sp = line.split("\\s{2,}");
if (sp.length < columns.size()) continue;
InetAddress addr = InetAddress.getByName(sp[ipIndex]);
lastLoadedIP = sp[ipIndex];

View File

@ -82,4 +82,15 @@ public class TXTExporterTest extends AbstractExporterTestCase {
assertEquals(7, results.size());
verify(feederGUI).unserialize("192.168.0.19", "192.168.0.255");
}
@Test
public void importFromBrokenFile() throws Exception {
String file = getClass().getResource("import-broken.txt").getPath();
AbstractFeederGUI feederGUI = mock(AbstractFeederGUI.class);
List<ScanningResult> results = ((TXTExporter) exporter).importResults(file, feederGUI);
assertEquals(7, results.size());
verify(feederGUI).unserialize("192.168.0.19", "192.168.0.255");
}
}

View File

@ -0,0 +1,14 @@
Scanned 192.168.0.0 - 192.168.0.255
IP Ping Hostname Ports Comments MAC Vendor MAC Address
192.168.0.1 3 ms router.local 80 [n/a] CiscoSpv E4:48:C7:EE:28:C2
192.168.0.3 1 ms accesspoint.local 22,80 [n/a] Tp-LinkT 30:B5:C2:B9:B9:4E
192.168.0.2 0 ms nas.local 22,80,443 [n/a] Synology 00:11:32:0E:92:BC
192.168.0.13 0 ms aziber.local 22 [n/a] IntelCor 7C:7A:91:15:8C:6D
192.168.0.10 2 ms [n/a] [n/a] [n/a] Azurewav 6C:AD:F8:74:CB:0F
192.168.0.15 5 ms [n/a] [n/a] [n/a] IntekDig 30:EB:25:00:A0:F8
Blah blah
192.168.0.18 60 ms [n/a] [n/a] [n/a] LgElectr 34:FC:EF:DC:29:2B