GP-0: Adding a ProgramLoader.Builder.loaders(String) method

This commit is contained in:
Ryan Kurtz 2025-07-09 11:33:58 -04:00
parent e28509c2ea
commit 60d0883b10
3 changed files with 28 additions and 9 deletions

View file

@ -32,8 +32,7 @@ import ghidra.program.model.lang.*;
import ghidra.program.model.listing.Program;
import ghidra.program.util.DefaultLanguageService;
import ghidra.util.Msg;
import ghidra.util.exception.CancelledException;
import ghidra.util.exception.VersionException;
import ghidra.util.exception.*;
import ghidra.util.task.TaskMonitor;
/**
@ -234,6 +233,25 @@ public class ProgramLoader {
cls != null ? loader -> loader.getClass().equals(cls) : LoaderService.ACCEPT_ALL;
return this;
}
/**
* Sets the acceptable {@link Loader} to use during import.
* <p>
* By default, all {@link Loader}s are accepted ({@link LoaderService#ACCEPT_ALL}).
*
* @param clsName The class name of the {@link Loader} to use during import. A {@code null}
* value will revert back to the default ({@link LoaderService#ACCEPT_ALL}).
* @return This {@link Builder}
* @throws InvalidInputException if the given loader class name did not correspond to a
* {@link Loader}
*/
public Builder loaders(String clsName) throws InvalidInputException {
Class<? extends Loader> cls = LoaderService.getLoaderClassByName(clsName);
if (cls == null) {
throw new InvalidInputException("Loader '%s' does not exist!".formatted(clsName));
}
return loaders(cls);
}
/**
* Sets the {@link Loader}s to use during import.
@ -436,8 +454,8 @@ public class ProgramLoader {
LoadSpec loadSpec = getLoadSpec(p);
List<Option> loaderOptions = getLoaderOptions(p, loadSpec);
String importName = Objects.requireNonNullElse(importNameOverride,
loadSpec.getLoader().getPreferredFileName(p));
String importName = importNameOverride != null ? importNameOverride
: loadSpec.getLoader().getPreferredFileName(p);
// Load
Msg.info(ProgramLoader.class, "Using Loader: " + loadSpec.getLoader().getName());

View file

@ -201,12 +201,13 @@ public interface Loader extends ExtensionPoint, Comparable<Loader> {
* if absolutely necessary.
*
* @param provider The bytes to load.
* @return The preferred file name to use when loading.
* @return The preferred file name to use when loading, or {@code null} if a name could not
* be determined from the provider.
*/
public default String getPreferredFileName(ByteProvider provider) {
FSRL fsrl = provider.getFSRL();
String name = (fsrl != null) ? fsrl.getName() : provider.getName();
return name.replaceAll("[\\\\:|]+", "/");
return name != null ? name.replaceAll("[\\\\:|]+", "/") : null;
}
/**

View file

@ -4,9 +4,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@ -92,7 +92,7 @@ public class LoaderService {
*
* @param name The name of the {@link Loader} to get the {@link Class} of.
* @return The {@link Loader} {@link Class} that corresponds to the given simple {@link Class}
* name.
* name, or {@code null} if it does not exist.
*/
public static Class<? extends Loader> getLoaderClassByName(String name) {
return getAllLoaders()