2 Copyright 2011-2015 David Robillard <http://drobilla.net>
4 Permission to use, copy, modify, and/or distribute this software for any
5 purpose with or without fee is hereby granted, provided that the above
6 copyright notice and this permission notice appear in all copies.
8 THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 #include "serd/serd.h"
23 #include "sord_config.h"
24 #include "sord_internal.h"
26 struct SordInserterImpl
{
32 sord_inserter_new(SordModel
* model
,
35 SordInserter
* inserter
= (SordInserter
*)malloc(sizeof(SordInserter
));
36 inserter
->model
= model
;
42 sord_inserter_free(SordInserter
* inserter
)
48 sord_inserter_set_base_uri(SordInserter
* inserter
,
49 const SerdNode
* uri_node
)
51 return serd_env_set_base_uri(inserter
->env
, uri_node
);
55 sord_inserter_set_prefix(SordInserter
* inserter
,
57 const SerdNode
* uri_node
)
59 return serd_env_set_prefix(inserter
->env
, name
, uri_node
);
63 sord_inserter_write_statement(SordInserter
* inserter
,
64 SerdStatementFlags flags
,
65 const SerdNode
* graph
,
66 const SerdNode
* subject
,
67 const SerdNode
* predicate
,
68 const SerdNode
* object
,
69 const SerdNode
* object_datatype
,
70 const SerdNode
* object_lang
)
72 SordWorld
* world
= sord_get_world(inserter
->model
);
73 SerdEnv
* env
= inserter
->env
;
75 SordNode
* g
= sord_node_from_serd_node(world
, env
, graph
, NULL
, NULL
);
76 SordNode
* s
= sord_node_from_serd_node(world
, env
, subject
, NULL
, NULL
);
77 SordNode
* p
= sord_node_from_serd_node(world
, env
, predicate
, NULL
, NULL
);
78 SordNode
* o
= sord_node_from_serd_node(world
, env
, object
,
79 object_datatype
, object_lang
);
82 return SERD_ERR_BAD_ARG
;
85 const SordQuad tup
= { s
, p
, o
, g
};
86 sord_add(inserter
->model
, tup
);
88 sord_node_free(world
, o
);
89 sord_node_free(world
, p
);
90 sord_node_free(world
, s
);
91 sord_node_free(world
, g
);
98 sord_new_reader(SordModel
* model
,
103 SordInserter
* inserter
= sord_inserter_new(model
, env
);
105 SerdReader
* reader
= serd_reader_new(
106 syntax
, inserter
, (void (*)(void*))sord_inserter_free
,
107 (SerdBaseSink
)sord_inserter_set_base_uri
,
108 (SerdPrefixSink
)sord_inserter_set_prefix
,
109 (SerdStatementSink
)sord_inserter_write_statement
,
113 serd_reader_set_default_graph(reader
, sord_node_to_serd_node(graph
));
120 write_statement(SordModel
* sord
,
123 SerdStatementFlags flags
)
125 const SordNode
* s
= tup
[SORD_SUBJECT
];
126 const SordNode
* p
= tup
[SORD_PREDICATE
];
127 const SordNode
* o
= tup
[SORD_OBJECT
];
128 const SordNode
* d
= sord_node_get_datatype(o
);
129 const SerdNode
* ss
= sord_node_to_serd_node(s
);
130 const SerdNode
* sp
= sord_node_to_serd_node(p
);
131 const SerdNode
* so
= sord_node_to_serd_node(o
);
132 const SerdNode
* sd
= sord_node_to_serd_node(d
);
134 const char* lang_str
= sord_node_get_language(o
);
135 size_t lang_len
= lang_str
? strlen(lang_str
) : 0;
136 SerdNode language
= SERD_NODE_NULL
;
138 language
.type
= SERD_LITERAL
;
139 language
.n_bytes
= lang_len
;
140 language
.n_chars
= lang_len
;
141 language
.buf
= (const uint8_t*)lang_str
;
144 // TODO: Subject abbreviation
146 if (sord_node_is_inline_object(s
) && !(flags
& SERD_ANON_CONT
)) {
150 SerdStatus st
= SERD_SUCCESS
;
151 if (sord_node_is_inline_object(o
)) {
152 SordQuad sub_pat
= { o
, 0, 0, 0 };
153 SordIter
* sub_iter
= sord_find(sord
, sub_pat
);
155 SerdStatementFlags start_flags
= flags
156 | ((sub_iter
) ? SERD_ANON_O_BEGIN
: SERD_EMPTY_O
);
158 st
= serd_writer_write_statement(
159 writer
, start_flags
, NULL
, ss
, sp
, so
, sd
, &language
);
161 if (!st
&& sub_iter
) {
162 flags
|= SERD_ANON_CONT
;
163 for (; !st
&& !sord_iter_end(sub_iter
); sord_iter_next(sub_iter
)) {
165 sord_iter_get(sub_iter
, sub_tup
);
166 st
= write_statement(sord
, writer
, sub_tup
, flags
);
168 sord_iter_free(sub_iter
);
169 serd_writer_end_anon(writer
, so
);
172 st
= serd_writer_write_statement(
173 writer
, flags
, NULL
, ss
, sp
, so
, sd
, &language
);
180 sord_write(SordModel
* model
,
184 SordQuad pat
= { 0, 0, 0, graph
};
185 SordIter
* iter
= sord_find(model
, pat
);
186 return sord_write_iter(iter
, writer
);
190 sord_write_iter(SordIter
* iter
,
197 SordModel
* model
= (SordModel
*)sord_iter_get_model(iter
);
198 SerdStatus st
= SERD_SUCCESS
;
199 for (; !st
&& !sord_iter_end(iter
); sord_iter_next(iter
)) {
201 sord_iter_get(iter
, tup
);
202 st
= write_statement(model
, writer
, tup
, 0);
204 sord_iter_free(iter
);