org.yaml.snakeyaml.resolver.Resolver Java Examples

The following examples show how to use org.yaml.snakeyaml.resolver.Resolver. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ConfigViewFactory.java    From thorntail with Apache License 2.0 7 votes vote down vote up
private static Yaml newYaml(Map<String, String> environment) {
    return new Yaml(new EnvironmentConstructor(environment),
                    new Representer(),
                    new DumperOptions(),
                    new Resolver() {
                        @Override
                        public Tag resolve(NodeId kind, String value, boolean implicit) {
                            if (value != null) {
                                if (value.startsWith("${env.")) {
                                    return new Tag("!env");
                                }
                                if (value.equalsIgnoreCase("on") ||
                                        value.equalsIgnoreCase("off") ||
                                        value.equalsIgnoreCase("yes") ||
                                        value.equalsIgnoreCase("no")) {
                                    return Tag.STR;
                                }
                            }
                            return super.resolve(kind, value, implicit);
                        }
                    });
}
 
Example #2
Source File: FlutterUtils.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private static Map<String, Object> loadPubspecInfo(@NotNull String yamlContents) {
  final Yaml yaml = new Yaml(new SafeConstructor(), new Representer(), new DumperOptions(), new Resolver() {
    @Override
    protected void addImplicitResolvers() {
      this.addImplicitResolver(Tag.BOOL, BOOL, "yYnNtTfFoO");
      this.addImplicitResolver(Tag.NULL, NULL, "~nN\u0000");
      this.addImplicitResolver(Tag.NULL, EMPTY, null);
      this.addImplicitResolver(new Tag("tag:yaml.org,2002:value"), VALUE, "=");
      this.addImplicitResolver(Tag.MERGE, MERGE, "<");
    }
  });

  try {
    //noinspection unchecked
    return (Map)yaml.load(yamlContents);
  }
  catch (Exception e) {
    return null;
  }
}
 
Example #3
Source File: Yaml.java    From snake-yaml with Apache License 2.0 6 votes vote down vote up
/**
 * Create Yaml instance. It is safe to create a few instances and use them
 * in different Threads.
 * 
 * @param constructor
 *            BaseConstructor to construct incoming documents
 * @param representer
 *            Representer to emit outgoing objects
 * @param dumperOptions
 *            DumperOptions to configure outgoing objects
 * @param resolver
 *            Resolver to detect implicit type
 */
public Yaml(BaseConstructor constructor, Representer representer, DumperOptions dumperOptions,
        Resolver resolver) {
    if (!constructor.isExplicitPropertyUtils()) {
        constructor.setPropertyUtils(representer.getPropertyUtils());
    } else if (!representer.isExplicitPropertyUtils()) {
        representer.setPropertyUtils(constructor.getPropertyUtils());
    }
    this.constructor = constructor;
    representer.setDefaultFlowStyle(dumperOptions.getDefaultFlowStyle());
    representer.setDefaultScalarStyle(dumperOptions.getDefaultScalarStyle());
    representer.getPropertyUtils().setAllowReadOnlyProperties(
            dumperOptions.isAllowReadOnlyProperties());
    representer.setTimeZone(dumperOptions.getTimeZone());
    this.representer = representer;
    this.dumperOptions = dumperOptions;
    this.resolver = resolver;
    this.name = "Yaml:" + System.identityHashCode(this);
}
 
Example #4
Source File: Yaml.java    From orion.server with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Create Yaml instance. It is safe to create a few instances and use them
 * in different Threads.
 * 
 * @param constructor
 *            BaseConstructor to construct incoming documents
 * @param representer
 *            Representer to emit outgoing objects
 * @param dumperOptions
 *            DumperOptions to configure outgoing objects
 * @param resolver
 *            Resolver to detect implicit type
 */
public Yaml(BaseConstructor constructor, Representer representer, DumperOptions dumperOptions,
        Resolver resolver) {
    if (!constructor.isExplicitPropertyUtils()) {
        constructor.setPropertyUtils(representer.getPropertyUtils());
    } else if (!representer.isExplicitPropertyUtils()) {
        representer.setPropertyUtils(constructor.getPropertyUtils());
    }
    this.constructor = constructor;
    representer.setDefaultFlowStyle(dumperOptions.getDefaultFlowStyle());
    representer.setDefaultScalarStyle(dumperOptions.getDefaultScalarStyle());
    representer.getPropertyUtils().setAllowReadOnlyProperties(
            dumperOptions.isAllowReadOnlyProperties());
    representer.setTimeZone(dumperOptions.getTimeZone());
    this.representer = representer;
    this.dumperOptions = dumperOptions;
    this.resolver = resolver;
    this.name = "Yaml:" + System.identityHashCode(this);
}
 
