Skip to content

Commit

Permalink
Some new functions to facilitate integration
Browse files Browse the repository at this point in the history
Functions to give access to the model, type specs.
  • Loading branch information
willemdj committed Dec 15, 2015
1 parent dbfe5ff commit 1d3afda
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 10 deletions.
3 changes: 3 additions & 0 deletions src/erlsom.erl
Expand Up @@ -46,6 +46,9 @@
-include("erlsom.hrl").
-include("erlsom_parse.hrl").

-opaque model() :: #model{}.
-export_type([model/0]).


%%----------------------------------------------------------------------
%% Function: compile_xsd/2
Expand Down
14 changes: 10 additions & 4 deletions src/erlsom_compile.erl
Expand Up @@ -74,7 +74,7 @@
includeFun, %% function to find included XSDs
includeDirs, %% directories to look for XSDs
includeFiles, %% tuples {Namespace, Prefix, Location}
%% or {Namespace, Schema, Prefix}, where
%% or {Namespace, Prefix, Schema}, where
%% Schema is a parsed XSD (used to parse WSDLs
%% with multiple XSDs).
imported = [], %% a list of imported namespaces, to prevent
Expand Down Expand Up @@ -221,20 +221,24 @@ compile_parsed_xsd(ParsedXsd, Prefix, Namespaces, IncludeFun, IncludeDirs, Inclu
Nsp = Pf1,
Nss = [#ns{prefix = Pf2, uri = TargetNs} | Namespaces];
#ns{prefix = Pf} ->
Nsp = Pf ++ ":",
case Pf of
undefined -> Nsp = "";
_ -> Nsp = Pf ++ ":"
end,
Nss = Namespaces
end
end,
ImportedNs = [Uri || {Uri, _} <- AlreadyImported],
ImportedNsMapping = [#ns{prefix = P, uri = U} || {U, P} <- AlreadyImported],
ToBeImportedNsMapping = [#ns{prefix = P, uri = U} || {U, P, _} <- IncludeFiles],
Acc = #p1acc{tns = TargetNs,
includeFun = IncludeFun,
includeDirs = IncludeDirs,
includeFiles = IncludeFiles,
efd = ParsedXsd#schemaType.elementFormDefault,
afd = ParsedXsd#schemaType.attributeFormDefault,
nsp = Nsp,
nss = ImportedNsMapping ++ Nss,
nss = Nss ++ ToBeImportedNsMapping ++ ImportedNsMapping,
imported = ImportedNs},
case catch transform(ParsedXsd, Acc) of
{error, Message} -> {error, Message};
Expand Down Expand Up @@ -917,5 +921,7 @@ translateQuasiAlternative(#localElementType{name=Name, type=Type, ref=undefined,
%% translateNs(#ns{prefix = Prefix, uri = Ns}) ->
%% {Ns, Prefix, undefined}.
%% TODO: sort this out - what is correct?
translateNs(X) ->
translateNs({Uri, Prefix}) ->
#ns{uri = Uri, prefix = Prefix};
translateNs(#ns{} = X) ->
X.
6 changes: 5 additions & 1 deletion src/erlsom_lib.erl
Expand Up @@ -51,7 +51,7 @@
getUriFromQname/1,
getTargetNamespaceFromXsd/1,
removePrefixes/1, unique/1,
getPrefixFromModel/2]).
getNamespacesFromModel/1, getPrefixFromModel/2]).

-include("erlsom_compile.hrl").
-include("erlsom_sax.hrl").
Expand Down Expand Up @@ -950,6 +950,10 @@ getPrefixFromModel(#model{nss = Namespaces}, Uri) ->
Prefix
end.

%%% hides the definition of #model{}
getNamespacesFromModel(#model{nss = Namespaces}) ->
[{Uri, Prefix} || #ns{prefix = Prefix, uri = Uri} <- Namespaces].


%% these are the top-level elements. They can occur in an '#any' type.
%% Check on [], because if for some reason there would be no alternatives
Expand Down
21 changes: 16 additions & 5 deletions src/erlsom_writeHrl.erl
Expand Up @@ -27,19 +27,28 @@

-module(erlsom_writeHrl).
-export([writeHrl/1]).
-export([write_hrl/1]).
-export([writeHrlFile/3]).
-export([writeXsdHrlFile/2]).

-include("erlsom_parse.hrl").
-include("erlsom.hrl").

-type hrl_header() :: iolist(). %% only explanatory text (comment)
-type hrl_types() :: iolist(). %% the actual record and type declarations

%% debug(Text) -> io:format("writeHrl: ~p~n", [Text]).

%% debug(Text1, Text2) ->
%% io:format("~p ~p~n", [Text1, Text2]).

writeHrl(#model{tps = Types, th = TypeHierarchy, any_attribs = AnyAtts}) ->
[header(AnyAtts), writeTypes(Types, TypeHierarchy, AnyAtts)].
-spec write_hrl(Model::erlsom:model()) -> {hrl_header(), hrl_types()}.
write_hrl(#model{tps = Types, th = TypeHierarchy, any_attribs = AnyAtts}) ->
{header(), writeTypes(Types, TypeHierarchy, AnyAtts)}.

writeHrl(#model{} = Model) ->
{Header, Types} = write_hrl(Model),
[Header, Types].

writeHrlFile(Xsd, Prefix, Namespaces) ->
%% compile file
Expand All @@ -61,15 +70,17 @@ writeXsdHrlFile(Xsd, Options) ->
throw({error, Error})
end.

header(AnyAtts) ->
header() ->
"%% HRL file generated by ERLSOM\n"
"%%\n"
"%% It is possible (and in some cases necessary) to change the name of\n"
"%% the record fields.\n"
"%%\n"
"%% It is possible to add default values, but be aware that these will\n"
"%% only be used when *writing* an xml document.\n\n"
"\n" ++
"\n".

standard_types(AnyAtts) ->
case AnyAtts of
true ->
"-type anyAttrib() :: {{string(), %% name of the attribute\n"
Expand All @@ -88,7 +99,7 @@ header(AnyAtts) ->
" mappedPrefix :: string()}).\n".

writeTypes(Types, TypeHierarchy, AnyAtts) ->
[writeType(T, TypeHierarchy, AnyAtts) || T <- Types].
[standard_types(AnyAtts), [writeType(T, TypeHierarchy, AnyAtts) || T <- Types]].

writeType(#type{nm = '_document'}, _, _) ->
[];
Expand Down

0 comments on commit 1d3afda

Please sign in to comment.