View source with raw comments or as raw
    1/*  Part of SWI-Prolog
    2
    3    Author:        Jan Wielemaker
    4    E-mail:        J.Wielemaker@vu.nl
    5    WWW:           http://www.swi-prolog.org
    6    Copyright (c)  2004-2015, University of Amsterdam
    7    All rights reserved.
    8
    9    Redistribution and use in source and binary forms, with or without
   10    modification, are permitted provided that the following conditions
   11    are met:
   12
   13    1. Redistributions of source code must retain the above copyright
   14       notice, this list of conditions and the following disclaimer.
   15
   16    2. Redistributions in binary form must reproduce the above copyright
   17       notice, this list of conditions and the following disclaimer in
   18       the documentation and/or other materials provided with the
   19       distribution.
   20
   21    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   22    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   23    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   24    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   25    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   26    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   27    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   28    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   29    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   30    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   31    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   32    POSSIBILITY OF SUCH DAMAGE.
   33*/
   34
   35:- module(xsdp_type,
   36          [ xsdp_type/1,                % ?Type
   37            xsdp_uri_type/2,            % ?URI, ?Type
   38            xsdp_numeric_uri/2,         % ?URI, ?Primary
   39            xsdp_subtype_of/2,          % ?Type, ?Super
   40            xsdp_convert/3              % +Type, +Content, -Value
   41          ]).   42:- autoload(library(sgml), [xsd_number_string/2]).

XML-Schema primitive types

This modules provides support for the primitive XML-Schema (XSD) datatypes. It defines the type hierarchy which allows for reasoning over types as well as xsdp_convert/3 to convert XML content to a natural Prolog representation of the XSD type.

Based on the W3C definitions at

The current implementation is incomplete and only there to test the API and its integration with rdf:dataType=Type handling in the RDF parser.

The extra 'p' in the module prefix (xsdp_*) is used to allow for a module xsd_*, providing full user-defined XSD types on top of this module.

   63ns('http://www.w3.org/2001/XMLSchema#').
   64
   65
   66                 /*******************************
   67                 *    PRIMITIVE TYPE HIERARCHY  *
   68                 *******************************/
 xsdp_type(?Type)
Test/generate the names for the XML schema primitive types
   74xsdp_type(Type) :-
   75    subtype_of(Type, _).
 xsdp_uri_type(?URI, ?Type)
True if URI is the URI for the the XML-Schema primitive Type.
   81xsdp_uri_type(URI, Type) :-
   82    xsd_local_id(URI, Type),
   83    subtype_of(Type, _).
 xsdp_subtype_of(?Type, ?Super)
True if Type is a (transitive) subtype of Super.
   89xsdp_subtype_of(Type, Type) :-
   90    subtype_of(Type, _).
   91xsdp_subtype_of(Type, Super) :-
   92    (   nonvar(Type)
   93    ->  subtype_of(Type, Super0),
   94        Super0 \== (-),
   95        xsdp_subtype_of(Super0, Super)
   96    ;   nonvar(Super)
   97    ->  subtype_of(Sub0, Super),
   98        xsdp_subtype_of(Type, Sub0)
   99    ;   subtype_of(Type, _),
  100        xsdp_subtype_of(Type, Super)
  101    ).
  102
  103subtype_of(anyType,            -).
  104subtype_of(anySimpleType,      anyType).
  105                                        % string hierarchy
  106subtype_of(string,             anySimpleType).
  107subtype_of(normalizedString,   string).
  108subtype_of(token,              normalizedString).
  109subtype_of(language,           token).
  110subtype_of('Name',             token).
  111subtype_of('NCName',           'Name').
  112subtype_of('NMTOKEN',          token).
  113subtype_of('NMTOKENS',         'NMTOKEN').
  114subtype_of('ID',               'NCName').
  115subtype_of('IDREF',            'NCName').
  116subtype_of('IDREFS',           'IDREF').
  117subtype_of('ENTITY',           'NCName').
  118subtype_of('ENTITIES',         'ENTITY').
  119                                        % numeric hierarchy
  120subtype_of(decimal,            anySimpleType).
  121subtype_of(integer,            decimal).
  122subtype_of(nonPositiveInteger, integer).
  123subtype_of(negativeInteger,    nonPositiveInteger).
  124subtype_of(long,               integer).
  125subtype_of(int,                long).
  126subtype_of(short,              int).
  127subtype_of(byte,               short).
  128subtype_of(nonNegativeInteger, integer).
  129subtype_of(unsignedLong,       nonNegativeInteger).
  130subtype_of(positiveInteger,    nonNegativeInteger).
  131subtype_of(unsignedInt,        unsignedLong).
  132subtype_of(unsignedShort,      unsignedInt).
  133subtype_of(unsignedByte,       unsignedShort).
  134                                        % other simple types
  135subtype_of(duration,           anySimpleType).
  136subtype_of(dateTime,           anySimpleType).
  137subtype_of(time,               anySimpleType).
  138subtype_of(date,               anySimpleType).
  139subtype_of(gYearMonth,         anySimpleType).
  140subtype_of(gYear,              anySimpleType).
  141subtype_of(gMonthDay,          anySimpleType).
  142subtype_of(gDay,               anySimpleType).
  143subtype_of(gMonth,             anySimpleType).
  144subtype_of(boolean,            anySimpleType).
  145subtype_of(base64Binary,       anySimpleType).
  146subtype_of(hexBinary,          anySimpleType).
  147subtype_of(float,              anySimpleType).
  148subtype_of(double,             anySimpleType).
  149subtype_of(anyURI,             anySimpleType).
  150subtype_of('QName',            anySimpleType).
  151subtype_of('NOTATION',         anySimpleType).
 xsdp_numeric_uri(?URI, -PromoteURI) is nondet