Example #5
Source File: FlutterUtils.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private static Map<String, Object> loadPubspecInfo(@NotNull String yamlContents) {
  final Yaml yaml = new Yaml(new SafeConstructor(), new Representer(), new DumperOptions(), new Resolver() {
    @Override
    protected void addImplicitResolvers() {
      this.addImplicitResolver(Tag.BOOL, BOOL, "yYnNtTfFoO");
      this.addImplicitResolver(Tag.NULL, NULL, "~nN\u0000");
      this.addImplicitResolver(Tag.NULL, EMPTY, null);
      this.addImplicitResolver(new Tag("tag:yaml.org,2002:value"), VALUE, "=");
      this.addImplicitResolver(Tag.MERGE, MERGE, "<");
    }
  });

  try {
    //noinspection unchecked
    return (Map)yaml.load(yamlContents);
  }
  catch (Exception e) {
    return null;
  }
}
 
Example #6
Source File: YamlDataSetProducer.java    From jpa-unit with Apache License 2.0 6 votes vote down vote up
public Yaml createYamlReader() {
    return new Yaml(new Constructor(), new Representer(), new DumperOptions(), new Resolver() {
        @Override
        protected void addImplicitResolvers() {
            // Intentionally left TIMESTAMP as string to let DBUnit deal with the conversion
            addImplicitResolver(Tag.BOOL, BOOL, "yYnNtTfFoO");
            addImplicitResolver(Tag.INT, INT, "-+0123456789");
            addImplicitResolver(Tag.FLOAT, FLOAT, "-+0123456789.");
            addImplicitResolver(Tag.MERGE, MERGE, "<");
            addImplicitResolver(Tag.NULL, NULL, "~nN\0");
            addImplicitResolver(Tag.NULL, EMPTY, null);
            addImplicitResolver(Tag.VALUE, VALUE, "=");
            addImplicitResolver(Tag.YAML, YAML, "!&*");
        }
    });
}
 
Example #7
Source File: Yaml.java    From Diorite with MIT License 6 votes vote down vote up
/**
 * Create Yaml instance. It is safe to create a few instances and use them
 * in different Threads.
 *
 * @param serialization
 *         serialization instance.
 * @param constructor
 *         BaseConstructor to construct incoming documents
 * @param representer
 *         Representer to emit outgoing objects
 * @param dumperOptions
 *         DumperOptions to configure outgoing objects
 * @param resolver
 *         Resolver to detect implicit type
 */
public Yaml(Serialization serialization, YamlConstructor constructor, Representer representer, DumperOptions dumperOptions, Resolver resolver)
{
    representer.initMultiRepresenters();
    this.serialization = serialization;
    if (! constructor.isExplicitPropertyUtils())
    {
        constructor.setPropertyUtils(representer.getPropertyUtils());
    }
    else if (! representer.isExplicitPropertyUtils())
    {
        representer.setPropertyUtils(constructor.getPropertyUtils());
    }
    this.constructor = constructor;
    representer.setDefaultFlowStyle(dumperOptions.getDefaultFlowStyle());
    representer.setDefaultScalarStyle(dumperOptions.getDefaultScalarStyle());
    representer.getPropertyUtils().setAllowReadOnlyProperties(dumperOptions.isAllowReadOnlyProperties());
    representer.setTimeZone(dumperOptions.getTimeZone());
    this.representer = representer;
    this.dumperOptions = dumperOptions;
    this.resolver = resolver;
    this.name = "Yaml:" + DioriteThreadUtils.getFullThreadName(Thread.currentThread());
}
 
Example #8
Source File: YamlUtils.java    From configuration-as-code-plugin with MIT License 6 votes vote down vote up
public static Node read(YamlSource source, Reader reader, ConfigurationContext context) throws IOException {
    LoaderOptions loaderOptions = new LoaderOptions();
    loaderOptions.setMaxAliasesForCollections(context.getYamlMaxAliasesForCollections());
    Composer composer = new Composer(
        new ParserImpl(new StreamReaderWithSource(source, reader)),
        new Resolver(),
        loaderOptions);
    try {
        return composer.getSingleNode();
    } catch (YAMLException e) {
        if (e.getMessage().startsWith("Number of aliases for non-scalar nodes exceeds the specified max")) {
            throw new ConfiguratorException(String.format(
                "%s%nYou can increase the maximum by setting an environment variable or property%n  ENV: %s=\"100\"%n  PROPERTY: -D%s=\"100\"",
                e.getMessage(), ConfigurationContext.CASC_YAML_MAX_ALIASES_ENV,
                ConfigurationContext.CASC_YAML_MAX_ALIASES_PROPERTY));
        }
        throw e;
    }
}
 
