diff --git a/pom.xml b/pom.xml index c8984ea..abb0fa4 100644 --- a/pom.xml +++ b/pom.xml @@ -32,6 +32,11 @@ jaxb-impl 2.4.0-b180830.0438 + + commons-io + commons-io + 2.18.0 + diff --git a/src/main/java/com/github/canbabel/canio/dbc/DbcReader.java b/src/main/java/com/github/canbabel/canio/dbc/DbcReader.java index 378cf63..bfab1b0 100755 --- a/src/main/java/com/github/canbabel/canio/dbc/DbcReader.java +++ b/src/main/java/com/github/canbabel/canio/dbc/DbcReader.java @@ -135,6 +135,14 @@ private static Signal findSignal(List messages, long id, boolean e, Str } return null; } + + public BufferedReader getFileReader(File file) throws UnsupportedEncodingException, FileNotFoundException { + return new BufferedReader(new InputStreamReader(new FileInputStream(file), "ISO-8859-1")); + } + + public boolean parseString(String input) { + return parseFile(null, System.out, new BufferedReader(new StringReader(input))); + } /** * Read in given CAN database file (*.dbc) @@ -143,7 +151,7 @@ private static Signal findSignal(List messages, long id, boolean e, Str * @param logStream OutputStream to write out stack traces * @return true, if file has been successfully read. */ - public boolean parseFile(File file, OutputStream logStream) { + public boolean parseFile(File file, OutputStream logStream, BufferedReader bufferedReader) { try { logWriter = new PrintWriter(new BufferedWriter(new OutputStreamWriter(logStream, "ISO-8859-1")), true); } catch (UnsupportedEncodingException ex) { @@ -153,7 +161,9 @@ public boolean parseFile(File file, OutputStream logStream) { network = (NetworkDefinition) (factory.createNetworkDefinition()); document = (Document) (factory.createDocument()); document.setContent(DOC_CONTENT); - document.setName(file.getName()); + if (file != null) { + document.setName(file.getName()); + } Date now = Calendar.getInstance().getTime(); document.setDate(now.toString()); network.setDocument(document); @@ -162,7 +172,7 @@ public boolean parseFile(File file, OutputStream logStream) { bus.setName("Private"); - if (!(file.canRead() && file.exists())) { + if (!(file != null && file.canRead() && file.exists()) && bufferedReader == null) { throw new RuntimeException("could not open file"); } @@ -170,7 +180,11 @@ public boolean parseFile(File file, OutputStream logStream) { BufferedReader reader = null; try { - reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "ISO-8859-1")); + if (bufferedReader != null) { + reader = bufferedReader; + } else { + reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "ISO-8859-1")); + } String text; boolean isFirstLine = true; @@ -1211,4 +1225,9 @@ public static int bigEndianLeastSignificantBitOffset(int msb, int length) { return lsb; } + + public Bus getBus() { + return bus; + } + } diff --git a/src/main/java/com/github/canbabel/canio/ui/MainFrame.java b/src/main/java/com/github/canbabel/canio/ui/MainFrame.java index 2dea578..3677cc9 100644 --- a/src/main/java/com/github/canbabel/canio/ui/MainFrame.java +++ b/src/main/java/com/github/canbabel/canio/ui/MainFrame.java @@ -409,7 +409,7 @@ private static void startCmdLine(String dbc, String kcd) { if (dbcfile.canRead()) { DbcReader reader = new DbcReader(); - if (reader.parseFile(dbcfile, System.out)) { + if (reader.parseFile(dbcfile, System.out, null)) { reader.writeKcdFile(kcdfile, true, false); /* Validate the result */ @@ -520,7 +520,7 @@ public void run() { try { DbcReader reader = new DbcReader(); reader.omitUnconsumedSignals(uselessCheckbox.isSelected()); - if (reader.parseFile(f, logOutput)) { + if (reader.parseFile(f, logOutput, null)) { reader.writeKcdFile(newFile, prettyprintCheckbox.isSelected(), gzippedCheckbox.isSelected()); /* Validate the result */ diff --git a/src/test/java/com/github/canbabel/canio/dbc/CANFDTest.java b/src/test/java/com/github/canbabel/canio/dbc/CANFDTest.java index fe5f1b5..89c2612 100644 --- a/src/test/java/com/github/canbabel/canio/dbc/CANFDTest.java +++ b/src/test/java/com/github/canbabel/canio/dbc/CANFDTest.java @@ -23,7 +23,7 @@ public void setUp() { dr = new DbcReader(); URL url = Thread.currentThread().getContextClassLoader().getResource("canfdtest.dbc"); File testFile = new File(url.getPath()); - dr.parseFile(testFile, System.out); + dr.parseFile(testFile, System.out, null); } /** diff --git a/src/test/java/com/github/canbabel/canio/dbc/DbcReadFileTest.java b/src/test/java/com/github/canbabel/canio/dbc/DbcReadFileTest.java index cdb7c3f..ba12060 100644 --- a/src/test/java/com/github/canbabel/canio/dbc/DbcReadFileTest.java +++ b/src/test/java/com/github/canbabel/canio/dbc/DbcReadFileTest.java @@ -74,7 +74,7 @@ public void readAndValidateTest(){ String kcd = filename.replaceAll("dbc", "kcd"); File fkcd = new File(kcd); - if (reader.parseFile(fdbc, System.out)) { + if (reader.parseFile(fdbc, System.out, null)) { reader.writeKcdFile(fkcd, true, false); StreamSource source = new StreamSource(fkcd); diff --git a/src/test/java/com/github/canbabel/canio/dbc/DbcReadStringTest.java b/src/test/java/com/github/canbabel/canio/dbc/DbcReadStringTest.java new file mode 100644 index 0000000..c12b045 --- /dev/null +++ b/src/test/java/com/github/canbabel/canio/dbc/DbcReadStringTest.java @@ -0,0 +1,63 @@ +/** + * CANBabel - Translator for Controller Area Network description formats + * Copyright (C) 2011-2025 julietkilo and Jan-Niklas Meier + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + **/ +package com.github.canbabel.canio.dbc; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.io.File; +import java.io.IOException; +import java.net.URL; +import java.nio.charset.StandardCharsets; + +import javax.xml.transform.stream.StreamSource; + +import org.apache.commons.io.FileUtils; +import org.junit.Before; +import org.junit.Test; + +import com.github.canbabel.canio.ui.SchemaValidator; + +public class DbcReadStringTest { + + private File testFile = null; + + @Before + public void setUp() { + URL url = Thread.currentThread().getContextClassLoader().getResource("read_in_test.dbc"); + testFile = new File(url.getPath()); + } + + @Test + public void readAndValidateTest() throws IOException{ + DbcReader reader = new DbcReader(); + File readInTestKcdFile = new File("read_in_test.kcd"); + + String testFileContent = FileUtils.readFileToString(testFile, StandardCharsets.UTF_8); + + if (reader.parseString(testFileContent)) { + assertNotNull(reader.getBus()); + + StreamSource source = new StreamSource(readInTestKcdFile); + SchemaValidator validator = new SchemaValidator(System.out); + + assertTrue(validator.validate(source)); + } + } + +}