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