1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
use super::grammar::{boolean, decimal, double, integer, pn_local};
use super::grammar_structs::Literal;
use constants;
use error::{Error, Result};
use graph::*;
use namespaces::*;
use nom::types::CompleteStr;
use nom::IResult;
use std::fmt::Display;
use std::io::Write;
use std::iter::Peekable;

struct TurtleWriter<'a, 'g, W: 'a, G: 'g>
where
    W: Write,
    G: Graph<'g>,
{
    buffer: Vec<u8>,
    base: String,
    writer: &'a mut W,
    xsd_string: Option<<G::LiteralPtr as LiteralPtr<'g>>::DatatypePtr>,
    xsd_boolean: Option<<G::LiteralPtr as LiteralPtr<'g>>::DatatypePtr>,
    xsd_integer: Option<<G::LiteralPtr as LiteralPtr<'g>>::DatatypePtr>,
    xsd_decimal: Option<<G::LiteralPtr as LiteralPtr<'g>>::DatatypePtr>,
    xsd_double: Option<<G::LiteralPtr as LiteralPtr<'g>>::DatatypePtr>,
    rdf_first: Option<G::IRIPtr>,
    rdf_nil: Option<G::IRIPtr>,
    rdf_rest: Option<G::IRIPtr>,
    rdf_type: Option<G::IRIPtr>,
    graph: &'g G,
}

/// Write out triples as pretty turtle.
pub fn write_pretty_turtle<'g, G: 'g, W>(
    namespaces: &Namespaces,
    graph: &'g G,
    writer: &mut W,
) -> Result<()>
where
    G: Graph<'g>,
    <G as Graph<'g>>::BlankNodePtr: Display,
    W: Write,
{
    let mut writer = TurtleWriter::<_, G> {
        buffer: Vec::new(),
        base: String::new(),
        writer,
        xsd_string: graph.find_datatype(constants::XSD_STRING),
        xsd_boolean: graph.find_datatype(constants::XSD_BOOLEAN),
        xsd_integer: graph.find_datatype(constants::XSD_INTEGER),
        xsd_decimal: graph.find_datatype(constants::XSD_DECIMAL),
        xsd_double: graph.find_datatype(constants::XSD_DOUBLE),
        rdf_first: graph.find_iri(constants::RDF_FIRST),
        rdf_nil: graph.find_iri(constants::RDF_NIL),
        rdf_rest: graph.find_iri(constants::RDF_REST),
        rdf_type: graph.find_iri(constants::RDF_TYPE),
        graph,
    };
    for ns in namespaces.iter() {
        writer.write_prefix(ns)?;
    }
    writer.writer.write_all(b"\n")?;
    writer.write_statements(namespaces)
}

impl<'a, 'g, W: 'a, G: 'g> TurtleWriter<'a, 'g, W, G>
where
    W: Write,
    G: Graph<'g>,
    <G as Graph<'g>>::BlankNodePtr: Display,
{
    fn write_prefix(&mut self, ns: &Namespace) -> Result<()> {
        self.writer.write_all(b"@prefix ")?;
        self.writer.write_all(ns.prefix())?;
        self.writer.write_all(b":\t")?;
        self.write_full_iri(ns.namespace())?;
        self.writer.write_all(b" .\n")?;
        Ok(())
    }
    fn write_iri_str(&mut self, iri: &str, namespaces: &Namespaces) -> Result<()> {
        match namespaces.find_prefix(iri) {
            Some((prefix, local)) => {
                if let Ok((CompleteStr(""), _)) = pn_local(CompleteStr(local)) {
                    self.write_prefixed_iri(prefix, local)
                } else {
                    self.write_full_iri(iri)
                }
            }
            None => self.write_full_iri(iri),
        }
    }
    fn write_iri(&mut self, iri: &G::IRIPtr, namespaces: &Namespaces) -> Result<()> {
        if Some(iri) == self.rdf_nil.as_ref() {
            self.writer.write_all(b"()")?;
        } else {
            self.write_iri_str(iri.as_str(), namespaces)?;
        }
        Ok(())
    }
    fn write_prefixed_iri(&mut self, prefix: &[u8], iri: &str) -> Result<()> {
        self.writer.write_all(prefix)?;
        self.writer.write_all(b":")?;
        self.writer.write_all(iri.as_bytes())?;
        Ok(())
    }
    fn write_full_iri(&mut self, mut iri: &str) -> Result<()> {
        if iri.starts_with(self.base.as_str()) {
            iri = &iri[self.base.len()..];
        }
        self.writer.write_all(b"<")?;
        self.buffer.clear();
        for b in iri.as_bytes() {
            if *b < 20 || b"<>\"{}|^`\\".contains(b) {
                write!(&mut self.buffer, "\\u00{:X}", *b).unwrap();
            } else {
                self.buffer.push(*b);
            }
        }
        self.writer.write_all(&self.buffer[..])?;
        self.writer.write_all(b">")?;
        Ok(())
    }
    fn write_blank_node(&mut self, blank_node: G::BlankNodePtr) -> Result<()> {
        self.writer.write_all(b"_:")?;
        write!(self.writer, "{}", blank_node)?;
        Ok(())
    }
    fn write_literal_value(&mut self, value: &str) -> Result<()> {
        self.buffer.clear();
        for b in value.as_bytes() {
            if *b == 0x22 || *b == 0x5C || *b == 0x0A || *b == 0x0D {
                self.buffer.push(b'\\');
            }
            self.buffer.push(*b);
        }
        self.writer.write_all(&self.buffer[..])?;
        Ok(())
    }
    fn write_literal(&mut self, literal: &G::LiteralPtr, namespaces: &Namespaces) -> Result<()> {
        let d = Some(literal.datatype());
        let v = literal.as_str();
        // if the literal matches the unquoted production for its datatype,
        // print without quotes
        let mut unquoted = false;
        for i in &[
            (
                &self.xsd_boolean,
                boolean as fn(CompleteStr) -> IResult<CompleteStr, Literal>,
            ),
            (&self.xsd_integer, integer),
            (&self.xsd_decimal, decimal),
            (&self.xsd_double, double),
        ] {
            if &d == i.0 {
                if let Ok((CompleteStr(""), _)) = (i.1)(CompleteStr(v)) {
                    unquoted = true;
                    break;
                }
            }
        }
        if unquoted {
            self.write_literal_value(literal.as_str())?;
        } else {
            self.writer.write_all(b"\"")?;
            self.write_literal_value(literal.as_str())?;
            self.writer.write_all(b"\"")?;
            if let Some(langtag) = literal.language() {
                self.writer.write_all(b"@")?;
                self.writer.write_all(langtag.as_bytes())?;
            } else if d != self.xsd_string {
                self.writer.write_all(b"^^")?;
                self.write_iri_str(literal.datatype_str(), namespaces)?;
            }
        }
        Ok(())
    }
    fn write_predicate(&mut self, predicate: &G::IRIPtr, namespaces: &Namespaces) -> Result<()> {
        if Some(predicate) == self.rdf_type.as_ref() {
            self.writer.write_all(b"a")?;
        } else {
            self.write_iri_str(predicate.as_str(), namespaces)?;
        }
        Ok(())
    }
    fn write_collection(
        &mut self,
        mut triple: G::SPOTriple,
        mut iter: G::SPORangeIter,
        namespaces: &Namespaces,
    ) -> Result<()> {
        loop {
            // write rdf:first value
            self.write_object(triple.object(), namespaces)?;
            let rest = iter.next()
                .ok_or_else(|| Error::Custom("An rdf:rest triple was expected."))?;
            if Some(rest.predicate()) != self.rdf_rest {
                return Err(Error::Custom("An rdf:rest triple was expected."));
            }
            if rest.object().as_iri() == self.rdf_nil.as_ref() {
                if iter.next().is_some() {
                    return Err(Error::Custom(
                        "No more triples were expected for the list node.",
                    ));
                }
                self.writer.write_all(b")")?;
                return Ok(());
            }
            let rest = rest.object()
                .as_blank_node()
                .ok_or_else(|| Error::Custom("A blank node was expected."))?
                .clone();
            if iter.next().is_some() {
                return Err(Error::Custom(
                    "No more triples were expected for the list node.",
                ));
            }
            self.writer.write_all(b"\n\t\t\t")?;
            iter = self.graph.iter_s(&rest.to_blank_node_or_iri());
            triple = iter.next().expect("The list node should have properties.");
            if Some(triple.predicate()) != self.rdf_first {
                return Err(Error::Custom("An rdf:first triple was expected."));
            }
        }
    }
    fn write_object(
        &mut self,
        object: Resource<'g, G::BlankNodePtr, G::IRIPtr, G::LiteralPtr>,
        namespaces: &Namespaces,
    ) -> Result<()> {
        match object {
            Resource::BlankNode(blank_node, _) => {
                // check how often the node is used as an object
                {
                    let mut object_iter = self.graph.iter_o(&blank_node.to_resource());
                    object_iter
                        .next()
                        .expect("Implementation error. There should be at least one triple.");
                    if object_iter.next().is_some() {
                        // blank node is used more than once, cannot be anonymous
                        return self.write_blank_node(blank_node);
                    }
                }
                // blank node is used as object only once
                let mut subject_iter = self.graph.iter_s(&blank_node.to_blank_node_or_iri());
                match subject_iter.next() {
                    None => {
                        // blank node has no properties, just write []
                        self.writer.write_all(b"[]")?;
                    }
                    Some(triple) => {
                        let predicate_iri = triple.predicate();
                        if Some(predicate_iri) == self.rdf_first {
                            self.writer.write_all(b"(")?;
                            self.write_collection(triple, subject_iter, namespaces)?;
                        } else {
                            self.writer.write_all(b"[")?;
                            let mut peekable = subject_iter.peekable();
                            self.write_predicate_object_list(&triple, &mut peekable, namespaces)?;
                            self.writer.write_all(b"]")?;
                        }
                    }
                }
            }
            Resource::IRI(iri) => self.write_iri(&iri, namespaces)?,
            Resource::Literal(literal) => self.write_literal(&literal, namespaces)?,
        }
        Ok(())
    }
    fn write_predicate_object_list<I>(
        &mut self,
        first_triple: &G::SPOTriple,
        iter: &mut Peekable<I>,
        namespaces: &Namespaces,
    ) -> Result<()>
    where
        I: Iterator<Item = G::SPOTriple>,
    {
        let subject = first_triple.subject();
        let mut triple = first_triple.clone();
        // loop until the subject changes
        loop {
            let predicate = triple.predicate();
            self.write_predicate(&predicate, namespaces)?;
            self.writer.write_all(b"\t")?;
            let mut same_subject = false;
            loop {
                self.write_object(triple.object(), namespaces)?;
                match iter.peek() {
                    None => {
                        break;
                    }
                    Some(next) => {
                        same_subject = next.subject() == subject;
                        if !same_subject || next.predicate() != predicate {
                            break;
                        }
                    }
                }
                self.writer.write_all(b" ,\n\t\t")?;
                triple = iter.next().unwrap();
            }
            if !same_subject {
                break;
            }
            self.writer.write_all(b" ;\n\t")?;
            triple = iter.next().unwrap();
        }
        Ok(())
    }
    fn write_statements(&mut self, namespaces: &Namespaces) -> Result<()> {
        let mut iter = self.graph.iter().peekable();
        while let Some(triple) = iter.next() {
            match triple.subject() {
                BlankNodeOrIRI::BlankNode(blank_node, _) => {
                    let mut object_iter = self.graph.iter_o(&blank_node.to_resource());
                    if object_iter.next().is_none() {
                        // node is never used as object, write anonymous node []
                        self.writer.write_all(b"[]")?;
                    } else if object_iter.next().is_none() {
                        // node is only used once, skip it now
                        // it will be written when it is an object
                        continue;
                    } else {
                        self.write_blank_node(blank_node)?;
                    }
                }
                BlankNodeOrIRI::IRI(ref iri) => self.write_iri(iri, namespaces)?,
            }
            self.writer.write_all(b"\t")?;
            self.write_predicate_object_list(&triple, &mut iter, namespaces)?;
            self.writer.write_all(b" .\n")?;
        }
        Ok(())
    }
}