RDFa Plugin
Dependency :
compile ":rdfa:0.1.1"
Summary
Installation
grails install-plugin rdfa
Description
RDFa Plug-in
The RDFa plug-in allows you to expose relevant parts of the structure of your application as RDFa annotations in your views. Both domain model (non-information resources) and controller actions (information resources) can be described.Installation
To install the RDFa plug-in just run the following commandgrails install-plugin rdfa
Usage
The RDFa plug-in provides a RDFaTagLib that can be used in any of your Grails views. To achieve this, the plug-in processes Groovy-to-RDF mappings in the domain model and in controllers.Model mapping
An example of how to expose the domain model as RDFa can be seen below.class Person {
static rdf = [
'':'http://xmlns.com/foaf/0.1/Person',
'name':'http://xmlns.com/foaf/0.1/name',
'friends':'http://xmlns.com/foaf/0.1/knows'
] String name
static hasMany = [friends:Person]
}<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN" "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd">
rdfa tags.<rdfa:ul about="${person}"> <g:each var="friend" in="${person.friends}"> <rdfa:li rel="friends" resource="${friend}" property="name" /> </g:each> </rdfa:ul>
<ul about="http://example.com/person/resource/1" typeof="ns1:Person" xmlns:ns1="http://xmlns.com/foaf/0.1/"> <li rel="ns1:knows" resource="http://example.com/person/resource/2" property="ns1:name"> John Doe II </li> <li rel="ns1:knows" resource="http://example.com/person/resource/3" property="ns1:name"> John Doe III </li> </ul>
Controller mapping
It is important to distinguish between the thing you're describing (the domain model object), and its description (the view rendered by a controller action). E.g., a Person in a domain model can have a name, whereas a document describing this person cannot, but can have other information such as creation date or terms of use. Hence, the controller needs an additional action for identifying domain objects, and the relation of controller actions to the domain model objects need to be specified.class PersonController {
static rdf = [
resource: { it.id }, // name of identifying action and identity field of domain model object
image:'http://xmlns.com/foaf/0.1/depiction',
show:'http://xmlns.com/foaf/0.1/isPrimaryTopicOf'
] def show = {
[person:Person.get(params.id)]
} def resource = {
redirect(action:show,id:params.id)
} def image = {
redirect(uri:"/images/default-avatar.png")
}
}<rdfa:p about="${person}"> <rdfa:img rel="image" alt="depiction of ${person.name}" /> </rdfa:p>
<p about="http://example.com/person/resource/1" typeof="ns1:Person" xmlns:ns1="http://xmlns.com/foaf/0.1/"> <img rel="ns1:depiction" src="http://example.com/person/image/1" alt="depiction of John Doe I" > </p>
Consuming Linked Data
You can also expose RDFa of other objects than just mapped domain model objects. Using the enclosed object triple mapping implementation, you can add resources from other datasets as well and reference them in views.GraphService graphService
def berlin = graphService.get("http://dbpedia.org/resource/Berlin", vocab.dbpedia.City)TODO
- beautiful XML namespace declarations (e.g., "foaf" instead of "ns1")
- automatically guess, e.g.,
rel="friends"from <g:each in="${person.friends}">...</g:each> - Make better use of existing URLMapping (do not require ID closure in controller)