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).
   89xsdp_subtype_of(Type, Super) :-
   90    (   nonvar(Type)
   91    ->  subtype_of(Type, Super0),
   92        Super0 \== (-),
   93        xsdp_subtype_of(Super0, Super)
   94    ;   subtype_of(Sub0, Super),
   95        xsdp_subtype_of(Type, Sub0)
   96    ).
   97
   98subtype_of(anyType,            -).
   99subtype_of(anySimpleType,      anyType).
  100                                        % string hierarchy
  101subtype_of(string,             anySimpleType).
  102subtype_of(normalizedString,   string).
  103subtype_of(token,              normalizedString).
  104subtype_of(language,           token).
  105subtype_of('Name',             token).
  106subtype_of('NCName',           'Name').
  107subtype_of('NMTOKEN',          token).
  108subtype_of('NMTOKENS',         'NMTOKEN').
  109subtype_of('ID',               'NCName').
  110subtype_of('IDREF',            'NCName').
  111subtype_of('IDREFS',           'IDREF').
  112subtype_of('ENTITY',           'NCName').
  113subtype_of('ENTITIES',         'ENTITY').
  114                                        % numeric hierarchy
  115subtype_of(decimal,            anySimpleType).
  116subtype_of(integer,            decimal).
  117subtype_of(nonPositiveInteger, integer).
  118subtype_of(negativeInteger,    nonPositiveInteger).
  119subtype_of(long,               integer).
  120subtype_of(int,                long).
  121subtype_of(short,              int).
  122subtype_of(byte,               short).
  123subtype_of(nonNegativeInteger, integer).
  124subtype_of(unsignedLong,       nonNegativeInteger).
  125subtype_of(positiveInteger,    nonNegativeInteger).
  126subtype_of(unsignedInt,        unsignedLong).
  127subtype_of(unsignedShort,      unsignedInt).
  128subtype_of(unsignedByte,       unsignedShort).
  129                                        % other simple types
  130subtype_of(duration,           anySimpleType).
  131subtype_of(dateTime,           anySimpleType).
  132subtype_of(time,               anySimpleType).
  133subtype_of(date,               anySimpleType).
  134subtype_of(gYearMonth,         anySimpleType).
  135subtype_of(gYear,              anySimpleType).
  136subtype_of(gMonthDay,          anySimpleType).
  137subtype_of(gDay,               anySimpleType).
  138subtype_of(gMonth,             anySimpleType).
  139subtype_of(boolean,            anySimpleType).
  140subtype_of(base64Binary,       anySimpleType).
  141subtype_of(hexBinary,          anySimpleType).
  142subtype_of(float,              anySimpleType).
  143subtype_of(double,             anySimpleType).
  144subtype_of(anyURI,             anySimpleType).
  145subtype_of('QName',            anySimpleType).
  146subtype_of('NOTATION',         anySimpleType).
  147
  148%!  xsdp_numeric_uri(?URI, -PromoteURI) is nondet.
  149%
  150%   Table mapping all XML-Schema numeric  URIs   into  the type they
  151%   promote to. Types are promoted   to =integer=, =float=, =double=
  152%   and =decimal=.
  153
  154term_expansion(integer_types, Clauses) :-
  155    findall(integer_type(Type), xsdp_subtype_of(Type, integer), Clauses).
  156term_expansion(xsd_local_ids, Clauses) :-
  157    ns(NS),
  158    findall(xsd_local_id(URI, Type),
  159            (   xsdp_type(Type),
  160                atom_concat(NS, Type, URI)
  161            ),
  162            Clauses).
  163term_expansion(numeric_uirs, Clauses) :-
  164    findall(xsdp_numeric_uri(URI, PrimaryURI),
  165            (   (   integer_type(Type),     Primary = integer
  166                ;   Type = float,           Primary = float
  167                ;   Type = double,          Primary = double
  168                ;   Type = decimal,         Primary = decimal
  169                ),
  170                xsd_local_id(URI, Type),
  171                xsd_local_id(PrimaryURI, Primary)
  172            ),
  173            Clauses).
  174
  175integer_types.
  176xsd_local_ids.
  177
  178numeric_uirs.
  179
  180%!  xsdp_convert(+Type, +Content, -Value)
  181%
  182%   Convert the content model Content to an  object of the given XSD
  183%   type and return the Prolog value in Value.
  184
  185xsdp_convert(URI, Content, Value) :-
  186    (   xsd_local_id(URI, Type)
  187    ->  convert(Type, Content, Value)
  188    ;   convert(URI, Content, Value)
  189    ).
  190
  191convert(anyType, Term, Term) :- !.
  192convert(anySimpleType, [Simple], Simple) :- !.
  193                                        % strings
  194convert(string, [String], String) :- !.
  195                                        % numbers
  196convert(IntType, [Text], Integer) :-
  197    integer_type(IntType),
  198    !,
  199    atom_number(Text, Integer),
  200    (   integer(Integer),
  201        validate_int_domain(IntType, Integer)
  202    ->  true
  203    ;   throw(error(domain_error(Text, IntType), _))
  204    ).
  205convert(float, [Text], Float) :-
  206    !,
  207    atom_number(Text, Number),
  208    Float is float(Number).
  209convert(double, [Text], Float) :-
  210    !,
  211    atom_number(Text, Number),
  212    Float is float(Number).
  213convert(_Any, [X], X) :- !.             % TBD: provide for more types
  214convert(_Any, X, X).
  215
  216validate_int_domain(integer, _).
  217validate_int_domain(int, _).
  218validate_int_domain(long, _).
  219validate_int_domain(nonPositiveInteger, I) :- \+ I > 0.
  220validate_int_domain(negativeInteger, I) :-    I < 0.
  221validate_int_domain(short, I) :-              between(-32768, 32767, I).
  222validate_int_domain(byte,  I) :-              between(-128, 127, I).
  223validate_int_domain(nonNegativeInteger, I) :- \+ I < 0.
  224validate_int_domain(unsignedLong,       I) :- I >= 0.
  225validate_int_domain(positiveInteger,    I) :- I > 0.
  226validate_int_domain(unsignedInt,        I) :- I >= 0.
  227validate_int_domain(unsignedShort,      I) :- between(0, 65535, I).
  228validate_int_domain(unsignedByte,       I) :- between(0, 255, I)