Example #9
Source File: Serializer.java    From Diorite with MIT License 6 votes vote down vote up
public Serializer(Serialization serialization, Emitable emitter, Resolver resolver, DumperOptions opts, @Nullable Tag rootTag)
{
    this.serialization = serialization;
    this.emitter = EmitableWrapper.wrap(emitter);
    this.resolver = resolver;
    this.explicitStart = opts.isExplicitStart();
    this.explicitEnd = opts.isExplicitEnd();
    if (opts.getVersion() != null)
    {
        this.useVersion = opts.getVersion();
    }
    this.useTags = opts.getTags();
    this.serializedNodes = new HashSet<>(50);
    this.anchors = new HashMap<>(10);
    this.anchorGenerator = opts.getAnchorGenerator();
    this.closed = null;
    this.explicitRoot = rootTag;
}
 
Example #10
Source File: YamlParser.java    From nexus-repository-helm with Eclipse Public License 1.0 6 votes vote down vote up
public Map<String, Object> load(InputStream is) throws IOException {
  checkNotNull(is);
  String data = IOUtils.toString(new UnicodeReader(is));

  Map<String, Object> map;

  try {
    Yaml yaml = new Yaml(new Constructor(), new Representer(),
        new DumperOptions(), new Resolver());
    map = yaml.load(data);
  }
  catch (YAMLException e) {
    map = (Map<String, Object>) mapper.readValue(data, Map.class);
  }
  return map;
}
 
Example #11
Source File: Yaml.java    From FastAsyncWorldedit with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Create Yaml instance. It is safe to create a few instances and use them
 * in different Threads.
 *
 * @param constructor
 *            BaseConstructor to construct incoming documents
 * @param representer
 *            Representer to emit outgoing objects
 * @param dumperOptions
 *            DumperOptions to configure outgoing objects
 * @param resolver
 *            Resolver to detect implicit type
 */
public Yaml(BaseConstructor constructor, Representer representer, DumperOptions dumperOptions,
            Resolver resolver) {
    if (!constructor.isExplicitPropertyUtils()) {
        constructor.setPropertyUtils(representer.getPropertyUtils());
    } else if (!representer.isExplicitPropertyUtils()) {
        representer.setPropertyUtils(constructor.getPropertyUtils());
    }
    this.constructor = constructor;
    representer.setDefaultFlowStyle(dumperOptions.getDefaultFlowStyle());
    representer.setDefaultScalarStyle(dumperOptions.getDefaultScalarStyle());
    representer.getPropertyUtils().setAllowReadOnlyProperties(
            dumperOptions.isAllowReadOnlyProperties());
    representer.setTimeZone(dumperOptions.getTimeZone());
    this.representer = representer;
    this.dumperOptions = dumperOptions;
    this.resolver = resolver;
    this.name = "Yaml:" + System.identityHashCode(this);
}
 
Example #12
Source File: YamlPropertySourceLoader.java    From canal-1.1.3 with Apache License 2.0 5 votes vote down vote up
@Override
protected Yaml createYaml() {
    return new Yaml(new StrictMapAppenderConstructor(), new Representer(), new DumperOptions(), new Resolver() {

        @Override
        public void addImplicitResolver(Tag tag, Pattern regexp, String first) {
            if (tag == Tag.TIMESTAMP) {
                return;
            }
            super.addImplicitResolver(tag, regexp, first);
        }
    });
}
 
Example #13
Source File: ConfigDecryptTest.java    From light-4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testAutoDecryptorClass() throws IOException {
    if (System.getenv("config_password") == null || !System.getenv("config_password").equals("light")) return;
    final Resolver resolver = new Resolver();
    resolver.addImplicitResolver(YmlConstants.CRYPT_TAG, YmlConstants.CRYPT_PATTERN, YmlConstants.CRYPT_FIRST);
    Yaml yaml = new Yaml(new DecryptConstructor("com.networknt.config.TestAutoDecryptor"), new Representer(), new DumperOptions(), resolver);

    Map<String, Object> secret=yaml.load(Config.getInstance().getInputStreamFromFile("secret-map-test2.yml"));

    Assert.assertEquals(SECRET+"-test", secret.get("serverKeystorePass"));
}
 
