Merge pull request #2343 from SiboVG/issue-2341

[#2341] Sanitize OBJ export groupnames
This commit is contained in:
Sibo Van Gool 2023-09-20 13:28:40 +02:00 committed by GitHub
commit 2ce09e8f90
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 30 deletions

View File

@ -154,6 +154,7 @@ public class OBJExporterFactory {
// Component exporting // Component exporting
String groupName = idx + "_" + component.getName(); String groupName = idx + "_" + component.getName();
groupName = sanitizeGroupName(groupName);
handleComponent(obj, this.configuration, this.options.getTransformer(), component, groupName, handleComponent(obj, this.configuration, this.options.getTransformer(), component, groupName,
materials.get(obj), this.options.getLOD(), options, warnings); materials.get(obj), this.options.getLOD(), options, warnings);
@ -293,6 +294,20 @@ public class OBJExporterFactory {
return sortedComponents; 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<RocketComponent> components, Set<RocketComponent> sortedComponents) { private void addChildComponentToList(RocketComponent parent, Set<RocketComponent> components, Set<RocketComponent> sortedComponents) {
for (RocketComponent child : parent.getChildren()) { for (RocketComponent child : parent.getChildren()) {
if (components.contains(child)) { if (components.contains(child)) {

View File

@ -9,9 +9,9 @@ import java.io.OutputStream;
import java.nio.file.Paths; import java.nio.file.Paths;
public abstract class FileUtils { public abstract class FileUtils {
private static final char[] ILLEGAL_CHARS = new char[] { '/', '\\', ':', '*', '?', '"', '<', '>', '|' };
public static void copy(InputStream is, OutputStream os) throws IOException { public static void copy(InputStream is, OutputStream os) throws IOException {
if (!(os instanceof BufferedOutputStream)) { if (!(os instanceof BufferedOutputStream)) {
os = new BufferedOutputStream(os); os = new BufferedOutputStream(os);
} }
@ -30,9 +30,7 @@ public abstract class FileUtils {
} }
public static byte[] readBytes(InputStream is) throws IOException { public static byte[] readBytes(InputStream is) throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream(1024); ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
copy(is, bos); copy(is, bos);
return bos.toByteArray(); return bos.toByteArray();
@ -61,4 +59,22 @@ public abstract class FileUtils {
return Paths.get(pathString).getFileName().toString(); 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;
}
} }

View File

@ -2,6 +2,7 @@ package net.sf.openrocket.gui.widgets;
import net.sf.openrocket.l10n.Translator; import net.sf.openrocket.l10n.Translator;
import net.sf.openrocket.startup.Application; import net.sf.openrocket.startup.Application;
import net.sf.openrocket.util.FileUtils;
import javax.swing.JFileChooser; import javax.swing.JFileChooser;
import javax.swing.JOptionPane; import javax.swing.JOptionPane;
@ -12,7 +13,6 @@ import java.util.regex.PatternSyntaxException;
public class SaveFileChooser extends JFileChooser { public class SaveFileChooser extends JFileChooser {
private static final Translator trans = Application.getTranslator(); private static final Translator trans = Application.getTranslator();
private static final char[] ILLEGAL_CHARS = new char[] { '/', '\\', ':', '*', '?', '"', '<', '>', '|' };
private File cwd = null; private File cwd = null;
private File currentFile = null; private File currentFile = null;
@ -30,7 +30,7 @@ public class SaveFileChooser extends JFileChooser {
if (file.getParentFile() != getCurrentDirectory()) { if (file.getParentFile() != getCurrentDirectory()) {
cwd = getCurrentDirectory(); cwd = getCurrentDirectory();
fileName = getFilenameInput(currentFile, cwd); fileName = getFilenameInput(currentFile, cwd);
if (getIllegalChar(fileName) != null) { if (FileUtils.getIllegalFilenameChar(fileName) != null) {
return; return;
} }
} }
@ -42,7 +42,7 @@ public class SaveFileChooser extends JFileChooser {
@Override @Override
public void approveSelection() { public void approveSelection() {
Character c = getIllegalChar(fileName); Character c = FileUtils.getIllegalFilenameChar(fileName);
if (c != null) { if (c != null) {
// Illegal character found // Illegal character found
JOptionPane.showMessageDialog(getParent(), 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. * 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. * You can't simply use getSelectedFile().getName() because it won't work for malformed filenames.