| | import java.io.IOException; |
| | import java.io.InputStream; |
| | -import java.util.Iterator; |
| | import java.util.Map.Entry; |
| | import javafx.scene.control.TreeItem; |
 |
| | public class YamlTreeAdapter { |
| | |
| | - public YamlTreeAdapter() { |
| | + protected YamlTreeAdapter() { |
| | } |
| | - |
| | - public static TreeView<String> adapt( final InputStream in ) throws IOException { |
| | - return adapt(YamlParser.parse(in)); |
| | + |
| | + /** |
| | + * Converts a YAML document to a TreeView based on the document keys. Only the |
| | + * first document in the stream is adapted. This does not close the stream. |
| | + * |
| | + * @param in Contains a YAML document. |
| | + * @param name Name of the root TreeItem. |
| | + * |
| | + * @return A TreeView populated with all the keys in the YAML document. |
| | + * |
| | + * @throws IOException Could not read from the stream. |
| | + */ |
| | + public static TreeView<String> adapt( |
| | + final InputStream in, final String name ) throws IOException { |
| | + final YamlTreeAdapter adapter = new YamlTreeAdapter(); |
| | + final JsonNode rootNode = YamlParser.parse( in ); |
| | + final TreeItem<String> rootItem = new TreeItem<>( name ); |
| | + |
| | + adapter.adapt( rootNode, rootItem ); |
| | + return new TreeView<>( rootItem ); |
| | } |
| | |
| | /** |
| | - * Iterate over a given root node (at any level of the tree) and process each |
| | + * Iterate over a given root node (at any level of the tree) and adapt each |
| | * leaf node. |
| | - * |
| | - * @param root A node to process. |
| | * |
| | - * @return |
| | + * @param rootNode A JSON node (YAML node) to adapt. |
| | + * @param rootItem The tree item to use as the root when processing the node. |
| | */ |
| | - private static TreeView<String> adapt( final JsonNode root ) { |
| | - return new TreeView( process( root ) ); |
| | + protected void adapt( |
| | + final JsonNode rootNode, |
| | + final TreeItem<String> rootItem ) { |
| | + rootNode.fields().forEachRemaining( |
| | + (Entry<String, JsonNode> leaf) -> adapt( leaf, rootItem ) |
| | + ); |
| | } |
| | |
| | - private static TreeItem<String> process( final JsonNode nodeRoot ) { |
| | - final Iterator<Entry<String, JsonNode>> fields = nodeRoot.fields(); |
| | - final TreeItem<String> itemRoot = new TreeItem<>(); |
| | + /** |
| | + * Recursively adapt each rootNode to a corresponding rootItem. |
| | + * |
| | + * @param rootNode The node to adapt. |
| | + * @param rootItem The item to adapt using the node's key. |
| | + */ |
| | + protected void adapt( |
| | + final Entry<String, JsonNode> rootNode, |
| | + final TreeItem<String> rootItem ) { |
| | + final JsonNode leafNode = rootNode.getValue(); |
| | + final TreeItem<String> leafItem = new TreeItem<>( rootNode.getKey() ); |
| | |
| | - while( fields.hasNext() ) { |
| | - final Entry<String, JsonNode> field = fields.next(); |
| | - final JsonNode leaf = field.getValue(); |
| | + rootItem.getChildren().add( leafItem ); |
| | |
| | - if( leaf.isObject() ) { |
| | - itemRoot.getChildren().add( process( leaf ) ); |
| | - } else { |
| | - itemRoot.setValue( field.getKey() ); |
| | - } |
| | + if( leafNode.isObject() ) { |
| | + adapt( leafNode, leafItem ); |
| | } |
| | - |
| | - return itemRoot; |
| | } |
| | } |