Dave Jarvis' Repositories

git clone https://repo.autonoma.ca/repo/keenwrite.git
/*
 * Copyright 2016 White Magic Software, Ltd.
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  o Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 *  o Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.scrivenvar.definition.yaml;

import static com.scrivenvar.Messages.get;
import com.scrivenvar.definition.FileDefinitionSource;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Map;
import javafx.scene.control.TreeView;

/**
 * Represents a definition data source for YAML files.
 *
 * @author White Magic Software, Ltd.
 */
public class YamlFileDefinitionSource extends FileDefinitionSource {

  private YamlTreeAdapter yamlTreeAdapter;
  private YamlParser yamlParser;

  /**
   * Constructs a new YAML definition source, populated from the given file.
   *
   * @param path Path to the YAML definition file.
   */
  public YamlFileDefinitionSource( final Path path ) {
    super( path );
    init();
  }
  
  private void init() {
    setYamlParser( createYamlParser() );
  }

  @Override
  public Map<String, String> getResolvedMap() {
    return getYamlParser().createResolvedMap();
  }

  private YamlTreeAdapter getYamlTreeAdapter() {
    if( this.yamlTreeAdapter == null ) {
      setYamlTreeAdapter( new YamlTreeAdapter( getYamlParser() ) );
    }

    return this.yamlTreeAdapter;
  }

  private void setYamlTreeAdapter( final YamlTreeAdapter yamlTreeAdapter ) {
    this.yamlTreeAdapter = yamlTreeAdapter;
  }

  private YamlParser getYamlParser() {
    if( this.yamlParser == null ) {
      setYamlParser( createYamlParser() );
    }

    return this.yamlParser;
  }

  private void setYamlParser( final YamlParser yamlParser ) {
    this.yamlParser = yamlParser;
  }

  private YamlParser createYamlParser() {
    try( final InputStream in = Files.newInputStream( getPath() ) ) {
      return new YamlParser( in );
    } catch( final Exception ex ) {
      throw new RuntimeException( ex );
    }
  }

  @Override
  protected TreeView<String> createTreeView() {
    return getYamlTreeAdapter().adapt(
      get( "Pane.defintion.node.root.title" )
    );
  }
}