From 19854154b4d13b19583fc0d23043cf926ac51628 Mon Sep 17 00:00:00 2001 From: SiboVG Date: Wed, 20 Sep 2023 13:27:38 +0200 Subject: [PATCH 1/2] Refactor filename illegal char method --- .../src/net/sf/openrocket/util/FileUtils.java | 36 +++++++++++++------ .../gui/widgets/SaveFileChooser.java | 23 ++---------- 2 files changed, 29 insertions(+), 30 deletions(-) diff --git a/core/src/net/sf/openrocket/util/FileUtils.java b/core/src/net/sf/openrocket/util/FileUtils.java index 09b77bfdc..b74b82d8a 100644 --- a/core/src/net/sf/openrocket/util/FileUtils.java +++ b/core/src/net/sf/openrocket/util/FileUtils.java @@ -9,31 +9,29 @@ import java.io.OutputStream; import java.nio.file.Paths; public abstract class FileUtils { + private static final char[] ILLEGAL_CHARS = new char[] { '/', '\\', ':', '*', '?', '"', '<', '>', '|' }; - public static void copy( InputStream is, OutputStream os ) throws IOException { - - if ( ! (os instanceof BufferedOutputStream ) ) { + public static void copy(InputStream is, OutputStream os) throws IOException { + if (!(os instanceof BufferedOutputStream)) { os = new BufferedOutputStream(os); } - if ( ! (is instanceof BufferedInputStream ) ) { + if (!(is instanceof BufferedInputStream)) { is = new BufferedInputStream(is); } byte[] buffer = new byte[1024]; int bytesRead = 0; - while( (bytesRead = is.read(buffer)) > 0 ) { - os.write(buffer,0,bytesRead); + while( (bytesRead = is.read(buffer)) > 0) { + os.write(buffer, 0, bytesRead); } os.flush(); } - public static byte[] readBytes( InputStream is ) throws IOException { - + public static byte[] readBytes(InputStream is) throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(1024); - - copy( is, bos ); + copy(is, bos); return bos.toByteArray(); @@ -61,4 +59,22 @@ public abstract class FileUtils { return Paths.get(pathString).getFileName().toString(); } + + /** + * Returns an illegal character if one is found in the filename, otherwise returns null. + * @param filename The filename to check + * @return The illegal character, or null if none is found + */ + public static Character getIllegalFilenameChar(String filename) { + if (filename == null || filename.isEmpty()) { + return null; + } + for (char c : ILLEGAL_CHARS) { + if (filename.indexOf(c) >= 0) { + return c; + } + } + return null; + } + } diff --git a/swing/src/net/sf/openrocket/gui/widgets/SaveFileChooser.java b/swing/src/net/sf/openrocket/gui/widgets/SaveFileChooser.java index 61b5ce3ba..9fb73c17c 100644 --- a/swing/src/net/sf/openrocket/gui/widgets/SaveFileChooser.java +++ b/swing/src/net/sf/openrocket/gui/widgets/SaveFileChooser.java @@ -2,6 +2,7 @@ package net.sf.openrocket.gui.widgets; import net.sf.openrocket.l10n.Translator; import net.sf.openrocket.startup.Application; +import net.sf.openrocket.util.FileUtils; import javax.swing.JFileChooser; import javax.swing.JOptionPane; @@ -12,7 +13,6 @@ import java.util.regex.PatternSyntaxException; public class SaveFileChooser extends JFileChooser { private static final Translator trans = Application.getTranslator(); - private static final char[] ILLEGAL_CHARS = new char[] { '/', '\\', ':', '*', '?', '"', '<', '>', '|' }; private File cwd = null; private File currentFile = null; @@ -30,7 +30,7 @@ public class SaveFileChooser extends JFileChooser { if (file.getParentFile() != getCurrentDirectory()) { cwd = getCurrentDirectory(); fileName = getFilenameInput(currentFile, cwd); - if (getIllegalChar(fileName) != null) { + if (FileUtils.getIllegalFilenameChar(fileName) != null) { return; } } @@ -42,7 +42,7 @@ public class SaveFileChooser extends JFileChooser { @Override public void approveSelection() { - Character c = getIllegalChar(fileName); + Character c = FileUtils.getIllegalFilenameChar(fileName); if (c != null) { // Illegal character found JOptionPane.showMessageDialog(getParent(), @@ -57,23 +57,6 @@ public class SaveFileChooser extends JFileChooser { } } - /** - * Returns an illegal character if one is found in the filename, otherwise returns null. - * @param filename The filename to check - * @return The illegal character, or null if none is found - */ - private Character getIllegalChar(String filename) { - if (filename == null || filename.isEmpty()) { - return null; - } - for (char c : ILLEGAL_CHARS) { - if (filename.indexOf(c) >= 0) { - return c; - } - } - return null; - } - /** * Returns the filename input by the user, or null if the filename is invalid. * You can't simply use getSelectedFile().getName() because it won't work for malformed filenames. From d36b5d3e9d5b7409e31671565dd88e35744b7787 Mon Sep 17 00:00:00 2001 From: SiboVG Date: Wed, 20 Sep 2023 13:27:58 +0200 Subject: [PATCH 2/2] [#2341] Sanitize groupnames --- .../wavefrontobj/export/OBJExporterFactory.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/core/src/net/sf/openrocket/file/wavefrontobj/export/OBJExporterFactory.java b/core/src/net/sf/openrocket/file/wavefrontobj/export/OBJExporterFactory.java index b9e6f52b3..11a607146 100644 --- a/core/src/net/sf/openrocket/file/wavefrontobj/export/OBJExporterFactory.java +++ b/core/src/net/sf/openrocket/file/wavefrontobj/export/OBJExporterFactory.java @@ -154,6 +154,7 @@ public class OBJExporterFactory { // Component exporting String groupName = idx + "_" + component.getName(); + groupName = sanitizeGroupName(groupName); handleComponent(obj, this.configuration, this.options.getTransformer(), component, groupName, materials.get(obj), this.options.getLOD(), options, warnings); @@ -293,6 +294,20 @@ public class OBJExporterFactory { return sortedComponents; } + /** + * Sanitize the group name by replacing illegal characters with underscores. + * @param groupName the group name to sanitize + * @return the sanitized group name + */ + private static String sanitizeGroupName(String groupName) { + Character c = FileUtils.getIllegalFilenameChar(groupName); + while (c != null) { + groupName = groupName.replace(c, '_'); + c = FileUtils.getIllegalFilenameChar(groupName); + } + return groupName; + } + private void addChildComponentToList(RocketComponent parent, Set components, Set sortedComponents) { for (RocketComponent child : parent.getChildren()) { if (components.contains(child)) {