Example #14
Source File: ConfigDecryptTest.java    From light-4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testDecryptorClass() {
    final Resolver resolver = new Resolver();
    resolver.addImplicitResolver(YmlConstants.CRYPT_TAG, YmlConstants.CRYPT_PATTERN, YmlConstants.CRYPT_FIRST);
    Yaml yaml = new Yaml(new DecryptConstructor("com.networknt.config.TestDecryptor"), new Representer(), new DumperOptions(), resolver);
	
    Map<String, Object> secret=yaml.load(Config.getInstance().getInputStreamFromFile("secret-map-test2.yml"));
    
    Assert.assertEquals(SECRET+"-test", secret.get("serverKeystorePass"));
}
 
Example #15
Source File: PyStructureTest.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
private List<Node> canonical_compose_all(InputStream file) {
    StreamReader reader = new StreamReader(new UnicodeReader(file));
    StringBuilder buffer = new StringBuilder();
    while (reader.peek() != '\0') {
        buffer.append(reader.peek());
        reader.forward();
    }
    CanonicalParser parser = new CanonicalParser(buffer.toString());
    Composer composer = new Composer(parser, new Resolver());
    List<Node> documents = new ArrayList<Node>();
    while (composer.checkNode()) {
        documents.add(composer.getNode());
    }
    return documents;
}
 
Example #16
Source File: PyStructureTest.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
private List<Node> compose_all(InputStream file) {
    Composer composer = new Composer(new ParserImpl(new StreamReader(new UnicodeReader(file))),
            new Resolver());
    List<Node> documents = new ArrayList<Node>();
    while (composer.checkNode()) {
        documents.add(composer.getNode());
    }
    return documents;
}
 
Example #17
Source File: ConstructorSequenceTest.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private List<Object> construct(Constructor constructor, String data) {
    StreamReader reader = new StreamReader(data);
    Parser parser = new ParserImpl(reader);
    Resolver resolver = new Resolver();
    Composer composer = new Composer(parser, resolver);
    constructor.setComposer(composer);
    List<Object> result = (List<Object>) constructor.getSingleData(Object.class);
    return result;
}
 
Example #18
Source File: ConstructorMappingTest.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
private Object construct(Constructor constructor, String data) {
    StreamReader reader = new StreamReader(data);
    Parser parser = new ParserImpl(reader);
    Resolver resolver = new Resolver();
    Composer composer = new Composer(parser, resolver);
    constructor.setComposer(composer);
    return constructor.getSingleData(Object.class);
}
 
Example #19
Source File: FragmentComposerTest.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
public void testFragment() {
    String document = "foo:  blargle\n"
            + "developer:  { name: \"Bjarne Stroustrup\", language: \"C++\"}\n"
            + "gee:  [ \"whiz\", \"bang\"]\n";//

    StreamReader reader = new StreamReader(document);
    Composer composer = new FragmentComposer(new ParserImpl(reader), new Resolver(),
            "developer");
    Constructor constructor = new Constructor();
    constructor.setComposer(composer);
    DeveloperBean developer = (DeveloperBean) constructor.getSingleData(DeveloperBean.class);
    assertEquals("Bjarne Stroustrup", developer.name);
    assertEquals("C++", developer.language);
}
 
Example #20
Source File: YamlPropertySourceLoader.java    From canal with Apache License 2.0 5 votes vote down vote up
@Override
protected Yaml createYaml() {
    return new Yaml(new StrictMapAppenderConstructor(), new Representer(), new DumperOptions(), new Resolver() {

        @Override
        public void addImplicitResolver(Tag tag, Pattern regexp, String first) {
            if (tag == Tag.TIMESTAMP) {
                return;
            }
            super.addImplicitResolver(tag, regexp, first);
        }
    });
}
 
Example #21
Source File: YamlPropertySourceFactory.java    From dubbo-2.6.5 with Apache License 2.0 5 votes vote down vote up
@Override
protected Yaml createYaml() {
    return new Yaml(new StrictMapAppenderConstructor(), new Representer(),
            new DumperOptions(), new Resolver() {
        @Override
        public void addImplicitResolver(Tag tag, Pattern regexp,
                                        String first) {
            if (tag == Tag.TIMESTAMP) {
                return;
            }
            super.addImplicitResolver(tag, regexp, first);
        }
    });
}
 
Example #22
Source File: VersionedYamlDoc.java    From onedev with MIT License 5 votes vote down vote up
public String toYaml() {
	StringWriter writer = new StringWriter();
	DumperOptions dumperOptions = new DumperOptions();
	Serializer serializer = new Serializer(new Emitter(writer, dumperOptions), 
			new Resolver(), dumperOptions, Tag.MAP);
	try {
		serializer.open();
		serializer.serialize(this);
		serializer.close();
		return writer.toString();
	} catch (IOException e) {
		throw new RuntimeException(e);
	}
}
 
Example #23
Source File: Serializer.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
public Serializer(Emitable emitter, Resolver resolver, DumperOptions opts, Tag rootTag) {
    this.emitter = emitter;
    this.resolver = resolver;
    this.explicitStart = opts.isExplicitStart();
    this.explicitEnd = opts.isExplicitEnd();
    if (opts.getVersion() != null) {
        this.useVersion = opts.getVersion();
    }
    this.useTags = opts.getTags();
    this.serializedNodes = new HashSet<Node>();
    this.anchors = new HashMap<Node, String>();
    this.anchorGenerator = opts.getAnchorGenerator();
    this.closed = null;
    this.explicitRoot = rootTag;
}
 
Example #24
Source File: YamlParser.java    From nexus-repository-helm with Eclipse Public License 1.0 5 votes vote down vote up
public String getYamlContent(final ChartIndex index) {
  Yaml yaml = new Yaml(new JodaPropertyConstructor(),
      setupRepresenter(),
      new DumperOptions(),
      new Resolver());
  return yaml.dumpAsMap(index);
}
 
Example #25
Source File: ConfigurationAsCode.java    From configuration-as-code-plugin with MIT License 5 votes vote down vote up
@VisibleForTesting
@Restricted(NoExternalUse.class)
public static void serializeYamlNode(Node root, Writer writer) throws IOException {
    DumperOptions options = new DumperOptions();
    options.setDefaultFlowStyle(BLOCK);
    options.setDefaultScalarStyle(PLAIN);
    options.setSplitLines(true);
    options.setPrettyFlow(true);
    Serializer serializer = new Serializer(new Emitter(writer, options), new Resolver(),
            options, null);
    serializer.open();
    serializer.serialize(root);
    serializer.close();
}
 
Example #26
Source File: JavaBeanLoader.java    From orion.server with Eclipse Public License 1.0 5 votes vote down vote up
public JavaBeanLoader(LoaderOptions options, BeanAccess beanAccess) {
    if (options == null) {
        throw new NullPointerException("LoaderOptions must be provided.");
    }
    if (options.getRootTypeDescription() == null) {
        throw new NullPointerException("TypeDescription must be provided.");
    }
    Constructor constructor = new Constructor(options.getRootTypeDescription());
    loader = new Yaml(constructor, options, new Representer(), new DumperOptions(),
            new Resolver());
    loader.setBeanAccess(beanAccess);
}
 
Example #27
Source File: Serializer.java    From orion.server with Eclipse Public License 1.0 5 votes vote down vote up
public Serializer(Emitable emitter, Resolver resolver, DumperOptions opts, Tag rootTag) {
    this.emitter = emitter;
    this.resolver = resolver;
    this.explicitStart = opts.isExplicitStart();
    this.explicitEnd = opts.isExplicitEnd();
    if (opts.getVersion() != null) {
        this.useVersion = opts.getVersion();
    }
    this.useTags = opts.getTags();
    this.serializedNodes = new HashSet<Node>();
    this.anchors = new HashMap<Node, String>();
    this.lastAnchorId = 0;
    this.closed = null;
    this.explicitRoot = rootTag;
}
 
Example #28
Source File: Yaml.java    From orion.server with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * @deprecated
 */
public Yaml(LoaderOptions loaderOptions) {
    this(new Constructor(), new Representer(), new DumperOptions(), new Resolver());
}
 
Example #29
Source File: Composer.java    From orion.server with Eclipse Public License 1.0 4 votes vote down vote up
public Composer(Parser parser, Resolver resolver) {
    this.parser = parser;
    this.resolver = resolver;
    this.anchors = new HashMap<String, Node>();
    this.recursiveNodes = new HashSet<Node>();
}
 
Example #30
Source File: Yaml.java    From orion.server with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * @deprecated use with Constructor instead of Loader
 */
public Yaml(Loader loader, Dumper dumper) {
    this(loader, dumper, new Resolver());
}