anchor.diff:  	 render_collection/1,
   	 render_entity/1]).
   -export([render/1]).
  +
  +-type ejson()        :: ejson_arr() | ejson_obj().
  +-type ejson_arr()    :: [ ejson_elem() ].
  +-type ejson_elem()   :: ejson_obj() | ejson_arr() | ejson_val().
  +-type ejson_obj()    :: {[ ejson_member() ]}.
  +-type ejson_member() :: {ejson_key(), ejson_obj() | ejson_arr() | ejson_val()}.
  +-type ejson_key()    :: binary() | atom().
  +-type ejson_val()    :: atom() | binary() | string().
   
   %%%
   %%% API
  
positive.code:     << BScheme/binary, BTerm/binary >>.
  
  render_attribute_specs(Attrs) ->
      render_attribute_specs(Attrs, []).
  
  render_attribute_specs([], Acc) ->
      {Acc};
  render_attribute_specs([#occi_attr{}=Attr|Tail], Acc) ->
      L = [
  	 {mutable, not occi_attribute:is_immutable(Attr)},
  
negative.code: 	 {type, occi_attribute:get_type_id(Attr)},
  	 {default, occi_attribute:get_default(Attr)}
  	],
      Id = split_attr_id(occi_attribute:get_id(Attr)),
      render_attribute_specs(Tail, insert_attr(Id, strip_list(L), Acc)).
  
  render_attribute_values(Attr) ->
      render_attribute_values(Attr, {[]}).
  
  render_attribute_values([], Acc) ->
      Acc;
  render_attribute_values([#occi_attr{}=Attr|Tail], Acc) ->
      Id = split_attr_id(occi_attribute:get_id(Attr)),
      case occi_attribute:get_value(Attr) of
  	undefined ->
  	    render_attribute_values(Tail, Acc);
  	Value ->
  	    render_attribute_values(Tail, insert_attr(Id, Value, Acc))
      end.
  
  %
  % insert attribute name/value into ejson tree.
  % Name is a list of binary
  %
  -spec insert_attr(Name :: [binary()], Value :: term(), ejson()) -> ejson().
  insert_attr([ Name ], Value, {Tree}) ->
      { [{Name, Value} | Tree ]};
  insert_attr([ Name | Tail ], Value, {[ {Name, Children} | OtherNS ]}) ->
      { [{Name, insert_attr(Tail, Value, Children) } | OtherNS] };
  insert_attr([ Name | Tail ], Value, {Tree}) ->
      { [{Name, insert_attr(Tail, Value, {[]}) } | Tree] }.
  
  split_attr_id(Name) when is_atom(Name) ->
      lists:map(fun (T) ->
  		      list_to_binary(T)
  	      end, string:tokens(atom_to_list(Name), ".")).
  
  strip_list(L) ->
      {strip_list(L, [])}.
  
  strip_list([], Acc) ->
      lists:reverse(Acc);
  strip_list([{_Key, undefined}|Tail], Acc) ->
      strip_list(Tail, Acc);
  strip_list([{_Key, <<>>}|Tail], Acc) ->
      strip_list(Tail, Acc);
  strip_list([{_Key, []}|Tail], Acc) ->
      strip_list(Tail, Acc);
  strip_list([{_Key, {[]}}|Tail], Acc) ->
      strip_list(Tail, Acc);
  strip_list([{Key, Val}|Tail], Acc) ->
      strip_list(Tail, [{Key, Val}|Acc]).
  