~ubuntu-branches/ubuntu/saucy/restlet/saucy

« back to all changes in this revision

Viewing changes to org.restlet.ext.rdf/src/org/restlet/ext/rdf/RdfRepresentation.java

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2012-06-11 16:25:45 UTC
  • Revision ID: package-import@ubuntu.com-20120611162545-5w2o0resi5y3pybc
Tags: upstream-2.0.14
ImportĀ upstreamĀ versionĀ 2.0.14

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * Copyright 2005-2012 Restlet S.A.S.
 
3
 * 
 
4
 * The contents of this file are subject to the terms of one of the following
 
5
 * open source licenses: Apache 2.0 or LGPL 3.0 or LGPL 2.1 or CDDL 1.0 or EPL
 
6
 * 1.0 (the "Licenses"). You can select the license that you prefer but you may
 
7
 * not use this file except in compliance with one of these Licenses.
 
8
 * 
 
9
 * You can obtain a copy of the Apache 2.0 license at
 
10
 * http://www.opensource.org/licenses/apache-2.0
 
11
 * 
 
12
 * You can obtain a copy of the LGPL 3.0 license at
 
13
 * http://www.opensource.org/licenses/lgpl-3.0
 
14
 * 
 
15
 * You can obtain a copy of the LGPL 2.1 license at
 
16
 * http://www.opensource.org/licenses/lgpl-2.1
 
17
 * 
 
18
 * You can obtain a copy of the CDDL 1.0 license at
 
19
 * http://www.opensource.org/licenses/cddl1
 
20
 * 
 
21
 * You can obtain a copy of the EPL 1.0 license at
 
22
 * http://www.opensource.org/licenses/eclipse-1.0
 
23
 * 
 
24
 * See the Licenses for the specific language governing permissions and
 
25
 * limitations under the Licenses.
 
26
 * 
 
27
 * Alternatively, you can obtain a royalty free commercial license with less
 
28
 * limitations, transferable or non-transferable, directly at
 
29
 * http://www.restlet.com/products/restlet-framework
 
30
 * 
 
31
 * Restlet is a registered trademark of Restlet S.A.S.
 
32
 */
 
33
 
 
34
package org.restlet.ext.rdf;
 
35
 
 
36
import java.io.IOException;
 
37
import java.io.Writer;
 
38
import java.util.logging.Level;
 
39
 
 
40
import org.restlet.Context;
 
41
import org.restlet.data.MediaType;
 
42
import org.restlet.data.Reference;
 
43
import org.restlet.ext.rdf.internal.n3.RdfN3Reader;
 
44
import org.restlet.ext.rdf.internal.n3.RdfN3Writer;
 
45
import org.restlet.ext.rdf.internal.ntriples.RdfNTriplesReader;
 
46
import org.restlet.ext.rdf.internal.ntriples.RdfNTriplesWriter;
 
47
import org.restlet.ext.rdf.internal.turtle.RdfTurtleReader;
 
48
import org.restlet.ext.rdf.internal.turtle.RdfTurtleWriter;
 
49
import org.restlet.ext.rdf.internal.xml.RdfXmlReader;
 
50
import org.restlet.ext.rdf.internal.xml.RdfXmlWriter;
 
51
import org.restlet.representation.Representation;
 
52
import org.restlet.representation.WriterRepresentation;
 
53
 
 
54
/**
 
55
 * Generic RDF representation. Provides support for the Resource Description
 
56
 * Framework (RDF) Semantic Web standard. It supports major RDF serialization
 
57
 * formats (n3, Turtle, N-Triples and RDF/XML) and is able to both serialize and
 
58
 * deserialize a {@link Graph}.
 
59
 * 
 
60
 * @author Jerome Louvel
 
61
 */
 
