Dave Jarvis' Repositories

git clone https://repo.autonoma.ca/repo/keenwrite.git

Make static YAML objects

AuthorDaveJarvis <email>
Date2021-01-06 18:05:19 GMT-0800
Commitd51b44412f28c17d912e690d900a46478dba2b04
Parent5a68c79
Delta12 lines added, 5 lines removed, 7-line increase
src/main/java/com/keenwrite/editors/definition/yaml/YamlTreeTransformer.java
import static com.fasterxml.jackson.dataformat.yaml.YAMLGenerator.Feature.MINIMIZE_QUOTES;
+import static com.fasterxml.jackson.dataformat.yaml.YAMLGenerator.Feature.SPLIT_LINES;
/**
* Transforms a JsonNode hierarchy into a tree that can be displayed in a user
* interface and vice-versa.
*/
public final class YamlTreeTransformer implements TreeTransformer {
+ private static final YAMLFactory sFactory;
+ private static final YAMLMapper sMapper;
+
+ static {
+ sFactory = new YAMLFactory();
+ sFactory.configure( MINIMIZE_QUOTES, true );
+ sFactory.configure( SPLIT_LINES, false );
+ sMapper = new YAMLMapper( sFactory );
+ }
/**
public String transform( final TreeItem<String> treeItem ) {
try {
- final var factory = new YAMLFactory();
- factory.configure( MINIMIZE_QUOTES, true );
- final var mapper = new YAMLMapper( factory );
- final var root = mapper.createObjectNode();
+ final var root = sMapper.createObjectNode();
// Iterate over the root item's children. The root item is used by the
// application to ensure definitions can always be added to a tree, as
// such it is not meant to be exported, only its children.
for( final var child : treeItem.getChildren() ) {
transform( child, root );
}
- return mapper.writeValueAsString( root );
+ return sMapper.writeValueAsString( root );
} catch( final Exception ex ) {
throw new RuntimeException( ex );