|
17 | 17 | (parsed [x]))
|
18 | 18 |
|
19 | 19 | (extend-protocol Parseable
|
| 20 | + jakarta.json.EmptyArray |
| 21 | + (parsed [x] []) |
20 | 22 | ;; JsonArray
|
21 | 23 | org.glassfish.json.JsonArrayBuilderImpl$JsonArrayImpl
|
22 | 24 | (parsed [x]
|
|
50 | 52 | (StringReader.)
|
51 | 53 | (JsonDocument/of)))
|
52 | 54 |
|
53 |
| -(defrecord StaticLoader [] |
| 55 | +(defn ->document |
| 56 | + "Takes a document-loader, which takes a url and options and returns a json string |
| 57 | + context document (must have an \"@context\" key with a context as its value). |
| 58 | +
|
| 59 | + document-loader: [url options] => json-string |
| 60 | + " |
| 61 | + [document-loader url options] |
| 62 | + (let [url-string (.toString ^URI url)] |
| 63 | + (try |
| 64 | + (let [json-string (document-loader url-string options)] |
| 65 | + (JsonDocument/of (io/input-stream (.getBytes ^String json-string)))) |
| 66 | + (catch Exception e |
| 67 | + (throw (JsonLdError. JsonLdErrorCode/LOADING_REMOTE_CONTEXT_FAILED |
| 68 | + (str "Unable to load context: " url-string))))))) |
| 69 | + |
| 70 | +(defrecord PluggableLoader [document-loader] |
54 | 71 | DocumentLoader
|
55 | 72 | (loadDocument [_ url options]
|
56 |
| - (if-let [{path :source} (external/context->file (.toString url))] |
57 |
| - (-> (FileLoader.) |
58 |
| - (.loadDocument (.toURI (io/resource path)) |
59 |
| - (DocumentLoaderOptions.))) |
60 |
| - (throw (JsonLdError. JsonLdErrorCode/LOADING_REMOTE_CONTEXT_FAILED |
61 |
| - (str "Unable to load static context: " (.toString url))))))) |
| 73 | + (->document document-loader url options))) |
| 74 | + |
| 75 | +(defn static-loader |
| 76 | + [url options] |
| 77 | + (if-let [{path :source} (external/context->file url)] |
| 78 | + (slurp (io/resource path)) |
| 79 | + (throw (ex-info (str "Unable to load static context: " url) |
| 80 | + {:url url})))) |
62 | 81 |
|
63 | 82 | (defn expand
|
64 |
| - [json-ld] |
65 |
| - (-> (->json-document json-ld) |
66 |
| - (JsonLd/expand) |
67 |
| - (.loader ^DocumentLoader (->StaticLoader)) |
68 |
| - (.get) |
69 |
| - (parsed))) |
| 83 | + ([json-ld] |
| 84 | + (expand json-ld {:document-loader static-loader})) |
| 85 | + ([json-ld opts] |
| 86 | + (-> (->json-document json-ld) |
| 87 | + (JsonLd/expand) |
| 88 | + (.loader ^DocumentLoader (->PluggableLoader (:document-loader opts))) |
| 89 | + (.get) |
| 90 | + (parsed)))) |
70 | 91 |
|
71 | 92 | (defn compact
|
72 |
| - [json-ld context] |
73 |
| - (-> (->json-document json-ld) |
74 |
| - (JsonLd/compact (->json-document context)) |
75 |
| - (.loader ^DocumentLoader (->StaticLoader)) |
76 |
| - (.get) |
77 |
| - (parsed))) |
| 93 | + ([json-ld context] |
| 94 | + (compact json-ld context {:document-loader static-loader})) |
| 95 | + ([json-ld context opts] |
| 96 | + (-> (->json-document json-ld) |
| 97 | + (JsonLd/compact (->json-document context)) |
| 98 | + (.loader ^DocumentLoader (->PluggableLoader (:document-loader opts))) |
| 99 | + (.get) |
| 100 | + (parsed)))) |
78 | 101 |
|
79 | 102 | (defn flatten
|
80 |
| - [json-ld] |
81 |
| - (-> (->json-document json-ld) |
82 |
| - (JsonLd/flatten) |
83 |
| - (.loader ^DocumentLoader (->StaticLoader)) |
84 |
| - (.get) |
85 |
| - (parsed))) |
| 103 | + ([json-ld] |
| 104 | + (flatten json-ld {:document-loader static-loader})) |
| 105 | + ([json-ld opts] |
| 106 | + (-> (->json-document json-ld) |
| 107 | + (JsonLd/flatten) |
| 108 | + (.loader ^DocumentLoader (->PluggableLoader (:document-loader opts))) |
| 109 | + (.get) |
| 110 | + (parsed)))) |
86 | 111 |
|
87 | 112 | (defn- ->statement
|
88 | 113 | [^RdfNQuad quad]
|
|
114 | 139 | " ."))
|
115 | 140 |
|
116 | 141 | (defn to-rdf
|
117 |
| - [json-ld] |
118 |
| - (-> (->json-document json-ld) |
119 |
| - (JsonLd/toRdf) |
120 |
| - (.loader ^DocumentLoader (->StaticLoader)) |
121 |
| - (.get) |
122 |
| - (.toList) |
123 |
| - (->> (reduce (fn [doc quad] (str doc (->statement quad) "\n")) "")))) |
| 142 | + ([json-ld] |
| 143 | + (to-rdf json-ld {:document-loader static-loader})) |
| 144 | + ([json-ld opts] |
| 145 | + (-> (->json-document json-ld) |
| 146 | + (JsonLd/toRdf) |
| 147 | + (.loader ^DocumentLoader (->PluggableLoader (:document-loader opts))) |
| 148 | + (.get) |
| 149 | + (.toList) |
| 150 | + (->> (reduce (fn [doc quad] (str doc (->statement quad) "\n")) ""))))) |
124 | 151 |
|
125 | 152 | (defn canonize
|
126 |
| - [json-ld] |
127 |
| - (-> (->json-document json-ld) |
128 |
| - (JsonLd/toRdf) |
129 |
| - (.loader ^DocumentLoader (->StaticLoader)) |
130 |
| - (.get) |
131 |
| - (RdfNormalize/normalize) |
132 |
| - (.toList) |
133 |
| - (->> (reduce (fn [doc quad] (str doc (->statement quad) "\n")) "")))) |
| 153 | + ([json-ld] |
| 154 | + (canonize json-ld {:document-loader static-loader})) |
| 155 | + ([json-ld opts] |
| 156 | + (-> (->json-document json-ld) |
| 157 | + (JsonLd/toRdf) |
| 158 | + (.loader ^DocumentLoader (->PluggableLoader (:document-loader opts))) |
| 159 | + (.get) |
| 160 | + (RdfNormalize/normalize) |
| 161 | + (.toList) |
| 162 | + (->> (reduce (fn [doc quad] (str doc (->statement quad) "\n")) ""))))) |
134 | 163 |
|
135 | 164 | (comment
|
136 | 165 | ;; These work up to translating the resulting json-ld back into edn
|
|
0 commit comments