diff options
Diffstat (limited to 'BJC-Utils2/src/main')
3 files changed, 81 insertions, 11 deletions
diff --git a/BJC-Utils2/src/main/java/bjc/utils/components/ComponentDescription.java b/BJC-Utils2/src/main/java/bjc/utils/components/ComponentDescription.java index 3cb16b4..a946cae 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/components/ComponentDescription.java +++ b/BJC-Utils2/src/main/java/bjc/utils/components/ComponentDescription.java @@ -35,15 +35,37 @@ public class ComponentDescription implements IDescribedComponent { * The description of the component * @param version * The version of the component + * @throws IllegalArgumentException + * thrown if name, author or description is null, or if + * version is less than 1 */ public ComponentDescription(String name, String author, String description, int version) { + sanityCheckArgs(name, author, description, version); + this.name = name; this.author = author; this.description = description; this.version = version; } + private static void sanityCheckArgs(String name, String author, + String description, int version) { + if (name == null) { + throw new IllegalArgumentException( + "Component name can't be null"); + } else if (author == null) { + throw new IllegalArgumentException( + "Component author can't be null"); + } else if (description == null) { + throw new IllegalArgumentException( + "Component description can't be null"); + } else if (version < 0) { + throw new IllegalArgumentException( + "Component version must be greater than 0"); + } + } + @Override public String getAuthor() { return author; @@ -63,4 +85,9 @@ public class ComponentDescription implements IDescribedComponent { public int getVersion() { return version; } + + @Override + public String toString() { + return name + " component v" + version + ", written by " + author; + } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/components/ComponentDescriptionFileParser.java b/BJC-Utils2/src/main/java/bjc/utils/components/ComponentDescriptionFileParser.java index bae9790..bf49762 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/components/ComponentDescriptionFileParser.java +++ b/BJC-Utils2/src/main/java/bjc/utils/components/ComponentDescriptionFileParser.java @@ -2,6 +2,7 @@ package bjc.utils.components; import java.io.InputStream; +import bjc.utils.exceptions.PragmaFormatException; import bjc.utils.funcutils.ListUtils; import bjc.utils.parserutils.RuleBasedConfigReader; @@ -23,20 +24,50 @@ public class ComponentDescriptionFileParser { }); reader.addPragma("name", (fst, stat) -> { - stat.setName(ListUtils.collapseTokens(fst.toList((s) -> s))); + if (!fst.hasMoreTokens()) { + throw new PragmaFormatException( + "Pragma name requires at least one argument"); + } else { + stat.setName( + ListUtils.collapseTokens(fst.toList((s) -> s))); + } }); reader.addPragma("author", (fst, stat) -> { - stat.setAuthor(ListUtils.collapseTokens(fst.toList((s) -> s))); + if (!fst.hasMoreTokens()) { + throw new PragmaFormatException( + "Pragma author requires at least one argument"); + } else { + stat.setAuthor( + ListUtils.collapseTokens(fst.toList((s) -> s))); + } }); reader.addPragma("description", (fst, stat) -> { - stat.setDescription( - ListUtils.collapseTokens(fst.toList((s) -> s))); + if (!fst.hasMoreTokens()) { + throw new PragmaFormatException( + "Pragma description requires at least one argument"); + } else { + stat.setDescription( + ListUtils.collapseTokens(fst.toList((s) -> s))); + } }); reader.addPragma("version", (fst, stat) -> { - stat.setVersion(Integer.parseInt(fst.nextToken())); + if (!fst.hasMoreTokens()) { + throw new PragmaFormatException( + "Pragma name requires at least one argument"); + } else { + String token = fst.nextToken(); + + try { + stat.setVersion(Integer.parseInt(token)); + } catch (NumberFormatException nfex) { + throw new PragmaFormatException("Argument " + token + + " to version pragma isn't a valid integer. " + + "This pragma requires a integer argument"); + } + } }); } diff --git a/BJC-Utils2/src/main/java/bjc/utils/components/FileComponentRepository.java b/BJC-Utils2/src/main/java/bjc/utils/components/FileComponentRepository.java index 1e67a72..f2d2cf8 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/components/FileComponentRepository.java +++ b/BJC-Utils2/src/main/java/bjc/utils/components/FileComponentRepository.java @@ -5,6 +5,8 @@ import java.util.HashMap; import java.util.Map; import java.util.function.Function; +import org.slf4j.LoggerFactory; + import bjc.utils.funcdata.FunctionalList; /** @@ -33,6 +35,10 @@ public class FileComponentRepository<E extends IDescribedComponent> * Create a new component repository sourcing components from files in * a directory * + * An exception thrown during the loading of a component will only + * cause the loading of that component to fail, but a warning will be + * logged. + * * @param dir * The directory to read component files from * @param reader @@ -47,9 +53,17 @@ public class FileComponentRepository<E extends IDescribedComponent> // Do nothing with directories. They probably contain // support files for components } else { - E comp = reader.apply(fle); + try { + E comp = reader.apply(fle); + + comps.put(comp.getName(), comp); + } catch (Exception ex) { + LoggerFactory.getLogger(getClass()) + .warn("Error found reading component from file " + + fle.toString() + + ". This component will not be loaded"); + } - comps.put(comp.getName(), comp); } } } @@ -58,9 +72,7 @@ public class FileComponentRepository<E extends IDescribedComponent> public FunctionalList<E> getComponentList() { FunctionalList<E> ret = new FunctionalList<>(); - comps.forEach((name, comp) -> { - ret.add(comp); - }); + comps.forEach((name, comp) -> ret.add(comp)); return ret; } @@ -72,6 +84,6 @@ public class FileComponentRepository<E extends IDescribedComponent> @Override public String getSource() { - return "Read from directory " + sourcePath + "."; + return "Components read from directory " + sourcePath + "."; } } |
