View source with formatted 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
   43/** <module> XML-Schema primitive types
   44
   45This  modules  provides  support  for  the  primitive  XML-Schema  (XSD)
   46datatypes. It defines the type hierarchy which allows for reasoning over
   47types as well as xsdp_convert/3 to  convert   XML  content  to a natural
   48Prolog representation of the XSD type.
   49
   50Based on the W3C definitions at
   51
   52        * http://www.w3.org/TR/xmlschema-2/#built-in-datatypes
   53
   54The current implementation is incomplete and only  there to test the API
   55and its integration with rdf:dataType=Type handling in the RDF parser.
   56
   57The extra 'p' in the module  prefix  (xsdp_*)   is  used  to allow for a
   58module xsd_*, providing full  user-defined  XSD   types  on  top of this
   59module.
   60*/
   61
   62ns('http://www.w3.org/2001/XMLSchema#').
   63
   64
   65                 /*******************************
   66                 *    PRIMITIVE TYPE HIERARCHY  *
   67                 *******************************/
   68
   69%!  xsdp_type(?Type)
   70%
   71%   Test/generate the names for the XML schema primitive types
   72
   73xsdp_type(Type) :-
   74    subtype_of(Type, _).
   75
   76%!  xsdp_uri_type(?URI, ?Type)
   77%
   78%   True if URI is the URI for the the XML-Schema primitive Type.
   79
   80xsdp_uri_type(URI, Type) :-
   81    xsd_local_id(URI, Type),
   82    subtype_of(Type, _).
   83
   84%!  xsdp_subtype_of(?Type, ?Super)
   85%
   86%   True if Type is a (transitive) subtype of Super.
   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                                        % string hierarchy
  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                                        % numeric hierarchy
  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                                        % other simple types
  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
  152%!  xsdp_numeric_uri(?URI, -PromoteURI) is nondet.
  153%
  154%   Table mapping all XML-Schema numeric  URIs   into  the type they
  155%   promote to. Types are promoted   to =integer=, =float=, =double=
  156%   and =decimal=.
  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
  184%!  xsdp_convert(+Type, +Content, -Value)
  185%
  186%   Convert the content model Content to an  object of the given XSD
  187%   type and return the Prolog value in Value.
  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                                        % strings
  198convert(string, [String], String) :- !.
  199                                        % numbers
  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) :- !.             % TBD: provide for more types
  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)