From 03c0f0dc264f6c2c34b9e5d44048d6e02b1498fe Mon Sep 17 00:00:00 2001 From: Cosmin Radu Date: Tue, 2 Jul 2024 12:28:43 +0300 Subject: [PATCH 1/4] #33600 - parseString --- .../github/canbabel/canio/dbc/DbcReader.java | 118 +++++++++++++----- .../github/canbabel/canio/ui/MainFrame.java | 24 ++-- .../github/canbabel/canio/dbc/CANFDTest.java | 16 +-- .../canbabel/canio/dbc/DbcReaderTest.java | 23 ++-- 4 files changed, 126 insertions(+), 55 deletions(-) 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..09048be 100755 --- a/src/main/java/com/github/canbabel/canio/dbc/DbcReader.java +++ b/src/main/java/com/github/canbabel/canio/dbc/DbcReader.java @@ -17,20 +17,60 @@ **/ package com.github.canbabel.canio.dbc; -import com.github.canbabel.canio.dbc.AttributeDefinition.AttrType; -import com.github.canbabel.canio.kcd.*; - -import javax.xml.bind.JAXBContext; -import javax.xml.bind.JAXBException; -import javax.xml.bind.Marshaller; -import java.io.*; +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.io.PrintWriter; +import java.io.StringReader; +import java.io.UnsupportedEncodingException; +import java.io.Writer; import java.math.BigInteger; -import java.util.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Calendar; +import java.util.Collection; +import java.util.Date; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.TreeMap; +import java.util.TreeSet; import java.util.logging.Level; import java.util.logging.Logger; import java.util.regex.Pattern; import java.util.zip.GZIPOutputStream; +import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBException; +import javax.xml.bind.Marshaller; + +import com.github.canbabel.canio.dbc.AttributeDefinition.AttrType; +import com.github.canbabel.canio.kcd.BasicLabelType; +import com.github.canbabel.canio.kcd.Bus; +import com.github.canbabel.canio.kcd.Consumer; +import com.github.canbabel.canio.kcd.Document; +import com.github.canbabel.canio.kcd.Label; +import com.github.canbabel.canio.kcd.LabelSet; +import com.github.canbabel.canio.kcd.Message; +import com.github.canbabel.canio.kcd.Multiplex; +import com.github.canbabel.canio.kcd.MuxGroup; +import com.github.canbabel.canio.kcd.NetworkDefinition; +import com.github.canbabel.canio.kcd.Node; +import com.github.canbabel.canio.kcd.NodeRef; +import com.github.canbabel.canio.kcd.ObjectFactory; +import com.github.canbabel.canio.kcd.Producer; +import com.github.canbabel.canio.kcd.Signal; +import com.github.canbabel.canio.kcd.Value; + /** * Reads industry widespread CAN database (*.dbc) format. * @@ -135,6 +175,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,26 +191,28 @@ 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) { Logger.getLogger(DbcReader.class.getName()).log(Level.FINE, null, ex); } factory = new ObjectFactory(); - network = (NetworkDefinition) (factory.createNetworkDefinition()); - document = (Document) (factory.createDocument()); + network = (factory.createNetworkDefinition()); + 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); - bus = (Bus) (factory.createBus()); + bus = (factory.createBus()); 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,8 +220,12 @@ public boolean parseFile(File file, OutputStream logStream) { BufferedReader reader = null; try { - reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "ISO-8859-1")); - String text; + if (bufferedReader != null) { + reader = bufferedReader; + } else { + reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "ISO-8859-1")); + } + String text; boolean isFirstLine = true; while ((text = reader.readLine()) != null) { @@ -365,8 +419,9 @@ public boolean writeKcdFile(File file, boolean prettyPrint, boolean gzip) { return false; } finally { try { - if (w != null) - w.close(); + if (w != null) { + w.close(); + } } catch (IOException e) { e.printStackTrace(logWriter); } @@ -477,7 +532,7 @@ private void parseNetworkNode(StringBuilder line) { nodes = Arrays.asList(lineArray); for (String nodeString : nodes) { - Node node = (Node) factory.createNode(); + Node node = factory.createNode(); node.setId(nodeString); node.setName(nodeString); network.getNode().add(node); @@ -706,7 +761,7 @@ private void parseMessageDefinition(StringBuilder line) { String[] lineArray = line.toString().split("\\s*SG_\\s+"); String[] messageArray = lineArray[0].split("\\s+"); - Message message = (Message) factory.createMessage(); + Message message = factory.createMessage(); int messageIdDecimal = getCanIdFromString(messageArray[0]); message.setId("0x" + Integer.toString(messageIdDecimal, 16).toUpperCase()); @@ -717,8 +772,8 @@ private void parseMessageDefinition(StringBuilder line) { message.setName(messageArray[1].replace(":", "")); message.setLength(messageArray[2]); if (!messageArray[3].contains(NOT_DEFINED)) { - Producer producer = (Producer) factory.createProducer(); - NodeRef ref = (NodeRef) factory.createNodeRef(); + Producer producer = factory.createProducer(); + NodeRef ref = factory.createNodeRef(); ref.setId(messageArray[3]); producer.getNodeRef().add(ref); message.setProducer(producer); @@ -792,7 +847,7 @@ public static boolean isExtendedFrameFormat(String canIdStr) { private enum SignalType { MULTIPLEXOR, MULTIPLEX, PLAIN - }; + } /** * Parses a the part of a signal line that is same for plain, multiplexor or @@ -809,7 +864,7 @@ private enum SignalType { private Signal parseSignalLine(Message message, String signalName, SignalType type, String line) { Value value = null; - Signal tSignal = (Signal) factory.createSignal(); + Signal tSignal = factory.createSignal(); tSignal.setName(signalName); String[] splitted = splitString(line); @@ -844,9 +899,9 @@ private Signal parseSignalLine(Message message, String signalName, SignalType ty // omit entry with NOT_DEFINED consumers if (sConsumers.length != 1 || !NOT_DEFINED.equals(sConsumers[0])) { - Consumer consumer = (Consumer) factory.createConsumer(); + Consumer consumer = factory.createConsumer(); for (String sConsumer : sConsumers) { - NodeRef ref = (NodeRef) factory.createNodeRef(); + NodeRef ref = factory.createNodeRef(); consumer.getNodeRef().add(ref); ref.setId(sConsumer); } @@ -856,7 +911,7 @@ private Signal parseSignalLine(Message message, String signalName, SignalType ty if ((intercept != 0.0) || (slope != 1.0) || !"".equals(splitted[8]) || "-".equals(splitted[3]) || (min != 0.0) || (max != 1.0)) { - value = (Value) factory.createValue(); + value = factory.createValue(); if ("-".equals(splitted[3])) { value.setType("signed"); @@ -896,7 +951,7 @@ private Signal parseSignalLine(Message message, String signalName, SignalType ty if (type == SignalType.MULTIPLEXOR) { - Multiplex mux = (Multiplex) factory.createMultiplex(); + Multiplex mux = factory.createMultiplex(); mux.setName(tSignal.getName()); mux.setOffset(tSignal.getOffset()); mux.setConsumer(tSignal.getConsumer()); @@ -911,7 +966,7 @@ private Signal parseSignalLine(Message message, String signalName, SignalType ty return null; } else { - Signal signal = (Signal) factory.createSignal(); + Signal signal = factory.createSignal(); signal.setName(tSignal.getName()); signal.setOffset(tSignal.getOffset()); signal.setConsumer(tSignal.getConsumer()); @@ -1211,4 +1266,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..cd8bae6 100644 --- a/src/main/java/com/github/canbabel/canio/ui/MainFrame.java +++ b/src/main/java/com/github/canbabel/canio/ui/MainFrame.java @@ -17,8 +17,6 @@ **/ package com.github.canbabel.canio.ui; -// TODO Version number(major.minor.build) -import com.github.canbabel.canio.dbc.DbcReader; import java.io.File; import java.io.FileFilter; import java.io.FileInputStream; @@ -28,12 +26,16 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import java.util.prefs.*; +import java.util.prefs.Preferences; import java.util.zip.GZIPInputStream; + import javax.swing.JFileChooser; import javax.swing.JOptionPane; import javax.xml.transform.stream.StreamSource; +// TODO Version number(major.minor.build) +import com.github.canbabel.canio.dbc.DbcReader; + /** * User interface * @@ -140,7 +142,8 @@ private void initComponents() { convertButton.setText("Convert"); convertButton.addActionListener(new java.awt.event.ActionListener() { - public void actionPerformed(java.awt.event.ActionEvent evt) { + @Override + public void actionPerformed(java.awt.event.ActionEvent evt) { convertButtonActionPerformed(evt); } }); @@ -217,7 +220,8 @@ public void actionPerformed(java.awt.event.ActionEvent evt) { addFilesOrFoldersButton.setText("Add files or folders"); addFilesOrFoldersButton.addActionListener(new java.awt.event.ActionListener() { - public void actionPerformed(java.awt.event.ActionEvent evt) { + @Override + public void actionPerformed(java.awt.event.ActionEvent evt) { addFilesOrFoldersButtonActionPerformed(evt); } }); @@ -243,7 +247,8 @@ public void actionPerformed(java.awt.event.ActionEvent evt) { removeButton.setText("Remove"); removeButton.addActionListener(new java.awt.event.ActionListener() { - public void actionPerformed(java.awt.event.ActionEvent evt) { + @Override + public void actionPerformed(java.awt.event.ActionEvent evt) { removeButtonActionPerformed(evt); } }); @@ -283,7 +288,8 @@ public void actionPerformed(java.awt.event.ActionEvent evt) { closeButton.setMaximumSize(new java.awt.Dimension(68, 30)); closeButton.setMinimumSize(new java.awt.Dimension(68, 30)); closeButton.addActionListener(new java.awt.event.ActionListener() { - public void actionPerformed(java.awt.event.ActionEvent evt) { + @Override + public void actionPerformed(java.awt.event.ActionEvent evt) { closeButtonHandler(evt); } }); @@ -409,7 +415,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 +526,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..efb425e 100644 --- a/src/test/java/com/github/canbabel/canio/dbc/CANFDTest.java +++ b/src/test/java/com/github/canbabel/canio/dbc/CANFDTest.java @@ -1,16 +1,18 @@ package com.github.canbabel.canio.dbc; -import org.junit.*; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import java.io.File; +import java.net.URL; + +import org.junit.Before; +import org.junit.Test; import com.github.canbabel.canio.kcd.Bus; import com.github.canbabel.canio.kcd.Message; import com.github.canbabel.canio.kcd.NetworkDefinition; -import static org.junit.Assert.*; - -import java.io.File; -import java.net.URL; - public class CANFDTest { DbcReader dr = null; @@ -23,7 +25,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/DbcReaderTest.java b/src/test/java/com/github/canbabel/canio/dbc/DbcReaderTest.java index 099f759..5acc3cc 100644 --- a/src/test/java/com/github/canbabel/canio/dbc/DbcReaderTest.java +++ b/src/test/java/com/github/canbabel/canio/dbc/DbcReaderTest.java @@ -17,19 +17,22 @@ **/ package com.github.canbabel.canio.dbc; -import java.io.*; -import java.net.URL; -import java.util.zip.GZIPInputStream; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; -import com.github.canbabel.canio.ui.SchemaValidator; -import org.junit.*; +import java.io.File; +import java.net.URL; -import javax.xml.bind.JAXBContext; -import javax.xml.bind.Marshaller; -import javax.xml.bind.Unmarshaller; import javax.xml.transform.stream.StreamSource; -import static org.junit.Assert.*; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import com.github.canbabel.canio.ui.SchemaValidator; /** * @author Jan-Niklas Meier < dschanoeh@googlemail.com > @@ -153,7 +156,7 @@ public void testParse() { DbcReader reader = new DbcReader(); File readInTestKcdFile = new File("read_in_test.kcd"); - if (reader.parseFile(testFile, System.out)) { + if (reader.parseFile(testFile, System.out, null)) { reader.writeKcdFile(readInTestKcdFile, true, false); StreamSource source = new StreamSource(readInTestKcdFile); From e165fb709e92ece8454db5747daa4dfd0360b6b4 Mon Sep 17 00:00:00 2001 From: Cosmin Radu Date: Wed, 8 Jan 2025 16:57:32 +0200 Subject: [PATCH 2/4] #36139 - remove unnecessary changes --- .../github/canbabel/canio/dbc/DbcReader.java | 97 ++++++------------- .../github/canbabel/canio/ui/MainFrame.java | 20 ++-- .../github/canbabel/canio/dbc/CANFDTest.java | 14 ++- .../canbabel/canio/dbc/DbcReaderTest.java | 21 ++-- 4 files changed, 50 insertions(+), 102 deletions(-) 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 09048be..bfab1b0 100755 --- a/src/main/java/com/github/canbabel/canio/dbc/DbcReader.java +++ b/src/main/java/com/github/canbabel/canio/dbc/DbcReader.java @@ -17,60 +17,20 @@ **/ package com.github.canbabel.canio.dbc; -import java.io.BufferedReader; -import java.io.BufferedWriter; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStreamReader; -import java.io.OutputStream; -import java.io.OutputStreamWriter; -import java.io.PrintWriter; -import java.io.StringReader; -import java.io.UnsupportedEncodingException; -import java.io.Writer; +import com.github.canbabel.canio.dbc.AttributeDefinition.AttrType; +import com.github.canbabel.canio.kcd.*; + +import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBException; +import javax.xml.bind.Marshaller; +import java.io.*; import java.math.BigInteger; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Calendar; -import java.util.Collection; -import java.util.Date; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.TreeMap; -import java.util.TreeSet; +import java.util.*; import java.util.logging.Level; import java.util.logging.Logger; import java.util.regex.Pattern; import java.util.zip.GZIPOutputStream; -import javax.xml.bind.JAXBContext; -import javax.xml.bind.JAXBException; -import javax.xml.bind.Marshaller; - -import com.github.canbabel.canio.dbc.AttributeDefinition.AttrType; -import com.github.canbabel.canio.kcd.BasicLabelType; -import com.github.canbabel.canio.kcd.Bus; -import com.github.canbabel.canio.kcd.Consumer; -import com.github.canbabel.canio.kcd.Document; -import com.github.canbabel.canio.kcd.Label; -import com.github.canbabel.canio.kcd.LabelSet; -import com.github.canbabel.canio.kcd.Message; -import com.github.canbabel.canio.kcd.Multiplex; -import com.github.canbabel.canio.kcd.MuxGroup; -import com.github.canbabel.canio.kcd.NetworkDefinition; -import com.github.canbabel.canio.kcd.Node; -import com.github.canbabel.canio.kcd.NodeRef; -import com.github.canbabel.canio.kcd.ObjectFactory; -import com.github.canbabel.canio.kcd.Producer; -import com.github.canbabel.canio.kcd.Signal; -import com.github.canbabel.canio.kcd.Value; - /** * Reads industry widespread CAN database (*.dbc) format. * @@ -179,7 +139,7 @@ private static Signal findSignal(List messages, long id, boolean e, Str 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))); } @@ -198,8 +158,8 @@ public boolean parseFile(File file, OutputStream logStream, BufferedReader buffe Logger.getLogger(DbcReader.class.getName()).log(Level.FINE, null, ex); } factory = new ObjectFactory(); - network = (factory.createNetworkDefinition()); - document = (factory.createDocument()); + network = (NetworkDefinition) (factory.createNetworkDefinition()); + document = (Document) (factory.createDocument()); document.setContent(DOC_CONTENT); if (file != null) { document.setName(file.getName()); @@ -208,7 +168,7 @@ public boolean parseFile(File file, OutputStream logStream, BufferedReader buffe document.setDate(now.toString()); network.setDocument(document); - bus = (factory.createBus()); + bus = (Bus) (factory.createBus()); bus.setName("Private"); @@ -225,7 +185,7 @@ public boolean parseFile(File file, OutputStream logStream, BufferedReader buffe } else { reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "ISO-8859-1")); } - String text; + String text; boolean isFirstLine = true; while ((text = reader.readLine()) != null) { @@ -419,9 +379,8 @@ public boolean writeKcdFile(File file, boolean prettyPrint, boolean gzip) { return false; } finally { try { - if (w != null) { - w.close(); - } + if (w != null) + w.close(); } catch (IOException e) { e.printStackTrace(logWriter); } @@ -532,7 +491,7 @@ private void parseNetworkNode(StringBuilder line) { nodes = Arrays.asList(lineArray); for (String nodeString : nodes) { - Node node = factory.createNode(); + Node node = (Node) factory.createNode(); node.setId(nodeString); node.setName(nodeString); network.getNode().add(node); @@ -761,7 +720,7 @@ private void parseMessageDefinition(StringBuilder line) { String[] lineArray = line.toString().split("\\s*SG_\\s+"); String[] messageArray = lineArray[0].split("\\s+"); - Message message = factory.createMessage(); + Message message = (Message) factory.createMessage(); int messageIdDecimal = getCanIdFromString(messageArray[0]); message.setId("0x" + Integer.toString(messageIdDecimal, 16).toUpperCase()); @@ -772,8 +731,8 @@ private void parseMessageDefinition(StringBuilder line) { message.setName(messageArray[1].replace(":", "")); message.setLength(messageArray[2]); if (!messageArray[3].contains(NOT_DEFINED)) { - Producer producer = factory.createProducer(); - NodeRef ref = factory.createNodeRef(); + Producer producer = (Producer) factory.createProducer(); + NodeRef ref = (NodeRef) factory.createNodeRef(); ref.setId(messageArray[3]); producer.getNodeRef().add(ref); message.setProducer(producer); @@ -847,7 +806,7 @@ public static boolean isExtendedFrameFormat(String canIdStr) { private enum SignalType { MULTIPLEXOR, MULTIPLEX, PLAIN - } + }; /** * Parses a the part of a signal line that is same for plain, multiplexor or @@ -864,7 +823,7 @@ private enum SignalType { private Signal parseSignalLine(Message message, String signalName, SignalType type, String line) { Value value = null; - Signal tSignal = factory.createSignal(); + Signal tSignal = (Signal) factory.createSignal(); tSignal.setName(signalName); String[] splitted = splitString(line); @@ -899,9 +858,9 @@ private Signal parseSignalLine(Message message, String signalName, SignalType ty // omit entry with NOT_DEFINED consumers if (sConsumers.length != 1 || !NOT_DEFINED.equals(sConsumers[0])) { - Consumer consumer = factory.createConsumer(); + Consumer consumer = (Consumer) factory.createConsumer(); for (String sConsumer : sConsumers) { - NodeRef ref = factory.createNodeRef(); + NodeRef ref = (NodeRef) factory.createNodeRef(); consumer.getNodeRef().add(ref); ref.setId(sConsumer); } @@ -911,7 +870,7 @@ private Signal parseSignalLine(Message message, String signalName, SignalType ty if ((intercept != 0.0) || (slope != 1.0) || !"".equals(splitted[8]) || "-".equals(splitted[3]) || (min != 0.0) || (max != 1.0)) { - value = factory.createValue(); + value = (Value) factory.createValue(); if ("-".equals(splitted[3])) { value.setType("signed"); @@ -951,7 +910,7 @@ private Signal parseSignalLine(Message message, String signalName, SignalType ty if (type == SignalType.MULTIPLEXOR) { - Multiplex mux = factory.createMultiplex(); + Multiplex mux = (Multiplex) factory.createMultiplex(); mux.setName(tSignal.getName()); mux.setOffset(tSignal.getOffset()); mux.setConsumer(tSignal.getConsumer()); @@ -966,7 +925,7 @@ private Signal parseSignalLine(Message message, String signalName, SignalType ty return null; } else { - Signal signal = factory.createSignal(); + Signal signal = (Signal) factory.createSignal(); signal.setName(tSignal.getName()); signal.setOffset(tSignal.getOffset()); signal.setConsumer(tSignal.getConsumer()); @@ -1266,8 +1225,8 @@ public static int bigEndianLeastSignificantBitOffset(int msb, int length) { return lsb; } - - public Bus getBus() { + + 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 cd8bae6..3677cc9 100644 --- a/src/main/java/com/github/canbabel/canio/ui/MainFrame.java +++ b/src/main/java/com/github/canbabel/canio/ui/MainFrame.java @@ -17,6 +17,8 @@ **/ package com.github.canbabel.canio.ui; +// TODO Version number(major.minor.build) +import com.github.canbabel.canio.dbc.DbcReader; import java.io.File; import java.io.FileFilter; import java.io.FileInputStream; @@ -26,16 +28,12 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import java.util.prefs.Preferences; +import java.util.prefs.*; import java.util.zip.GZIPInputStream; - import javax.swing.JFileChooser; import javax.swing.JOptionPane; import javax.xml.transform.stream.StreamSource; -// TODO Version number(major.minor.build) -import com.github.canbabel.canio.dbc.DbcReader; - /** * User interface * @@ -142,8 +140,7 @@ private void initComponents() { convertButton.setText("Convert"); convertButton.addActionListener(new java.awt.event.ActionListener() { - @Override - public void actionPerformed(java.awt.event.ActionEvent evt) { + public void actionPerformed(java.awt.event.ActionEvent evt) { convertButtonActionPerformed(evt); } }); @@ -220,8 +217,7 @@ public void actionPerformed(java.awt.event.ActionEvent evt) { addFilesOrFoldersButton.setText("Add files or folders"); addFilesOrFoldersButton.addActionListener(new java.awt.event.ActionListener() { - @Override - public void actionPerformed(java.awt.event.ActionEvent evt) { + public void actionPerformed(java.awt.event.ActionEvent evt) { addFilesOrFoldersButtonActionPerformed(evt); } }); @@ -247,8 +243,7 @@ public void actionPerformed(java.awt.event.ActionEvent evt) { removeButton.setText("Remove"); removeButton.addActionListener(new java.awt.event.ActionListener() { - @Override - public void actionPerformed(java.awt.event.ActionEvent evt) { + public void actionPerformed(java.awt.event.ActionEvent evt) { removeButtonActionPerformed(evt); } }); @@ -288,8 +283,7 @@ public void actionPerformed(java.awt.event.ActionEvent evt) { closeButton.setMaximumSize(new java.awt.Dimension(68, 30)); closeButton.setMinimumSize(new java.awt.Dimension(68, 30)); closeButton.addActionListener(new java.awt.event.ActionListener() { - @Override - public void actionPerformed(java.awt.event.ActionEvent evt) { + public void actionPerformed(java.awt.event.ActionEvent evt) { closeButtonHandler(evt); } }); 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 efb425e..89c2612 100644 --- a/src/test/java/com/github/canbabel/canio/dbc/CANFDTest.java +++ b/src/test/java/com/github/canbabel/canio/dbc/CANFDTest.java @@ -1,18 +1,16 @@ package com.github.canbabel.canio.dbc; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -import java.io.File; -import java.net.URL; - -import org.junit.Before; -import org.junit.Test; +import org.junit.*; import com.github.canbabel.canio.kcd.Bus; import com.github.canbabel.canio.kcd.Message; import com.github.canbabel.canio.kcd.NetworkDefinition; +import static org.junit.Assert.*; + +import java.io.File; +import java.net.URL; + public class CANFDTest { DbcReader dr = null; diff --git a/src/test/java/com/github/canbabel/canio/dbc/DbcReaderTest.java b/src/test/java/com/github/canbabel/canio/dbc/DbcReaderTest.java index 5acc3cc..55b315f 100644 --- a/src/test/java/com/github/canbabel/canio/dbc/DbcReaderTest.java +++ b/src/test/java/com/github/canbabel/canio/dbc/DbcReaderTest.java @@ -17,22 +17,19 @@ **/ package com.github.canbabel.canio.dbc; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -import java.io.File; +import java.io.*; import java.net.URL; +import java.util.zip.GZIPInputStream; -import javax.xml.transform.stream.StreamSource; +import com.github.canbabel.canio.ui.SchemaValidator; +import org.junit.*; -import org.junit.After; -import org.junit.AfterClass; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; +import javax.xml.bind.JAXBContext; +import javax.xml.bind.Marshaller; +import javax.xml.bind.Unmarshaller; +import javax.xml.transform.stream.StreamSource; -import com.github.canbabel.canio.ui.SchemaValidator; +import static org.junit.Assert.*; /** * @author Jan-Niklas Meier < dschanoeh@googlemail.com > From b9ef505f7f408e7fe0efbafe45d1b096d0ff9d49 Mon Sep 17 00:00:00 2001 From: Cosmin Radu Date: Wed, 8 Jan 2025 17:38:29 +0200 Subject: [PATCH 3/4] #36139 - test for parsing string --- pom.xml | 5 +++++ .../canbabel/canio/dbc/DbcReaderTest.java | 20 +++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/pom.xml b/pom.xml index 78422ef..194788f 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/test/java/com/github/canbabel/canio/dbc/DbcReaderTest.java b/src/test/java/com/github/canbabel/canio/dbc/DbcReaderTest.java index 55b315f..88e026e 100644 --- a/src/test/java/com/github/canbabel/canio/dbc/DbcReaderTest.java +++ b/src/test/java/com/github/canbabel/canio/dbc/DbcReaderTest.java @@ -19,9 +19,12 @@ import java.io.*; import java.net.URL; +import java.nio.charset.StandardCharsets; import java.util.zip.GZIPInputStream; import com.github.canbabel.canio.ui.SchemaValidator; + +import org.apache.commons.io.FileUtils; import org.junit.*; import javax.xml.bind.JAXBContext; @@ -162,5 +165,22 @@ public void testParse() { assertTrue(validator.validate(source)); } } + + @Test + public void testParseString() 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)); + } + } } From 338023809823d6778833a4d18fbbf1d12c149fde Mon Sep 17 00:00:00 2001 From: Cosmin Radu Date: Wed, 9 Jul 2025 16:48:12 +0300 Subject: [PATCH 4/4] #36139 - moved test for string --- .../canbabel/canio/dbc/DbcReadFileTest.java | 2 +- .../canbabel/canio/dbc/DbcReadStringTest.java | 63 +++++++++++++++++++ 2 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 src/test/java/com/github/canbabel/canio/dbc/DbcReadStringTest.java 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)); + } + } + +}