diff --git a/rebar.config b/rebar.config index ff1beb6..d99d6bf 100644 --- a/rebar.config +++ b/rebar.config @@ -15,7 +15,7 @@ ]} ]}. -{plugins, [pc]}. +{plugins, [pc, rebar3_format]}. % Interrupt compilation, if the artifact is not found {artifacts, ["priv/exml_nif.so"]}. @@ -50,3 +50,8 @@ {cover_export_enabled, true}. {coveralls_coverdata, "_build/test/cover/eunit.coverdata"}. {coveralls_service_name, "travis-ci"}. + +{format, [ + {formatter, default_formatter}, + {options, #{sub_indent => 4}} +]}. diff --git a/src/exml.erl b/src/exml.erl index e8bfd76..b0fe348 100644 --- a/src/exml.erl +++ b/src/exml.erl @@ -14,20 +14,15 @@ -include("exml_stream.hrl"). -export([parse/1]). - -export([to_list/1, to_binary/1, to_iolist/1, xml_size/1, xml_sort/1, to_pretty_iolist/1]). - -export([]). --export_type([attr/0, - cdata/0, - element/0, - item/0]). +-export_type([attr/0, cdata/0, element/0, item/0]). -type attr() :: {binary(), binary()}. -type cdata() :: #xmlcdata{}. @@ -39,23 +34,23 @@ xml_size([]) -> 0; xml_size([Elem | Rest]) -> xml_size(Elem) + xml_size(Rest); -xml_size(#xmlcdata{ content = Content }) -> +xml_size(#xmlcdata{content = Content}) -> iolist_size(exml_nif:escape_cdata(Content)); -xml_size(#xmlel{ name = Name, attrs = Attrs, children = [] }) -> +xml_size(#xmlel{name = Name, attrs = Attrs, children = []}) -> 3 % Self-closing: - + byte_size(Name) + xml_size(Attrs); -xml_size(#xmlel{ name = Name, attrs = Attrs, children = Children }) -> + + byte_size(Name) + + xml_size(Attrs); +xml_size(#xmlel{name = Name, attrs = Attrs, children = Children}) -> % Opening and closing: <> - 5 + byte_size(Name)*2 - + xml_size(Attrs) + xml_size(Children); -xml_size(#xmlstreamstart{ name = Name, attrs = Attrs }) -> + 5 + byte_size(Name) * 2 + xml_size(Attrs) + xml_size(Children); +xml_size(#xmlstreamstart{name = Name, attrs = Attrs}) -> byte_size(Name) + 2 + xml_size(Attrs); -xml_size(#xmlstreamend{ name = Name }) -> +xml_size(#xmlstreamend{name = Name}) -> byte_size(Name) + 3; xml_size({Key, Value}) -> - byte_size(Key) - + 4 % ="" and whitespace before - + byte_size(Value). + byte_size(Key) + + 4 % ="" and whitespace before + + byte_size(Value). %% @doc Sort a (list of) `xmlel()`. %% @@ -71,17 +66,14 @@ xml_size({Key, Value}) -> xml_sort(#xmlcdata{} = Cdata) -> Cdata; xml_sort(#xmlel{} = El) -> - #xmlel{ attrs = Attrs, children = Children } = El, - El#xmlel{ - attrs = lists:sort(Attrs), - children = [ xml_sort(C) || C <- Children ] - }; -xml_sort(#xmlstreamstart{ attrs = Attrs } = StreamStart) -> - StreamStart#xmlstreamstart{ attrs = lists:sort(Attrs) }; + #xmlel{attrs = Attrs, children = Children} = El, + El#xmlel{attrs = lists:sort(Attrs), children = [xml_sort(C) || C <- Children]}; +xml_sort(#xmlstreamstart{attrs = Attrs} = StreamStart) -> + StreamStart#xmlstreamstart{attrs = lists:sort(Attrs)}; xml_sort(#xmlstreamend{} = StreamEnd) -> StreamEnd; xml_sort(Elements) when is_list(Elements) -> - lists:sort([ xml_sort(E) || E <- Elements ]). + lists:sort([xml_sort(E) || E <- Elements]). -spec to_list(element() | [exml_stream:element()]) -> string(). to_list(Element) -> @@ -112,10 +104,8 @@ to_iolist([_ | _] = Elements, Pretty) -> Head = hd(Elements), [Last | RevChildren] = lists:reverse(tl(Elements)), case {Head, Last} of - {#xmlstreamstart{name = Name, attrs = Attrs}, - #xmlstreamend{name = Name}} -> - Element = #xmlel{name = Name, attrs = Attrs, - children = lists:reverse(RevChildren)}, + {#xmlstreamstart{name = Name, attrs = Attrs}, #xmlstreamend{name = Name}} -> + Element = #xmlel{name = Name, attrs = Attrs, children = lists:reverse(RevChildren)}, to_binary_nif(Element, Pretty); _ -> [to_iolist(El, Pretty) || El <- Elements] @@ -133,6 +123,9 @@ to_iolist(#xmlcdata{content = Content}, _Pretty) -> -spec to_binary_nif(element(), pretty | term()) -> binary(). to_binary_nif(#xmlel{} = Element, Pretty) -> case catch exml_nif:to_binary(Element, Pretty) of - {'EXIT', Reason} -> erlang:error({badxml, Element, Reason}); - Result when is_binary(Result) -> Result + {'EXIT', Reason} -> + erlang:error({badxml, Element, Reason}); + Result when is_binary(Result) -> + Result end. + diff --git a/src/exml_nif.erl b/src/exml_nif.erl index f430615..e8e4b2a 100644 --- a/src/exml_nif.erl +++ b/src/exml_nif.erl @@ -11,11 +11,11 @@ -type parser() :: term(). -type stream_element() :: exml:element() | exml_stream:start() | exml_stream:stop(). --export([create/2, parse/1, parse_next/2, escape_cdata/1, - to_binary/2, reset_parser/1]). +-export([create/2, parse/1, parse_next/2, escape_cdata/1, to_binary/2, reset_parser/1]). + -export_type([parser/0, stream_element/0]). --on_load(load/0). +-on_load({load, 0}). %%%=================================================================== %%% Public API @@ -33,29 +33,34 @@ load() -> end, erlang:load_nif(filename:join(PrivDir, ?MODULE_STRING), none). --spec create(MaxChildSize :: non_neg_integer(), InfiniteStream :: boolean()) -> - {ok, parser()} | {error, Reason :: any()}. +-spec create(MaxChildSize :: non_neg_integer(), InfiniteStream :: boolean()) -> {ok, + parser()} | + {error, + Reason :: any()}. create(_, _) -> erlang:nif_error(not_loaded). -spec escape_cdata(Bin :: iodata()) -> binary(). escape_cdata(_Bin) -> - erlang:nif_error(not_loaded). + erlang:nif_error(not_loaded). -spec to_binary(Elem :: exml:element(), pretty | not_pretty) -> binary(). to_binary(_Elem, _Pretty) -> erlang:nif_error(not_loaded). --spec parse(Bin :: binary() | [binary()]) -> {ok, exml:element()} | {error, Reason :: any()}. +-spec parse(Bin :: binary() | [binary()]) -> {ok, exml:element()} | + {error, Reason :: any()}. parse(_) -> erlang:nif_error(not_loaded). --spec parse_next(parser(), Data :: binary() | [binary()]) -> - {ok, stream_element() | undefined, non_neg_integer()} | - {error, Reason :: any()}. +-spec parse_next(parser(), Data :: binary() | [binary()]) -> {ok, + stream_element() | undefined, + non_neg_integer()} | + {error, Reason :: any()}. parse_next(_, _) -> erlang:nif_error(not_loaded). -spec reset_parser(parser()) -> any(). reset_parser(_) -> erlang:nif_error(not_loaded). + diff --git a/src/exml_query.erl b/src/exml_query.erl index 5a609d5..70d1148 100644 --- a/src/exml_query.erl +++ b/src/exml_query.erl @@ -21,16 +21,24 @@ -export([cdata/1]). -type element_with_ns() :: {element_with_ns, binary()}. --type element_with_name_and_ns () :: {element_with_ns, binary(), binary()}. --type element_with_attr_of_value () :: {element_with_attr, binary(), binary()}. +-type element_with_name_and_ns() :: {element_with_ns, binary(), binary()}. +-type element_with_attr_of_value() :: {element_with_attr, binary(), binary()}. +-type path() :: [cdata | + {element, binary()} | + {attr, binary()} | + element_with_ns() | + element_with_name_and_ns() | + element_with_attr_of_value()]. --type path() :: [cdata | %% selects cdata from the element - {element, binary()} | % selects subelement with given name - {attr, binary()} | % selects attr of given name - element_with_ns() | % selects subelement with given namespace - element_with_name_and_ns() | % selects subelement with given name and namespace - element_with_attr_of_value() % selects subelement with given attribute and value - ]. + %% selects cdata from the element + % selects subelement with given name + + % selects attr of given name + % selects subelement with given namespace + + % selects subelement with given name and namespace + + % selects subelement with given attribute and value -export_type([path/0]). @@ -44,7 +52,8 @@ path(Element, Path) -> path(#xmlel{} = Element, [], _) -> Element; path(#xmlel{} = Element, [{element, Name} | Rest], Default) -> - Child = subelement(Element, Name), % may return undefined + Child = subelement(Element, + Name), % may return undefined path(Child, Rest, Default); path(#xmlel{} = Element, [{element_with_ns, NS} | Rest], Default) -> Child = subelement_with_ns(Element, NS), @@ -118,17 +127,20 @@ child_with_ns([#xmlel{} = Element | Rest], NS, Default) -> child_with_ns([_ | Rest], NS, Default) -> child_with_ns(Rest, NS, Default). --spec subelement_with_attr(exml:element(), AttrName :: binary(), AttrValue :: binary()) -> - exml:element() | undefined. +-spec subelement_with_attr(exml:element(), + AttrName :: binary(), + AttrValue :: binary()) -> exml:element() | undefined. subelement_with_attr(Element, AttrName, AttrValue) -> subelement_with_attr(Element, AttrName, AttrValue, undefined). --spec subelement_with_attr(Element, AttrName, AttrValue, Other) -> SubElement | Other when - Element :: exml:element(), - AttrName :: binary(), - AttrValue :: binary(), - SubElement :: exml:element(), - Other :: term(). +-spec subelement_with_attr(Element, AttrName, AttrValue, Other) -> SubElement | + Other when Element :: + exml:element(), + AttrName :: binary(), + AttrValue :: binary(), + SubElement :: + exml:element(), + Other :: term(). subelement_with_attr(#xmlel{children = Children}, AttrName, AttrValue, Default) -> child_with_attr(Children, AttrName, AttrValue, Default). @@ -144,14 +156,15 @@ child_with_attr([#xmlel{} = Element | Rest], AttrName, AttrVal, Default) -> child_with_attr([_ | Rest], AttrName, AttrVal, Default) -> child_with_attr(Rest, AttrName, AttrVal, Default). - --spec subelement_with_name_and_ns(exml:element(), binary(), binary()) -> - exml:element() | undefined. +-spec subelement_with_name_and_ns(exml:element(), binary(), binary()) -> exml:element() | + undefined. subelement_with_name_and_ns(Element, Name, NS) -> subelement_with_name_and_ns(Element, Name, NS, undefined). --spec subelement_with_name_and_ns(exml:element(), binary(), binary(), Other) -> - exml:element() | Other. +-spec subelement_with_name_and_ns(exml:element(), + binary(), + binary(), + Other) -> exml:element() | Other. subelement_with_name_and_ns(Element, Name, NS, Default) -> case subelement(Element, Name, undefined) of undefined -> @@ -170,36 +183,41 @@ subelement_or_default(SubElement, NS, Default) -> -spec subelements(exml:element(), binary()) -> [exml:element()]. subelements(#xmlel{children = Children}, Name) -> - lists:filter(fun(#xmlel{name = N}) when N =:= Name -> - true; - (_) -> - false - end, Children). + lists:filter(fun (#xmlel{name = N}) when N =:= Name -> + true; + (_) -> + false + end, + Children). -spec subelements_with_ns(exml:element(), binary()) -> [exml:element()]. subelements_with_ns(#xmlel{children = Children}, NS) -> - lists:filter(fun(#xmlel{} = Child) -> - NS =:= attr(Child, <<"xmlns">>); - (_) -> - false - end, Children). - --spec subelements_with_name_and_ns(exml:element(), binary(), binary()) -> [exml:element()]. -subelements_with_name_and_ns(#xmlel{children = Children}, Name, NS) -> - lists:filter(fun(#xmlel{name = SubName} = Child) -> - SubName =:= Name andalso + lists:filter(fun (#xmlel{} = Child) -> NS =:= attr(Child, <<"xmlns">>); - (_) -> - false - end, Children). + (_) -> + false + end, + Children). + +-spec subelements_with_name_and_ns(exml:element(), + binary(), + binary()) -> [exml:element()]. +subelements_with_name_and_ns(#xmlel{children = Children}, Name, NS) -> + lists:filter(fun (#xmlel{name = SubName} = Child) -> + SubName =:= Name andalso NS =:= attr(Child, <<"xmlns">>); + (_) -> + false + end, + Children). -spec subelements_with_attr(exml:element(), binary(), binary()) -> [exml:element()]. subelements_with_attr(#xmlel{children = Children}, AttrName, Value) -> - lists:filter(fun(#xmlel{} = Child) -> - Value =:= attr(Child, AttrName); - (_) -> - false - end, Children). + lists:filter(fun (#xmlel{} = Child) -> + Value =:= attr(Child, AttrName); + (_) -> + false + end, + Children). -spec cdata(exml:element()) -> binary(). cdata(#xmlel{children = Children}) -> @@ -217,3 +235,4 @@ attr(#xmlel{attrs = Attrs}, Name, Default) -> false -> Default end. + diff --git a/src/exml_stream.erl b/src/exml_stream.erl index 27a910c..abd70d1 100644 --- a/src/exml_stream.erl +++ b/src/exml_stream.erl @@ -10,22 +10,11 @@ -include("exml_stream.hrl"). --export([new_parser/0, - new_parser/1, - parse/2, - reset_parser/1, - free_parser/1]). +-export([new_parser/0, new_parser/1, parse/2, reset_parser/1, free_parser/1]). --export_type([element/0, - start/0, - stop/0, - parser/0, - parser_opt/0]). +-export_type([element/0, start/0, stop/0, parser/0, parser_opt/0]). --record(parser, { - event_parser :: exml_nif:parser(), - buffer :: [binary()] - }). +-record(parser, {event_parser :: exml_nif:parser(), buffer :: [binary()]}). -type start() :: #xmlstreamstart{}. -type stop() :: #xmlstreamend{}. @@ -46,7 +35,7 @@ new_parser() -> new_parser([]). -spec new_parser([parser_opt()]) -> {ok, parser()} | {error, any()}. -new_parser(Opts)-> +new_parser(Opts) -> MaxChildSize = proplists:get_value(max_child_size, Opts, 0), InfiniteStream = proplists:get_value(infinite_stream, Opts, false), case exml_nif:create(MaxChildSize, InfiniteStream) of @@ -56,8 +45,8 @@ new_parser(Opts)-> Error end. --spec parse(parser(), binary()) -> {ok, parser(), [exml_stream:element()]} - | {error, Reason :: any()}. +-spec parse(parser(), binary()) -> {ok, parser(), [exml_stream:element()]} | + {error, Reason :: any()}. parse(Parser, Input) when is_binary(Input) -> #parser{event_parser = EventParser, buffer = OldBuf} = Parser, Buffer = OldBuf ++ [Input], @@ -101,3 +90,4 @@ drop_offset([Front | Rest], Offset) when byte_size(Front) =< Offset -> drop_offset([Front | Rest], Offset) -> <<_:Offset/binary, Part/binary>> = Front, [Part | Rest]. +