62
public class RdfRepresentation extends WriterRepresentation {
 
63
 
 
64
    /** The inner graph of links. */
 
65
    private Graph graph;
 
66
 
 
67
    /** The inner RDF representation. */
 
68
    private Representation rdfRepresentation;
 
69
 
 
70
    /**
 
71
     * Constructor.
 
72
     */
 
73
    public RdfRepresentation() {
 
74
        super(MediaType.TEXT_XML);
 
75
    }
 
76
 
 
77
    /**
 
78
     * Constructor with argument.
 
79
     * 
 
80
     * @param linkSet
 
81
     *            The graph of links.
 
82
     * @param mediaType
 
83
     *            The representation's mediaType.
 
84
     */
 
85
    public RdfRepresentation(Graph linkSet, MediaType mediaType) {
 
86
        super(mediaType);
 
87
        this.graph = linkSet;
 
88
    }
 
89
 
 
90
    /**
 
91
     * Constructor with argument.
 
92
     * 
 
93
     * @param mediaType
 
94
     *            The representation's mediaType.
 
95
     */
 
96
    public RdfRepresentation(MediaType mediaType) {
 
97
        super(mediaType);
 
98
    }
 
99
 
 
100
    /**
 
101
     * Constructor that parsed a given RDF representation into a link set.
 
102
     * 
 
103
     * @param rdfRepresentation
 
104
     *            The RDF representation to parse.
 
105
     * @throws IOException
 
106
     */
 
107
    public RdfRepresentation(Representation rdfRepresentation)
 
108
            throws IOException {
 
109
        super(rdfRepresentation.getMediaType());
 
110
        this.rdfRepresentation = rdfRepresentation;
 
111
    }
 
112
 
 
113
    /**
 
114
     * Returns an instance of a graph handler used when parsing the inner RDF
 
115
     * representation.
 
116
     * 
 
117
     * @param graph
 
118
     *            The graph to build.
 
119
     * @return An instance of a graph handler used when parsing the inner RDF
 
120
     *         representation.
 
121
     */
 
122
    public GraphHandler createBuilder(Graph graph) {
 
123
        return new GraphBuilder(this.graph);
 
124
    }
 
125
 
 
126
    /**
 
127
     * Returns an instance of a graph handler used when writing the inner set of
 
128
     * links.
 
129
     * 
 
130
     * @param mediaType
 
131
     *            The given media type of the parsed RDF representation.
 
132
     * @param writer
 
133
     *            The character writer to write to.
 
134
     * @return An instance of a graph handler used when writing the inner set of
 
135
     *         links.
 
136
     * @throws IOException
 
137
     */
 
138
    public GraphHandler createWriter(MediaType mediaType, Writer writer)
 
139
            throws IOException {
 
140
        if (MediaType.TEXT_RDF_N3.equals(getMediaType())) {
 
141
            return new RdfN3Writer(writer);
 
142
        } else if (MediaType.TEXT_XML.equals(getMediaType())) {
 
143
            return new RdfXmlWriter(writer);
 
144
        } else if (MediaType.APPLICATION_ALL_XML.includes(getMediaType())) {
 
145
            return new RdfXmlWriter(writer);
 
146
        } else if (MediaType.TEXT_PLAIN.equals(getMediaType())) {
 
147
            return new RdfNTriplesWriter(writer);
 
148
        } else if (MediaType.TEXT_RDF_NTRIPLES.equals(getMediaType())) {
 
149
            return new RdfNTriplesWriter(writer);
 
150
        } else if (MediaType.APPLICATION_RDF_TURTLE.equals(getMediaType())) {
 
151
            return new RdfTurtleWriter(writer);
 
152
        }
 
153
 
 
154
        // Writing for other media types goes here.
 
155
        return null;
 
156
    }
 
157
 
 
158
    /**
 
159
     * Updates the list of known namespaces for the given graph of links.
 
160
     * 
 
161
     * @param linkset
 
162
     *            The given graph of links.
 
163
     * @param GraphHandler
 
164
     *            the graph handler.
 
165
     */
 
166
    private void discoverNamespaces(Graph linkset, GraphHandler graphHandler) {
 
167
        for (Link link : linkset) {
 
168
            discoverNamespaces(link, graphHandler);
 
169
        }
 
170
    }
 
171
 
 
172
    /**
 
173
     * Updates the list of known namespaces of the XML writer for the given
 
174
     * link.
 
175
     * 
 
176
     * @param link
 
177
     *            The given link.
 
178
     * @param GraphHandler
 
179
     *            the graph handler.
 
180
     */
 
181
    private void discoverNamespaces(Link link, GraphHandler graphHandler) {
 
182
        // The subject of the link is not discovered, it is generated as the
 
183
        // value of an "about" attribute.
 
184
        if (link.hasLinkSource()) {
 
185
            discoverNamespaces(link.getSourceAsLink(), graphHandler);
 
186
        } else if (link.hasGraphSource()) {
 
187
            discoverNamespaces(link.getSourceAsGraph(), graphHandler);
 
188
        }
 
189
 
 
190
        discoverNamespaces(link.getTypeRef(), graphHandler);
 
191
 
 
192
        if (link.hasLinkTarget()) {
 
193
            discoverNamespaces(link.getTargetAsLink(), graphHandler);
 
194
        } else if (link.hasGraphSource()) {
 
195
            discoverNamespaces(link.getSourceAsGraph(), graphHandler);
 
196
        }
 
197
    }
 
198
 
 
199
    /**
 
200
     * Updates the list of known namespaces of the XML writer for the given
 
201
     * reference.
 
202
     * 
 
203
     * @param reference
 
204
     *            The given reference.
 
205
     * @param xmlWriter
 
206
     *            the XML writer.
 
207
     */
 
208
    private void discoverNamespaces(Reference reference,
 
209
            GraphHandler graphHandler) {
 
210
        if (!Link.isBlankRef(reference)) {
 
211
            graphHandler.startPrefixMapping(null, reference);
 
212
        }
 
213
    }
 
214
 
 
215
    /**
 
216
     * Returns the graph of links.
 
217
     * 
 
218
     * @return The graph of links.
 
219
     * @throws Exception
 
220
     */
 
221
    public Graph getGraph() throws Exception {
 
222
        if (this.graph == null) {
 
223
            this.graph = new Graph();
 
224
            parse(createBuilder(this.graph));
 
225
        }
 
226
        return this.graph;
 
227
    }
 
228
 
 
229
    /**
 
230
     * Parses the inner RDF representation. The given graph handler is invoked
 
231
     * each time a link is detected.
 
232
     * 
 
233
     * @param graphHandler
 
234
     *            The graph handler.
 
235
     * @throws Exception
 
236
     */
 
237
    public void parse(GraphHandler graphHandler) throws Exception {
 
238
        if (rdfRepresentation != null) {
 
239
            if (MediaType.TEXT_RDF_N3.equals(rdfRepresentation.getMediaType())) {
 
240
                new RdfN3Reader(rdfRepresentation, graphHandler).parse();
 
241
            } else if (MediaType.TEXT_XML.equals(rdfRepresentation
 
242
                    .getMediaType())) {
 
243
                new RdfXmlReader(rdfRepresentation, graphHandler).parse();
 
244
            } else if (MediaType.APPLICATION_ALL_XML.includes(rdfRepresentation
 
245
                    .getMediaType())) {
 
246
                new RdfXmlReader(rdfRepresentation, graphHandler).parse();
 
247
            } else if (MediaType.TEXT_PLAIN.equals(rdfRepresentation
 
248
                    .getMediaType())) {
 
249
                new RdfNTriplesReader(rdfRepresentation, graphHandler).parse();
 
250
            } else if (MediaType.TEXT_RDF_NTRIPLES.equals(rdfRepresentation
 
251
                    .getMediaType())) {
 
252
                new RdfNTriplesReader(rdfRepresentation, graphHandler).parse();
 
253
            } else if (MediaType.APPLICATION_RDF_TURTLE
 
254
                    .equals(rdfRepresentation.getMediaType())) {
 
255
                new RdfTurtleReader(rdfRepresentation, graphHandler).parse();
 
256
            } else if (MediaType.valueOf("text/rdf+n3").equals(
 
257
                    rdfRepresentation.getMediaType())) {
 
258
                // Deprecated media type still in usage
 
259
                new RdfN3Reader(rdfRepresentation, graphHandler).parse();
 
260
            }
 
261
            // Parsing for other media types goes here.
 
262
        }
 
263
    }
 
264
 
 
265
    /**
 
266
     * Sets the graph of links.
 
267
     * 
 
268
     * @param linkSet
 
269
     *            The graph of links.
 
270
     */
 
271
    public void setGraph(Graph linkSet) {
 
272
        this.graph = linkSet;
 
273
    }
 
274
 
 
275
    /**
 
276
     * Writes the
 
277
     * 
 
278
     * @param graphHandler
 
279
     * @throws IOException
 
280
     */
 
281
    public void write(GraphHandler graphHandler) throws IOException {
 
282
        try {
 
283
            if (graph != null) {
 
284
                discoverNamespaces(graph, graphHandler);
 
285
                graphHandler.startGraph();
 
286
 
 
287
                for (Link link : graph) {
 
288
                    if (link.hasReferenceSource()) {
 
289
                        if (link.hasReferenceTarget()) {
 
290
                            graphHandler.link(link.getSourceAsReference(),
 
291
                                    link.getTypeRef(),
 
292
                                    link.getTargetAsReference());
 
293
                        } else if (link.hasLiteralTarget()) {
 
294
                            graphHandler.link(link.getSourceAsReference(),
 
295
                                    link.getTypeRef(),
 
296
                                    link.getTargetAsLiteral());
 
297
                        } else if (link.hasLinkTarget()) {
 
298
                            Context.getCurrentLogger()
 
299
                                    .warning(
 
300
                                            "Cannot write the representation of a statement due to the fact that the object is neither a Reference nor a literal.");
 
301
                        } else {
 
302
                            Context.getCurrentLogger()
 
303
                                    .warning(
 
304
                                            "Cannot write the representation of a statement due to the fact that the object is neither a Reference nor a literal.");
 
305
                        }
 
306
                    } else if (link.hasGraphSource()) {
 
307
                        if (link.hasReferenceTarget()) {
 
308
                            graphHandler.link(link.getSourceAsGraph(),
 
309
                                    link.getTypeRef(),
 
310
                                    link.getTargetAsReference());
 
311
                        } else if (link.hasLiteralTarget()) {
 
312
                            graphHandler.link(link.getSourceAsGraph(),
 
313
                                    link.getTypeRef(),
 
314
                                    link.getTargetAsLiteral());
 
315
                        } else if (link.hasLinkTarget()) {
 
316
                            Context.getCurrentLogger()
 
317
                                    .warning(
 
318
                                            "Cannot write the representation of a statement due to the fact that the object is neither a Reference nor a literal.");
 
319
                        } else {
 
320
                            Context.getCurrentLogger()
 
321
                                    .warning(
 
322
                                            "Cannot write the representation of a statement due to the fact that the object is neither a Reference nor a literal.");
 
323
                        }
 
324
                    }
 
325
                }
 
326
                graphHandler.endGraph();
 
327
            }
 
328
        } catch (Exception e) {
 
329
            Context.getCurrentLogger()
 
330
                    .log(Level.WARNING,
 
331
                            "Cannot write the RDF graph due to an unexpected exception",
 
332
                            e);
 
333
        }
 
334
    }
 
335
 
 
336
    @Override
 
337
    public void write(Writer writer) throws IOException {
 
338
        write(createWriter(getMediaType(), writer));
 
339
    }
 
340
 
 
341
}