Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update SnakeYAML #618

Merged
merged 4 commits into from
Oct 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ repositories {

dependencies {
implementation 'org.apache.velocity:velocity-engine-core:2.3'
implementation 'org.yaml:snakeyaml:1.33'
implementation 'org.yaml:snakeyaml:2.3'
implementation 'commons-io:commons-io:2.13.0'
implementation 'org.apache.commons:commons-lang3:3.12.0'
implementation 'org.slf4j:slf4j-api:2.0.7'
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/robotbuilder/ActionsClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import robotbuilder.actions.TogglePaletteViewAction;
import robotbuilder.actions.UndoAction;
import robotbuilder.actions.VerifyAction;
import robotbuilder.utils.YamlUtils;

/**
*
Expand Down Expand Up @@ -130,7 +131,7 @@ public JToolBar getToolBar() {
}

private LinkedList<ExporterAction> getExporters() {
Yaml yaml = new Yaml();
Yaml yaml = YamlUtils.yaml;
InputStreamReader in = new InputStreamReader(Utils.getResourceAsStream(EXPORTERS_PATH + "exporters.yaml"));
List<String> exporterNames = (List<String>) yaml.load(in);

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/robotbuilder/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

import robotbuilder.data.RobotComponent;
import robotbuilder.data.properties.ParametersProperty;
import robotbuilder.utils.YamlUtils;

/**
*
Expand Down Expand Up @@ -133,7 +134,7 @@ public static <T> T deepCopy(T original) {
if (original == null) {
return null;
}
Yaml y = new Yaml();
Yaml y = YamlUtils.yaml;
String yaml = y.dump(original);
return (T) y.load(yaml);
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/robotbuilder/data/RobotComponentModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.yaml.snakeyaml.Yaml;

import robotbuilder.data.properties.Property;
import robotbuilder.utils.YamlUtils;

/**
* Model class holding the data used by {@link RobotComponent}.
Expand All @@ -37,7 +38,7 @@ public boolean equals(Object o) {
if (o instanceof RobotComponentModel) {
// compare serialization to avoid recursion
RobotComponentModel other = (RobotComponentModel) o;
Yaml yaml = new Yaml();
Yaml yaml = YamlUtils.yaml;
return yaml.dump(this).equals(yaml.dump(other));
}
return false;
Expand Down
11 changes: 5 additions & 6 deletions src/main/java/robotbuilder/exporters/GenericExporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.context.Context;
import org.yaml.snakeyaml.LoaderOptions;
import org.yaml.snakeyaml.TypeDescription;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;

import org.yaml.snakeyaml.constructor.SafeConstructor;
import robotbuilder.ActionsClass;
import robotbuilder.MainFrame;
import robotbuilder.RobotBuilder;
Expand All @@ -37,6 +39,7 @@
import robotbuilder.data.RobotComponent;
import robotbuilder.extensions.ExtensionComponent;
import robotbuilder.extensions.Extensions;
import robotbuilder.utils.YamlUtils;

/**
*
Expand Down Expand Up @@ -66,7 +69,7 @@ public GenericExporter(String path) {
List<ExtensionComponent> extensions = Extensions.getComponents();

// Load YAML Description
Yaml yaml = new Yaml();
Yaml yaml = YamlUtils.yaml;
Map<String, Object> description = yaml.load(
new InputStreamReader(Utils.getResourceAsStream(path + "ExportDescription.yaml")));
name = (String) description.get("Name");
Expand Down Expand Up @@ -253,11 +256,7 @@ private void loadExportDescription(Map<String, Map<String, String>> defaults,

private ArrayList<ExportFile> getFiles() throws FileNotFoundException {
String filesString = evalResource(path + filesPath);
Constructor constructor = new Constructor();
constructor.addTypeDescription(new TypeDescription(ExportFile.class, "!File"));
Yaml yaml = new Yaml(constructor);
ArrayList<ExportFile> filesYaml = yaml.load(filesString);
return filesYaml;
return YamlUtils.yaml.load(filesString);
}

String evalResource(String resource, Context context) {
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/robotbuilder/extensions/Extensions.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@

import lombok.experimental.UtilityClass;

import org.yaml.snakeyaml.Yaml;

import robotbuilder.utils.YamlUtils;
import robotbuilder.Utils;
import static robotbuilder.extensions.ExtensionComponent.CONFIG_FILE_NAME;
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/robotbuilder/robottree/RobotTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
import robotbuilder.data.RobotWalker;
import robotbuilder.data.Validator;
import robotbuilder.data.properties.Property;
import robotbuilder.utils.YamlUtils;

/**
* RobotTree is the tree representation of the robot map. It will contain nodes
Expand Down Expand Up @@ -351,7 +352,7 @@ public Object visit(RobotComponent self, Object... extra) {
return me;
}
}, (Object[]) null);
Yaml yaml = new Yaml();
Yaml yaml = YamlUtils.yaml;
return yaml.dump("Version " + RobotBuilder.VERSION) + "\n---\n" + yaml.dump(out);
}

Expand Down Expand Up @@ -389,7 +390,7 @@ public void load(Reader in) {

LoaderOptions loaderOptions = new LoaderOptions();
loaderOptions.setMaxAliasesForCollections(100);
Iterator docs = new Yaml(loaderOptions).loadAll(in).iterator();
Iterator docs = YamlUtils.yaml.loadAll(in).iterator();

String version = (String) docs.next();
if (!isVersionCompatible(version, RobotBuilder.VERSION)) {
Expand Down
9 changes: 3 additions & 6 deletions src/main/java/robotbuilder/utils/CodeFileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
import lombok.Data;
import lombok.experimental.UtilityClass;

import org.yaml.snakeyaml.LoaderOptions;
import org.yaml.snakeyaml.TypeDescription;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;

import org.yaml.snakeyaml.constructor.SafeConstructor;
import robotbuilder.Utils;
import static robotbuilder.ActionsClass.EXPORTERS_PATH;

Expand Down Expand Up @@ -94,10 +96,7 @@ public String apply(String text) {

private static final Map<String, Function<String, String>> textParserMap = new HashMap<>();
private static final Map<String, Function<File, String>> fileParserMap = new HashMap<>();
private static final TypeDescription fileParserDescriptor = new TypeDescription(FileParser.class, "!Parser");
private static final TypeDescription textFilterDescriptor = new TypeDescription(TextFilter.class, "!Filter");
private static final Constructor constructor = new Constructor();
private static final Yaml yaml = new Yaml(constructor);
private static final Yaml yaml = YamlUtils.yaml;

static {
try {
Expand All @@ -109,8 +108,6 @@ public String apply(String text) {
}

private static void init() {
constructor.addTypeDescription(fileParserDescriptor);
constructor.addTypeDescription(textFilterDescriptor);
String path = EXPORTERS_PATH + "parsers.yaml";
Map<String, Object> m = (Map) yaml.load(Utils.getResourceAsStream(path));
List<FileParser> loadedParsers = (List<FileParser>) m.get("parsers");
Expand Down
41 changes: 40 additions & 1 deletion src/main/java/robotbuilder/utils/YamlUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,58 @@

import lombok.experimental.UtilityClass;

import org.yaml.snakeyaml.LoaderOptions;
import org.yaml.snakeyaml.TypeDescription;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;

import org.yaml.snakeyaml.constructor.SafeConstructor;
import org.yaml.snakeyaml.inspector.TagInspector;
import robotbuilder.RobotBuilder;
import robotbuilder.data.DistinctValidator;
import robotbuilder.data.ExistsValidator;
import robotbuilder.data.ListValidator;
import robotbuilder.data.PaletteComponent;
import robotbuilder.data.UniqueValidator;
import robotbuilder.data.Validator;
import robotbuilder.data.properties.*;
import robotbuilder.exporters.ExportFile;

@UtilityClass
public class YamlUtils {

public static final Yaml yaml;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personal opinion: I'd suggest a public static accessor method over a statically accessed field.


static {
Constructor constructor = new Constructor();
LoaderOptions loaderOptions = new LoaderOptions();
loaderOptions.setMaxAliasesForCollections(100);
TagInspector taginspector = tag ->
tag.getClassName().equals(StringProperty.class.getName()) ||
tag.getClassName().equals(BooleanProperty.class.getName()) ||
tag.getClassName().equals(IntegerProperty.class.getName()) ||
tag.getClassName().equals(DoubleProperty.class.getName()) ||
tag.getClassName().equals(PositiveDoubleProperty.class.getName()) ||
tag.getClassName().equals(FileProperty.class.getName()) ||
tag.getClassName().equals(ChoicesProperty.class.getName()) ||
tag.getClassName().equals(ChildSelectionProperty.class.getName()) ||
tag.getClassName().equals(TypeSelectionProperty.class.getName()) ||
tag.getClassName().equals(ComponentSelectionProperty.class.getName()) ||
tag.getClassName().equals(ParentProperty.class.getName()) ||
tag.getClassName().equals(ParametersProperty.class.getName()) ||
tag.getClassName().equals(ParameterSetProperty.class.getName()) ||
tag.getClassName().equals(ConstantsProperty.class.getName()) ||
tag.getClassName().equals(ListProperty.class.getName()) ||
tag.getClassName().equals(TeamNumberProperty.class.getName()) ||
tag.getClassName().equals(ParameterDescriptor.class.getName()) ||

tag.getClassName().equals(DistinctValidator.class.getName()) ||
tag.getClassName().equals(ExistsValidator.class.getName()) ||
tag.getClassName().equals(UniqueValidator.class.getName()) ||
tag.getClassName().equals(ListValidator.class.getName());

loaderOptions.setTagInspector(taginspector);
Constructor constructor = new Constructor(loaderOptions);

constructor.addTypeDescription(new TypeDescription(PaletteComponent.class, "!Component"));

// Properties
Expand All @@ -40,12 +73,18 @@ public class YamlUtils {
constructor.addTypeDescription(new TypeDescription(ConstantsProperty.class, "!ConstantsProperty"));
constructor.addTypeDescription(new TypeDescription(ListProperty.class, "!ListProperty"));
constructor.addTypeDescription(new TypeDescription(TeamNumberProperty.class, "!TeamNumberProperty"));
constructor.addTypeDescription(new TypeDescription(ParameterDescriptor.class, "!ParameterDescriptor"));

constructor.addTypeDescription(new TypeDescription(DistinctValidator.class, "!DistinctValidator"));
constructor.addTypeDescription(new TypeDescription(ExistsValidator.class, "!ExistsValidator"));
constructor.addTypeDescription(new TypeDescription(UniqueValidator.class, "!UniqueValidator"));
constructor.addTypeDescription(new TypeDescription(ListValidator.class, "!ListValidator"));

constructor.addTypeDescription(new TypeDescription(ExportFile.class, "!File"));

constructor.addTypeDescription(new TypeDescription(CodeFileUtils.FileParser.class, "!Parser"));
constructor.addTypeDescription(new TypeDescription(CodeFileUtils.TextFilter.class, "!Filter"));

yaml = new Yaml(constructor);
}

Expand Down
Loading