1 package eu.fbk.knowledgestore.internal.rdf;
2
3 import java.io.IOException;
4 import java.io.OutputStream;
5 import java.io.OutputStreamWriter;
6 import java.io.Writer;
7 import java.util.Map;
8
9 import com.google.common.base.Charsets;
10 import com.google.common.base.Preconditions;
11 import com.google.common.collect.Maps;
12
13 import org.openrdf.model.Statement;
14 import org.openrdf.rio.RDFFormat;
15 import org.openrdf.rio.RDFHandlerException;
16 import org.openrdf.rio.RDFWriter;
17 import org.openrdf.rio.RDFWriterFactory;
18 import org.openrdf.rio.helpers.RDFWriterBase;
19
20 public class HtmlRDF implements RDFWriterFactory {
21
22
23 public static final RDFFormat FORMAT = new RDFFormat("RDFHTML", "text/html", Charsets.UTF_8,
24 "html", true, false);
25
26 static {
27 RDFFormat.register(FORMAT);
28 }
29
30
31
32
33
34
35 public static void register() {
36
37 }
38
39 @Override
40 public RDFFormat getRDFFormat() {
41 return FORMAT;
42 }
43
44 @Override
45 public RDFWriter getWriter(final OutputStream out) {
46 return getWriter(new OutputStreamWriter(out, Charsets.UTF_8));
47 }
48
49 @Override
50 public RDFWriter getWriter(final Writer writer) {
51 Preconditions.checkNotNull(writer);
52 return new HTMLWriter(writer);
53 }
54
55 private static class HTMLWriter extends RDFWriterBase {
56
57 private final Writer writer;
58
59 private final Map<String, String> prefixes;
60
61 HTMLWriter(final Writer writer) {
62 this.writer = writer;
63 this.prefixes = Maps.newLinkedHashMap();
64 }
65
66 @Override
67 public RDFFormat getRDFFormat() {
68 return FORMAT;
69 }
70
71 @Override
72 public void startRDF() throws RDFHandlerException {
73 try {
74 this.writer.write("<html>\n<head>\n"
75 + "<meta http-equiv=\"Content-type\" "
76 + "content=\"text/html;charset=UTF-8\"/>\n"
77 + "</head>\n<body>\n"
78 + "<table class=\"rdf\">\n<thead>\n"
79 + "<tr><th>Subject</th><th>Predicate</th><th>Object</th></tr>\n"
80 + "</thead>\n<tbody>\n");
81 } catch (final IOException ex) {
82 throw new RDFHandlerException(ex);
83 }
84 }
85
86 @Override
87 public void handleComment(final String comment) throws RDFHandlerException {
88
89 }
90
91 @Override
92 public void handleNamespace(final String prefix, final String uri)
93 throws RDFHandlerException {
94 this.prefixes.put(uri, prefix);
95 }
96
97 @Override
98 public void handleStatement(final Statement statement) throws RDFHandlerException {
99 try {
100 this.writer.write("<tr><td>");
101 RDFUtil.toHtml(statement.getSubject(), this.prefixes, this.writer);
102 this.writer.write("</td><td>");
103 RDFUtil.toHtml(statement.getPredicate(), this.prefixes, this.writer);
104 this.writer.write("</td><td>");
105 RDFUtil.toHtml(statement.getObject(), this.prefixes, this.writer);
106 this.writer.write("</td></tr>\n");
107 } catch (final IOException ex) {
108 throw new RDFHandlerException(ex);
109 }
110 }
111
112 @Override
113 public void endRDF() throws RDFHandlerException {
114 try {
115 this.writer.write("</tbody>\n</table>\n</html>\n");
116 this.writer.flush();
117 } catch (final IOException ex) {
118 throw new RDFHandlerException(ex);
119 }
120 }
121
122 }
123
124 }