Created
February 22, 2010 07:27
-
-
Save maxp/310902 to your computer and use it in GitHub Desktop.
yanmnode helper for SnakeYAML
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // YamlNode helper | |
| // author: mpenzin@gmail.com | |
| // date: 2010-02-22 | |
| // | |
| package util; | |
| import java.util.ArrayList; | |
| import java.util.Collection; | |
| import java.util.Collections; | |
| import java.util.HashMap; | |
| import java.util.List; | |
| import java.util.Map; | |
| import java.util.logging.Logger; | |
| // - - - | |
| import java.io.File; | |
| import java.io.FileInputStream; | |
| import org.yaml.snakeyaml.Yaml; | |
| // - - - | |
| public class YamlNode | |
| { | |
| String val; | |
| Map<String,YamlNode> map; | |
| List<YamlNode> lst; | |
| static final Logger L = Logger.getLogger(YamlNode.class.getName()); | |
| static HashMap<String,Boolean> BOOL_VAL = new HashMap<String,Boolean>(); | |
| static { | |
| BOOL_VAL.put( "", false ); | |
| BOOL_VAL.put( "0", false ); | |
| BOOL_VAL.put( "no", false ); | |
| BOOL_VAL.put( "false", false ); | |
| BOOL_VAL.put( "1", true ); | |
| BOOL_VAL.put( "yes", true ); | |
| BOOL_VAL.put( "true", true ); | |
| } | |
| static final YamlNode NULL_NODE = new YamlNode( null ); | |
| public YamlNode( String val ) | |
| { | |
| this.val = val; | |
| } | |
| public boolean is_null() | |
| { | |
| return (this.val == null && this.map == null && this.lst == null); | |
| } | |
| @SuppressWarnings("unchecked") | |
| public YamlNode( Object yaml_tree ) | |
| { | |
| if( yaml_tree instanceof List<?> ) | |
| { | |
| this.lst = new ArrayList<YamlNode>(); | |
| for( Object n: (List<Object>)yaml_tree ) | |
| { | |
| this.lst.add( new YamlNode(n) ); | |
| } | |
| } | |
| else if( yaml_tree instanceof Map<?,?> ) | |
| { | |
| this.map = new HashMap<String,YamlNode>(); | |
| for( Map.Entry<String, Object> m: ((Map<String,Object>)yaml_tree).entrySet() ) | |
| { | |
| this.map.put( m.getKey(), new YamlNode( m.getValue() ) ); | |
| } | |
| } | |
| else { | |
| this.val = (yaml_tree != null)? yaml_tree.toString(): null; | |
| } | |
| } | |
| public boolean bool() | |
| { | |
| return this.bool(false); | |
| } | |
| public boolean bool( boolean def ) | |
| { | |
| if( this.val == null ) return def; | |
| Boolean b = BOOL_VAL.get( this.val.trim().toLowerCase() ); | |
| return (b != null)? b.booleanValue(): def; | |
| } | |
| public int num() | |
| { | |
| return this.num(0); | |
| } | |
| public int num( int def ) | |
| { | |
| try { return Integer.parseInt(this.val); } | |
| catch( Exception ign ) { return def; } | |
| } | |
| public String str() | |
| { | |
| return this.str(""); | |
| } | |
| public String str( String def ) | |
| { | |
| return (this.val != null)? this.val: def; | |
| } | |
| public List<YamlNode> list() | |
| { | |
| return (this.lst != null)? this.lst: Collections.<YamlNode>emptyList(); | |
| } | |
| public Collection<String> keys() | |
| { | |
| return (this.map != null)? this.map.keySet(): Collections.<String>emptyList(); | |
| } | |
| public YamlNode item( String key ) | |
| { | |
| if( this.map == null ) return NULL_NODE; | |
| YamlNode n = this.map.get(key); | |
| return (n != null)? n: NULL_NODE; | |
| } | |
| public static YamlNode load( File yf ) | |
| { | |
| try { | |
| return new YamlNode( new Yaml().load( new FileInputStream( yf ) ) ); | |
| } | |
| catch( Exception ex ) { | |
| L.warning("YamlNode.load("+yf+") "+ex.toString()); | |
| } | |
| return NULL_NODE; | |
| } | |
| // - - - | |
| public static void main( String[] argv ) | |
| { | |
| YamlNode root = YamlNode.load( new File("/path/to/file.yaml") ); | |
| for( YamlNode n: root.list() ) { | |
| System.out.println( "id: "+n.item("id").str() ); | |
| System.out.println( "addr: "+n.item("town").str()+" "+n.item("addr").str() ); | |
| System.out.println( "note: "+n.item("note").str() ); | |
| for( YamlNode li: n.item("test").list() ) { | |
| System.out.println( "test item: "+li.str() ); | |
| } | |
| } | |
| } | |
| } | |
| //. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment