Changeset 453 for trunk/src/json/JTM


Ignore:
Timestamp:
05/05/11 10:51:40 (14 years ago)
Author:
lgiessmann
Message:

JTM: added some fundamental functions for the jtm-importer that resolves jtm-references to the corresponding Topic Maps items => added also some unit-tests for these functions

Location:
trunk/src/json/JTM
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • TabularUnified trunk/src/json/JTM/jtm_importer.lisp

    r429 r453  
    1010
    1111(in-package :jtm)
     12
     13
     14(defun get-item-from-jtm-reference (reference-string &key (revision *TM-REVISION*)
     15                                    prefixes)
     16  "Returns a ReifiableConstructC that is bound to the reference that is
     17   passed to this function. If the construct cannot be found the error
     18   tm-reference-error is thrown."
     19  (declare (Integer revision)
     20           (List prefixes)
     21           (String reference-string))
     22  (let* ((identifier-type
     23          (get-identifier-type-from-jtm-reference reference-string))
     24         (identifier-value (subseq reference-string 3))
     25         (identifier-uri
     26          (compute-uri-from-jtm-identifier identifier-value prefixes))
     27         (construct
     28          (d::get-item-by-identifier identifier-uri :revision revision
     29                                     :identifier-type-symbol identifier-type)))
     30    (if construct
     31        construct
     32        (error (make-condition 'missing-reference-error :message (format nil "From get-item-from-jtm-reference(): cannot find the item identified by \"~a\"(~a)" identifier-uri reference-string)
     33                               :reference identifier-uri)))))   
     34
     35
     36(defun get-items-from-jtm-references (reference-strings &key (revision *TM-REVISion*)
     37                                      prefixes)
     38  "Returns a list of ReifiableConstructCs that are referenced via the
     39   string-values in reference-strings."
     40  (declare (List reference-strings prefixes)
     41           (Integer revision))
     42  (map 'list #'(lambda(reference-string)
     43                 (get-item-from-jtm-reference reference-string :revision revision
     44                                              :prefixes prefixes))
     45       reference-strings))
     46
     47
     48(defun compute-uri-from-jtm-identifier (identifier-value prefixes)
     49  "Returns the full uri of an identifier string, i.e.
     50   * if the value is of the form '[pref:value]' the return value is
     51     the concatenation of 'value-of-pref' and 'value'.
     52   * if the value is of the form 'full-uri' the return value is
     53     'full-uri'"
     54  (declare (String identifier-value)
     55           (List prefixes))
     56  (cond ((and (string-starts-with identifier-value "[")
     57              (string-ends-with identifier-value "]"))
     58         (let* ((pref-name
     59                 (let ((value (string-until identifier-value ":")))
     60                   (when value
     61                     (subseq value 1))))
     62                (suffix
     63                 (when pref-name
     64                   (let ((value
     65                          (subseq identifier-value (1+ (length pref-name)))))
     66                     (when value
     67                       (subseq value (min 1 (length value))
     68                               (max 0 (1- (length value)))))))))
     69           (when (or (not pref-name) (not suffix))
     70             (error (make-condition 'JTM-error :message (format nil "From compute-uri-from-jtm-identifier: the section within the range of \"[\" and \"]\" must be of the form prefix:suffix, but is: \"~a\"" identifier-value))))
     71           (compute-full-uri prefixes pref-name suffix)))
     72        ((> (length identifier-value) 0)
     73         identifier-value)
     74        (t
     75         (error (make-condition 'JTM-error :message (format nil "From compute-uri-from-jtm-identifier(): the identifier-value must be of the form \"[pref:value]\" or \"full-uri\", but is: \"~a\"" identifier-value))))))
     76
     77
     78(defun get-identifier-type-from-jtm-reference (identifier-string)
     79  "Returns the symbol 'PersistentIdC if identifier-string starts
     80   with si:, 'SubjectLocatorC if identifier-string starts with
     81   sl:, or 'ItemIdentifierC if identifier-string starts with ii:.
     82   If identifier-string do not start with one of these strings
     83   the error JTM-error is thrown."
     84  (cond ((string-starts-with identifier-string "ii:")
     85         'ItemIdentifierC)
     86        ((string-starts-with identifier-string "si:")
     87         'PersistentIdC)
     88        ((string-starts-with identifier-string "sl:")
     89         'SubjectLocatorC)
     90        (t
     91         (error (make-condition 'JTM-error :message (format nil "From get-identifier-type(): the identifier value must start with one of \"ii:\", \"si:\", or \"sl:\", but is: \"~a\"" identifier-string))))))
  • TabularUnified trunk/src/json/JTM/jtm_tools.lisp

    r450 r453  
    4646  (:documentation "Returns a list of the following structure:
    4747                   ((:pref 'pref_1' :value 'uri-pref') (...))."))
     48
     49
     50(defun compute-full-uri (prefix-list prefix-name suffix)
     51  "Returns a full uri if the prefix-name can found in an item of the form
     52   (:pref 'prefix-name' :value 'uri-value/'), the returnvalue would be of
     53   the form uri-value/suffix. Otherwise the return value is nil."
     54  (declare (List prefix-list)
     55           (String prefix-name suffix))
     56  (if (= (length suffix) 0)
     57      (error (make-condition 'JTM-error :message (format nil "From compute-full-uri(): suffix must no be of length 0, but is \"~a\"" suffix)))
     58      (let ((result (loop for item in prefix-list
     59                       when (string= (getf item :pref) prefix-name)
     60                       return (concat (getf item :value) suffix))))
     61        (if result
     62            result
     63            (error (make-condition 'JTM-error :message (format nil "From compute-full-uri(): prefix \"~a\" not found in the available prefixes \"~a\"" prefix-name prefix-list)))))))
    4864
    4965
Note: See TracChangeset for help on using the changeset viewer.