Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@
<artifactId>jaxb-impl</artifactId>
<version>2.4.0-b180830.0438</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.18.0</version>
</dependency>
</dependencies>


Expand Down
27 changes: 23 additions & 4 deletions src/main/java/com/github/canbabel/canio/dbc/DbcReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,14 @@ private static Signal findSignal(List<Message> 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)
Expand All @@ -143,7 +151,7 @@ private static Signal findSignal(List<Message> 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) {
Expand All @@ -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);
Expand All @@ -162,15 +172,19 @@ 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");
}

StringBuilder contents = new StringBuilder();
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;

Expand Down Expand Up @@ -1211,4 +1225,9 @@ public static int bigEndianLeastSignificantBitOffset(int msb, int length) {

return lsb;
}

public Bus getBus() {
return bus;
}

}
4 changes: 2 additions & 2 deletions src/main/java/com/github/canbabel/canio/ui/MainFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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 */
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/com/github/canbabel/canio/dbc/CANFDTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
63 changes: 63 additions & 0 deletions src/test/java/com/github/canbabel/canio/dbc/DbcReadStringTest.java
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
**/
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));
}
}

}