34
35:- module(xsdp_type,
36 [ xsdp_type/1, 37 xsdp_uri_type/2, 38 xsdp_numeric_uri/2, 39 xsdp_subtype_of/2, 40 xsdp_convert/3 41 ]).
42
43
62
63ns('http://www.w3.org/2001/XMLSchema#').
64
65
66 69
73
74xsdp_type(Type) :-
75 subtype_of(Type, _).
76
80
81xsdp_uri_type(URI, Type) :-
82 xsd_local_id(URI, Type),
83 subtype_of(Type, _).
84
88
89xsdp_subtype_of(Type, Type).
90xsdp_subtype_of(Type, Super) :-
91 ( nonvar(Type)
92 -> subtype_of(Type, Super0),
93 Super0 \== (-),
94 xsdp_subtype_of(Super0, Super)
95 ; subtype_of(Sub0, Super),
96 xsdp_subtype_of(Type, Sub0)
97 ).
98
99subtype_of(anyType, -).
100subtype_of(anySimpleType, anyType).
101 102subtype_of(string, anySimpleType).
103subtype_of(normalizedString, string).
104subtype_of(token, normalizedString).
105subtype_of(language, token).
106subtype_of('Name', token).
107subtype_of('NCName', 'Name').
108subtype_of('NMTOKEN', token).
109subtype_of('NMTOKENS', 'NMTOKEN').
110subtype_of('ID', 'NCName').
111subtype_of('IDREF', 'NCName').
112subtype_of('IDREFS', 'IDREF').
113subtype_of('ENTITY', 'NCName').
114subtype_of('ENTITIES', 'ENTITY').
115 116subtype_of(decimal, anySimpleType).
117subtype_of(integer, decimal).
118subtype_of(nonPositiveInteger, integer).
119subtype_of(negativeInteger, nonPositiveInteger).
120subtype_of(long, integer).
121subtype_of(int, long).
122subtype_of(short, int).
123subtype_of(byte, short).
124subtype_of(nonNegativeInteger, integer).
125subtype_of(unsignedLong, nonNegativeInteger).
126subtype_of(positiveInteger, nonNegativeInteger).
127subtype_of(unsignedInt, unsignedLong).
128subtype_of(unsignedShort, unsignedInt).
129subtype_of(unsignedByte, unsignedShort).
130 131subtype_of(duration, anySimpleType).
132subtype_of(dateTime, anySimpleType).
133subtype_of(time, anySimpleType).
134subtype_of(date, anySimpleType).
135subtype_of(gYearMonth, anySimpleType).
136subtype_of(gYear, anySimpleType).
137subtype_of(gMonthDay, anySimpleType).
138subtype_of(gDay, anySimpleType).
139subtype_of(gMonth, anySimpleType).
140subtype_of(boolean, anySimpleType).
141subtype_of(base64Binary, anySimpleType).
142subtype_of(hexBinary, anySimpleType).
143subtype_of(float, anySimpleType).
144subtype_of(double, anySimpleType).
145subtype_of(anyURI, anySimpleType).
146subtype_of('QName', anySimpleType).
147subtype_of('NOTATION', anySimpleType).
148
154
155term_expansion(integer_types, Clauses) :-
156 findall(integer_type(Type), xsdp_subtype_of(Type, integer), Clauses).
157term_expansion(xsd_local_ids, Clauses) :-
158 ns(NS),
159 findall(xsd_local_id(URI, Type),
160 ( xsdp_type(Type),
161 atom_concat(NS, Type, URI)
162 ),
163 Clauses).
164term_expansion(numeric_uirs, Clauses) :-
165 findall(xsdp_numeric_uri(URI, PrimaryURI),
166 ( ( integer_type(Type), Primary = integer
167 ; Type = float, Primary = float
168 ; Type = double, Primary = double
169 ; Type = decimal, Primary = decimal
170 ),
171 xsd_local_id(URI, Type),
172 xsd_local_id(PrimaryURI, Primary)
173 ),
174 Clauses).
175
176integer_types.
177xsd_local_ids.
178
179numeric_uirs.
180
185
186xsdp_convert(URI, Content, Value) :-
187 ( xsd_local_id(URI, Type)
188 -> convert(Type, Content, Value)
189 ; convert(URI, Content, Value)
190 ).
191
192convert(anyType, Term, Term) :- !.
193convert(anySimpleType, [Simple], Simple) :- !.
194 195convert(string, [String], String) :- !.
196 197convert(IntType, [Text], Integer) :-
198 integer_type(IntType),
199 !,
200 atom_number(Text, Integer),
201 ( integer(Integer),
202 validate_int_domain(IntType, Integer)
203 -> true
204 ; throw(error(domain_error(Text, IntType), _))
205 ).
206convert(float, [Text], Float) :-
207 !,
208 atom_number(Text, Number),
209 Float is float(Number).
210convert(double, [Text], Float) :-
211 !,
212 atom_number(Text, Number),
213 Float is float(Number).
214convert(_Any, [X], X) :- !. 215convert(_Any, X, X).
216
217validate_int_domain(integer, _).
218validate_int_domain(int, _).
219validate_int_domain(long, _).
220validate_int_domain(nonPositiveInteger, I) :- \+ I > 0.
221validate_int_domain(negativeInteger, I) :- I < 0.
222validate_int_domain(short, I) :- between(-32768, 32767, I).
223validate_int_domain(byte, I) :- between(-128, 127, I).
224validate_int_domain(nonNegativeInteger, I) :- \+ I < 0.
225validate_int_domain(unsignedLong, I) :- I >= 0.
226validate_int_domain(positiveInteger, I) :- I > 0.
227validate_int_domain(unsignedInt, I) :- I >= 0.
228validate_int_domain(unsignedShort, I) :- between(0, 65535, I).
229validate_int_domain(unsignedByte, I) :- between(0, 255, I).