Table mapping all XML-Schema numeric URIs into the type they promote to. Types are promoted to integer, float, double and decimal.
  159term_expansion(integer_types, Clauses) :-
  160    findall(integer_type(Type), xsdp_subtype_of(Type, integer), Clauses).
  161term_expansion(xsd_local_ids, Clauses) :-
  162    ns(NS),
  163    findall(xsd_local_id(URI, Type),
  164            (   xsdp_type(Type),
  165                atom_concat(NS, Type, URI)
  166            ),
  167            Clauses).
  168term_expansion(numeric_uirs, Clauses) :-
  169    findall(xsdp_numeric_uri(URI, PrimaryURI),
  170            (   (   integer_type(Type),     Primary = integer
  171                ;   Type = float,           Primary = float
  172                ;   Type = double,          Primary = double
  173                ;   Type = decimal,         Primary = decimal
  174                ),
  175                xsd_local_id(URI, Type),
  176                xsd_local_id(PrimaryURI, Primary)
  177            ),
  178            Clauses).
  179
  180integer_types.
  181xsd_local_ids.
  182
  183numeric_uirs.
 xsdp_convert(+Type, +Content, -Value)
Convert the content model Content to an object of the given XSD type and return the Prolog value in Value.
  190xsdp_convert(URI, Content, Value) :-
  191    (   xsd_local_id(URI, Type)
  192    ->  convert(Type, Content, Value)
  193    ;   convert(URI, Content, Value)
  194    ).
  195
  196convert(anyType, Term, Term) :- !.
  197convert(anySimpleType, [Simple], Simple) :- !.
  198                                        % strings
  199convert(string, [String], String) :- !.
  200                                        % numbers
  201convert(IntType, [Text], Integer) :-
  202    integer_type(IntType),
  203    !,
  204    (   xsd_number(Text, Integer),
  205        integer(Integer),
  206        validate_int_domain(IntType, Integer)
  207    ->  true
  208    ;   throw(error(domain_error(IntType, Text), _))
  209    ).
  210convert(float, [Text], Float) :-
  211    !,
  212    xsd_float(Text, float, Float).
  213convert(double, [Text], Float) :-
  214    !,
  215    xsd_float(Text, double, Float).
  216convert(_Any, [X], X) :- !.             % TBD: provide for more types
  217convert(_Any, X, X).
  218
  219%   xsd_number(+Text, -Number) is semidet.
  220%
  221%   Parse Text using the XSD lexical rules, allowing for leading and
  222%   trailing white space.
  223
  224xsd_number(Text, Number) :-
  225    split_string(Text, "", " \t\r\n", [Trimmed]),
  226    catch(xsd_number_string(Number, Trimmed), error(syntax_error(_), _), fail).
  227
  228xsd_float(Text, Type, Float) :-
  229    (   xsd_number(Text, Number)
  230    ->  Float is float(Number)
  231    ;   throw(error(domain_error(Type, Text), _))
  232    ).
  233
  234validate_int_domain(integer, _).
  235validate_int_domain(int, I) :-   between(-2147483648, 2147483647, I).
  236validate_int_domain(long, I) :-  between(-9223372036854775808,
  237                                         9223372036854775807, I).
  238validate_int_domain(nonPositiveInteger, I) :- \+ I > 0.
  239validate_int_domain(negativeInteger, I) :-    I < 0.
  240validate_int_domain(short, I) :-              between(-32768, 32767, I).
  241validate_int_domain(byte,  I) :-              between(-128, 127, I).
  242validate_int_domain(nonNegativeInteger, I) :- \+ I < 0.
  243validate_int_domain(unsignedLong,       I) :- between(0, 18446744073709551615, I).
  244validate_int_domain(positiveInteger,    I) :- I > 0.
  245validate_int_domain(unsignedInt,        I) :- between(0, 4294967295, I).
  246validate_int_domain(unsignedShort,      I) :- between(0, 65535, I).
  247validate_int_domain(unsignedByte,       I) :- between(0, 255, I)