/** * Prints the value and it's child if any into a structured string. * * @param value the value to print * @param tabs the current level * @return a pretty string. */ public static String prettyPrint(MetadataValue value, String tabs) { StringBuffer str = new StringBuffer(tabs); str.append(value.getName()).append("\n"); if (value instanceof MetadataParent) { MetadataParent node = (MetadataParent) value; prettyPrint(node.getChildren(), tabs + "\t"); } return str.toString(); }
/** * Adds a value with the specified path to the root. * * @param root the root to add the tree to. * @param value the value of the leaf. * @param generated the parentPath should be marked as generated or not. * @param parentPath the path of the parent of the leaf from the root. * @return true if there was no merge conflicts. */ public static boolean addValue( MetadataParent root, AbstractMetadataValue value, boolean generated, String... parentPath) { if (parentPath == null || parentPath.length <= 0) { return root.addChild(value) == null; } else { TreeNodeMetadataValue path = createPath(value, generated, parentPath); return root.addChild(path) == null; } }
/** * Returns the node with the given path. * * @param root the root to start from when searching for the node with the given path. * @param path the path for which a node should be returned. * @param <T> The type of metadata. * @return the value or null if it wasn't found. */ public static <T extends Metadata> T getPath(MetadataParent<T> root, String... path) { if (path == null) { return null; } MetadataParent<T> parent = root; T currentValue = null; for (int i = 0; i < path.length; i++) { String name = path[i]; currentValue = parent.getChild(name); if (currentValue == null) { return null; } else if (i == path.length - 1) { return currentValue; } else if (currentValue instanceof MetadataParent) { parent = (MetadataParent) currentValue; } else { return null; } } return null; }